在 ruby 至 php 中签名

Signature in ruby to php

我已经创建 api 来接收 Typeform(调查 https://www.typeform.com/)的数据。它工作正常。 但是,在安全的 webhooks 中,https://developer.typeform.com/webhooks/secure-your-webhooks/ 显示了如何在 ruby 中创建签名以在请求中进行比较的示例。

在 laravel 我使用这个:

$body = $req->getContent();
$sig_internal = base64_encode(hash_hmac('sha256', 'testing', $body, true));

我在 webhooks 中配置的 "secret" 是 "testing"。 但是价值观总是不同的。 有人试过这个吗?

在 运行 代码之前,请确保您使用 Typeform 正确设置了您的密码: How to set up your webhook

更多信息在这里: https://developer.typeform.com/webhooks/secure-your-webhooks/

完成后让我们进入代码。我在这里粘贴你需要一步一步做的事情:

<?php

  namespace App\Http\Controllers;
  use Illuminate\Http\Request;

  class WebhookController extends Controller
  {
     public function index(Request $request){

     // Get your data (toString) and the typeform signature
     $data = (string) $request->getContent();
     $typeformSignature = $request->header('typeform-signature');        

     // Set your key 
     $key = 'test'; // <-- In prod I recommend adding it to the .env file and referring it here 

     // Run your hash
     $hashed = hash_hmac('sha256', $data, $key, $raw_output = TRUE);

     // Encode to Base64
     $base64 = base64_encode($hashed);

     // Append to string
     $endValue = "sha256=". $base64;

     // This bit here will output stuff to your console if you are running artisan
     error_log($endValue);
     error_log($request->header('typeform-signature'));

     // If it's good it should praise the good Bro, otherwise you are a bad bro. :) 
     if($request->header('typeform-signature') === $endValue){
        error_log('Well done bro!');
     } else {
        error_log('Sorry bro!');
     }        
    }  
  }