Shopify / koa-shopify-auth

DEPRECATED Middleware to authenticate a Koa application with Shopify
MIT License
80 stars 63 forks source link

HMAC verification and Koa Body Parser #140

Closed lexus65 closed 1 year ago

lexus65 commented 2 years ago

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); });

devopsangel commented 2 years ago

@lexus65 fyi @shopify/koa-shopify-webhooks

lexus65 commented 2 years ago

@devopsangel thanks, but what is your idea?

devopsangel commented 2 years ago

You can validate webhooks using that library. It will act as middleware.

github-actions[bot] commented 1 year ago

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.