Skip to content

Commit 06e69c9

Browse files
Shadosshockey
andcommitted
feat: x-tokenName OAuth extension support in OpenAPI 3.0 (#1489)
* Support x-tokenName extension in OpenAPI 3.0 specs * `oauthToken` -> `tokenValue` Co-authored-by: kyle shockey <[email protected]>
1 parent 5536c05 commit 06e69c9

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/execute/oas3/build-request.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,15 @@ export function applySecurities({request, securities = {}, operation = {}, spec}
159159
}
160160
else if (type === 'oauth2') {
161161
const token = auth.token || {}
162-
const accessToken = token.access_token
162+
const tokenName = schema['x-tokenName'] || 'access_token'
163+
const tokenValue = token[tokenName]
163164
let tokenType = token.token_type
164165

165166
if (!tokenType || tokenType.toLowerCase() === 'bearer') {
166167
tokenType = 'Bearer'
167168
}
168169

169-
result.headers.Authorization = `${tokenType} ${accessToken}`
170+
result.headers.Authorization = `${tokenType} ${tokenValue}`
170171
}
171172
}
172173
}

test/oas3/execute/authorization.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,4 +596,50 @@ describe('Authorization - OpenAPI Specification 3.0', () => {
596596
}
597597
)
598598
})
599+
test('should use a custom oAuth token name if defined', () => {
600+
const spec = {
601+
openapi: '3.0.0',
602+
components: {
603+
securitySchemes: {
604+
myOAuth2Implicit: {
605+
type: 'oauth2',
606+
'x-tokenName': 'id_token'
607+
}
608+
}
609+
},
610+
paths: {
611+
'/': {
612+
get: {
613+
operationId: 'myOperation',
614+
security: [
615+
{myOAuth2Implicit: []}
616+
]
617+
}
618+
}
619+
}
620+
}
621+
622+
const req = buildRequest({
623+
spec,
624+
operationId: 'myOperation',
625+
securities: {
626+
authorized: {
627+
myOAuth2Implicit: {
628+
token: {
629+
access_token: 'otherTokenValue',
630+
id_token: 'myTokenValue'
631+
}
632+
}
633+
}
634+
}
635+
})
636+
expect(req).toEqual({
637+
method: 'GET',
638+
url: '/',
639+
credentials: 'same-origin',
640+
headers: {
641+
Authorization: 'Bearer myTokenValue'
642+
},
643+
})
644+
})
599645
})

0 commit comments

Comments
 (0)