Polar.sh Webhook Signature Verification & License Handler

'use strict'; const crypto = require('crypto'); /** * Verify Polar.sh webhook signature using Standard Webhooks HMAC-SHA256 */ function verifyPolarWebhook(rawBody, headers, webhookSecret) { const webhookId = headers['webhook-id']; const webhookTimestamp = headers['webhook-timestamp']; const webhookSignature = headers['webhook-signature']; if (!webhookId || !webhookTimestamp || !webhookSignature || !webhookSecret) { return false; } const signedPayload = `${webhookId}.${webhookTimestamp}.${rawBody}`; const key = webhookSecret.startsWith('whsec_') ? webhookSecret.slice(6) : webhookSecret; const keyBuffer = Buffer.from(key, 'base64'); const expectedSig = crypto .createHmac('sha256', keyBuffer) .update(signedPayload, 'utf8') .digest('base64'); const passedSignatures = webhookSignature.split(' ').map(s => s.replace(/^v1,/, '')); return passedSignatures.some(sig => { try { return crypto.timingSafeEqual(Buffer.from(sig, 'utf8'), Buffer.from(expectedSig, 'utf8')); } catch { return false; } }); } module.exports = { verifyPolarWebhook };
Polar.sh Webhook Signature Verification & License Handler
Note
Notification