@whook/oauth2 25.2.1 → 26.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +147 -98
- package/dist/index.d.ts +34 -11
- package/dist/index.js +18 -8
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +1671 -393
- package/dist/index.test.js.map +1 -1
- package/dist/libs/authorizationRequests.d.ts +6 -0
- package/dist/libs/authorizationRequests.js +22 -0
- package/dist/libs/authorizationRequests.js.map +1 -0
- package/dist/libs/authorizationRequests.test.js +24 -0
- package/dist/libs/authorizationRequests.test.js.map +1 -0
- package/dist/libs/clients.d.ts +2 -0
- package/dist/libs/clients.js +13 -0
- package/dist/libs/clients.js.map +1 -0
- package/dist/libs/errors.d.ts +143 -0
- package/dist/libs/errors.js +227 -0
- package/dist/libs/errors.js.map +1 -0
- package/dist/libs/grants.d.ts +2 -0
- package/dist/libs/grants.js +10 -0
- package/dist/libs/grants.js.map +1 -0
- package/dist/libs/redirectURI.d.ts +8 -0
- package/dist/libs/redirectURI.js +94 -0
- package/dist/libs/redirectURI.js.map +1 -0
- package/dist/libs/schemas.d.ts +31 -0
- package/dist/libs/schemas.js +33 -0
- package/dist/libs/schemas.js.map +1 -0
- package/dist/libs/scopes.d.ts +16 -0
- package/dist/libs/scopes.js +37 -0
- package/dist/libs/scopes.js.map +1 -1
- package/dist/libs/utils.d.ts +2 -0
- package/dist/libs/utils.js +17 -0
- package/dist/libs/utils.js.map +1 -0
- package/dist/libs/verifier.d.ts +10 -0
- package/dist/libs/verifier.js +51 -0
- package/dist/libs/verifier.js.map +1 -0
- package/dist/libs/verifier.test.d.ts +1 -0
- package/dist/libs/verifier.test.js +21 -0
- package/dist/libs/verifier.test.js.map +1 -0
- package/dist/routes/getOAuth2Authorize.d.ts +38 -59
- package/dist/routes/getOAuth2Authorize.js +129 -93
- package/dist/routes/getOAuth2Authorize.js.map +1 -1
- package/dist/routes/getOAuth2Authorize.test.js +959 -73
- package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.d.ts +18 -11
- package/dist/routes/getOAuth2WellKnownMetadata.js +43 -11
- package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.test.js +51 -49
- package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.d.ts +3 -6
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js +3 -6
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js.map +1 -1
- package/dist/routes/postAuthLogin.d.ts +14 -9
- package/dist/routes/postAuthLogin.js +10 -5
- package/dist/routes/postAuthLogin.js.map +1 -1
- package/dist/routes/postAuthLogin.test.js +79 -55
- package/dist/routes/postAuthLogin.test.js.map +1 -1
- package/dist/routes/postAuthLogout.d.ts +5 -2
- package/dist/routes/postAuthLogout.js +12 -7
- package/dist/routes/postAuthLogout.js.map +1 -1
- package/dist/routes/postAuthLogout.test.js +35 -17
- package/dist/routes/postAuthLogout.test.js.map +1 -1
- package/dist/routes/postAuthRefresh.d.ts +14 -9
- package/dist/routes/postAuthRefresh.js +10 -6
- package/dist/routes/postAuthRefresh.js.map +1 -1
- package/dist/routes/postAuthRefresh.test.js +90 -59
- package/dist/routes/postAuthRefresh.test.js.map +1 -1
- package/dist/routes/postOAuth2Acknowledge.d.ts +11 -10
- package/dist/routes/postOAuth2Acknowledge.js +43 -37
- package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
- package/dist/routes/postOAuth2Acknowledge.test.js +135 -70
- package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -1
- package/dist/routes/postOAuth2PushedAuthorizationRequest.d.ts +137 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.js +187 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.js.map +1 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.test.d.ts +1 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js +554 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js.map +1 -0
- package/dist/routes/postOAuth2Revoke.d.ts +99 -0
- package/dist/routes/postOAuth2Revoke.js +102 -0
- package/dist/routes/postOAuth2Revoke.js.map +1 -0
- package/dist/routes/postOAuth2Revoke.test.d.ts +1 -0
- package/dist/routes/postOAuth2Revoke.test.js +161 -0
- package/dist/routes/postOAuth2Revoke.test.js.map +1 -0
- package/dist/routes/postOAuth2Token.d.ts +47 -108
- package/dist/routes/postOAuth2Token.js +65 -114
- package/dist/routes/postOAuth2Token.js.map +1 -1
- package/dist/routes/postOAuth2Token.test.js +493 -61
- package/dist/routes/postOAuth2Token.test.js.map +1 -1
- package/dist/services/authCookies.d.ts +9 -13
- package/dist/services/authCookies.js +9 -5
- package/dist/services/authCookies.js.map +1 -1
- package/dist/services/authCookies.test.js.map +1 -1
- package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +124 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.js +204 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.d.ts +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js +345 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -0
- package/dist/services/oAuth2AuthorizationRequests.d.ts +41 -0
- package/dist/services/oAuth2AuthorizationRequests.js +61 -0
- package/dist/services/oAuth2AuthorizationRequests.js.map +1 -0
- package/dist/services/oAuth2AuthorizationRequests.test.d.ts +1 -0
- package/dist/services/oAuth2AuthorizationRequests.test.js +249 -0
- package/dist/services/oAuth2AuthorizationRequests.test.js.map +1 -0
- package/dist/services/oAuth2ClientCredentialsGranter.d.ts +28 -12
- package/dist/services/oAuth2ClientCredentialsGranter.js +39 -21
- package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
- package/dist/services/oAuth2ClientCredentialsGranter.test.d.ts +1 -0
- package/dist/services/oAuth2ClientCredentialsGranter.test.js +265 -0
- package/dist/services/oAuth2ClientCredentialsGranter.test.js.map +1 -0
- package/dist/services/oAuth2Granters.d.ts +79 -86
- package/dist/services/oAuth2Granters.js +2 -67
- package/dist/services/oAuth2Granters.js.map +1 -1
- package/dist/services/oAuth2ImplicitGranter.d.ts +38 -0
- package/dist/services/oAuth2ImplicitGranter.js +61 -0
- package/dist/services/oAuth2ImplicitGranter.js.map +1 -0
- package/dist/services/oAuth2ImplicitGranter.test.d.ts +1 -0
- package/dist/services/oAuth2ImplicitGranter.test.js +589 -0
- package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -0
- package/dist/services/oAuth2PasswordGranter.d.ts +43 -10
- package/dist/services/oAuth2PasswordGranter.js +53 -18
- package/dist/services/oAuth2PasswordGranter.js.map +1 -1
- package/dist/services/oAuth2PasswordGranter.test.d.ts +1 -0
- package/dist/services/oAuth2PasswordGranter.test.js +455 -0
- package/dist/services/oAuth2PasswordGranter.test.js.map +1 -0
- package/dist/services/oAuth2RefreshTokenGranter.d.ts +42 -9
- package/dist/services/oAuth2RefreshTokenGranter.js +60 -21
- package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.test.d.ts +1 -0
- package/dist/services/oAuth2RefreshTokenGranter.test.js +403 -0
- package/dist/services/oAuth2RefreshTokenGranter.test.js.map +1 -0
- package/package.json +14 -13
- package/src/index.test.ts +1773 -407
- package/src/index.ts +63 -53
- package/src/libs/authorizationRequests.test.ts +40 -0
- package/src/libs/authorizationRequests.ts +42 -0
- package/src/libs/clients.ts +22 -0
- package/src/libs/errors.ts +411 -0
- package/src/libs/grants.ts +15 -0
- package/src/libs/redirectURI.ts +142 -0
- package/src/libs/schemas.ts +37 -0
- package/src/libs/scopes.ts +49 -0
- package/src/libs/utils.ts +24 -0
- package/src/libs/verifier.test.ts +37 -0
- package/src/libs/verifier.ts +75 -0
- package/src/routes/getOAuth2Authorize.test.ts +1102 -102
- package/src/routes/getOAuth2Authorize.ts +243 -151
- package/src/routes/getOAuth2WellKnownMetadata.test.ts +73 -74
- package/src/routes/getOAuth2WellKnownMetadata.ts +63 -16
- package/src/routes/getOAuth2WellKnownProtectedResourceMetadata.ts +4 -6
- package/src/routes/postAuthLogin.test.ts +86 -57
- package/src/routes/postAuthLogin.ts +24 -14
- package/src/routes/postAuthLogout.test.ts +42 -18
- package/src/routes/postAuthLogout.ts +22 -8
- package/src/routes/postAuthRefresh.test.ts +98 -61
- package/src/routes/postAuthRefresh.ts +25 -13
- package/src/routes/postOAuth2Acknowledge.test.ts +167 -108
- package/src/routes/postOAuth2Acknowledge.ts +90 -64
- package/src/routes/postOAuth2PushedAuthorizationRequest.test.ts +681 -0
- package/src/routes/postOAuth2PushedAuthorizationRequest.ts +292 -0
- package/src/routes/postOAuth2Revoke.test.ts +179 -0
- package/src/routes/postOAuth2Revoke.ts +156 -0
- package/src/routes/postOAuth2Token.test.ts +570 -108
- package/src/routes/postOAuth2Token.ts +104 -136
- package/src/services/authCookies.test.ts +5 -5
- package/src/services/authCookies.ts +24 -22
- package/src/services/oAuth2AuthorizationCodeGranter.test.ts +407 -0
- package/src/services/oAuth2AuthorizationCodeGranter.ts +365 -0
- package/src/services/oAuth2AuthorizationRequests.test.ts +282 -0
- package/src/services/oAuth2AuthorizationRequests.ts +141 -0
- package/src/services/oAuth2ClientCredentialsGranter.test.ts +303 -0
- package/src/services/oAuth2ClientCredentialsGranter.ts +68 -43
- package/src/services/oAuth2Granters.ts +100 -196
- package/src/services/oAuth2ImplicitGranter.test.ts +650 -0
- package/src/services/oAuth2ImplicitGranter.ts +147 -0
- package/src/services/oAuth2PasswordGranter.test.ts +493 -0
- package/src/services/oAuth2PasswordGranter.ts +116 -40
- package/src/services/oAuth2RefreshTokenGranter.test.ts +441 -0
- package/src/services/oAuth2RefreshTokenGranter.ts +118 -51
- package/src/types.d.ts +1 -0
- package/dist/services/oAuth2CodeGranter.d.ts +0 -41
- package/dist/services/oAuth2CodeGranter.js +0 -105
- package/dist/services/oAuth2CodeGranter.js.map +0 -1
- package/dist/services/oAuth2CodeGranter.test.js +0 -123
- package/dist/services/oAuth2CodeGranter.test.js.map +0 -1
- package/dist/services/oAuth2TokenGranter.d.ts +0 -22
- package/dist/services/oAuth2TokenGranter.js +0 -64
- package/dist/services/oAuth2TokenGranter.js.map +0 -1
- package/src/__snapshots__/index.test.ts.snap +0 -401
- package/src/routes/__snapshots__/getOAuth2Authorize.test.ts.snap +0 -37
- package/src/routes/__snapshots__/postOAuth2Acknowledge.test.ts.snap +0 -61
- package/src/routes/__snapshots__/postOAuth2Token.test.ts.snap +0 -52
- package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +0 -52
- package/src/services/oAuth2CodeGranter.test.ts +0 -176
- package/src/services/oAuth2CodeGranter.ts +0 -210
- package/src/services/oAuth2TokenGranter.ts +0 -119
- /package/dist/{services/oAuth2CodeGranter.test.d.ts → libs/authorizationRequests.test.d.ts} +0 -0
|
@@ -1,75 +1,86 @@
|
|
|
1
1
|
import { autoService, location } from 'knifecycle';
|
|
2
|
-
import camelCase from 'camelcase';
|
|
3
2
|
import { YError, printStackTrace } from 'yerror';
|
|
4
3
|
import {
|
|
5
4
|
refersTo,
|
|
6
5
|
type WhookRouteDefinition,
|
|
7
6
|
type WhookAPIParameterDefinition,
|
|
8
7
|
type WhookErrorsDescriptors,
|
|
9
|
-
type WhookErrorDescriptor,
|
|
10
|
-
type WhookAPISchemaDefinition,
|
|
11
8
|
} from '@whook/whook';
|
|
12
9
|
import {
|
|
13
|
-
type
|
|
14
|
-
type
|
|
10
|
+
type WhookOAuth2Options,
|
|
11
|
+
type WhookOAuth2GranterService,
|
|
12
|
+
type WhookOAuth2GranterDefinitions,
|
|
13
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
15
14
|
} from '../services/oAuth2Granters.js';
|
|
16
15
|
import { type LogService } from 'common-services';
|
|
17
16
|
import {
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
filterScopes,
|
|
18
|
+
parseOAuth2Scope,
|
|
19
|
+
stringifyScopes,
|
|
20
|
+
} from '../libs/scopes.js';
|
|
21
|
+
import {
|
|
22
|
+
checkCodeChallengeParameters,
|
|
23
|
+
type CodeChallengeMethod,
|
|
20
24
|
PLAIN_CODE_CHALLENGE_METHOD,
|
|
21
|
-
} from '../
|
|
25
|
+
} from '../libs/verifier.js';
|
|
26
|
+
import {
|
|
27
|
+
AUTHORIZATION_CODE_RESPONSE_TYPE,
|
|
28
|
+
codeChallengeMethodSchema,
|
|
29
|
+
codeChallengeMethodParameter,
|
|
30
|
+
codeChallengeSchema,
|
|
31
|
+
codeChallengeParameter,
|
|
32
|
+
} from '../services/oAuth2AuthorizationCodeGranter.js';
|
|
33
|
+
import { IMPLICIT_RESPONSE_TYPE } from '../services/oAuth2ImplicitGranter.js';
|
|
34
|
+
import {
|
|
35
|
+
addParamsToURL,
|
|
36
|
+
buildParamsFromError,
|
|
37
|
+
getUsableRedirectURI,
|
|
38
|
+
} from '../libs/redirectURI.js';
|
|
39
|
+
import { requestURISchema, scopeSchema } from '../libs/schemas.js';
|
|
40
|
+
import {
|
|
41
|
+
DEFAULT_OAUTH2_PAR,
|
|
42
|
+
type WhookOAuth2AuthorizationRequestsConfig,
|
|
43
|
+
type WhookOAuth2AuthorizationRequestsService,
|
|
44
|
+
} from '../services/oAuth2AuthorizationRequests.js';
|
|
45
|
+
import { isRequestURI } from '../libs/authorizationRequests.js';
|
|
46
|
+
import { toUsableClientId } from '../libs/clients.js';
|
|
47
|
+
|
|
48
|
+
export {
|
|
49
|
+
requestURISchema,
|
|
50
|
+
scopeSchema,
|
|
51
|
+
codeChallengeMethodSchema,
|
|
52
|
+
codeChallengeMethodParameter,
|
|
53
|
+
codeChallengeSchema,
|
|
54
|
+
codeChallengeParameter,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export interface WhookOAuth2AuthorizeRequestParameters {
|
|
58
|
+
client_id: string;
|
|
59
|
+
response_type: string;
|
|
60
|
+
redirect_uri?: string;
|
|
61
|
+
scope?: string;
|
|
62
|
+
state?: string;
|
|
63
|
+
code_challenge?: string;
|
|
64
|
+
code_challenge_method?: CodeChallengeMethod;
|
|
65
|
+
}
|
|
22
66
|
|
|
23
67
|
/* Architecture Note #1: OAuth2 authorize
|
|
24
|
-
|
|
25
|
-
|
|
68
|
+
|
|
69
|
+
This endpoint simply redirects the user to the authentication
|
|
70
|
+
server page and checks the application details are
|
|
26
71
|
fine.
|
|
27
|
-
*/
|
|
28
72
|
|
|
29
|
-
|
|
30
|
-
name: 'CodeChallenge',
|
|
31
|
-
schema: {
|
|
32
|
-
type: 'string',
|
|
33
|
-
minLength: 43,
|
|
34
|
-
maxLength: 128,
|
|
35
|
-
pattern: '^[A-Za-z0-9\\-._~]{43,128}$',
|
|
36
|
-
},
|
|
37
|
-
} as const satisfies WhookAPISchemaDefinition;
|
|
38
|
-
export const codeChallengeParameter = {
|
|
39
|
-
name: 'code_challenge',
|
|
40
|
-
parameter: {
|
|
41
|
-
in: 'query',
|
|
42
|
-
name: 'code_challenge',
|
|
43
|
-
required: false,
|
|
44
|
-
schema: refersTo(codeChallengeSchema),
|
|
45
|
-
},
|
|
46
|
-
} as const satisfies WhookAPIParameterDefinition;
|
|
47
|
-
export const codeChallengeMethodSchema = {
|
|
48
|
-
name: 'CodeChallengeMethod',
|
|
49
|
-
schema: {
|
|
50
|
-
type: 'string',
|
|
51
|
-
enum: CODE_CHALLENGE_METHODS.concat(),
|
|
52
|
-
},
|
|
53
|
-
} as const satisfies WhookAPISchemaDefinition;
|
|
54
|
-
export const codeChallengeMethodParameter = {
|
|
55
|
-
name: 'code_challenge_method',
|
|
56
|
-
parameter: {
|
|
57
|
-
in: 'query',
|
|
58
|
-
name: 'code_challenge_method',
|
|
59
|
-
required: false,
|
|
60
|
-
schema: refersTo(codeChallengeMethodSchema),
|
|
61
|
-
},
|
|
62
|
-
} as const satisfies WhookAPIParameterDefinition;
|
|
73
|
+
*/
|
|
63
74
|
|
|
64
75
|
export const responseTypeParameter = {
|
|
65
76
|
name: 'responseType',
|
|
66
77
|
parameter: {
|
|
67
78
|
in: 'query',
|
|
68
79
|
name: 'response_type',
|
|
69
|
-
required:
|
|
80
|
+
required: false,
|
|
70
81
|
schema: {
|
|
71
82
|
type: 'string',
|
|
72
|
-
enum: [
|
|
83
|
+
enum: [AUTHORIZATION_CODE_RESPONSE_TYPE, IMPLICIT_RESPONSE_TYPE],
|
|
73
84
|
},
|
|
74
85
|
},
|
|
75
86
|
} as const satisfies WhookAPIParameterDefinition;
|
|
@@ -102,11 +113,8 @@ export const scopeParameter = {
|
|
|
102
113
|
parameter: {
|
|
103
114
|
in: 'query',
|
|
104
115
|
name: 'scope',
|
|
105
|
-
description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',
|
|
106
116
|
required: false,
|
|
107
|
-
schema:
|
|
108
|
-
type: 'string',
|
|
109
|
-
},
|
|
117
|
+
schema: refersTo(scopeSchema),
|
|
110
118
|
},
|
|
111
119
|
} as const satisfies WhookAPIParameterDefinition;
|
|
112
120
|
export const stateParameter = {
|
|
@@ -114,20 +122,29 @@ export const stateParameter = {
|
|
|
114
122
|
parameter: {
|
|
115
123
|
in: 'query',
|
|
116
124
|
name: 'state',
|
|
117
|
-
required:
|
|
125
|
+
required: false,
|
|
118
126
|
schema: {
|
|
119
127
|
type: 'string',
|
|
120
128
|
},
|
|
121
129
|
},
|
|
122
130
|
} as const satisfies WhookAPIParameterDefinition;
|
|
131
|
+
export const requestURIParameter = {
|
|
132
|
+
name: 'requestURI',
|
|
133
|
+
parameter: {
|
|
134
|
+
in: 'query',
|
|
135
|
+
name: 'request_uri',
|
|
136
|
+
required: false,
|
|
137
|
+
schema: refersTo(requestURISchema),
|
|
138
|
+
},
|
|
139
|
+
} as const satisfies WhookAPIParameterDefinition;
|
|
123
140
|
|
|
124
141
|
export const definition = {
|
|
125
142
|
method: 'get',
|
|
126
143
|
path: '/oauth2/authorize',
|
|
127
144
|
operation: {
|
|
128
145
|
operationId: 'getOAuth2Authorize',
|
|
129
|
-
summary: `Implements the [Authorization Endpoint](https://
|
|
130
|
-
as defined per the OAuth2 RFC.`,
|
|
146
|
+
summary: `Implements the [Authorization Endpoint](https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-15.html#section-3.1)
|
|
147
|
+
as defined per the OAuth2.1 RFC.`,
|
|
131
148
|
tags: ['oauth2'],
|
|
132
149
|
parameters: [
|
|
133
150
|
refersTo(responseTypeParameter),
|
|
@@ -135,6 +152,7 @@ export const definition = {
|
|
|
135
152
|
refersTo(redirectURIParameter),
|
|
136
153
|
refersTo(scopeParameter),
|
|
137
154
|
refersTo(stateParameter),
|
|
155
|
+
refersTo(requestURIParameter),
|
|
138
156
|
refersTo(codeChallengeParameter),
|
|
139
157
|
refersTo(codeChallengeMethodParameter),
|
|
140
158
|
],
|
|
@@ -146,136 +164,210 @@ export const definition = {
|
|
|
146
164
|
},
|
|
147
165
|
} as const satisfies WhookRouteDefinition;
|
|
148
166
|
|
|
149
|
-
// The OAuth2 standard uses snake case names so we are
|
|
150
|
-
// converting them to the project standards asap
|
|
151
|
-
export function camelCaseObjectProperties<T>(
|
|
152
|
-
object: Record<string, T>,
|
|
153
|
-
): Record<string, T> {
|
|
154
|
-
return Object.keys(object).reduce(
|
|
155
|
-
(camelCasedObject, key) => {
|
|
156
|
-
const newKey = key === 'redirect_uri' ? 'redirectURI' : camelCase(key);
|
|
157
|
-
|
|
158
|
-
camelCasedObject[newKey] = object[key];
|
|
159
|
-
return camelCasedObject;
|
|
160
|
-
},
|
|
161
|
-
{} as Record<string, T>,
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export function setURLError(
|
|
166
|
-
url: URL,
|
|
167
|
-
err: YError | Error,
|
|
168
|
-
oAuth2Error: WhookErrorDescriptor,
|
|
169
|
-
): void {
|
|
170
|
-
url.searchParams.set('error', oAuth2Error.code);
|
|
171
|
-
if (oAuth2Error.description) {
|
|
172
|
-
url.searchParams.set(
|
|
173
|
-
'error_description',
|
|
174
|
-
oAuth2Error.description.replace(
|
|
175
|
-
/\$([0-9]+)/g,
|
|
176
|
-
(_: string, paramIndex: string): string => {
|
|
177
|
-
return ((err as YError).debug || [])[
|
|
178
|
-
parseInt(paramIndex, 10)
|
|
179
|
-
] as string;
|
|
180
|
-
},
|
|
181
|
-
),
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
167
|
async function initGetOAuth2Authorize({
|
|
187
168
|
OAUTH2,
|
|
169
|
+
OAUTH2_PAR = DEFAULT_OAUTH2_PAR,
|
|
188
170
|
ERRORS_DESCRIPTORS,
|
|
189
171
|
oAuth2Granters,
|
|
172
|
+
oAuth2AuthorizationRequests = undefined,
|
|
173
|
+
readClientGrants,
|
|
190
174
|
log,
|
|
191
|
-
}: {
|
|
192
|
-
OAUTH2:
|
|
175
|
+
}: WhookOAuth2AuthorizationRequestsConfig & {
|
|
176
|
+
OAUTH2: WhookOAuth2Options;
|
|
193
177
|
ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
|
|
194
|
-
oAuth2Granters:
|
|
178
|
+
oAuth2Granters: WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
|
|
179
|
+
readClientGrants: WhookOAuth2ReadClientGrantsService;
|
|
180
|
+
oAuth2AuthorizationRequests?: Pick<
|
|
181
|
+
WhookOAuth2AuthorizationRequestsService,
|
|
182
|
+
'check'
|
|
183
|
+
>;
|
|
195
184
|
log: LogService;
|
|
196
185
|
}) {
|
|
186
|
+
if (OAUTH2_PAR?.mode !== 'disabled' && !oAuth2AuthorizationRequests) {
|
|
187
|
+
log('error', `💥 - OAuth2 PAR endpoint required to enable PAR.`);
|
|
188
|
+
throw new YError('E_OAUTH2_MISCONFIGURED');
|
|
189
|
+
}
|
|
190
|
+
|
|
197
191
|
return async ({
|
|
198
|
-
query: {
|
|
199
|
-
response_type: responseType,
|
|
200
|
-
client_id: clientId,
|
|
201
|
-
redirect_uri: demandedRedirectURI = '',
|
|
202
|
-
scope: demandedScope = '',
|
|
203
|
-
state,
|
|
204
|
-
code_challenge: codeChallenge,
|
|
205
|
-
code_challenge_method: codeChallengeMethod,
|
|
206
|
-
...authorizeParameters
|
|
207
|
-
},
|
|
192
|
+
query: { client_id: givenClientId, request_uri: requestURI, ...query },
|
|
208
193
|
}: {
|
|
209
194
|
query: {
|
|
210
|
-
response_type: string;
|
|
211
195
|
client_id: string;
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
state: string;
|
|
215
|
-
code_challenge?: string;
|
|
216
|
-
code_challenge_method?: CodeChallengeMethod;
|
|
217
|
-
} & Record<string, unknown>;
|
|
196
|
+
request_uri?: string;
|
|
197
|
+
} & Partial<Omit<WhookOAuth2AuthorizeRequestParameters, 'client_id'>>;
|
|
218
198
|
}) => {
|
|
219
|
-
|
|
199
|
+
let usableRedirectURI: string | undefined = undefined;
|
|
200
|
+
let finalQuery: WhookOAuth2AuthorizeRequestParameters | undefined =
|
|
201
|
+
undefined;
|
|
202
|
+
|
|
203
|
+
// If everything goes well we proxy the request
|
|
204
|
+
// to the authentication server for acknowledgment
|
|
205
|
+
let url = new URL(OAUTH2.authenticateURL);
|
|
220
206
|
|
|
221
207
|
try {
|
|
222
|
-
const
|
|
223
|
-
(granter) =>
|
|
224
|
-
granter.authorizer &&
|
|
225
|
-
granter.authorizer.responseType === responseType,
|
|
226
|
-
);
|
|
208
|
+
const clientGrants = await readClientGrants(givenClientId);
|
|
227
209
|
|
|
228
|
-
if (
|
|
229
|
-
throw new YError('
|
|
210
|
+
if (givenClientId !== clientGrants.authenticationData.clientId) {
|
|
211
|
+
throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
|
|
212
|
+
givenClientId,
|
|
213
|
+
clientGrants.authenticationData.clientId,
|
|
214
|
+
]);
|
|
230
215
|
}
|
|
231
216
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
217
|
+
// Default to client URI to redirect errors
|
|
218
|
+
// before the request URI is decoded
|
|
219
|
+
usableRedirectURI = clientGrants.allowedRedirectURIS[0];
|
|
220
|
+
|
|
221
|
+
if (OAUTH2_PAR.mode === 'required' && !requestURI) {
|
|
222
|
+
throw new YError('E_OAUTH2_PAR_REQUIRED');
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (OAUTH2_PAR.mode === 'disabled' && requestURI) {
|
|
226
|
+
throw new YError('E_OAUTH2_PAR_NOT_SUPPORTED');
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (requestURI && isRequestURI(requestURI)) {
|
|
230
|
+
if (
|
|
231
|
+
Object.keys(query)
|
|
232
|
+
.filter(
|
|
233
|
+
(key) =>
|
|
234
|
+
typeof (query as Record<string, unknown>)[key] !== 'undefined',
|
|
235
|
+
)
|
|
236
|
+
.some((key) => !['client_id', 'request_uri'].includes(key))
|
|
237
|
+
) {
|
|
238
|
+
throw new YError(
|
|
239
|
+
'E_OAUTH2_BAD_REQUEST_URI_PARAMETERS',
|
|
240
|
+
Object.keys(query),
|
|
241
|
+
);
|
|
237
242
|
}
|
|
238
|
-
|
|
239
|
-
|
|
243
|
+
|
|
244
|
+
const result = await oAuth2AuthorizationRequests?.check(
|
|
245
|
+
givenClientId,
|
|
246
|
+
requestURI,
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
if (!result) {
|
|
250
|
+
throw new YError('E_OAUTH2_BAD_REQUEST_URI', [
|
|
251
|
+
requestURI,
|
|
252
|
+
undefined,
|
|
253
|
+
undefined,
|
|
254
|
+
]);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
finalQuery = {
|
|
258
|
+
...result?.parameters,
|
|
259
|
+
client_id: toUsableClientId([
|
|
260
|
+
givenClientId,
|
|
261
|
+
result.parameters.client_id,
|
|
262
|
+
]),
|
|
263
|
+
};
|
|
264
|
+
} else {
|
|
265
|
+
const responseType = query.response_type;
|
|
266
|
+
|
|
267
|
+
if (!responseType) {
|
|
268
|
+
throw new YError('E_OAUTH2_UNKNOWN_RESPONSE_TYPE', [responseType]);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
finalQuery = {
|
|
272
|
+
...query,
|
|
273
|
+
client_id: givenClientId,
|
|
274
|
+
response_type: responseType,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const {
|
|
279
|
+
response_type: responseType,
|
|
280
|
+
redirect_uri: demandedRedirectURI,
|
|
281
|
+
scope: demandedScope = '',
|
|
282
|
+
state,
|
|
283
|
+
code_challenge: codeChallenge,
|
|
284
|
+
code_challenge_method: codeChallengeMethod,
|
|
285
|
+
} = finalQuery;
|
|
286
|
+
|
|
287
|
+
// Have a valid redirect URI asap
|
|
288
|
+
usableRedirectURI = getUsableRedirectURI(
|
|
289
|
+
clientGrants.allowedRedirectURIS,
|
|
290
|
+
demandedRedirectURI,
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
const granter = oAuth2Granters.find(
|
|
294
|
+
(granter) => granter.responseType === responseType,
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
if (!granter?.authorize) {
|
|
298
|
+
throw new YError('E_OAUTH2_UNKNOWN_RESPONSE_TYPE', [responseType]);
|
|
240
299
|
}
|
|
241
300
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
clientId,
|
|
247
|
-
redirectURI: demandedRedirectURI,
|
|
248
|
-
scope: demandedScope,
|
|
249
|
-
},
|
|
250
|
-
camelCaseObjectProperties(authorizeParameters),
|
|
301
|
+
checkCodeChallengeParameters(
|
|
302
|
+
responseType,
|
|
303
|
+
codeChallenge,
|
|
304
|
+
OAUTH2.forcePKCE,
|
|
251
305
|
);
|
|
252
306
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
307
|
+
const demandedScopes = filterScopes(
|
|
308
|
+
filterScopes(
|
|
309
|
+
parseOAuth2Scope(demandedScope),
|
|
310
|
+
OAUTH2.allowedScopes,
|
|
311
|
+
!!OAUTH2.strictScopesChecks,
|
|
312
|
+
),
|
|
313
|
+
clientGrants.allowedScopes,
|
|
314
|
+
!!OAUTH2.strictScopesChecks,
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
const { scopes } = await granter.authorize({
|
|
318
|
+
clientId: givenClientId,
|
|
319
|
+
clientGrants,
|
|
320
|
+
demandedScopes,
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
const paramsHash: Record<string, string> = {
|
|
324
|
+
type: responseType,
|
|
325
|
+
redirect_uri: usableRedirectURI,
|
|
326
|
+
scope: stringifyScopes(scopes),
|
|
327
|
+
client_id: givenClientId,
|
|
328
|
+
};
|
|
257
329
|
|
|
258
330
|
if (responseType === 'code' && codeChallenge) {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
331
|
+
paramsHash.code_challenge = codeChallenge;
|
|
332
|
+
paramsHash.code_challenge_method =
|
|
333
|
+
codeChallengeMethod || PLAIN_CODE_CHALLENGE_METHOD;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (state) {
|
|
337
|
+
paramsHash.state = state;
|
|
264
338
|
}
|
|
339
|
+
|
|
340
|
+
addParamsToURL(url, paramsHash, 'query');
|
|
265
341
|
} catch (err) {
|
|
266
342
|
log('debug', '👫 - OAuth2 authorize error.');
|
|
267
343
|
log('error-stack', printStackTrace(err));
|
|
268
344
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
345
|
+
// If errors happen we try to directly redirect
|
|
346
|
+
// to the demanded redirect URI when valid
|
|
347
|
+
try {
|
|
348
|
+
if (usableRedirectURI) {
|
|
349
|
+
url = new URL(usableRedirectURI);
|
|
350
|
+
}
|
|
351
|
+
} catch (err) {
|
|
352
|
+
log('debug', `💥 - Could not redirect to demanded uri.`);
|
|
353
|
+
log('debug-stack', printStackTrace(err));
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const paramsHash = buildParamsFromError(
|
|
272
357
|
err as YError,
|
|
273
|
-
ERRORS_DESCRIPTORS[(err as YError).code] ||
|
|
358
|
+
ERRORS_DESCRIPTORS[(err as YError).code] ||
|
|
359
|
+
ERRORS_DESCRIPTORS.E_OAUTH2_UNEXPECTED_ERROR,
|
|
274
360
|
);
|
|
275
|
-
}
|
|
276
361
|
|
|
277
|
-
|
|
278
|
-
|
|
362
|
+
if (finalQuery && finalQuery.state) {
|
|
363
|
+
paramsHash.state = finalQuery.state;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
addParamsToURL(
|
|
367
|
+
url,
|
|
368
|
+
paramsHash,
|
|
369
|
+
finalQuery?.response_type === 'token' ? 'fragment' : 'query',
|
|
370
|
+
);
|
|
279
371
|
}
|
|
280
372
|
|
|
281
373
|
return {
|