| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- function uuidv4() {
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
- const r = Math.random() * 16 | 0;
- const v = c === 'x' ? r : (r & 0x3 | 0x8);
- return v.toString(16);
- });
- }
- function upsertHeader(key, value) {
- pm.request.headers.remove(key);
- if (value !== undefined && value !== null && String(value) !== '') {
- pm.request.headers.add({ key: key, value: String(value) });
- }
- }
- const appKey = pm.variables.get('appKey');
- const appSecret = pm.variables.get('appSecret');
- const tagId = pm.variables.get('tagId');
- const accept = '*/*';
- const contentType = 'application/json;charset=UTF-8';
- const nonce = uuidv4();
- const timestamp = String(Date.now());
- const bodyRaw = pm.request.body && pm.request.body.mode === 'raw'
- ? pm.variables.replaceIn(pm.request.body.raw || '')
- : '';
- const contentMd5 = CryptoJS.enc.Base64.stringify(CryptoJS.MD5(CryptoJS.enc.Utf8.parse(bodyRaw)));
- const apiPath = '/' + pm.request.url.path.join('/');
- const signHeaders = [
- ['x-ca-key', appKey],
- ['x-ca-nonce', nonce],
- ['x-ca-timestamp', timestamp]
- ];
- const signHeaderNames = signHeaders.map(function(row) { return row[0]; }).join(',');
- const signTextLines = [
- 'POST',
- accept,
- contentMd5,
- contentType
- ];
- signHeaders.forEach(function(row) {
- signTextLines.push(row[0] + ':' + (row[1] == null ? '' : String(row[1]).trim()));
- });
- signTextLines.push(apiPath);
- const stringToSign = signTextLines.join('\n');
- const signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(stringToSign, appSecret));
- upsertHeader('Accept', accept);
- upsertHeader('Content-Type', contentType);
- upsertHeader('Content-MD5', contentMd5);
- upsertHeader('X-Ca-Key', appKey);
- upsertHeader('X-Ca-Nonce', nonce);
- upsertHeader('X-Ca-Timestamp', timestamp);
- upsertHeader('X-Ca-Signature-Headers', signHeaderNames);
- upsertHeader('X-Ca-Signature', signature);
- upsertHeader('tagId', tagId);
- pm.environment.set('lastApiPath', apiPath);
- pm.environment.set('lastContentMd5', contentMd5);
- pm.environment.set('lastStringToSign', stringToSign);
- pm.environment.set('lastSignature', signature);
|