-
Notifications
You must be signed in to change notification settings - Fork 0
/
security.js
29 lines (23 loc) · 871 Bytes
/
security.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
const crypto = require("crypto");
exports.validateSlackRequest = (event, signingSecret) => {
const requestBody = event["body"];
const headers = makeLower(event.headers);
const timestamp = headers["x-slack-request-timestamp"];
const slackSignature = headers["x-slack-signature"];
const baseString = 'v0:' + timestamp + ':' + requestBody;
const hmac = crypto.createHmac("sha256", signingSecret)
.update(baseString)
.digest("hex");
const computedSlackSignature = "v0=" + hmac;
const isValid = computedSlackSignature === slackSignature;
return isValid;
};
const makeLower = (headers) => {
let lowerCaseHeaders = {}
for (const key in headers) {
if (headers.hasOwnProperty(key)) {
lowerCaseHeaders[key.toLowerCase()] = headers[key].toLowerCase()
}
}
return lowerCaseHeaders
}