Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/execute/oas3/build-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ export function applySecurities({request, securities = {}, operation = {}, spec}
}
}
else if (type === 'http') {
if (schema.scheme === 'basic') {
if (/^basic$/i.test(schema.scheme)) {
const username = value.username || ''
const password = value.password || ''
const encoded = btoa(`${username}:${password}`)
result.headers.Authorization = `Basic ${encoded}`
}

if (schema.scheme === 'bearer') {
if (/^bearer$/i.test(schema.scheme)) {
result.headers.Authorization = `Bearer ${value}`
}
}
Expand Down
94 changes: 94 additions & 0 deletions test/oas3/execute/authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,53 @@ describe('Authorization - OpenAPI Specification 3.0', () => {
},
})
})

test('should consider scheme to be case insensitive', () => {
const spec = {
openapi: '3.0.0',
components: {
securitySchemes: {
myBasicAuth: {
type: 'http',
scheme: 'Basic'
}
}
},
paths: {
'/': {
get: {
operationId: 'myOperation',
security: [{
myBasicAuth: []
}],
}
}
}
}

const req = buildRequest({
spec,
operationId: 'myOperation',
securities: {
authorized: {
myBasicAuth: {
username: 'somebody',
password: 'goodpass'
}
}
}
})

expect(req).toEqual({
method: 'GET',
url: '/',
credentials: 'same-origin',
headers: {
Authorization: `Basic ${btoa('somebody:goodpass')}`
},
})
})

test(
'should not add credentials to operations without the security requirement',
() => {
Expand Down Expand Up @@ -230,6 +277,53 @@ describe('Authorization - OpenAPI Specification 3.0', () => {
},
})
})

test('should consider scheme to be case insensitive', () => {
const spec = {
openapi: '3.0.0',
components: {
securitySchemes: {
myBearerAuth: {
type: 'http',
scheme: 'Bearer'
}
}
},
paths: {
'/': {
get: {
operationId: 'myOperation',
security: [{
myBearerAuth: []
}]
}
}
}
}

// when
const req = buildRequest({
spec,
operationId: 'myOperation',
securities: {
authorized: {
myBearerAuth: {
value: 'Asdf1234'
}
}
}
})

expect(req).toEqual({
method: 'GET',
url: '/',
credentials: 'same-origin',
headers: {
Authorization: 'Bearer Asdf1234'
},
})
})

test(
'should not add credentials to operations without the security requirement',
() => {
Expand Down