0

每当 smartsheet 向我的回调 URL 发出 POST 请求时,我都会尝试验证 Smartsheet 的 Webhook API。以前有人用过这个吗?

每当对我的回调 URL 进行调用时,我都需要验证 POST 请求是否来自 Smartsheet。

按照这里的指南:

To authenticate a callback request:

1. Calculate the HMAC of the webhook's sharedSecret and the request body.
This must be done using the SHA-256 cryptographic hash algorithm.

2. Format the calculated value as a string in base 16.

3. Compare your result with the value of the Smartsheet-Hmac-SHA256 header of the request.

我正在使用 Javascript。我能够生成一个哈希。我尝试了几种方法,但都没有奏效。根据最佳实践和我之前的工作,这应该有效:

crypto.createHash('sha256', sharedSecret).update(JSON.stringify(body)).digest('hex');

但事实并非如此,我什至也试过这个:

crypto.createHash('sha256').update(sharedSecret+JSON.stringify(body)).digest('hex');

它不工作。

这里的 body 变量来自 req.body,来自 Smartsheet 发送到我的回调 URL 的有效负载,sharedSecret 是 Smartsheet 在我创建 webhook 时提供的秘密。

4

1 回答 1

0

我终于弄明白了。我使用了错误的功能。正确的做法是:

crypto.createHmac('sha256',sharedSecret).update(JSON.stringify(body)).digest('hex');

根据规范,'hex' 与 base 16 相同。

sharedSecret 将是关键,并且请求的主体需要转换为字符串才能使其工作。运行此代码会生成与我们在“smartsheet-hmac-sha256”中完全相同的字符串,因此我们可以进行比较和验证。

于 2020-02-28T18:31:53.543 回答