Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth flow doesn't complete when using AWS Lambda #473

Closed
6 tasks done
rbhalla opened this issue May 7, 2023 · 2 comments
Closed
6 tasks done

Auth flow doesn't complete when using AWS Lambda #473

rbhalla opened this issue May 7, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@rbhalla
Copy link

rbhalla commented May 7, 2023

Checklist

Description

This is my configuration:

auth({
    session: {
      store: new RedisStore({ client: redisClient }),
    },
    // I manually specify which endpoints need auth when defining them
    authRequired: false,
    issuerBaseURL: process.env.ISSUER_BASE_URL,
    baseURL: process.env.BASE_URL,
    clientID: process.env.CLIENT_ID,
    clientSecret: process.env.CLIENT_SECRET,
    secret: process.env.SESSION_SECRET,
    idpLogout: true,
    errorOnRequiredAuth: true,
    routes: {
      postLogoutRedirect: '/logged-out',
    },
    authorizationParams: {
      response_type: 'code',
      audience: process.env.EXTERNAL_API_AUDIENCE,
      scope: process.env.EXTERNAL_API_SCOPE,
    },
  })

Locally this works fine. I've been using Serverless Framework to simulate lambdas on my machine, and the auth flow works correctly:

  1. I hit /login
  2. Middleware redirects to my issuer url
  3. Auth0 redirects back to /callback
  4. The middleware saves info to session, redirects back to /

In production, I also go through the auth flow all the way to step 4, and I even see a Success Login log entry in the Auth0 logs. However, I am not authenticated despite a session being persisted (in redis) with an access token.

Observations

  • Locally the /callback endpoint sets an appSession cookie when it gets a code back. This doesn't seem to be happening in production. Another cookie is present on the final requests instead: auth_verification
  • Despite not having attemptSilentLogin set to true, in production, I can see the skipSilentLogin cookie being set indicating it is being attempted. Locally, I have to login every time I logout, but in production I don't. This seems to indicate my config is not being respected in production, and yet, response_type is respected.
  • Locally the sessions stored only have two keys present: id_token and state. In prod there are more keys: id_token, scope, access_token, refresh_token, expires_at and token_type.
  • When I removed the response_type so the default is used (id_token), again this works fine locally, but in production I get a 400 (Bad Request) on my /callback route. I can see an id_token and state as part of the request, indicating the call from my tenant is correct. However, I get the following error:
BadRequestError: checks.state argument is missing
    at ResponseContext.callback (/var/task/node_modules/express-openid-connect/lib/context.js:354:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

My naive understanding of this code is that references to requests are being stored in memory. If that is correct, this middleware cannot be used in lambdas. However, another github comment seems to indicate it did work for them.

Reproduction

  1. Create a simple app using the connect sample app
  2. Export a handler using serverless-http as described here
  3. Deploy to AWS lambda and try and hit /login

express-openid-connect version

2.16.0

Express version

4.17

Node.js version

16

@rbhalla rbhalla added the bug Something isn't working label May 7, 2023
@rbhalla
Copy link
Author

rbhalla commented May 7, 2023

A follow up, I ran this with DEBUG=* and it seems a little clearer that this is an issue with /callback not setting cookies correctly.

I would suspect my lambda was misconfigured and I wasn't setting cookies correctly, but /login is setting the transactionCookie correctly.

@rbhalla
Copy link
Author

rbhalla commented May 7, 2023

Seems like this was an issue with how AWS handles multiple set-cookie headers on a single response. TLDR: badly.

Not related to this library though, hopefully still useful for anyone who stumbles on the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant