【问题标题】:Verifying a Trello webhook验证 Trello webhook
【发布时间】:2021-06-26 02:07:19
【问题描述】:

我在验证 PHP 或 Node.js 中的 Trello webhook 时遇到问题。我直接从这里复制了 Node.js 版本:https://developers.trello.com/page/webhooks

var crypto = require('crypto');

function verifyTrelloWebhookRequest(request, secret, callbackURL) {
  var base64Digest = function (s) {
    return crypto.createHmac('sha1', secret).update(s).digest('base64');
  }
  var content = JSON.stringify(request.body) + callbackURL;
  var doubleHash = base64Digest(base64Digest(content));
  var headerHash = base64Digest(request.headers['x-trello-webhook']);
  return doubleHash == headerHash;
}

但是,在 Node.js 版本中,我确实用值替换了 request、secret 和 callbackURL。我得到的 PHP 版本是:

function h( $s ) {
    return base64_encode( hash_hmac( 'sha1', $s, $secret ) );
}

$incomingHash = $_SERVER['HTTP_X_TRELLO_WEBHOOK'];
$h2 = h( h( $post . $callbackURL ) );
$h1 = h( $incomingHash );   

此时我认为我的“秘密”错了,尽管我已经尝试过令牌、oauth 秘密和 api 密钥或 Trello 更改了哈希算法,但我不想排除我的专业(头脑发热的错误)。 FWIW 我确实验证了回调 URL 与 Trello 的 API 所说的完全一致。

【问题讨论】:

    标签: php node.js


    【解决方案1】:

    所以我通过返回 HMAC 哈希的二进制文件并使用 OAuth1 密钥来使其工作。

    function h( $s ) {
        return base64_encode( hash_hmac( 'sha1', $s, $secret, true ) );
    }
    
    $incomingHash = $_SERVER['HTTP_X_TRELLO_WEBHOOK'];
    $h2 = h( h( $post . $callbackURL ) );
    $h1 = h( $incomingHash );
    

    您可以通过以下方式查找使用的回调 URL:https://developers.trello.com/v1.0/reference#tokenstokenwebhooks

    【讨论】:

    • 您的代码似乎缺少所有变量,或者我不确定您是否将整个代码放在这里。
    • @AshishDwivedi 对不起,我认为它会更清楚。 $secret 是您在此处获取的 oAuth1 密码:trello.com/app-key $callbackURL 是您在创建 webhook 时设置的 URL Trello 调用。 $post 是传入的 post 请求,例如:$post = file_get_contents( "php://input" );。因此,如果 $h1 == $h2 则 webhook 有效。这有帮助吗?
    猜你喜欢
    • 2016-03-16
    • 2019-09-05
    • 1970-01-01
    • 2017-06-24
    • 2021-01-14
    • 2015-11-02
    • 2015-03-28
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多