fastify / fastify-auth

Run multiple auth functions in Fastify
Other
341 stars 56 forks source link

feat: extend composite auth #217

Closed yakovenkodenis closed 7 months ago

yakovenkodenis commented 7 months ago

This PR addresses Issue #216: Extending composite authentication to allow nesting auth arrays with both and and or relations.

If the relation (defaultRelation) parameter is and, then the relation inside sub-arrays will be or. If the relation (defaultRelation) parameter is or, then the relation inside sub-arrays will be and.

auth code resulting logical expression
fastify.auth([f1, f2, [f3, f4]], { relation: 'or' }) f1 OR f2 OR (f3 AND f4)
fastify.auth([f1, f2, [f3, f4]], { relation: 'and' }) f1 AND f2 AND (f3 OR f4)

Checklist

isnifer commented 7 months ago

So, this PR breaks my preHandler:

// I expect [[f1 AND f2 AND Fn] OR [F3 AND F4 AND Fn]], { relation: 'or' }
fastify.auth(
  [
    [fastify.checkAuth('admins'), ...someOtherAdminChecks],
    [fastify.checkAuth('clients'), ...someOtherClientChecks],
  ],
  { relation: 'or' }
)

It runs both paths even first path is passing. I'll try to find the issue in the changes.