【问题标题】:Shopify Webhooks Hmac Python verification failsShopify Webhooks Hmac Python 验证失败
【发布时间】:2021-09-25 13:30:30
【问题描述】:

我正在尝试验证从 Shopify 收到的 webhook,但 Hmac 验证失败。

def verify_webhook(data, hmac_header):
    digest = hmac.new(SECRET.encode('utf-8'), data, hashlib.sha256).digest()
    computed_hmac = base64.b64encode(digest)
    return hmac.compare_digest(computed_hmac, hmac_header.encode('utf-8'))


@app.route('/productCreation', methods=['POST'])
def productCreation():
    data = request.data
    verified = verify_webhook(
        data, request.headers.get('X-Shopify-Hmac-SHA256'))
    
    if(verified):
        return ("Success", 200)
    return("Integrity error", 401)

得到错误

hash = hmac.new(SECRET.encode('utf-8'), body.encode('utf-8'), hashlib.sha256)
AttributeError: 'bytes' object has no attribute 'encode'

有人可以帮忙吗?我正在为此开发一个 Flask 应用程序。

【问题讨论】:

    标签: python flask shopify hmac shopify-app


    【解决方案1】:

    您收到属性错误,这意味着您的 body 对象不是字符串(encode() 对类似字符串的对象有效),如错误消息中所述,它是类似“字节”的目的。去除那个 .encode('utf-8') 来自 正文

    【讨论】:

      【解决方案2】:

      2022 年 3 月更新

      Shopify 似乎已经用我在下面建议的相同更改更新了他们的 Flask 示例。

      原答案

      我在使用Shopify's Flask example时遇到了同样的错误

      from flask import Flask, request, abort
      import hmac
      import hashlib
      import base64
      
      app = Flask(__name__)
      
      SECRET = 'hush'
      
      def verify_webhook(data, hmac_header):
          digest = hmac.new(SECRET, data.encode('utf-8'), hashlib.sha256).digest()
          computed_hmac = base64.b64encode(digest)
      
          return hmac.compare_digest(computed_hmac, hmac_header.encode('utf-8'))
      
      @app.route('/webhook', methods=['POST'])
      def handle_webhook():
          data = request.get_data()
          verified = verify_webhook(data, request.headers.get('X-Shopify-Hmac-SHA256'))
      
          if not verified:
              abort(401)
      
          # process webhook payload
          # ...
      
          return ('', 200)
      

      为了让它工作,我必须通过以下方式修改 verify_webhook:

      • 编码 SECRET,因为 hmac.new() 需要以字节为单位的密钥,而不是字符串。
      • 不编码数据,因为 Flask 的 response.get_data 已经返回编码的字节串。

      最终代码

      from flask import Flask, request, abort
      import hmac
      import hashlib
      import base64
      
      app = Flask(__name__)
      
      SECRET = 'hush'
      
      def verify_webhook(data, hmac_header):
          digest = hmac.new(SECRET.encode('utf-8'), data, hashlib.sha256).digest()
          computed_hmac = base64.b64encode(digest)
      
          return hmac.compare_digest(computed_hmac, hmac_header.encode('utf-8'))
      
      @app.route('/webhook', methods=['POST'])
      def handle_webhook():
          data = request.get_data()
          verified = verify_webhook(data, request.headers.get('X-Shopify-Hmac-SHA256'))
      
          if not verified:
              abort(401)
      
          # process webhook payload
          # ...
      
          return ('', 200)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多