-
Notifications
You must be signed in to change notification settings - Fork 30
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 middleware expects credentials for CORS-preflight requests #44
Comments
Hi @nfadili - thanks for raising this I see what you mean and I know we put some logic in app.use(cors());
app.use(auth()); I also feel a little uncomfortable about letting the request decide if it should bypass authentication checks (although this is probably overkill) |
Hey @adamjmcgrath thanks for the quick reply! It is my understanding that the I do agree that it makes sense for this lib to not be opinionated on what requests to bypass auth on 😄 so I think the existing behavior makes sense! It is an important behavioral difference from the last lib Auth0 maintained ( |
Hi @nfadili
The default value for Also, the cors middleware docs recommend you put the cors middleware before other routes (see https://expressjs.com/en/resources/middleware/cors.html#enabling-cors-pre-flight). Which is what we do in the example app https://github.com/auth0/node-oauth2-jwt-bearer/blob/main/packages/examples/express-api.ts#L20 |
Ahhhh I see now. Thanks for the explanation and links, the current behavior makes total sense 👍 please feel free to close this out! 👍 |
Description
I have noticed through testing and looking at the source code that the
auth
middleware from theexpress-oauth2-jwt-bearer
package is checking for credentials for all requests. The potential issue here is that credentials are not meant to be included in CORS-preflight OPTIONS requests, yet the middleware still expects credentials to exist and will dutifully respond with a 401 to the preflight request.The reason I ran into this situation is that I used your doc examples that apply the middleware with
app.use(auth({ ... }))
. Theapp.use
attaches the middleware to every request, and therefore this issue arises. I feel like the options here are:auth
middleware to and if they apply it to all requests, then cross-origin requests will not work.app.get('/', auth({ ... }), handler)
auth
middleware explicitly skips OPTIONS requests because it shouldn't expect credentials to be included.I can see why this middleware would want to remain un-opinionated about things like this, but please let me know what you think. I'd be happy to contribute a PR if that would be helpful.
References:
Reproduction
Using the example in your getting started section:
Environment
The text was updated successfully, but these errors were encountered: