Skip to content

Instantly share code, notes, and snippets.

@armiiller
Last active March 9, 2021 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armiiller/72e4729372036cd43536f4f799dd2b22 to your computer and use it in GitHub Desktop.
Save armiiller/72e4729372036cd43536f4f799dd2b22 to your computer and use it in GitHub Desktop.
// 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