【问题标题】:Xero - Intent To Receive RubyXero - 接收 Ruby 的意图
【发布时间】:2018-08-22 09:01:37
【问题描述】:

这是来自 Xero 的参数

{"events"=>nil,
 "firstEventSequence"=>0,
 "lastEventSequence"=>0,
 "entropy"=>"KFDXIMNLPDAMRBOEVAVF",
 "controller"=>"admin/billing/webhooks",
 "action"=>"handle_hook",
 "webhook"=>
  {"events"=>nil,
   "firstEventSequence"=>0,
   "lastEventSequence"=>0,
   "entropy"=>"KFDXIMNLPDAMRBOEVAVF"}}

我要验证Intent To Receivehttps://developer.xero.com/documentation/webhooks/configuring-your-server#intent

data = {"events"=>nil, "firstEventSequence"=>0, "lastEventSequence"=>0, "entropy"=>"KFDXIMNLPDAMRBOEVAVF"}

Key = HRj6QPub9BNE4MWewrcLrkKFjpiikV1KrlMZCvDawyDR95MGkkuE2y1DXFP1tifsEWaJygLx6zG0r9rXVTflcg==

在下面尝试过

Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), key, data)).strip()

*** TypeError Exception: no implicit conversion of ActionController::Parameters into String

hash  = OpenSSL::HMAC.digest('sha256', key, data)
*** TypeError Exception: no implicit conversion of ActionController::Parameters into String

如何在这里实现?

为确保您收到的请求来自 Xero,您需要验证 x-xero-signature 标头中提供的签名。创建或重新启用 webhook 订阅(或更新订阅 url)时,将提示用户启动“接收意向”验证。此验证过程将是对订阅中提供的 url 的一系列 HTTPS POST 请求。

编辑 Rails 正在将空白数组转换为 nil,所以我根据文档自己制作了有效载荷,使其看起来完全相同

data = params[:webhook]

payload = {
             "events": [],
             "lastEventSequence": data[:lastEventSequence],
             "firstEventSequence": data[:firstEventSequence],
             "entropy": data[:entropy]
          }

【问题讨论】:

    标签: ruby-on-rails ruby webhooks xero-api xeroizer


    【解决方案1】:

    研究调试代码终于找到了办法

      def handle_hook
        key = ENV['XERO_WEBHOOK_KEY']
        payload = request.body.read
        calculated_hmac = Base64.encode64(OpenSSL::HMAC.digest('sha256', key, payload))
        if calculated_hmac.strip() == request.headers['x-xero-signature']
          head :ok
        else
          head :unauthorized
        end
      end
    

    在这里使用 Strip() 很神奇

    【讨论】:

      【解决方案2】:

      data 应该是一个字符串。

      digest = OpenSSL::Digest::Digest.new('sha256')
      hmac_digest = OpenSSL::HMAC.digest(digest, key, data['firstEventSequence'].to_s)
      Base64.encode64(hmac_digest).strip() => "JvORQ/sWHvAXO/3nm9vG7+VgAqA93bTSMMIbVHIRgnM="
      

      【讨论】:

      • 其实这不正确。这是我在这里做的第一件事
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多