Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to veirfy Shopify webhook signature in node.js/express?

Shopify provides examples in Ruby and PHP to accomplish this. In my node/express app I try:

var data = querystring.stringify(req.body);
var calculatedSha256 = crypto.createHmac("SHA256", APP_SECRET).update(new Buffer(data, 'utf8')).digest('base64');

and also

var data = req.body;
var calculatedSha256 = crypto.createHmac("SHA256", APP_SECRET).update(new Buffer(data, 'utf8')).digest('base64');

but none of them provides an identical string to the one Shopify sends as a signature.

like image 416
shaharsol Avatar asked Jan 12 '23 02:01

shaharsol


1 Answers

A bit old, but figured I'd post my solution:

var express      = require('express')
    , bodyParser = require('body-parser')
    , crypto     = require('crypto');

var app = express();

app.use(bodyParser.json({ verify: function(req, res, buf, encoding) {
  req.headers['x-generated-signature'] = crypto.createHmac('sha256', 'SHARED_SECRET')
   .update(buf)
   .digest('base64');
} }));

app.post('/webhook', function(req, res) {
  if (req.headers['x-generated-signature'] != req.headers['x-shopify-hmac-sha256']) {
    return res.status(401).send('Invalid Signature');
  }
});
like image 73
knation Avatar answered Jan 16 '23 20:01

knation