Webhook Security
Every webhook request from ConsentForge includes an HMAC-SHA256 signature. Verify this signature before processing the payload to ensure the request is genuine and hasn't been tampered with.
Request headers
| Header | Description |
|---|---|
X-ConsentForge-Signature | HMAC-SHA256 hex digest of {timestamp}.{body} |
X-ConsentForge-Timestamp | Unix timestamp (seconds) when the event was sent |
X-ConsentForge-Delivery-ID | Unique ID for this delivery (use for idempotency) |
Verification algorithm
- Read
X-ConsentForge-Timestampfrom the request header - Read
X-ConsentForge-Signaturefrom the request header - Build the signing string:
{timestamp}.{raw_request_body} - Compute HMAC-SHA256 of the signing string using your webhook secret
- Compare (timing-safe) with the received signature
- Reject if timestamp is more than 5 minutes old (replay protection)
Code examples
- PHP
- Node.js
- Python
- Ruby
function verifyConsentForgeWebhook(
string $rawBody,
string $signature,
string $timestamp,
string $secret
): bool {
// Reject if too old (5 minutes)
if (abs(time() - (int)$timestamp) > 300) {
return false;
}
$signingString = $timestamp . '.' . $rawBody;
$expected = hash_hmac('sha256', $signingString, $secret);
return hash_equals($expected, $signature);
}
// Usage in a Laravel controller:
$rawBody = $request->getContent();
$signature = $request->header('X-ConsentForge-Signature');
$timestamp = $request->header('X-ConsentForge-Timestamp');
$secret = config('services.consentforge.webhook_secret');
if (!verifyConsentForgeWebhook($rawBody, $signature, $timestamp, $secret)) {
return response('Unauthorized', 401);
}
$payload = $request->json()->all();
const crypto = require('crypto');
function verifyConsentForgeWebhook(rawBody, signature, timestamp, secret) {
// Reject if too old (5 minutes)
if (Math.abs(Date.now() / 1000 - parseInt(timestamp)) > 300) {
return false;
}
const signingString = `${timestamp}.${rawBody}`;
const expected = crypto
.createHmac('sha256', secret)
.update(signingString)
.digest('hex');
return crypto.timingSafeEqual(
Buffer.from(expected, 'hex'),
Buffer.from(signature, 'hex')
);
}
// Usage in Express:
app.post('/webhooks/consentforge', express.raw({ type: '*/*' }), (req, res) => {
const valid = verifyConsentForgeWebhook(
req.body.toString(),
req.headers['x-consentforge-signature'],
req.headers['x-consentforge-timestamp'],
process.env.CONSENTFORGE_WEBHOOK_SECRET
);
if (!valid) return res.status(401).send('Unauthorized');
const payload = JSON.parse(req.body);
// handle payload...
res.json({ received: true });
});
import hmac
import hashlib
import time
def verify_consentforge_webhook(raw_body, signature, timestamp, secret):
# Reject if too old (5 minutes)
if abs(time.time() - int(timestamp)) > 300:
return False
signing_string = f"{timestamp}.{raw_body}"
expected = hmac.new(
secret.encode('utf-8'),
signing_string.encode('utf-8'),
hashlib.sha256
).hexdigest()
return hmac.compare_digest(expected, signature)
# Usage in Flask:
from flask import request, abort
import os
@app.route('/webhooks/consentforge', methods=['POST'])
def handle_webhook():
raw_body = request.get_data(as_text=True)
valid = verify_consentforge_webhook(
raw_body,
request.headers.get('X-ConsentForge-Signature'),
request.headers.get('X-ConsentForge-Timestamp'),
os.environ['CONSENTFORGE_WEBHOOK_SECRET']
)
if not valid:
abort(401)
payload = request.get_json()
# handle payload...
return {'received': True}
require 'openssl'
require 'rack/utils'
def verify_consentforge_webhook(raw_body, signature, timestamp, secret)
# Reject if too old (5 minutes)
return false if (Time.now.to_i - timestamp.to_i).abs > 300
signing_string = "#{timestamp}.#{raw_body}"
expected = OpenSSL::HMAC.hexdigest('SHA256', secret, signing_string)
Rack::Utils.secure_compare(expected, signature)
end
# Usage in Rails:
class WebhooksController < ApplicationController
skip_before_action :verify_authenticity_token
def consentforge
raw_body = request.body.read
valid = verify_consentforge_webhook(
raw_body,
request.headers['X-ConsentForge-Signature'],
request.headers['X-ConsentForge-Timestamp'],
ENV['CONSENTFORGE_WEBHOOK_SECRET']
)
return head :unauthorized unless valid
payload = JSON.parse(raw_body)
# handle payload...
head :ok
end
end
Idempotency
Use X-ConsentForge-Delivery-ID to deduplicate retried deliveries. Store processed delivery IDs and skip duplicates.
Secret rotation
To rotate your webhook secret:
- Generate a new secret in the Dashboard (old secret still active for 24h)
- Update your server to use the new secret
- After 24h, the old secret is invalidated