@@ -4,38 +4,38 @@ import getOAuthRoutes from './routes'
44import { request as Request } from '../../../request'
55
66type AuthState = import ( './types' ) . AuthPluginState
7+ type EndpointOptions = import ( './types' ) . EndpointOptions
78
89const routes = getOAuthRoutes ( oAuth2Spec ) . getToken
910export const getOAuthAccessToken = async ( state : AuthState ) : Promise < any > => {
1011 const { auth : authState } = state
11- const grantType = authState . grant_type
12-
13- let headers
14- if ( authState . grant_type === 'bitbucketCloudJWTGrant' ) {
15- headers = {
16- authorization : `JWT ${ btoa ( `${ authState . jwt_token } ` ) } ` ,
17- }
18- } else {
19- headers = {
20- authorization : `Basic ${ btoa (
21- `${ authState . client_id } :${ authState . client_secret } `
22- ) } `,
23- }
12+ const { grant_type : grantType } = authState
13+ const { accepts, url, method, grant_type } = routes [ grantType ]
14+
15+ const endpointOptions : EndpointOptions = {
16+ accepts,
17+ url,
18+ method,
19+ headers : { } ,
20+ request : { } ,
21+ grant_type,
2422 }
2523
26- const requestOptions = Object . assign ( routes [ grantType ] , {
27- headers ,
28- request : { } ,
29- } )
24+ endpointOptions . headers ! . authorization =
25+ authState . grant_type === 'bitbucketCloudJWTGrant'
26+ ? `JWT ${ btoa ( ` ${ authState . jwt_token } ` ) } `
27+ : `Basic ${ btoa ( ` ${ authState . client_id } : ${ authState . client_secret } ` ) } `
3028
31- const request = Request . defaults ( requestOptions )
32- const response = await request (
33- requestOptions ,
34- Object . assign ( authState , {
35- grant_type : routes [ grantType ] . grant_type ,
36- } )
37- )
29+ if ( authState . grant_type === 'authorizationCodeGrant' ) {
30+ endpointOptions . code = authState . code
31+ }
32+
33+ if ( authState . grant_type === 'resourceOwnerPasswordCredentialsGrant' ) {
34+ endpointOptions . username = authState . username
35+ endpointOptions . password = authState . password
36+ }
3837
38+ const response = await Request ( endpointOptions )
3939 const { data } = response
4040
4141 const newToken = {
0 commit comments