我正在使用 GitHub webhook 将事件通过管道传输到我的一个应用程序(GitHub 的 Hubot 的一个实例),它使用 sha1 key 进行保护。
我正在使用以下代码来验证传入 webhook 上的哈希值
crypto = require('crypto')
signature = "sha1=" + crypto.createHmac('sha1', process.env.HUBOT_GITHUB_SECRET).update( new Buffer request.body ).digest('hex')
unless request.headers['x-hub-signature'] is signature
response.send "Signature not valid"
return
在 webhook 中传递的 X-Hub-Signature header 如下所示
X-Hub-Signature: sha1=1cffc5d4c77a3f696ecd9c19dbc2575d22ffebd4
我按照 GitHub 的文档准确地传递了 key 和数据,但哈希值总是不同。
这是 GitHub 的文档。 https://developer.github.com/v3/repos/hooks/#example
这是我最有可能误解的部分
secret: An optional string that’s passed with the HTTP requests as an X-Hub-Signature header. The value of this header is computed as the HMAC hex digest of the body, using the secret as the key.
谁能看出我哪里出错了?
最佳答案
似乎不适用于缓冲区,但 JSON.stringify();这是我的工作代码:
var
hmac,
calculatedSignature,
payload = req.body;
hmac = crypto.createHmac('sha1', config.github.secret);
hmac.update(JSON.stringify(payload));
calculatedSignature = 'sha1=' + hmac.digest('hex');
if (req.headers['x-hub-signature'] === calculatedSignature) {
console.log('all good');
} else {
console.log('not good');
}
关于node.js - GitHub Webhook secret 从不验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25767805/