Last active
March 9, 2021 14:29
-
-
Save armiiller/72e4729372036cd43536f4f799dd2b22 to your computer and use it in GitHub Desktop.
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
// Verify this actually came from our vendor | |
const signature = _.toString(req.headers['x-${BRAND}-signature']); | |
const timestamp = _.toString(req.headers['x-${BRAND}-timestamp']); | |
if(!signature || !timestamp || !_.parseInt(timestamp) || !moment.unix(_.parseInt(timestamp)).isBetween(moment().add(-5, 'm'), moment().add(1, 'm'))){ | |
res.status(httpStatusCodes.BAD_REQUEST).send(); | |
return; | |
} | |
// The initial required fields are there and they are within the time flex range, compute the expected hash | |
const hmac = crypto.createHmac('sha256', config.SIGNING_SECRET); | |
const [version, hash] = signature.split('='); | |
hmac.update(`${version}:${timestamp}:${JSON.stringify(req.body)}`); | |
// check the computed hash is what we expect | |
if(hmac.digest('hex') !== hash){ | |
res.status(httpStatusCodes.BAD_REQUEST).send(); | |
return; | |
} | |
// Ok, looks like the vendor actually sent the request, lets try to process their request |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment