[英]Why does my Github secret fail (change on each event)?
我正在尝试验证 Github webhook 密码,但每次我在 Github 中触发事件时, req.headers['x-hub-signature'])
的值都会发生变化,这没有任何意义。
NodeJs: sha1=b57ad18e45f71ac069d15618f6ca547ed75bb2e9
Github: sha1=0b6ff08d557b240dbadedb2a0c1054ce69f2d93e <----
NodeJs: sha1=b57ad18e45f71ac069d15618f6ca547ed75bb2e9
Github: sha1=15e3d5edae00951abb180e9eaea9a6278d8f8d0b <----
请注意,来自 Githit hub 的秘密每次都不同!
我发现其他人验证了这个秘密,但我看不出他们的代码与我的代码有何不同。
谁能弄清楚为什么我在每个事件中都从 Github 得到不同的秘密? 还是我做错了什么?
const express = require("express");
const bodyParser = require("body-parser");
const crypto = require('crypto');
const secret = "x";
const app = express();
const PORT = 8080;
app.use(bodyParser.json());
app.post("/", (req, res) => {
let sig = "sha1=" + crypto.createHmac('sha1', secret).digest('hex');
console.log('NodeJs: ' + sig);
console.log('Github: ' + req.headers['x-hub-signature']);
res.status(200).end();
});
app.listen(PORT, () => console.log(`Github wekhook listening on port ${PORT}`));
req.headers['x-hub-signature'])
不是秘密的 hash,而是用秘密签名的req.body
。 这就是为什么每个事件都不同的原因。
const express = require("express");
const bodyParser = require("body-parser");
const crypto = require('crypto');
const secret = "x";
const app = express();
const PORT = 8080;
app.use(bodyParser.json());
function isSigOk(request, secret) {
// calculate the signature
const expectedSignature = "sha1=" +
crypto.createHmac("sha1", secret)
.update(JSON.stringify(request.body))
.digest("hex");
// compare the signature against the one in the request
const signature = request.headers["x-hub-signature"];
if (signature !== expectedSignature) {
throw new Error("Invalid signature.");
};
};
app.post("/", (req, res) => {
// will throw an error if not ok
isSigOk(req, secret);
// Do stuff here
res.status(200).end();
});
app.listen(PORT, () => console.log(`Github wekhook listening on port ${PORT}`));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.