【问题标题】:How to Validate a Xero webhook payload with HMACSHA256 Node js如何使用 HMACSHA256 Node js 验证 Xero webhook 有效负载
【发布时间】:2020-12-24 06:49:51
【问题描述】:

我需要在我的 node js 项目中验证 Xero webhook。这是要验证的 Xero 文档步骤:https://developer.xero.com/documentation/webhooks/creating-webhooks#STATUS

var crypto = require("crypto")
function getHmacSha256(message, secret) {
        return crypto.createHmac("sha256", secret).update(message).digest("base64")
}

// webhookPayload and signature get from webhook body and header
const webhookPayload = {
  events: [],
  firstEventSequence: 0,
  lastEventSequence: 0,
  entropy: 'OSHPXTUSXASRFBBCJFEN'
}
const signature = "OXLaeyZanKI5QDnLkXIVB35XrZygYsPMeK8WfoXUMU8="


const myKey = "1y5VYfv7WbimUQIMXiQCB6W6TKIp+5ZZJNjn3Fsa/veK5X/C8BZ4yzvPkmr7LvuL+yfKwm4imnfAB5tEoJfc4A=="

var hash = getHmacSha256(JSON.stringify(webhookPayload), myKey)

//If the payload is hashed using HMACSHA256 with your webhook signing key and base64 encoded, it should match the signature in the header.

if (signature === hash) {
     return res.status(200).end()
}else{
     return res.status(401).end() 
}

每次我的签名和哈希都不同,所以每次都返回 401。 所以我没能完成Intent to receive

【问题讨论】:

    标签: node.js webhooks hmac xero-api xero


    【解决方案1】:

    根据您的描述,我猜您无意中修改了请求正文。您需要接受来自 webhook 事件的原始请求正文而不进行修改。如果这个主体被完全修改,您的代码将无法验证签名,并将无法通过 Xero 的“接收意图”验证。详情请查看blog post。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      • 2017-05-12
      • 2017-02-11
      • 2018-08-08
      相关资源
      最近更新 更多