|
1 | 1 | import btoa from 'btoa-lite' |
2 | 2 | import oAuth2Spec from './spec.json' |
3 | 3 | import getOAuthRoutes from './routes' |
| 4 | +import { request as Request } from '../../../request' |
4 | 5 |
|
5 | | -type APIClient = import('./types').APIClient |
6 | 6 | type AuthState = import('./types').AuthPluginState |
7 | 7 |
|
8 | 8 | const routes = getOAuthRoutes(oAuth2Spec).getToken |
9 | | -export const getOAuthAccessToken = async ( |
10 | | - client: APIClient, |
11 | | - state: AuthState |
12 | | -): Promise<any> => { |
| 9 | +export const getOAuthAccessToken = async (state: AuthState): Promise<any> => { |
13 | 10 | const { auth: authState } = state |
14 | 11 | const grantType = authState.grant_type |
15 | 12 |
|
16 | | - let request |
| 13 | + let headers |
17 | 14 | if (authState.grant_type === 'bitbucketCloudJWTGrant') { |
18 | | - request = client.oauth[grantType].defaults({ |
19 | | - headers: { |
20 | | - accept: 'application/x-www-form-urlencoded', |
21 | | - authorization: `JWT ${btoa(`${authState.jwt_token}`)}`, |
22 | | - }, |
23 | | - }) |
| 15 | + headers = { |
| 16 | + authorization: `JWT ${btoa(`${authState.jwt_token}`)}`, |
| 17 | + } |
24 | 18 | } else { |
25 | | - request = client.oauth[grantType].defaults({ |
26 | | - headers: { |
27 | | - // accept: 'application/x-www-form-urlencoded', |
28 | | - authorization: `Basic ${btoa( |
29 | | - `${authState.client_id}:${authState.client_secret}` |
30 | | - )}`, |
31 | | - }, |
32 | | - }) |
| 19 | + headers = { |
| 20 | + authorization: `Basic ${btoa( |
| 21 | + `${authState.client_id}:${authState.client_secret}` |
| 22 | + )}`, |
| 23 | + } |
33 | 24 | } |
34 | 25 |
|
35 | | - authState.grant_type = routes[grantType].grant_type |
36 | | - const response = await request(authState) |
| 26 | + const requestOptions = Object.assign(routes[grantType], { |
| 27 | + headers, |
| 28 | + request: {}, |
| 29 | + }) |
| 30 | + |
| 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 | + ) |
| 38 | + |
37 | 39 | const { data } = response |
38 | 40 |
|
39 | 41 | const newToken = { |
40 | 42 | access_token: data.access_token, |
41 | | - refresh_token: data.refresh_tokenaccess_token, |
42 | | - scopes: data.scope.split(/,\s*/).filter(Boolean), |
| 43 | + refresh_token: data.refresh_token, |
| 44 | + scopes: data.scopes.split(/\s/).filter(Boolean), |
43 | 45 | } |
44 | 46 |
|
45 | 47 | return newToken |
|
0 commit comments