Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. const signatureMiddleware = function signatureMiddleware(req, res, next) {
  2. const payload = JSON.stringify(req.body);
  3. // convert the payload to unicode chars
  4. const unicode = payload.replace(/[\u007f-\uffff]/g, (a, i) => {
  5. let hex = payload.charCodeAt(i).toString(16);
  6. let str = `\\u${('000'+hex).slice(-4)}`;
  7. return str;
  8. });
  9.  
  10. // choosing crypto type as sha1
  11. // the hash key is the same as the app secret found in the Facebook developers app page
  12. const hmac = crypto.createHmac('sha1', CONFIG.APP_SECRET);
  13. // hash the unicode-escaped JSON
  14. hmac.update(unicode);
  15.  
  16. const signature = `sha1=${hmac.digest('hex')}`;
  17.  
  18. if (signature === req.headers['x-hub-signature']) {
  19. // signature passed
  20. next();
  21. } else {
  22. // no signature or signature is not valid
  23. console.log(`signature ${signature} is not valid`);
  24. res.status(403).send('forbidden access');
  25. }
  26. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement