Skip to content

Instantly share code, notes, and snippets.

@rgr2k
Forked from compwright/prerequest.js
Created May 17, 2020 21:27
Show Gist options
  • Select an option

  • Save rgr2k/ca7b1120c2cdd3afd6278325e21a1096 to your computer and use it in GitHub Desktop.

Select an option

Save rgr2k/ca7b1120c2cdd3afd6278325e21a1096 to your computer and use it in GitHub Desktop.
Postman pre-request script for Joyent Signature Authentication Scheme
// Postman pre-request script for Joyent Signature Authentication
// (https://github.com/joyent/node-http-signature/blob/master/http_signing.md)
//
// Set the following vars in your Postman environment:
// apiHost
// apiKey
// apiSecret
//
// Set the following headers in Postman (copy/paste):
// Authorization: Signature keyId="{{apiKey}}",algorithm="hmac-sha256",headers="host date",signature="{{signature}}"
// Date: {{date}}
//
// Add the following pre-request script (copy/paste):
const apiSecret = postman.getEnvironmentVariable('apiSecret');
const host = postman.getEnvironmentVariable('apiHost');
const date = new Date().toUTCString();
var signStr = [
'host: ' + host,
'date: ' + date
].join("\n");
var signature = CryptoJS.HmacSHA256(signStr, apiSecret).toString(CryptoJS.enc.Base64);
postman.setGlobalVariable("date", date);
postman.setGlobalVariable("signature", signature);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment