Skip to content

Commit 1d25d15

Browse files
committed
fix(scheme): make scheme comparison case insensitive
As per RFC7235 auth scheme is case insensitive. 2.1. Challenge and Response HTTP provides a simple challenge-response authentication framework that can be used by a server to challenge a client request and by a client to provide authentication information. It uses a case- insensitive token as a means to identify the authentication scheme, followed by additional information necessary for achieving. https://tools.ietf.org/html/rfc7235#section-2.1 Refs #1531, #1473 Refs OAI/OpenAPI-Specification#1876 Refs swagger-api/swagger-ui#5965
1 parent 78599a3 commit 1d25d15

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

src/execute/oas3/build-request.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ export function applySecurities({request, securities = {}, operation = {}, spec}
120120
}
121121
}
122122
else if (type === 'http') {
123-
if (schema.scheme === 'basic') {
123+
if (/^basic$/i.test(schema.scheme)) {
124124
const username = value.username || ''
125125
const password = value.password || ''
126126
const encoded = btoa(`${username}:${password}`)
127127
result.headers.Authorization = `Basic ${encoded}`
128128
}
129129

130-
if (schema.scheme === 'bearer') {
130+
if (/^bearer$/i.test(schema.scheme)) {
131131
result.headers.Authorization = `Bearer ${value}`
132132
}
133133
}

test/oas3/execute/authorization.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,54 @@ describe('Authorization - OpenAPI Specification 3.0', () => {
9595
},
9696
})
9797
})
98+
99+
test('should consider scheme to be case insensitive', () => {
100+
const spec = {
101+
openapi: '3.0.0',
102+
components: {
103+
securitySchemes: {
104+
myBasicAuth: {
105+
type: 'http',
106+
in: 'header',
107+
scheme: 'Basic'
108+
}
109+
}
110+
},
111+
paths: {
112+
'/': {
113+
get: {
114+
operationId: 'myOperation',
115+
security: [{
116+
myBasicAuth: []
117+
}],
118+
}
119+
}
120+
}
121+
}
122+
123+
const req = buildRequest({
124+
spec,
125+
operationId: 'myOperation',
126+
securities: {
127+
authorized: {
128+
myBasicAuth: {
129+
username: 'somebody',
130+
password: 'goodpass'
131+
}
132+
}
133+
}
134+
})
135+
136+
expect(req).toEqual({
137+
method: 'GET',
138+
url: '/',
139+
credentials: 'same-origin',
140+
headers: {
141+
Authorization: `Basic ${btoa('somebody:goodpass')}`
142+
},
143+
})
144+
})
145+
98146
test(
99147
'should not add credentials to operations without the security requirement',
100148
() => {
@@ -238,6 +286,54 @@ describe('Authorization - OpenAPI Specification 3.0', () => {
238286
},
239287
})
240288
})
289+
290+
test('should consider scheme to be case insensitive', () => {
291+
const spec = {
292+
openapi: '3.0.0',
293+
components: {
294+
securitySchemes: {
295+
myBearerAuth: {
296+
type: 'http',
297+
in: 'header',
298+
scheme: 'Bearer'
299+
}
300+
}
301+
},
302+
paths: {
303+
'/': {
304+
get: {
305+
operationId: 'myOperation',
306+
security: [{
307+
myBearerAuth: []
308+
}]
309+
}
310+
}
311+
}
312+
}
313+
314+
// when
315+
const req = buildRequest({
316+
spec,
317+
operationId: 'myOperation',
318+
securities: {
319+
authorized: {
320+
myBearerAuth: {
321+
value: 'Asdf1234'
322+
}
323+
}
324+
}
325+
})
326+
327+
expect(req).toEqual({
328+
method: 'GET',
329+
url: '/',
330+
credentials: 'same-origin',
331+
headers: {
332+
Authorization: 'Bearer Asdf1234'
333+
},
334+
})
335+
})
336+
241337
test(
242338
'should not add credentials to operations without the security requirement',
243339
() => {

0 commit comments

Comments
 (0)