-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifySignature.js
33 lines (25 loc) · 958 Bytes
/
verifySignature.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
const crypto = require('crypto');
const timingSafeCompare = require('tsscmp');
const getRawBody = require('raw-body');
const moment = require('moment');
const TIMESTAMP_WINDOW_MS = 1000 * 60 * 5;
const isVerified = async (req) => {
const signature = req.headers['x-slack-signature'];
const timestamp = req.headers['x-slack-request-timestamp'];
if (!signature || !timestamp) return false;
const hmac = crypto.createHmac('sha256', process.env.SLACK_SIGNING_SECRET);
const [version, hash] = signature.split('=');
// Replay attack protection
if (Math.abs(moment().unix() - Number(timestamp)) > TIMESTAMP_WINDOW_MS) return false;
const rawBody = await getRawBody(req, {
length: req.headers['content-length'],
limit: '1mb',
encoding: true,
});
hmac.update(`${version}:${timestamp}:${rawBody}`);
// Request signature verification
return timingSafeCompare(hmac.digest('hex'), hash);
};
module.exports = {
isVerified,
};