-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostman_prescript.js
More file actions
90 lines (70 loc) · 2.74 KB
/
postman_prescript.js
File metadata and controls
90 lines (70 loc) · 2.74 KB
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
const version = 'latest';
const FP_SIGNATURE_PACKAGE = "fp_signature_package";
const FP_SIGNATURE_VERSION = "fp_signature_version";
// Postman-specific header filtering constants
// Only include headers that match these patterns
const POSTMAN_HEADERS_TO_INCLUDE = ["x-fp-.*", "x-currency-code", "x-location-detail", "host"];
// Function to filter headers for Postman
function filterHeadersForPostman(allHeaders) {
const filteredHeaders = {};
for (const [key, value] of Object.entries(allHeaders)) {
// Check if header matches include patterns
let shouldInclude = false;
for (const pattern of POSTMAN_HEADERS_TO_INCLUDE) {
if (new RegExp(pattern, "i").test(key)) {
shouldInclude = true;
break;
}
}
if (shouldInclude) {
filteredHeaders[key] = value;
}
}
return filteredHeaders;
}
const fp_signature_saved_package = pm.collectionVariables.get(FP_SIGNATURE_PACKAGE);
const fp_signature_saved_version = pm.collectionVariables.get(FP_SIGNATURE_VERSION);
if(fp_signature_saved_package && fp_signature_saved_version === version){
addSignature();
}
else{
pm.collectionVariables.unset(FP_SIGNATURE_PACKAGE);
pm.collectionVariables.unset(FP_SIGNATURE_VERSION);
pm.sendRequest(`https://cdn.jsdelivr.net/npm/@gofynd/fp-signature@${version}`, (err, res) => {
if (err || !res || res.code !== 200) {
throw Error("Failed to fetch FP-Signature package.")
}
pm.collectionVariables.set(FP_SIGNATURE_PACKAGE, res.text());
pm.collectionVariables.set(FP_SIGNATURE_VERSION, version);
addSignature();
});
}
function addSignature(){
eval(pm.collectionVariables.get(FP_SIGNATURE_PACKAGE));
// Filter headers using Postman-specific rules
const allHeaders = pm.request.headers.toObject();
const filteredHeaders = filterHeadersForPostman(allHeaders);
// Add x-fp-date header to filtered headers for signature generation
const fpDate = new Date().toISOString().replace(/[:\-]|\.\d{3}/g, '');
filteredHeaders["x-fp-date"] = fpDate;
// Note: Make sure to set FP_SIGNATURE_SECRET in your Postman environment
let signingOptions = {
method: pm.request.method.toUpperCase(),
host: pm.request.url.getHost(),
path: pm.request.url.getPathWithQuery(),
body: pm.request.body ? pm.request.body.toString() : null,
headers: filteredHeaders
}
let signature = FPSignature.sign(signingOptions, { secret: pm.environment.get("FP_SIGNATURE_SECRET") });
if (!signature) {
throw Error("Error generating signature")
}
pm.request.headers.add({
key: "x-fp-date",
value: fpDate
});
pm.request.headers.add({
key: "x-fp-signature",
value: signature
});
}