Skip to content

Commit 79fd3d7

Browse files
authored
fix(scheme): make scheme comparison case insensitive (#1562)
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 Co-authored-by: Helen Kosova <[email protected]> Refs #1531, #1473 Refs OAI/OpenAPI-Specification#1876 Refs swagger-api/swagger-ui#5965
1 parent be654cd commit 79fd3d7

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,53 @@ describe('Authorization - OpenAPI Specification 3.0', () => {
9090
},
9191
})
9292
})
93+
94+
test('should consider scheme to be case insensitive', () => {
95+
const spec = {
96+
openapi: '3.0.0',
97+
components: {
98+
securitySchemes: {
99+
myBasicAuth: {
100+
type: 'http',
101+
scheme: 'Basic'
102+
}
103+
}
104+
},
105+
paths: {
106+
'/': {
107+
get: {
108+
operationId: 'myOperation',
109+
security: [{
110+
myBasicAuth: []
111+
}],
112+
}
113+
}
114+
}
115+
}
116+
117+
const req = buildRequest({
118+
spec,
119+
operationId: 'myOperation',
120+
securities: {
121+
authorized: {
122+
myBasicAuth: {
123+
username: 'somebody',
124+
password: 'goodpass'
125+
}
126+
}
127+
}
128+
})
129+
130+
expect(req).toEqual({
131+
method: 'GET',
132+
url: '/',
133+
credentials: 'same-origin',
134+
headers: {
135+
Authorization: `Basic ${btoa('somebody:goodpass')}`
136+
},
137+
})
138+
})
139+
93140
test(
94141
'should not add credentials to operations without the security requirement',
95142
() => {
@@ -230,6 +277,53 @@ describe('Authorization - OpenAPI Specification 3.0', () => {
230277
},
231278
})
232279
})
280+
281+
test('should consider scheme to be case insensitive', () => {
282+
const spec = {
283+
openapi: '3.0.0',
284+
components: {
285+
securitySchemes: {
286+
myBearerAuth: {
287+
type: 'http',
288+
scheme: 'Bearer'
289+
}
290+
}
291+
},
292+
paths: {
293+
'/': {
294+
get: {
295+
operationId: 'myOperation',
296+
security: [{
297+
myBearerAuth: []
298+
}]
299+
}
300+
}
301+
}
302+
}
303+
304+
// when
305+
const req = buildRequest({
306+
spec,
307+
operationId: 'myOperation',
308+
securities: {
309+
authorized: {
310+
myBearerAuth: {
311+
value: 'Asdf1234'
312+
}
313+
}
314+
}
315+
})
316+
317+
expect(req).toEqual({
318+
method: 'GET',
319+
url: '/',
320+
credentials: 'same-origin',
321+
headers: {
322+
Authorization: 'Bearer Asdf1234'
323+
},
324+
})
325+
})
326+
233327
test(
234328
'should not add credentials to operations without the security requirement',
235329
() => {

0 commit comments

Comments
 (0)