Last active
January 12, 2021 04:16
-
-
Save svenmalvik/1fafc314ce589d4ce6145bc08f29ec0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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