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

fix: add cookie support for HTTP bearer authentication #949

Merged
merged 5 commits into from
Oct 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/middlewares/openapi.security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,21 @@ class AuthValidator {
const authHeader =
req.headers['authorization'] &&
req.headers['authorization'].toLowerCase();
const authCookie = req.cookies[scheme.name] || req.signedCookies?.[scheme.name];

if (!authHeader) {
throw Error(`Authorization header required`);
if (!authHeader && !authCookie) {
throw Error(`Authorization header or cookie required`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks so much for the CR!
i'm requesting a small update here.
Please retain the existing error message when !authHeader (and the securitySchemw does not specify in: cookie. When in: cookie is specified by the securityScheme throw Cookie authentication required. ref: https://swagger.io/docs/specification/authentication/cookie-authentication/

Copy link
Contributor Author

@SeokHoChoi SeokHoChoi Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks so much for the CR! i'm requesting a small update here. Please retain the existing error message when !authHeader (and the securitySchemw does not specify in: cookie. When in: cookie is specified by the securityScheme throw Cookie authentication required. ref: https://swagger.io/docs/specification/authentication/cookie-authentication/

Thanks for the detailed code review.!!

Implemented the requested changes:

  • Updated auth validation in AuthValidator class
  • Added checks for cookie-based authentication
  • Refined error handling for header and cookie auth
  • Updated error messages as suggested

PR updated and ready for another look. Let me know if anything else needs adjustment.

}

const type = scheme.scheme && scheme.scheme.toLowerCase();
if (type === 'bearer' && !authHeader.includes('bearer')) {
throw Error(`Authorization header with scheme 'Bearer' required`);
if (type === 'bearer') {
if (authHeader && !authHeader.includes('bearer')) {
throw Error(`Authorization header with scheme 'Bearer' required`);
}

if (!authHeader && authCookie === undefined) {
throw Error(`Bearer token required in authorization header or cookie`);
}
}

if (type === 'basic' && !authHeader.includes('basic')) {
Expand Down