json - 如何验证 uber webhook api?

标签 json node.js hmac webhooks uber-api

优步 documentation

The value of this field is a hexadecimal HMAC signature of the webhook HTTP request body, using the client secret as a key and SHA256 as the hash function.

但是 HTTP 请求主体是什么?我假设它是从 webhook ( https://developer.uber.com/docs/webhooks#section-example-post ) 收到的 JSON 正文。

如果是,那么如何在 NodeJS 中验证它,因为 HMAC 的加密模块不接受 JSON[我尝试将 JSON 字符串化,但它生成了不同的哈希值]。或者如何将 JSON 转换为缓冲区,因为这是下一个最佳选择

如果没有,那我应该使用什么?

[UPDATE1] 用于任务的代码:

app.post("/",function(req,res){
const crypto = require('crypto');
var input = res.body
var str_input=JSON.stringify(input)

const hmac = crypto.createHmac('sha256', '<CLIENT SECRET>');

hmac.update(str_input);
console.log(hmac.digest('hex')); // print same as below
console.log("e034ac7db29c3c0c10dfeced41a6cd850ed74c1c3c620863d47654cc7390359a")
})

最佳答案

更新的答案

Uber 认为将反斜杠插入 webhook 主体是一个错误,并发布了一个修复程序。下面的解决方法现在将中断比较。自 2016 年 4 月 28 日起,用 Node 编写的客户端应该只执行比较而不修改 webhook 主体。使用不具有 Node 忽略转义序列中黑斜杠行为的语言的客户端不受影响。

原始答案

JS ignores the backslash when reading escape sequences in a string .缺少的反斜杠破坏了您的比较,因为它包含在 webhook 事件签名中。

一个直接的解决方法是使用正则表达式重新插入这些反斜杠。

var reconstitutedWebhookBody = input.replace(/\//g, '\\' + '/');

如果 webhook 开始包含其他可转义字符,则需要扩展该正则表达式。

关于json - 如何验证 uber webhook api?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35950162/

相关文章:

javascript - 在 npm 包中使用对等依赖

Node.js setTimeout 分辨率很低且不稳定

node.js - 如何在开发人员/客户端计算机上的工作目录上监听 svn commit 事件?

openssl - 如何将二进制 key 传递给 openssl

c# - 使用 CryptoStreams 加密和 HMAC 数据

php - 使用Ajax,使用mysql数据库从服务器下载文件

json - AWS 云形成 : "Parameter [subnetIds] is invalid"

javascript - 将嵌套 JSON 输出到带有子列表项的 <ul> 中

java - 来自 StackExchange API 的 JSON URL 返回乱码?

python - Python : HMAC Library vs Hashlib Produces Different Results 中的 HMAC SHA256