保护uber webhook无法正常工作

时间:2017-02-17 01:25:13

标签: node.js cryptography uber-api

出于某种原因,我无法完成这项工作

require crypto = require('crypto')

const hmac = crypto.createHmac('sha256', 'clientSecret')
const hash = hmac.update(JSON.stringify(req.body)).digest('hex')

if (hash !== req.header('X-Uber-Signature')) {
   return res.json('something is wrong ' + hash + ' ' + req.header('X-Uber-Signature'))
}

return res.json('you got in!')

我按照此处的说明https://developer.uber.com/docs/riders/guides/webhooks#security

但哈希生成不同的值

其他方法也是受欢迎的。

2 个答案:

答案 0 :(得分:1)

这是Python中用于验证webhook的工作实现:

SELECT CONCAT_WS('-', SUBSTRING(p.rrnhs, 1, 3), 
                 SUBSTRING(p.rrnhs, 4, 3),SUBSTRING(p.rrnhs, 7, LENGTH(p.rrnhs))) FROM T

答案 1 :(得分:0)

我遇到了同样的问题。 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();
});

然后

const hash = crypto.createHmac('sha256', secret)
.update(req.originalUberReq)
.digest('hex');
相关问题