Last active
November 25, 2020 07:01
-
-
Save t404/5d8f3594285cbbb48de0b78c2307c1a5 to your computer and use it in GitHub Desktop.
[API签名] #signature #postman
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
var access_key_id = pm.environment.get('access_key_id'); | |
var secret_access_key = pm.environment.get('secret_access_key'); | |
pm.request.headers.add({ | |
key: 'Content-Type', | |
value: 'application/json' | |
}); | |
pm.request.headers.add({ | |
key: 'x-iot-timestamp', | |
value: Date.parse(new Date())/1000 | |
}); | |
pm.request.headers.add({ | |
key: 'x-iot-signature-method', | |
value: 'HmacSHA256' | |
}); | |
pm.request.headers.add({ | |
key: 'x-iot-signature-version', | |
value: '1' | |
}); | |
var signature = computeHttpSignature({ | |
'algorithm': 'hmac-sha256', | |
'secretkey': secret_access_key | |
}); | |
console.log('signature:'+ signature); | |
pm.request.headers.add({ | |
key: 'Authorization', | |
value: 'QC'+access_key_id+':'+signature | |
}); | |
// console.log('headers:'); | |
console.log('query:'+ pm.request.url.query) | |
function computeHttpSignature(config) { | |
// compute sig here | |
var sign_to_string = pm.request.method.toUpperCase(); | |
var header_string = buildCanonicalizedHeaders(); | |
var query_string = buildCanonicalizedQuery(); | |
var body_string = buildCanonicalizedBody(); | |
if (pm.request.url.getPath() != ''){ | |
sign_to_string += '\n'; | |
pm.request.url.path.forEach(function(h){ | |
sign_to_string += '/' + encodeURIComponent(h); | |
}); | |
} | |
if (header_string != ''){ | |
sign_to_string += header_string; | |
} | |
if (query_string != ''){ | |
sign_to_string += '\n' + query_string; | |
} | |
if (body_string != ''){ | |
sign_to_string += '\n' + body_string; | |
} | |
console.log(sign_to_string); | |
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(sign_to_string); | |
var sig = hashf(sign_to_string, config.secretkey); | |
return encodeURIComponent(CryptoJS.enc.Base64.stringify(sig)); | |
} | |
function buildCanonicalizedHeaders() { | |
var headers = pm.request.headers.all(); | |
var header_string = ''; | |
headers.sort(function(a, b) { | |
if (a.key.toLowerCase() > b.key.toLowerCase()) return 1; | |
return -1; | |
}); | |
headers.forEach(function(h){ | |
key = h.key.toLowerCase(); | |
if (key.startsWith('x-iot-')){ | |
header_string += '\n' + key + ":" + h.value; | |
} | |
}); | |
return header_string; | |
} | |
function buildCanonicalizedQuery() { | |
var query = pm.request.url.query.all(); | |
var query_string = ''; | |
query.sort(function(a, b) { | |
if (a.key>b.key) return 1; | |
return -1; | |
}); | |
query.forEach(function(h){ | |
if (query_string != '') { | |
query_string += '&' | |
} | |
query_string += h.key + "=" + encodeURIComponent(h.value); | |
}); | |
return query_string; | |
} | |
function buildCanonicalizedBody() { | |
if (pm.request.body){ | |
return pm.request.body.raw | |
} else { | |
return '' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment