Skip to content

Instantly share code, notes, and snippets.

@elvisciotti
Last active June 12, 2020 10:09
Show Gist options
  • Save elvisciotti/0e2922b0733af1369447689e48bbc3ee to your computer and use it in GitHub Desktop.
Save elvisciotti/0e2922b0733af1369447689e48bbc3ee to your computer and use it in GitHub Desktop.
Postman pre-request script to sign requests adding api-key and api-sign created using HmacRIPEMD160 based on secret and URI, for two different environments
const API_KEYS = {
'prod': {
'key': '',
'secret': '',
},
'uat': {
'key': '',
'secret': '',
}
};
if (!pm.request.headers.get('api-key') && !pm.request.headers.get('api-sig')) {
const env = /(CHANGEME)/.test(pm.request.url) ? 'prod' : 'uat';
pm.request.headers.add({
key: 'api-key',
value: API_KEYS[env]['key']
});
const path = pm.request.url.getPath();
const ciphertext = crypto.HmacRIPEMD160(path, API_KEYS[env]['secret']).toString();
pm.request.headers.add({
key: 'api-sig',
value: ciphertext
});
// console.log('api-key:' + pm.request.headers.get('api-key'));
// console.log('api-sig:' + pm.request.headers.get('api-sig'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment