根据此处的说明(https://developer.xero.com/documentation/webhooks/configuring-your-server https://developer.xero.com/documentation/webhooks/configuring-your-server)用于设置和验证 Xero webhook 的接收意图。
计算出的签名应与标头中的签名匹配,以获得正确签名的有效负载。
但是,使用 python 3,计算出的签名与标头中的签名根本不匹配。 Xero 会向订阅 webhook url 发送大量请求,无论正确还是错误。在我的日志中,所有这些请求都返回为 401。因此,下面是我的测试代码,也被证明不匹配。我不知道缺少什么或者我做错了什么。
不要担心这里显示的密钥,我已经生成了另一个密钥,但这是分配给我用于此时散列的密钥。
根据他们的指示,运行此代码应该使签名与标头之一匹配。但根本不接近。
XERO_KEY =
"lyXWmXrha5MqWWzMzuX8q7aREr/sCWyhN8qVgrW09OzaqJvzd1PYsDAmm7Au+oeR5AhlpHYalba81hrSTBeKAw=="
def create_sha256_signature(key, message):
message = bytes(message, 'utf-8')
return base64.b64encode(hmac.new(key.encode(), message,
digestmod=hashlib.sha256).digest()).decode()
# first request header (possibly the incorrect one)
header = "onoTrUNvGHG6dnaBv+JBJxFod/Vp0m0Dd/B6atdoKpM="
# second request header (possibly the correct one)
header = "onoTrUNvGHG6dnaBv+JBJxFodKVp0m0Dd/B6atdoKpM="
payload = {
'events':[],
'firstEventSequence':0,
'lastEventSequence':0,
'entropy':
'YSXCMKAQBJOEMGUZEPFZ'
}
payload = json.dumps(payload, separators=(",", ":")).strip()
signature = create_sha256_signature(XERO_KEY, str(payload))
if hmac.compare_digest(header, signature):
print(True)
return 200
else:
print(False)
return 401