Signature Verification
Webhook Signatures
Setting Up a Webhook URL:
When you setup a webhook URL for a terminal using the endpoint
/Terminals/{id}/set-webhook-url
, BeadPay generates a newsigningSecret
in base64 format.This
signingSecret
is included in the HTTP response.
Receiving a Webhook Request:
When BeadPay sends a webhook request to your specified URL, it includes an
x-webhook-signature
header in the request.The
x-webhook-signature
header contains the timestamp of the request and the signature, formatted ast=<timestamp>,s=<base64 signature>
.
Preparing for Signature Verification:
To begin the verification process, extract the timestamp (
t
) and the base64 signature (s
) from thex-webhook-signature
header.Prepare a concatenated string using the extracted timestamp and the raw body of the request. The format should be
<timestamp>.<raw body>
.
Generating a Signature for Verification:
Decode the base64 formatted
signingSecret
that you received when setting up the webhook.Using a suitable cryptographic library in your programming environment, generate a new HMAC-SHA256 signature using the decoded
signingSecret
as the key and the concatenated string from step 3 as the message.
Comparing the Signatures:
Compare the HMAC-SHA256 signature you generated with the base64 signature (
s
) extracted from thex-webhook-signature
header.If the two signatures match, it confirms the integrity of the request and the authenticity of the sender.
If they do not match, the request should be considered tampered with or not from BeadPay, and appropriate action should be taken.
Node.js examples
Webhook receiver
This code snippet creates a Node.js server that acts as a Webhook Receiver. It listens for incoming webhook requests at /webhook
, verifies the HMAC SHA256 signature to ensure the requests are from a trusted source, and processes the valid requests.
Webhook sender
If you want to test your Webhook Receiver, here is a Node.js example that simulates sending webhook requests. The script creates a request with an HMAC signature and sends it to the specified webhook endpoint (in this case, http://localhost:3000/webhook
). The receiver can then validate this request to ensure its Webhook Receiver is functioning correctly.
Last updated