Created
September 14, 2021 20:04
-
-
Save joawan/182afb868d694cec797e1eae5f29e7da 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