Security
HMAC Verification
Verification process
Verification examples
Node.js
const crypto = require('crypto');
function verifyWebhook(rawBody, hmacHeader, secretKey) {
const calculatedHmac = crypto
.createHmac('sha256', secretKey)
.update(rawBody, 'utf8')
.digest('base64');
return crypto.timingSafeEqual(
Buffer.from(calculatedHmac),
Buffer.from(hmacHeader)
);
}
// Express middleware
app.post('/webhook/points', express.raw({type: 'application/json'}), (req, res) => {
const hmac = req.get('X-Joy-Loyalty-Hmac-Sha256');
const isValid = verifyWebhook(req.body, hmac, process.env.JOY_SECRET_KEY);
if (!isValid) {
return res.status(401).send('Invalid signature');
}
const payload = JSON.parse(req.body);
// Process webhook...
res.status(200).send('OK');
});Python
PHP
Security considerations
HMAC verification best practices
HTTPS requirements
IP whitelisting
Data privacy
Last updated