Security

HMAC Verification

To ensure webhooks are authentically from Joy Loyalty, each webhook request includes an X-Joy-Loyalty-Hmac-Sha256 header containing the HMAC-SHA256 signature.

Verification process

  1. Get the raw body of the incoming webhook request

  2. Calculate HMAC-SHA256 using your shop's secret key

  3. Compare the calculated hash with the header value

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

  • Always verify HMAC signatures to ensure webhook authenticity

  • Use timing-safe comparison functions to prevent timing attacks

  • Store secret keys securely using environment variables or secure key management

HTTPS requirements

  • Webhook endpoints must use HTTPS for secure data transmission

  • Use valid SSL certificates - self-signed certificates are not supported

  • Consider certificate pinning for additional security

IP whitelisting

Consider implementing IP whitelisting for additional security:

Data privacy

  • Webhook payloads contain customer PII including emails and names

  • Ensure GDPR/CCPA compliance in your data handling

  • Implement proper access controls and audit logging

  • Consider data retention policies for webhook data

Last updated