gpt4 book ai didi

javascript - 验证 Node/Express 中的 pubsubhubbub 内容签名

转载 作者:太空宇宙 更新时间:2023-11-04 01:09:17 28 4
gpt4 key购买 nike

我是 Express 新手,正在艰难地实现一个中间件来处理 X-Hub-Signature,如下所述:https://pubsubhubbub.googlecode.com/git/pubsubhubbub-core-0.4.html#authednotify

我想添加一个中间件来处理此问题,然后将请求传递到标准 express.json() 中间件以实际解码正文。

var sigVerifier = function(req, res, next) {

var buf = '';
// Need to accumulate all the bytes... <--- HOW TO DO THIS?

// then calculate HMAC-SHA1 on the content.
var hmac = crypto.createHmac('sha1', app.get('client_secret'));
hmac.update(buf);
var providedSignature = req.headers['X-Hub-Signature'];
var calculatedSignature = 'sha1=' + hmac.digest(encoding='hex');
if (providedSignature != calculatedSignature) {
console.log(providedSignature);
console.log(calculatedSignature);
res.send("ERROR");
return;
}
next();
};

app.use(sigVerifier);
app.use(express.json());

最佳答案

Express 使用 connect 的中间件来处理 json。您可以将选项对象传递给 json 正文解析器,以在继续解析之前验证内容。

function verifyHmac(req, res, buf) {
// then calculate HMAC-SHA1 on the content.
var hmac = crypto.createHmac('sha1', app.get('client_secret'));
hmac.update(buf);
var providedSignature = req.headers['X-Hub-Signature'];
var calculatedSignature = 'sha1=' + hmac.digest(encoding='hex');
if (providedSignature != calculatedSignature) {
console.log(
"Wrong signature - providedSignature: %s, calculatedSignature: %s",
providedSignature,
calculatedSignature);
var error = { status: 400, body: "Wrong signature" };
throw error;
}
}

app.use(express.json({verify: verifyHmac}));

关于javascript - 验证 Node/Express 中的 pubsubhubbub 内容签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19557887/

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