gpt4 book ai didi

javascript - 在 Node 中验证 TypeForm Webhook 负载

转载 作者:行者123 更新时间:2023-11-30 19:33:54 25 4
gpt4 key购买 nike

我设置了一个 Typeform webhook它运作良好。

现在我正在尝试保护它,但我被困在了 Validate payload from Typeform 中部分。

我将概述的步骤和 Ruby 示例(以及 Typeform Helpcenter 发送给我的 PHP example)改编为 Node (Meteor):

const crypto = require('crypto');

function post() {
const payload = this.bodyParams;
const stringifiedPayload = JSON.stringify(payload);

const secret = 'the-random-string';

const receivedSignature = lodash.get(request, 'headers.typeform-signature', '');

const hash = crypto
.createHmac('sha256', secret)
.update(stringifiedPayload, 'binary')
.digest('base64');
const actualSignature = `sha256=${hash}`;

console.log('actualSignature:', actualSignature);
console.log('receivedSignature:', receivedSignature);

if (actualSignature !== receivedSignature) {
return { statusCode: 200 };
}

// .. continue ..
});

但是 actualSignaturereceivedSignature 永远不匹配,我得到的结果如下:

actualSignature: sha256=4xe1AF0apjIgJNf1jSBG+OFwLYZsKoyFBOzRCesXM0g=
receivedSignature: sha256=b+ZdBUL5KcMAjITxkpzIFibOL1eEtvN84JhF2+schPo=

为什么会这样?

最佳答案

您需要使用原始二进制请求,它在文档中指定 here

Using the HMAC SHA-256 algorithm, create a hash (using created_token as a key) of the entire received payload as binary.

这是一个使用 express 和 body-parser 中间件的例子

const crypto = require('crypto');
const express = require("express");
const bodyParser = require('body-parser');

const TYPEFORM_SECRET = 'your-secret';

const app = express();
const port = 3000;

app.use(bodyParser.raw({ type: 'application/json' }));

app.post(`/webhook`, (req, res) => {
const expectedSig = req.header('Typeform-Signature');

const hash = crypto.createHmac('sha256', TYPEFORM_SECRET)
.update(req.body)
.digest('base64');

const actualSig = `sha256=${hash}`;

if (actualSig !== expectedSig) {
// invalid request
res.status(403).send();
return;
}

// successful

res.status(200).send();
});

app.listen(port, () => {
console.log(`listening on port ${port}!`);
});

关于javascript - 在 Node 中验证 TypeForm Webhook 负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56149652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com