Created
June 30, 2020 14:07
-
-
Save akeller/265377d9f317e7665cc96b522189a80a to your computer and use it in GitHub Desktop.
Postman pre-request script for wsc hmac auth
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
pm.variables.set("wsc-api-key", "XXXXXXXXX"); | |
// get and set a timestamp in seconds (Unix epoch) | |
var timestamp = Math.floor(Date.now() / 1000); | |
pm.variables.set("wsc-timestamp", timestamp); | |
console.log("timestamp=" + timestamp); | |
//normalize Postman path object | |
console.log("pm.request.url.path=" + pm.request.url.path) | |
var path = "/"; // force leading slash | |
// add path, converting commas to slashes | |
path = path.concat(pm.request.url.path.toString().replace(/,/g, "/")); | |
// defensive code to make sure last char is NOT a slash | |
if (path.charAt(path.length - 1) == "/") { | |
path = path.slice(0,path.length - 1); | |
} | |
console.log("normalized path=" + path); | |
// get secret API key from environment | |
var apikey = pm.variables.get("wsc-api-key"); | |
//construct data string with required components | |
var data = timestamp + ":" + path + ":" + apikey; | |
console.log("data=" + data); | |
// compute signature | |
var signature = CryptoJS.HmacSHA256(data, apikey).toString(); | |
console.log("signature=" + signature); | |
// set Postman variable for signature | |
pm.variables.set("wsc-signature", signature); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment