Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created October 28, 2024 13:18
Show Gist options
  • Save sibelius/b88e50b8e8d0e08fd77078a3f33534bd to your computer and use it in GitHub Desktop.
Save sibelius/b88e50b8e8d0e08fd77078a3f33534bd to your computer and use it in GitHub Desktop.
validate whatsapp sha 256
const signature = ctx.request.headers['x-hub-signature-256'];
if (
!signature ||
typeof signature !== 'string' ||
!config.WHATSAPP_APP_SECRET
) {
ctx.status = 403;
ctx.body = {
message: 'invalid signature',
}
return;
}
const sha256 = signature.split('=')[1];
// eslint-disable-next-line no-restricted-syntax
const result = crypto
.createHmac('sha256', config.WHATSAPP_APP_SECRET)
.update(ctx.request.rawBody)
.digest('hex');
if (!sha256 || sha256 !== result) {
ctx.status = 403;
ctx.body = {
message: 'invalid signature',
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment