Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var credential = "YOUR-APP-CONFIGURATION-CREDENTIAL-ID";
var secret = "YOUR-APP-CONFIGURATION-CREDENTIAL-VALUE";
function signRequest(host,
method, // GET, PUT, POST, DELETE
url, // path+query
body, // request body (undefined of none)
credential, // access key id
secret) // access key value (base64 encoded)
{
var verb = method.toUpperCase();
var utcNow = new Date().toUTCString();
var contentHash = CryptoJS.SHA256(body).toString(CryptoJS.enc.Base64);
//
// SignedHeaders
var signedHeaders = "x-ms-date;host;x-ms-content-sha256"; // Semicolon separated header names
//
// String-To-Sign
var stringToSign =
verb + '\n' + // VERB
url + '\n' + // path_and_query
utcNow + ';' + host + ';' + contentHash; // Semicolon separated SignedHeaders values
//
// Signature
var signature = CryptoJS.HmacSHA256(stringToSign, CryptoJS.enc.Base64.parse(secret)).toString(CryptoJS.enc.Base64);
//
// Result request headers
return [
{ name: "x-ms-date", value: utcNow },
{ name: "x-ms-content-sha256", value: contentHash },
{ name: "Authorization", value: "HMAC-SHA256 Credential=" + credential + "&SignedHeaders=" + signedHeaders + "&Signature=" + signature }
];
}
var isBodyEmpty = pm.request.body === null || pm.request.body === undefined || pm.request.body.isEmpty();
var headers = signRequest(
pm.request.url.getHost(),
pm.request.method,
pm.request.url.getPathWithQuery(),
isBodyEmpty ? undefined : pm.request.body.toString(),
credential,
secret);
// Add headers to the request
headers.forEach(header => {
pm.request.headers.upsert({key: header.name, value: header.value});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.