代码之家  ›  专栏  ›  技术社区  ›  Vishal G

Xero-打算接收Ruby

  •  0
  • Vishal G  · 技术社区  · 8 年前

    这是来自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 Receive https://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,因此根据文档,我自己制作了payload以使其看起来完全相同

    data = params[:webhook]
    
    payload = {
                 "events": [],
                 "lastEventSequence": data[:lastEventSequence],
                 "firstEventSequence": data[:firstEventSequence],
                 "entropy": data[:entropy]
              }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Vishal G    8 年前

    在研究和调试代码之后,终于找到了一种方法

      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
  •  0
  •   Robin van Dijk    8 年前

    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="
    
    推荐文章