如何验证 uber webhook api?
How to validate the uber webhook api?
Uber 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 开始包含其他可转义字符,则需要扩展正则表达式。
我遇到了同样的问题。 Uber 发送 json,在键和值前加空格。像这样
{"event_id": "...", "resource_href": "...", "meta": {"status": "...", "rider_id": "...", "user_id": "...", "resource_id": "..."}, "event_type": "...", "event_time": ...}
您可以在激活 boryparser 之前执行此操作。并根据这些数据创建十六进制
app.use(function (req, res, next) {
let data = "";
req.on('data', function(chunk){data += chunk});
req.on('end', function(){
req.jsonBody = JSON.parse(data);
req.rawBody = data;
req.originalUberReq = data;
});
next();
});
Uber 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 开始包含其他可转义字符,则需要扩展正则表达式。
我遇到了同样的问题。 Uber 发送 json,在键和值前加空格。像这样
{"event_id": "...", "resource_href": "...", "meta": {"status": "...", "rider_id": "...", "user_id": "...", "resource_id": "..."}, "event_type": "...", "event_time": ...}
您可以在激活 boryparser 之前执行此操作。并根据这些数据创建十六进制
app.use(function (req, res, next) {
let data = "";
req.on('data', function(chunk){data += chunk});
req.on('end', function(){
req.jsonBody = JSON.parse(data);
req.rawBody = data;
req.originalUberReq = data;
});
next();
});