Sign requests with HMAC
Build the string to sign, compute the HMAC signature and send the headers that let Servicely verify a signed REST request.
Sign each request with a System API token’s shared secret when Servicely must verify who sent it. HMAC body validation also proves that the method, body and path were not changed in transit.
Before you begin
- You have a System API token with the validation type HMAC body validation or HMAC header validation, its token and its shared secret. An administrator creates it, see Create a System API token in the Administrator guide.
- You know the token’s Hash method. It defaults to SHA256, which the examples use. MD5 and SHA1 are also offered.
- The token’s Impersonate user is a person’s user account. Servicely refuses HMAC sign-in for non-human users such as AI agents.
- The clock of the calling system is accurate. Servicely rejects a request whose date is more than five minutes in the past.
Steps
These steps sign a request for HMAC body validation.
-
Format the current time in UTC as an HTTP date, for example
Tue, 12 Jan 2016 14:57:28 GMT. Send it in theDateheader. -
If the request has a body, compute its MD5 digest and Base64-encode it. Send the result in the
Content-MD5header. Without that header, Servicely signs an empty line in its place. -
Build the string to sign: five values joined by newline characters, with no newline at the end.
Line Value 1 The method, for example POST2 The Base64 MD5 digest of the body, or empty 3 The Content-Typeheader exactly as sent, or empty4 The Dateheader exactly as sent5 The path, for example /v1/Incident, then?and the URL-decoded query string if there is onePOST vVqHE1k/uBRCoWe0FAh95g== application/json Tue, 12 Jan 2016 14:57:28 GMT /v1/Incident -
Compute the HMAC of the string with the shared secret and the token’s hash method, then Base64-encode it. This is the signature.
-
Send the token and the signature in the
Authorizationheader, separated by a colon.Authorization: HMAC <token>:<signature>
This Postman pre-request script does all five steps. Store the token and secret as Postman variables rather than in the script.
const token = pm.variables.get("servicelyToken");
const sharedSecret = pm.variables.get("servicelySecret");
const formattedDate = (new Date()).toUTCString();
const contentType = "application/json";
const body = pm.request.body ? pm.request.body.toString() : "";
const bodyMD5 = body !== "" ? CryptoJS.enc.Base64.stringify(CryptoJS.MD5(body)) : "";
// The server signs the path plus the URL-decoded query string
const query = pm.request.url.getQueryString();
const path = pm.request.url.getPath() + (query ? "?" + decodeURIComponent(query.replace(/\+/g, " ")) : "");
const stringToSign = [pm.request.method, bodyMD5, contentType, formattedDate, path].join("\n");
const signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(stringToSign, sharedSecret));
pm.request.headers.upsert({ key: "Date", value: formattedDate });
pm.request.headers.upsert({ key: "Content-Type", value: contentType });
if (bodyMD5 !== "") {
pm.request.headers.upsert({ key: "Content-MD5", value: bodyMD5 });
}
pm.request.headers.upsert({ key: "Authorization", value: "HMAC " + token + ":" + signature });
Variations
HMAC header validation
With HMAC header validation, the string to sign is the values of the headers named in the token’s Header key, joined by colons in the order listed. With the default, Date, it is the date alone. Header validation proves the sender knows the secret. It does not protect the body.
This script signs the Date header and a custom header. It assumes Header key is Date,X-Custom.
const token = pm.variables.get("servicelyToken");
const sharedSecret = pm.variables.get("servicelySecret");
const formattedDate = (new Date()).toUTCString();
const customValue = pm.variables.replaceIn("{{$guid}}");
const stringToSign = formattedDate + ":" + customValue;
const signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(stringToSign, sharedSecret));
pm.request.headers.upsert({ key: "Date", value: formattedDate });
pm.request.headers.upsert({ key: "X-Custom", value: customValue });
pm.request.headers.upsert({ key: "Authorization", value: "HMAC " + token + ":" + signature });
Result
Servicely recomputes the signature from the request it received and compares it with yours. If they match and the date is recent, the request runs as the token’s Impersonate user. Any change to the signed values, or a date older than five minutes, makes the request unauthenticated.