-
Notifications
You must be signed in to change notification settings - Fork 0
/
Postman.Pre-request Script.js
179 lines (168 loc) · 4.96 KB
/
Postman.Pre-request Script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const sdk = require("postman-collection");
// Parameters
const signatureClientId =
pm.variables.get("signatureClientId") ||
pm.environment.get("signatureClientId");
if (!signatureClientId) {
throw new Error(
"Missing signatureClientId variable to configure the client id for request signature."
);
}
const signatureClientSecret =
pm.variables.get("signatureClientSecret") ||
pm.environment.get("signatureClientSecret");
if (!signatureClientSecret) {
throw new Error(
"Missing signatureClientSecret variable to configure the secret for request signature."
);
}
const signatureDebug =
pm.variables.get("signatureDebug") || pm.environment.get("signatureDebug");
const signatureHeaderName =
pm.variables.get("signatureHeaderName") ||
pm.environment.get("signatureHeaderName") ||
"X-RequestSignature";
let signatureBodySourceComponents = pm.variables.get(
"signatureBodySourceComponents"
) ||
pm.environment.get("signatureBodySourceComponents") || [
"Nonce",
"Timestamp",
"Method",
"Scheme",
"Host",
"LocalPath",
"QueryString",
"Body"
];
if (typeof signatureBodySourceComponents === "string") {
signatureBodySourceComponents = JSON.parse(signatureBodySourceComponents);
}
const signaturePattern =
pm.variables.get("signaturePattern") ||
pm.environment.get("signaturePattern") ||
"{ClientId}:{Nonce}:{Timestamp}:{SignatureBody}";
// Variables
const timestamp = Math.round(new Date() / 1000);
const nonce = require("uuid").v4();
const resolvedRequest = new sdk.Request(
pm.request.toJSON()
).toObjectResolved(null, [pm.variables.toObject()], {
ignoreOwnVariables: true
});
const url = new sdk.Url(resolvedRequest.url);
// Utilities
const utf8Bytes = input => input.split("").map(c => c.charCodeAt(0));
const byteArrayToWordArray = byteArray => {
const wordArray = [];
for (let i = 0; i < byteArray.length; i++) {
wordArray[(i / 4) | 0] |= byteArray[i] << (24 - 8 * i);
}
return CryptoJS.lib.WordArray.create(wordArray, byteArray.length);
};
if (
resolvedRequest.header &&
resolvedRequest.header.find(x => x.key === signatureHeaderName)
) {
return;
}
// Signature body computation
let signatureBodySource = [];
signatureBodySourceComponents.forEach(component => {
switch (component) {
case "Method":
signatureBodySource.push(
...utf8Bytes(resolvedRequest.method.toUpperCase())
);
break;
case "Scheme":
let scheme = url.getRaw().split("://")[0];
signatureBodySource.push(...utf8Bytes(scheme));
break;
case "Host":
let host = url
.getRaw()
.split("://")[1]
.split(":")[0];
signatureBodySource.push(...utf8Bytes(host));
break;
case "Port":
let port = url
.getRaw()
.split("://")[1]
.split(":")[1];
if (port) {
signatureBodySource.push(...utf8Bytes(port));
}
break;
case "LocalPath":
let localPath = url.getPath();
if (localPath) {
signatureBodySource.push(...utf8Bytes(localPath));
}
break;
case "QueryString":
let queryString = url.getQueryString();
if (queryString) {
signatureBodySource.push(...utf8Bytes(`?${queryString}`));
}
break;
case "Body":
if (resolvedRequest.body) {
let requestBody = new sdk.RequestBody(resolvedRequest.body);
if (requestBody.urlencoded) {
// This is a patch for faulty Postman RequestBody implementation.
requestBody.urlencoded.remove(x => x.disabled);
requestBody.urlencoded.each(x => {
if (x.value) {
x.value = encodeURI(x.value);
}
});
}
if (requestBody.toString()) {
signatureBodySource.push(...utf8Bytes(requestBody.toString()));
}
}
break;
case "Timestamp":
signatureBodySource.push(...utf8Bytes(`${timestamp}`));
break;
case "Nonce":
signatureBodySource.push(...utf8Bytes(`${nonce}`));
break;
default:
if (!component.startsWith("Header")) {
throw new Error(
`Unrecognized signature body source component ${component}.`
);
}
const headerName = component.substr("Header".length);
const header =
resolvedRequest.header &&
resolvedRequest.header.find(x => x.key === headerName);
if (header && header.value) {
signatureBodySource.push(...utf8Bytes(header.value));
}
}
});
if (signatureDebug) {
console.info(
"SignatureBodySource",
signatureBodySource.map(x => String.fromCharCode(x)).join("")
);
}
const signatureBody = CryptoJS.enc.Base64.stringify(
CryptoJS.HmacSHA256(
byteArrayToWordArray(signatureBodySource),
signatureClientSecret
)
);
const signature = signaturePattern
.replace("{ClientId}", signatureClientId)
.replace("{Nonce}", `${nonce}`)
.replace("{Timestamp}", `${timestamp}`)
.replace("{SignatureBody}", signatureBody);
pm.request.headers.add({
key: signatureHeaderName,
value: signature
});