Created
January 19, 2022 18:38
-
-
Save rmirabelli/6bb72becce6c0237226b41e26f11ff59 to your computer and use it in GitHub Desktop.
Reference Postman implementation for accessing Azure App Configuration.
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
// Set the Date header to our Date as a UTC String. | |
const dateStr = new Date().toUTCString(); | |
pm.request.headers.upsert({key:'Date', value: dateStr}); | |
// Hash the request body using SHA256 and encode it as Base64 | |
const hashedBodyStr = CryptoJS.SHA256(pm.request.body.raw).toString(CryptoJS.enc.Base64) | |
// And add that to the header x-ms-content-sha256 | |
pm.request.headers.upsert({ | |
key:'x-ms-content-sha256', | |
value: hashedBodyStr | |
}); | |
// Get our previously specified endpoint variable | |
const endpoint = pm.variables.get('endpoint') | |
// Remove the https, prefix to create a suitable "Host" value | |
const hostStr = endpoint.replace('https://',''); | |
// This gets the part of our URL that is after the endpoint, for example in https://something.azure.com/sms, it will get ‘/sms' | |
const url = pm.request.url.toString().replace('{{endpoint}}',''); | |
// Construct our string which we'll sign, using various previously created values. | |
const stringToSign = pm.request.method + '\n' + url + '\n' + dateStr + ';' + hostStr + ';' + hashedBodyStr; | |
// Decode our access key from previously created variables, into bytes from base64. | |
const key = CryptoJS.enc.Base64.parse(pm.variables.get('key')); | |
// Sign our previously calculated string with HMAC 256 and our key. Convert it to Base64. | |
const signature = CryptoJS.HmacSHA256(stringToSign, key).toString(CryptoJS.enc.Base64); | |
// Add our final signature in Base64 to the authorization header of the request. | |
pm.request.headers.upsert({ | |
key:'Authorization', | |
value: "HMAC-SHA256 Credential=" + pm.variables.get('credential') + "&SignedHeaders=date;host;x-ms-content-sha256&Signature=" + signature | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment