Skip to content

Commit b3de5e9

Browse files
author
Artem Shteltser
committed
⚡ Fixing circular auth hook call
1 parent 6a28d06 commit b3de5e9

File tree

5 files changed

+32
-44
lines changed

5 files changed

+32
-44
lines changed

src/plugins/auth/OAuth/auth.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import { getOAuthAccessToken } from './get-oauth-access-token'
22

3-
type APIClient = import('./types').APIClient
43
type Authentication = import('./types').Authentication
54
type AuthState = import('./types').AuthPluginState
65

7-
// eslint-disable-next-line @typescript-eslint/require-await
8-
export async function auth(
9-
client: APIClient,
10-
authState: AuthState
11-
): Promise<Authentication | {}> {
6+
export async function auth(authState: AuthState): Promise<Authentication | {}> {
127
if (!authState.token) {
13-
const newToken = await getOAuthAccessToken(client, authState)
8+
const newToken = await getOAuthAccessToken(authState)
149
authState.token = newToken
1510
}
1611

src/plugins/auth/OAuth/get-oauth-access-token.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
11
import btoa from 'btoa-lite'
22
import oAuth2Spec from './spec.json'
33
import getOAuthRoutes from './routes'
4+
import { request as Request } from '../../../request'
45

5-
type APIClient = import('./types').APIClient
66
type AuthState = import('./types').AuthPluginState
77

88
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> => {
1310
const { auth: authState } = state
1411
const grantType = authState.grant_type
1512

16-
let request
13+
let headers
1714
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+
}
2418
} 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+
}
3324
}
3425

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+
3739
const { data } = response
3840

3941
const newToken = {
4042
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),
4345
}
4446

4547
return newToken

src/plugins/auth/OAuth/hook.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import { auth } from './auth'
22

3-
type APIClient = import('./types').APIClient
4-
type Authentication = import('./types').Authentication
53
type AuthState = import('./types').AuthPluginState
64
type RequestOptions = import('./types').RequestOptions
75

8-
// eslint-disable-next-line @typescript-eslint/require-await
96
export async function beforeHook(
10-
client: APIClient,
117
authState: AuthState,
128
requestOptions: RequestOptions
139
): Promise<void> {
1410
if (!authState.token) {
15-
await auth(client, authState)
11+
await auth(authState)
1612
} else if (authState.token.access_token) {
1713
requestOptions.headers.authorization = `Bearer ${authState.token.access_token}`
1814
}

src/plugins/auth/OAuth/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ const OAuth = (client: APIClient, clientOptions: Options): void => {
1616
auth: clientOptions.auth,
1717
}
1818

19-
// eslint-disable-next-line @typescript-eslint/require-await
20-
client.auth = auth.bind(null, client, state)
19+
client.auth = auth.bind(null, state)
2120
// eslint-disable-next-line @typescript-eslint/no-misused-promises
22-
client.requestHook.before(beforeHook.bind(null, client, state))
21+
client.requestHook.before(beforeHook.bind(null, state))
2322
}
2423

2524
export default OAuth

src/plugins/auth/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import basicAuth from './basicAuth'
22
import OAuth from './OAuth'
3-
import oAuth2Spec from './OAuth/spec.json'
4-
import getOAuthRoutes from './OAuth/routes'
53

64
type APIClient = import('./types').APIClient
75
type Options = import('./types').Options
86

9-
const routes = getOAuthRoutes(oAuth2Spec)
107
function authPlugin(client: APIClient, clientOptions: Options): void {
11-
client.registerEndpoints({ oauth: routes.getToken })
12-
138
if (!clientOptions.auth) return
9+
1410
switch (clientOptions.authStrategy) {
1511
case 'OAuth':
1612
OAuth(client, clientOptions)

0 commit comments

Comments
 (0)