Created
October 28, 2024 13:18
-
-
Save sibelius/b88e50b8e8d0e08fd77078a3f33534bd to your computer and use it in GitHub Desktop.
validate whatsapp sha 256
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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