如何在node.js/express中修改Shopify webhook签名?

sha*_*sol 4 webhooks node.js shopify express

Shopify提供Ruby和PHP中的示例来实现此目的.在我的节点/快递应用程序中,我尝试:

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

并且

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

但是它们都没有提供与Shopify作为签名发送的字符串相同的字符串.

小智 5

有点旧,但我想发布我的解决方案:

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');
  }
});
Run Code Online (Sandbox Code Playgroud)