Closed lexus65 closed 1 year ago
@lexus65 fyi @shopify/koa-shopify-webhooks
@devopsangel thanks, but what is your idea?
You can validate webhooks using that library. It will act as middleware.
Note that this repo is no longer maintained and this issue will not be reviewed. Prefer the official JavaScript API library. If you still want to use Koa, see simple-koa-shopify-auth for a potential community solution.
Struggling with HMAC verification.
But if I'm adding koaBody parser, it hangs up.
I expect it to validate hmac and return 401 if not valid. If HMAC is fine then process webhook.
Any ideas how to solve it?
router.post("/webhooks", koaBody({includeUnparsed: true}), async (ctx, next) => {
try {
if(!webhookValid(ctx.request.body[unparsed], ctx.req.headers["x-shopify-hmac-sha256"])){
ctx.throw('INVALID HMAC', 401);
return next();
}
await Shopify.Webhooks.Registry.process(ctx.req, ctx.res);
} catch (err) {
console.error(err);
err.status = err.statusCode || err.status || 401;
throw err;
}
console.log(
Webhook processed with status code 200);
});