π Webhook Security Guide
Mava webhook security guide
Overview
Key Components
Implementation Guide
async function verifyEventSignature(
encryptedEvent: string,
signature: string,
encryptedSymmetricKey: string,
signingKey: string
) {
try {
// Split the encrypted key into IV and symmetric key components
const [iv, symmetricKey] = encryptedSymmetricKey.split(':');
// Extract the private key (removing mava_wh_ prefix)
const key = signingKey.split('_')[2];
const privateKeyBuffer = Buffer.from(key, 'base64');
// Decrypt the symmetric key using RSA with OAEP padding
const decryptedSymmetricKey = crypto.privateDecrypt(
{
key: privateKeyBuffer,
format: 'der',
type: 'pkcs8',
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING
},
Buffer.from(symmetricKey, 'base64')
);
// Create HMAC using the decrypted symmetric key
const hmac = crypto.createHmac('sha256', decryptedSymmetricKey.toString('base64'));
hmac.update(encryptedEvent);
const regeneratedSignature = hmac.digest('hex');
// Compare signatures using a timing-safe comparison
return crypto.timingSafeEqual(
Buffer.from(regeneratedSignature, 'hex'),
Buffer.from(signature, 'hex')
);
} catch (err) {
throw new Error('Failed to verify event signature');
}
}2. Decrypting the Payload
Processing a Webhook
Security Notes
Last updated