Last active
June 12, 2020 10:09
-
-
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
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
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