yubinTW / fastify-keycloak-adapter

a Fastify plugin for Keycloak
MIT License
19 stars 5 forks source link

feat: :sparkles: add a bypass fn in opts #52

Closed ArnaudTA closed 2 months ago

ArnaudTA commented 2 months ago

As requested in #51 and cause I need it too.

Here is my proposal, a bypass function can be send in options, the function receive the request and return a boolean, if true prevent the authentication methods, if false let the flow goes as usual.

Let me know what you think before I start writing tests.

yubinTW commented 2 months ago

Hi, thanks for your PR. Could you add the document on the Configuration section in READMD.md? and add some test cases for it?

ArnaudTA commented 2 months ago

done but I cannot run test on my computer and I still don't know why, can you give a try to the CI ?

yubinTW commented 2 months ago

hey, I found the cause, since Task is lazy Promise, you have to add () to execute it

please refer the code below (the second line from the bottom)

fastify.addHook('preValidation', (request: FastifyRequest, reply: FastifyReply, done) => {
    pipe(
      request,
      bypassFn,
      TO.match(
        () => {
          done()
        },
        (request) =>
          pipe(
            request,
            filterGrantRoute,
            O.chain(filterExcludedPattern),
            O.match(
              () => {
                done()
              },
              (request) =>
                pipe(
                  request,
                  getBearerTokenFromRequest,
                  O.match(
                    () => authenticationByGrant(request, reply, done),
                    (bearerToken) => authenticationByToken(request, reply, bearerToken, done)
                  )
                )
            )
          )
      )
    )() // <----- Here! 
  })
ArnaudTA commented 2 months ago

thanks for your help fp-ts totally lost me

It works well now !

yubinTW commented 2 months ago

Update to v2.2.0, Thanks your PR !