Using Postman with Lingk APIs
Last updated
function computeHttpSignature(config, headerHash) {
var template = 'keyid="${keyId}",algorithm="${algorithm}",headers="${headers}",signature="${signature}"',
sig = template;
console.log(template);
console.log(headerHash);
// compute sig here
var signingBase = '';
config.headers.forEach(function(h){
console.log(h);
if (signingBase !== '') { signingBase += '\n'; }
signingBase += h.toLowerCase() + ": " + headerHash[h];
});
console.log(signingBase);
var hashf = (function() {
switch (config.algorithm) {
case 'hmac-sha1': return CryptoJS.HmacSHA1;
case 'hmac-sha256': return CryptoJS.HmacSHA256;
case 'hmac-sha512': return CryptoJS.HmacSHA512;
default : return null;
}
}());
console.log("hashAlgorithm: " + config.algorithm);
var hash = hashf(signingBase, config.secretkey);
console.log("hash: " + hash);
var signatureOptions = {
keyId : config.keyId,
algorithm: config.algorithm,
headers: config.headers,
signature : encodeURIComponent(CryptoJS.enc.Base64.stringify(hash))
};
console.log(signatureOptions);
// build sig string here
Object.keys(signatureOptions).forEach(function(key) {
var pattern = "${" + key + "}",
value = (typeof signatureOptions[key] != 'string') ? signatureOptions[key].join(' ') : signatureOptions[key];
sig = sig.replace(pattern, value);
});
console.log(sig);
return sig;
}
var curDate = new Date().toUTCString();
var targetUrl = request.url.trim(); // there may be surrounding ws
targetUrl = targetUrl.replace(new RegExp('^https?://[^/]+/'),'/'); // strip hostname
var method = request.method.toLowerCase();
var sha256digest = CryptoJS.SHA256(request.data);
var base64sha256 = CryptoJS.enc.Base64.stringify(sha256digest);
var computedDigest = 'sha-256=' + base64sha256;
var headerHash = {
date : curDate,
'(request-target)' : method + ' ' + targetUrl
};
var config = {
algorithm : 'hmac-sha1',
keyId : environment['client-key'],
secretkey : environment['client-secret'],
headers : [ 'date', '(request-target)' ]
};
var sig = computeHttpSignature(config, headerHash);
postman.setEnvironmentVariable('httpsig', sig);
postman.setEnvironmentVariable("current-date", curDate);