Last active
July 25, 2019 12:02
-
-
Save ltpquang/ae716b181889465be03df00d18eba5b1 to your computer and use it in GitHub Desktop.
Postman pre-request script with sig computation
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.globals.set("$reqdate", new Date().getTime()); | |
///////////////////////////////// | |
// Example: | |
// - Request params | |
// keya: {{$global_variable_a}} | |
// keyb: {{$global_variable_b}} | |
// keyc: fixed_value_c | |
// keyd: {{env_variable_d}} | |
// - Required signature data | |
// <value_a>|<value_d>|<value_c>|<value_b>|<not_in_param_e> | |
// | |
// The config should be | |
// - signParams: ["keya", "keyd", "keyc", "keyb", "keye"], | |
// - override: { | |
// keye: <not_in_param_e>, | |
// }, | |
///////////////////////////////// | |
var config = { | |
// Hash function | |
hashFn : hashFn('sha256'), | |
// Appearance order of request params in signature data | |
signParams: ["", ""], | |
// Key-Value that appear in signature but not in request params | |
override: { | |
keye: 'value e', | |
}, | |
}; | |
pm.globals.set("$sig", computeSig(config, pm.request)); | |
function computeSig(config, request) { | |
var params = getRequestParams(request) | |
var paramsMap = buildParamsMap(config, params) | |
paramsMap = overrideParamsMap(config.override, paramsMap) | |
const sigDataString = buildSigDataString(config.signParams, paramsMap) | |
const sigString = config.hashFn(sigDataString).toString() | |
console.log('hashStr ', sigString) | |
refineRequestBody(request, sigString) | |
return sigString; | |
} | |
function getRequestParams(request) { | |
const method = pm.request.method | |
if (method == 'GET') { | |
return pm.request.url.query.all(); | |
} else { | |
const reqType = pm.request.body.mode | |
var bodyContent = pm.request.body[reqType] | |
bodyContent = refineParamsBasedOnRequestType(reqType, bodyContent) | |
return bodyContent | |
} | |
} | |
function refineParamsBasedOnRequestType(reqType, params) { | |
if (reqType == 'raw') { | |
const obj = JSON.parse(params) | |
params = [] | |
Object.keys(obj).forEach(function(key,index) { | |
params.push({ | |
key: key, | |
value: obj[key], | |
}) | |
}); | |
} else { | |
params = params.all() | |
} | |
return params | |
} | |
function buildParamsMap(config, params) { | |
const paramsMap = {} | |
params.forEach(o => { | |
paramsMap[o.key] = extractValue(o.value) | |
}) | |
return paramsMap | |
} | |
function extractValue(value) { | |
if (!value.startsWith('{{')) { // Fixed value | |
return value | |
} | |
value = value.replace('{{', '') | |
value = value.replace('}}', ''); | |
if (value.startsWith('$')) { | |
return pm.globals.get(value) | |
} else { | |
return pm.environment.get(value) | |
} | |
} | |
function overrideParamsMap(overrideParamsMap, paramsMap) { | |
for(var key in overrideParamsMap) { | |
paramsMap[key] = overrideParamsMap[key] | |
} | |
return paramsMap | |
} | |
function buildSigDataString(sigComponentKeys, dataMap) { | |
const sigComponents = sigComponentKeys.map(o => { | |
if (!dataMap[o]) { | |
console.log('empty value for key ', o) | |
} | |
return dataMap[o] | |
}) | |
const result = sigComponents.join('|') | |
console.log('hashData ', result) | |
return result | |
} | |
function refineRequestBody(request, paramsMap, sigStr) { | |
const method = pm.request.method | |
if (method == 'GET') { | |
return | |
} | |
const reqType = pm.request.body.mode | |
if (reqType == 'raw') { | |
paramsMap['sig'] = sigStr | |
pm.request.body['raw'] = JSON.stringify(paramsMap) | |
console.log('final pm.request.body ', pm.request.body) | |
} | |
} | |
function hashFn(algorithm) { | |
switch (algorithm) { | |
case 'sha256': return CryptoJS.SHA256; | |
case 'hmac-sha1': return CryptoJS.HmacSHA1; | |
case 'hmac-sha256': return CryptoJS.HmacSHA256; | |
case 'hmac-sha512': return CryptoJS.HmacSHA512; | |
} | |
return null | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment