【发布时间】:2019-10-21 03:31:03
【问题描述】:
我完全按照 shopify 文档 (Shopify app with Node and Express) 中的代码将应用程序与 express 集成,但似乎我仍然遇到 HMAC 验证失败。
const map = Object.assign({}, req.query);
delete map['signature'];
delete map['hmac'];
const message = querystring.stringify(map);
const providedHmac = Buffer.from(hmac, 'utf-8');
const generatedHash = Buffer.from(
crypto
.createHmac('sha256',this.configService.get('SHOPIFY_API_SECRET'))
.update(message)
.digest('hex'),
'utf-8'
);
let hashEquals = false;
// timingSafeEqual will prevent any timing attacks. Arguments must be buffers
try {
hashEquals = crypto.timingSafeEqual(generatedHash, providedHmac)
// timingSafeEqual will return an error if the input buffers are not the same length.
} catch (e) {
hashEquals = false;
};
if (!hashEquals) {
return res.status(400).send('HMAC validation failed');
}
我希望上面的代码能够正常工作并且不会返回错误。
【问题讨论】:
标签: node.js express shopify hmac shopify-app