forked from hygraph/hygraph-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (29 loc) · 1020 Bytes
/
index.js
File metadata and controls
38 lines (29 loc) · 1020 Bytes
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
const { createHmac } = require("crypto");
exports.verifyWebhookSignature = ({ body, signature, secret, rawPayload }) => {
const [rawSign, rawEnv, rawTimestamp] = signature.split(", ");
const sign = rawSign.replace("sign=", "");
const EnvironmentName = rawEnv.replace("env=", "");
const Timestamp = parseInt(rawTimestamp.replace("t=", ""));
let payload = JSON.stringify({
Body: rawPayload || JSON.stringify(body),
EnvironmentName,
TimeStamp: Timestamp,
});
const hash = createHmac("sha256", secret).update(payload).digest("base64");
return sign === hash;
};
exports.generateWebhookSignature = ({
body,
environmentName = "master",
secret,
rawPayload,
}) => {
const TimeStamp = Date.now();
const payload = JSON.stringify({
Body: rawPayload || JSON.stringify(body),
EnvironmentName: environmentName,
TimeStamp,
});
const hash = createHmac("sha256", secret).update(payload).digest("base64");
return `sign=${hash}, env=${environmentName}, t=${TimeStamp}`;
};