Skip to content

Commit 3e5f51b

Browse files
author
Artem Shteltser
committed
⚡️ Fixing request initialization
- Added validation for strategies - Fixed Endpoint interface
1 parent b3de5e9 commit 3e5f51b

File tree

5 files changed

+47
-64
lines changed

5 files changed

+47
-64
lines changed

src/endpoint/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface Endpoint {
4343
DEFAULTS: EndpointDefaults
4444
defaults(endpointOptions: EndpointParams): Endpoint
4545
merge(
46-
endpointRoute: string,
46+
endpointRoute: string | EndpointOptions,
4747
endpointOptions?: EndpointParams
4848
): EndpointDefaults
4949
merge(endpointOptions: EndpointParams): EndpointDefaults

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,38 @@ import getOAuthRoutes from './routes'
44
import { request as Request } from '../../../request'
55

66
type AuthState = import('./types').AuthPluginState
7+
type EndpointOptions = import('./types').EndpointOptions
78

89
const routes = getOAuthRoutes(oAuth2Spec).getToken
910
export 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 = {

src/plugins/auth/OAuth/routes.ts

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,12 @@ const getOAuthRoutes = (info: SpecType): any => ({
77
grant_type: 'authorization_code',
88
method: 'POST',
99
params: {
10-
client_id: {
11-
in: '_body',
12-
required: true,
13-
type: 'string',
14-
},
15-
client_secret: {
10+
grant_type: {
1611
in: '_body',
1712
required: true,
1813
type: 'string',
1914
},
2015
code: {
21-
required: true,
22-
type: 'string',
23-
},
24-
grant_type: {
2516
in: '_body',
2617
required: true,
2718
type: 'string',
@@ -34,16 +25,6 @@ const getOAuthRoutes = (info: SpecType): any => ({
3425
grant_type: 'password',
3526
method: 'POST',
3627
params: {
37-
client_id: {
38-
in: '_body',
39-
required: true,
40-
type: 'string',
41-
},
42-
client_secret: {
43-
in: '_body',
44-
required: true,
45-
type: 'string',
46-
},
4728
grant_type: {
4829
in: '_body',
4930
required: true,
@@ -67,16 +48,6 @@ const getOAuthRoutes = (info: SpecType): any => ({
6748
grant_type: 'client_credentials',
6849
method: 'POST',
6950
params: {
70-
client_id: {
71-
in: '_body',
72-
required: true,
73-
type: 'string',
74-
},
75-
client_secret: {
76-
in: '_body',
77-
required: true,
78-
type: 'string',
79-
},
8051
grant_type: {
8152
in: '_body',
8253
required: true,
@@ -95,11 +66,6 @@ const getOAuthRoutes = (info: SpecType): any => ({
9566
required: true,
9667
type: 'string',
9768
},
98-
jwtToken: {
99-
in: 'headers.authorization:JWT',
100-
required: true,
101-
type: 'string',
102-
},
10369
},
10470
url: `${info.tokenUrl}`,
10571
},

src/plugins/auth/OAuth/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export type APIClient = import('../../../client/types').APIClient
22
export type Options = import('../../../client/types').Options
3-
export type RequestOptions = import('../../../endpoint/types').RequestOptions
43
export type AuthOptions = import('../types').OAuthOptions
4+
export type EndpointOptions = import('../../../endpoint/types').EndpointOptions
55

66
export type AuthPluginState = {
77
authStrategy: string
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
type AuthOptions = import('./types').AuthOptions
22

33
export function validateOptions(auth: AuthOptions): void {
4+
if (!auth.grant_type) {
5+
throw new Error(`Specify grant_type`)
6+
}
7+
48
if (
59
auth.grant_type !== 'bitbucketCloudJWTGrant' &&
610
(!auth.client_id || !auth.client_secret)
711
) {
8-
throw new Error(`Specify client id and client secret for authentication`)
12+
throw new Error(`Specify client_id and client_secret for authentication`)
913
}
1014

11-
if (!auth.grant_type) {
12-
throw new Error(`Specify grant type`)
15+
if (auth.grant_type === 'authorizationCodeGrant' && !auth.code) {
16+
throw new Error(`code is required for Code Grant Authorization`)
17+
}
18+
19+
if (
20+
auth.grant_type === 'resourceOwnerPasswordCredentialsGrant' &&
21+
(!auth.username || auth.password)
22+
) {
23+
throw new Error(
24+
`Both username and password are required for Resource Owner Password Credentials Grant`
25+
)
26+
}
27+
28+
if (auth.grant_type === 'bitbucketCloudJWTGrant' && !auth.jwt_token) {
29+
throw new Error(`jwt_token is required for Bitbucket Cloud JWT Grant`)
1330
}
1431
}

0 commit comments

Comments
 (0)