网络回调签名(Webhook Signature)

Teapplix如何签名推出的网路回调(Webhook)

所有Teapplix HTTP webhooks都带有SHA-256签名。与APIToken一起,每个帐户都有签名密钥(可在Setup-API菜单中找到)。

常用的签名计算算法很简单:提交数据并使用带签名密钥的SHA-256哈希算法计算HMAC哈希值。然后使用base64进行编码并附加为名为X-HMAC-SHA256的HTTP标头。

webhook的接收者应该如何检查签名

检查签名的步骤如下:

1、首先知道您的签名密钥(设置 - API菜单里获取)。
2、当收到数据时,读取标头X-HMAC-SHA256。这是请求签名。
3、使用SHA256算法计算请求体的HMAC哈希值。
4、将步骤3的值用BASE64来编码。这是计算的签名。
5、比较步骤2和步骤4的值。如果它们相等 - 请求正确。

注意:Teapplix以UTF-8编码发送数据。请确保在计算签名之前不做任何编码转换。

代码示例

这是检查接受传入webhooks的PHP脚本的示例。

<?php
$signatureKey = 'a12345';
$headers = apache_request_headers();
$content = @file_get_contents('php://input');
$receivedSignature = isset($headers['X-HMAC-SHA256']) ? $headers['X-HMAC-SHA256'] : '';
$calculatedSignature = base64_encode(hash_hmac('sha256', $content, $signatureKey, true));
if($receivedSignature == $calculatedSignature) {
//correctly signed webhook, accept
} else {
//incorrectly signed webhook, decline
}