-
-
Save tiagosiebler/0f835525dd658aba7cbe43a6b3dbebb6 to your computer and use it in GitHub Desktop.
Method to verify slack messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require('crypto'); | |
const signSecret = process.env.SLACK_SIGN_SECRET; | |
const validateRequest = (requestSignature, requestTime, rawBody, validFor = 300) => { | |
const requestValidFrom = Math.floor(Date.now() / 1000) - validFor; | |
if (requestTime < requestValidFrom) { | |
throw new Error(`Request outdated: !(${requestTime} < ${requestValidFrom})`); | |
} | |
const hmac = crypto.createHmac('sha256', signSecret); | |
const [version, hash] = requestSignature.split('='); | |
hmac.update(`${version}:${requestTime}:${rawBody}`); | |
const digest = hmac.digest('hex'); | |
if (hash !== digest) { | |
throw new Error(`Request signature mismatch: !('${hash}' === '${digest}')`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment