Webhooks signature
To ensure the webhook is authentic and sent by B2Brouter, verify the signature included in the X-B2Brouter-Signature header.
Here is how you can verify the signature on your server:
-
Extract the Header Information:
TheX-B2Brouter-Signatureheader contains two components:t: The UNIX timestamp when the payload was signed.s: The HMAC-SHA256 signature.
Example header:
t=1732530076,s=9f117925ea533a2ea74b19699e03ab5de5664b4ff021d3d01eaff8868ed29e4d -
Recreate the Payload:
Combine the received timestamp (t) and the raw JSON payload (data) using a period (.):payload = "#{timestamp}.#{data}" -
Compute the HMAC-SHA256 Signature:
Use the sharedsignature_keyand the payload to compute the signature:computed_signature = HMAC-SHA256(signature_key, payload) -
Compare Signatures:
Compare thecomputed_signaturewith thesvalue from the header. If they match, the webhook is valid.
Examples
- PHP:
$headers = getallheaders();
$header = $headers['X-B2Brouter-Signature'];
preg_match('/t=([^,]+),s=(.+)/', $header, $matches);
$timestamp = $matches[1];
$signatureHash = $matches[2];
$recalculatedHash = hash_hmac(
'sha256',
$timestamp . '.' . file_get_contents('php://input'),
'your_webhook_secret'
);
$isValid = hash_equals($signatureHash, $recalculatedHash);Updated 3 days ago