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
5 changes: 3 additions & 2 deletions src/execute/oas3/build-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,15 @@ export function applySecurities({request, securities = {}, operation = {}, spec}
}
else if (type === 'oauth2') {
const token = auth.token || {}
const accessToken = token.access_token
const tokenName = schema['x-tokenName'] || 'access_token'
const tokenValue = token[tokenName]
let tokenType = token.token_type

if (!tokenType || tokenType.toLowerCase() === 'bearer') {
tokenType = 'Bearer'
}

result.headers.Authorization = `${tokenType} ${accessToken}`
result.headers.Authorization = `${tokenType} ${tokenValue}`
}
}
}
Expand Down
46 changes: 46 additions & 0 deletions test/oas3/execute/authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,50 @@ describe('Authorization - OpenAPI Specification 3.0', () => {
}
)
})
test('should use a custom oAuth token name if defined', () => {
const spec = {
openapi: '3.0.0',
components: {
securitySchemes: {
myOAuth2Implicit: {
type: 'oauth2',
'x-tokenName': 'id_token'
}
}
},
paths: {
'/': {
get: {
operationId: 'myOperation',
security: [
{myOAuth2Implicit: []}
]
}
}
}
}

const req = buildRequest({
spec,
operationId: 'myOperation',
securities: {
authorized: {
myOAuth2Implicit: {
token: {
access_token: 'otherTokenValue',
id_token: 'myTokenValue'
}
}
}
}
})
expect(req).toEqual({
method: 'GET',
url: '/',
credentials: 'same-origin',
headers: {
Authorization: 'Bearer myTokenValue'
},
})
})
})