Skip to content

Commit 79424b2

Browse files
authored
fix: add cookie support for HTTP bearer authentication (#949)
* fix: add cookie support for HTTP bearer authentication - Updated validateHttp() to handle bearer tokens in both authorization header and cookies. - Adapted logic to ensure flexibility for projects using HTTP-only cookies instead of headers for authentication. * fix: Refine HTTP authentication validation based on code review feedback - Maintain existing error for missing Authorization header - Add specific error for cookie authentication when specified in security scheme - Consider both Authorization header and cookie for bearer token validation * fix: Revert unintended code style changes made during previous commit * fix: Revert unintended code style changes made during previous commit * fix: fix: update validateHttp to handle missing auth headers properly - Restructure Basic auth validation to check header existence first - Maintain original error messages for non-cookie authentication - Add proper cookie authentication check when specified - Fix undefined.includes() error in Basic auth validation
1 parent 94a281c commit 79424b2

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/middlewares/openapi.security.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ export function security(
8686
if (success) {
8787
next();
8888
} else {
89-
const errors = extractErrorsFromResults(results)
90-
throw errors[0]
89+
const errors = extractErrorsFromResults(results);
90+
throw errors[0];
9191
}
9292
} catch (e) {
9393
const message = e?.error?.message || 'unauthorized';
@@ -232,18 +232,31 @@ class AuthValidator {
232232
const authHeader =
233233
req.headers['authorization'] &&
234234
req.headers['authorization'].toLowerCase();
235-
236-
if (!authHeader) {
237-
throw Error(`Authorization header required`);
238-
}
239-
235+
const authCookie =
236+
req.cookies[scheme.name] || req.signedCookies?.[scheme.name];
237+
240238
const type = scheme.scheme && scheme.scheme.toLowerCase();
241-
if (type === 'bearer' && !authHeader.includes('bearer')) {
242-
throw Error(`Authorization header with scheme 'Bearer' required`);
239+
if (type === 'bearer') {
240+
if (authHeader && !authHeader.includes('bearer')) {
241+
throw Error(`Authorization header with scheme 'Bearer' required`);
242+
}
243+
244+
if (!authHeader && !authCookie) {
245+
if (scheme.in === 'cookie') {
246+
throw Error(`Cookie authentication required`);
247+
} else {
248+
throw Error(`Authorization header required`);
249+
}
250+
}
243251
}
244-
245-
if (type === 'basic' && !authHeader.includes('basic')) {
246-
throw Error(`Authorization header with scheme 'Basic' required`);
252+
253+
if (type === 'basic') {
254+
if (!authHeader) {
255+
throw Error(`Authorization header required`);
256+
}
257+
if (!authHeader.includes('basic')) {
258+
throw Error(`Authorization header with scheme 'Basic' required`);
259+
}
247260
}
248261
}
249262
}
@@ -276,4 +289,4 @@ class Util {
276289
o.constructor === Object
277290
);
278291
}
279-
}
292+
}

0 commit comments

Comments
 (0)