@whook/oauth2 25.2.0 → 26.0.0
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 +30 -11
- package/dist/index.js +15 -8
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +1258 -399
- package/dist/index.test.js.map +1 -1
- 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 +113 -0
- package/dist/libs/errors.js +178 -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 +7 -0
- package/dist/libs/redirectURI.js +78 -0
- package/dist/libs/redirectURI.js.map +1 -0
- package/dist/libs/schemas.d.ts +23 -0
- package/dist/libs/schemas.js +25 -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 +7 -0
- package/dist/libs/verifier.js +37 -0
- package/dist/libs/verifier.js.map +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 +13 -50
- package/dist/routes/getOAuth2Authorize.js +60 -86
- package/dist/routes/getOAuth2Authorize.js.map +1 -1
- package/dist/routes/getOAuth2Authorize.test.js +147 -68
- package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.d.ts +8 -8
- package/dist/routes/getOAuth2WellKnownMetadata.js +20 -9
- package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.test.js +38 -37
- 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 +20 -10
- package/dist/routes/postOAuth2Acknowledge.js +54 -38
- package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
- package/dist/routes/postOAuth2Acknowledge.test.js +137 -72
- package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -1
- 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 +35 -108
- package/dist/routes/postOAuth2Token.js +53 -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 +1 -1
- package/dist/services/authCookies.js.map +1 -1
- package/dist/services/authCookies.test.js.map +1 -1
- package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +129 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.js +221 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.d.ts +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js +474 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.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 +81 -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 +72 -0
- package/dist/services/oAuth2ImplicitGranter.js.map +1 -0
- package/dist/services/oAuth2ImplicitGranter.test.d.ts +1 -0
- package/dist/services/oAuth2ImplicitGranter.test.js +727 -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 +11 -11
- package/src/index.test.ts +1314 -404
- package/src/index.ts +46 -53
- package/src/libs/clients.ts +22 -0
- package/src/libs/errors.ts +325 -0
- package/src/libs/grants.ts +15 -0
- package/src/libs/redirectURI.ts +120 -0
- package/src/libs/schemas.ts +27 -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 +57 -0
- package/src/routes/getOAuth2Authorize.test.ts +179 -96
- package/src/routes/getOAuth2Authorize.ts +97 -124
- package/src/routes/getOAuth2WellKnownMetadata.test.ts +58 -60
- package/src/routes/getOAuth2WellKnownMetadata.ts +26 -11
- 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 +169 -110
- package/src/routes/postOAuth2Acknowledge.ts +101 -61
- 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 +92 -136
- package/src/services/authCookies.test.ts +5 -5
- package/src/services/authCookies.ts +16 -18
- package/src/services/oAuth2AuthorizationCodeGranter.test.ts +564 -0
- package/src/services/oAuth2AuthorizationCodeGranter.ts +388 -0
- package/src/services/oAuth2ClientCredentialsGranter.test.ts +303 -0
- package/src/services/oAuth2ClientCredentialsGranter.ts +68 -43
- package/src/services/oAuth2Granters.ts +101 -195
- package/src/services/oAuth2ImplicitGranter.test.ts +811 -0
- package/src/services/oAuth2ImplicitGranter.ts +163 -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/verifier.test.d.ts} +0 -0
|
@@ -1,65 +1,53 @@
|
|
|
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,
|
|
15
13
|
} from '../services/oAuth2Granters.js';
|
|
16
14
|
import { type LogService } from 'common-services';
|
|
17
15
|
import {
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
filterScopes,
|
|
17
|
+
parseOAuth2Scope,
|
|
18
|
+
stringifyScopes,
|
|
19
|
+
} from '../libs/scopes.js';
|
|
20
|
+
import {
|
|
21
|
+
type CodeChallengeMethod,
|
|
20
22
|
PLAIN_CODE_CHALLENGE_METHOD,
|
|
21
|
-
} from '../
|
|
23
|
+
} from '../libs/verifier.js';
|
|
24
|
+
import { scopeSchema } from '../libs/schemas.js';
|
|
25
|
+
import {
|
|
26
|
+
AUTHORIZATION_CODE_RESPONSE_TYPE,
|
|
27
|
+
codeChallengeMethodSchema,
|
|
28
|
+
codeChallengeMethodParameter,
|
|
29
|
+
codeChallengeSchema,
|
|
30
|
+
codeChallengeParameter,
|
|
31
|
+
} from '../services/oAuth2AuthorizationCodeGranter.js';
|
|
32
|
+
import { IMPLICIT_RESPONSE_TYPE } from '../services/oAuth2ImplicitGranter.js';
|
|
33
|
+
import { camelCaseObjectProperties } from '../libs/utils.js';
|
|
34
|
+
import { addParamsToURL, buildParamsFromError } from '../libs/redirectURI.js';
|
|
35
|
+
|
|
36
|
+
export {
|
|
37
|
+
scopeSchema,
|
|
38
|
+
codeChallengeMethodSchema,
|
|
39
|
+
codeChallengeMethodParameter,
|
|
40
|
+
codeChallengeSchema,
|
|
41
|
+
codeChallengeParameter,
|
|
42
|
+
};
|
|
22
43
|
|
|
23
44
|
/* Architecture Note #1: OAuth2 authorize
|
|
24
|
-
|
|
25
|
-
|
|
45
|
+
|
|
46
|
+
This endpoint simply redirects the user to the authentication
|
|
47
|
+
server page and checks the application details are
|
|
26
48
|
fine.
|
|
27
|
-
*/
|
|
28
49
|
|
|
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;
|
|
50
|
+
*/
|
|
63
51
|
|
|
64
52
|
export const responseTypeParameter = {
|
|
65
53
|
name: 'responseType',
|
|
@@ -69,7 +57,7 @@ export const responseTypeParameter = {
|
|
|
69
57
|
required: true,
|
|
70
58
|
schema: {
|
|
71
59
|
type: 'string',
|
|
72
|
-
enum: [
|
|
60
|
+
enum: [AUTHORIZATION_CODE_RESPONSE_TYPE, IMPLICIT_RESPONSE_TYPE],
|
|
73
61
|
},
|
|
74
62
|
},
|
|
75
63
|
} as const satisfies WhookAPIParameterDefinition;
|
|
@@ -102,11 +90,8 @@ export const scopeParameter = {
|
|
|
102
90
|
parameter: {
|
|
103
91
|
in: 'query',
|
|
104
92
|
name: 'scope',
|
|
105
|
-
description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',
|
|
106
93
|
required: false,
|
|
107
|
-
schema:
|
|
108
|
-
type: 'string',
|
|
109
|
-
},
|
|
94
|
+
schema: refersTo(scopeSchema),
|
|
110
95
|
},
|
|
111
96
|
} as const satisfies WhookAPIParameterDefinition;
|
|
112
97
|
export const stateParameter = {
|
|
@@ -114,7 +99,7 @@ export const stateParameter = {
|
|
|
114
99
|
parameter: {
|
|
115
100
|
in: 'query',
|
|
116
101
|
name: 'state',
|
|
117
|
-
required:
|
|
102
|
+
required: false,
|
|
118
103
|
schema: {
|
|
119
104
|
type: 'string',
|
|
120
105
|
},
|
|
@@ -126,8 +111,8 @@ export const definition = {
|
|
|
126
111
|
path: '/oauth2/authorize',
|
|
127
112
|
operation: {
|
|
128
113
|
operationId: 'getOAuth2Authorize',
|
|
129
|
-
summary: `Implements the [Authorization Endpoint](https://
|
|
130
|
-
as defined per the OAuth2 RFC.`,
|
|
114
|
+
summary: `Implements the [Authorization Endpoint](https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-15.html#section-3.1)
|
|
115
|
+
as defined per the OAuth2.1 RFC.`,
|
|
131
116
|
tags: ['oauth2'],
|
|
132
117
|
parameters: [
|
|
133
118
|
refersTo(responseTypeParameter),
|
|
@@ -146,58 +131,21 @@ export const definition = {
|
|
|
146
131
|
},
|
|
147
132
|
} as const satisfies WhookRouteDefinition;
|
|
148
133
|
|
|
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
134
|
async function initGetOAuth2Authorize({
|
|
187
135
|
OAUTH2,
|
|
188
136
|
ERRORS_DESCRIPTORS,
|
|
189
137
|
oAuth2Granters,
|
|
190
138
|
log,
|
|
191
139
|
}: {
|
|
192
|
-
OAUTH2:
|
|
140
|
+
OAUTH2: WhookOAuth2Options;
|
|
193
141
|
ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
|
|
194
|
-
oAuth2Granters:
|
|
142
|
+
oAuth2Granters: WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
|
|
195
143
|
log: LogService;
|
|
196
144
|
}) {
|
|
197
145
|
return async ({
|
|
198
146
|
query: {
|
|
199
147
|
response_type: responseType,
|
|
200
|
-
client_id:
|
|
148
|
+
client_id: givenClientId,
|
|
201
149
|
redirect_uri: demandedRedirectURI = '',
|
|
202
150
|
scope: demandedScope = '',
|
|
203
151
|
state,
|
|
@@ -214,68 +162,93 @@ async function initGetOAuth2Authorize({
|
|
|
214
162
|
state: string;
|
|
215
163
|
code_challenge?: string;
|
|
216
164
|
code_challenge_method?: CodeChallengeMethod;
|
|
217
|
-
} & Record<string,
|
|
165
|
+
} & Record<string, string>;
|
|
218
166
|
}) => {
|
|
219
|
-
|
|
167
|
+
// If everything goes well we proxy the request
|
|
168
|
+
// to the authentication server for acknowledgment
|
|
169
|
+
let url = new URL(OAUTH2.authenticateURL);
|
|
220
170
|
|
|
221
171
|
try {
|
|
222
172
|
const granter = oAuth2Granters.find(
|
|
223
|
-
(granter) =>
|
|
224
|
-
granter.authorizer &&
|
|
225
|
-
granter.authorizer.responseType === responseType,
|
|
173
|
+
(granter) => granter.responseType === responseType,
|
|
226
174
|
);
|
|
227
175
|
|
|
228
|
-
if (!granter) {
|
|
229
|
-
throw new YError('
|
|
176
|
+
if (!granter?.authorize) {
|
|
177
|
+
throw new YError('E_OAUTH2_UNKNOWN_RESPONSE_TYPE', [responseType]);
|
|
230
178
|
}
|
|
231
179
|
|
|
232
180
|
if (responseType === 'code') {
|
|
233
181
|
if (!codeChallenge) {
|
|
234
182
|
if (OAUTH2.forcePKCE) {
|
|
235
|
-
throw new YError('
|
|
183
|
+
throw new YError('E_OAUTH2_PKCE_REQUIRED', [responseType]);
|
|
236
184
|
}
|
|
237
185
|
}
|
|
238
186
|
} else if (codeChallenge) {
|
|
239
|
-
throw new YError('
|
|
187
|
+
throw new YError('E_OAUTH2_PKCE_NOT_SUPPORTED', [responseType]);
|
|
240
188
|
}
|
|
241
189
|
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
190
|
+
const demandedScopes = filterScopes(
|
|
191
|
+
parseOAuth2Scope(demandedScope),
|
|
192
|
+
OAUTH2.allowedScopes,
|
|
193
|
+
!!OAUTH2.strictScopesChecks,
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
const { clientId, redirectURI, scopes } = await granter.authorize(
|
|
245
197
|
{
|
|
246
|
-
clientId,
|
|
247
|
-
|
|
248
|
-
|
|
198
|
+
clientId: givenClientId,
|
|
199
|
+
demandedRedirectURI,
|
|
200
|
+
demandedScopes,
|
|
249
201
|
},
|
|
250
202
|
camelCaseObjectProperties(authorizeParameters),
|
|
251
203
|
);
|
|
252
204
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
205
|
+
const paramsHash: Record<string, string> = {
|
|
206
|
+
type: responseType,
|
|
207
|
+
redirect_uri: redirectURI,
|
|
208
|
+
scope: stringifyScopes(scopes),
|
|
209
|
+
client_id: clientId,
|
|
210
|
+
};
|
|
257
211
|
|
|
258
212
|
if (responseType === 'code' && codeChallenge) {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
codeChallengeMethod || PLAIN_CODE_CHALLENGE_METHOD,
|
|
263
|
-
);
|
|
213
|
+
paramsHash.code_challenge = codeChallenge;
|
|
214
|
+
paramsHash.code_challenge_method =
|
|
215
|
+
codeChallengeMethod || PLAIN_CODE_CHALLENGE_METHOD;
|
|
264
216
|
}
|
|
217
|
+
|
|
218
|
+
if (state) {
|
|
219
|
+
paramsHash.state = state;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
addParamsToURL(url, paramsHash, 'query');
|
|
265
223
|
} catch (err) {
|
|
266
224
|
log('debug', '👫 - OAuth2 authorize error.');
|
|
267
225
|
log('error-stack', printStackTrace(err));
|
|
268
226
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
227
|
+
// If errors happen we try to directly redirect
|
|
228
|
+
// to the demanded redirect URI
|
|
229
|
+
try {
|
|
230
|
+
url = new URL(demandedRedirectURI);
|
|
231
|
+
} catch (err) {
|
|
232
|
+
log('debug', `💥 - Could not redirect to demanded uri.`);
|
|
233
|
+
log('debug-stack', printStackTrace(err));
|
|
234
|
+
url = new URL(OAUTH2.authenticateURL);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const paramsHash = buildParamsFromError(
|
|
272
238
|
err as YError,
|
|
273
|
-
ERRORS_DESCRIPTORS[(err as YError).code] ||
|
|
239
|
+
ERRORS_DESCRIPTORS[(err as YError).code] ||
|
|
240
|
+
ERRORS_DESCRIPTORS.E_OAUTH2_UNEXPECTED_ERROR,
|
|
274
241
|
);
|
|
275
|
-
}
|
|
276
242
|
|
|
277
|
-
|
|
278
|
-
|
|
243
|
+
if (state) {
|
|
244
|
+
paramsHash.state = state;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
addParamsToURL(
|
|
248
|
+
url,
|
|
249
|
+
paramsHash,
|
|
250
|
+
responseType === 'token' ? 'fragment' : 'query',
|
|
251
|
+
);
|
|
279
252
|
}
|
|
280
253
|
|
|
281
254
|
return {
|
|
@@ -2,80 +2,66 @@
|
|
|
2
2
|
import { describe, test, beforeEach, jest, expect } from '@jest/globals';
|
|
3
3
|
import initGetOAuth2WellKnown from './getOAuth2WellKnownMetadata.js';
|
|
4
4
|
import { YError } from 'yerror';
|
|
5
|
-
import { type
|
|
6
|
-
import { type
|
|
5
|
+
import { type WhookOAuth2AuthorizationCodeGranterService } from '../services/oAuth2AuthorizationCodeGranter.js';
|
|
6
|
+
import { type WhookOAuth2ImplicitGranterService } from '../services/oAuth2ImplicitGranter.js';
|
|
7
7
|
import { type LogService } from 'common-services';
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
type WhookOAuth2GranterDefinitions,
|
|
10
|
+
type WhookOAuth2GranterService,
|
|
11
|
+
} from '../index.js';
|
|
9
12
|
import { type WhookRoutesDefinitionsService } from '@whook/whook';
|
|
10
13
|
|
|
11
14
|
describe('getOAuth2WellKnown', () => {
|
|
12
15
|
const log = jest.fn<LogService>();
|
|
13
16
|
const codeGranter = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
},
|
|
29
|
-
authenticator: {
|
|
30
|
-
grantType: 'authorization_code',
|
|
31
|
-
authenticate:
|
|
32
|
-
jest.fn<
|
|
33
|
-
NonNullable<OAuth2CodeGranterService['authenticator']>['authenticate']
|
|
34
|
-
>(),
|
|
35
|
-
},
|
|
17
|
+
grantType: 'authorization_code',
|
|
18
|
+
responseType: 'code',
|
|
19
|
+
authorize:
|
|
20
|
+
jest.fn<
|
|
21
|
+
NonNullable<WhookOAuth2AuthorizationCodeGranterService['authorize']>
|
|
22
|
+
>(),
|
|
23
|
+
acknowledge:
|
|
24
|
+
jest.fn<
|
|
25
|
+
NonNullable<WhookOAuth2AuthorizationCodeGranterService['acknowledge']>
|
|
26
|
+
>(),
|
|
27
|
+
authenticate:
|
|
28
|
+
jest.fn<
|
|
29
|
+
NonNullable<WhookOAuth2AuthorizationCodeGranterService['authenticate']>
|
|
30
|
+
>(),
|
|
36
31
|
};
|
|
37
32
|
const tokenGranter = {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
>(),
|
|
47
|
-
},
|
|
48
|
-
acknowledger: {
|
|
49
|
-
acknowledgmentType: 'token',
|
|
50
|
-
acknowledge:
|
|
51
|
-
jest.fn<
|
|
52
|
-
NonNullable<OAuth2TokenGranterService['acknowledger']>['acknowledge']
|
|
53
|
-
>(),
|
|
54
|
-
},
|
|
33
|
+
grantType: 'implicit',
|
|
34
|
+
responseType: 'token',
|
|
35
|
+
authorize:
|
|
36
|
+
jest.fn<
|
|
37
|
+
NonNullable<NonNullable<WhookOAuth2ImplicitGranterService['authorize']>>
|
|
38
|
+
>(),
|
|
39
|
+
acknowledge:
|
|
40
|
+
jest.fn<NonNullable<WhookOAuth2ImplicitGranterService['acknowledge']>>(),
|
|
55
41
|
};
|
|
56
42
|
const oAuth2Granters = [
|
|
57
43
|
codeGranter,
|
|
58
44
|
tokenGranter,
|
|
59
|
-
] as unknown as
|
|
45
|
+
] as unknown as WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
|
|
60
46
|
|
|
61
47
|
beforeEach(() => {
|
|
62
48
|
log.mockReset();
|
|
63
49
|
[
|
|
64
|
-
codeGranter.
|
|
65
|
-
codeGranter.
|
|
66
|
-
codeGranter.
|
|
67
|
-
tokenGranter.
|
|
68
|
-
tokenGranter.
|
|
50
|
+
codeGranter.authorize,
|
|
51
|
+
codeGranter.acknowledge,
|
|
52
|
+
codeGranter.authenticate,
|
|
53
|
+
tokenGranter.authorize,
|
|
54
|
+
tokenGranter.acknowledge,
|
|
69
55
|
].forEach((mock) => mock.mockReset());
|
|
70
56
|
});
|
|
71
57
|
|
|
72
58
|
test('should return OAuth2 metadata', async () => {
|
|
73
59
|
[
|
|
74
|
-
codeGranter.
|
|
75
|
-
codeGranter.
|
|
76
|
-
codeGranter.
|
|
77
|
-
tokenGranter.
|
|
78
|
-
tokenGranter.
|
|
60
|
+
codeGranter.authorize,
|
|
61
|
+
codeGranter.acknowledge,
|
|
62
|
+
codeGranter.authenticate,
|
|
63
|
+
tokenGranter.authorize,
|
|
64
|
+
tokenGranter.acknowledge,
|
|
79
65
|
].forEach((mock: any) =>
|
|
80
66
|
mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
|
|
81
67
|
);
|
|
@@ -97,6 +83,13 @@ describe('getOAuth2WellKnown', () => {
|
|
|
97
83
|
},
|
|
98
84
|
},
|
|
99
85
|
},
|
|
86
|
+
postOAuth2Revoke: {
|
|
87
|
+
module: {
|
|
88
|
+
definition: {
|
|
89
|
+
path: '/v0/oauth2/revoke',
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
100
93
|
} as unknown as WhookRoutesDefinitionsService,
|
|
101
94
|
API: {
|
|
102
95
|
openapi: '3.2',
|
|
@@ -124,16 +117,14 @@ describe('getOAuth2WellKnown', () => {
|
|
|
124
117
|
expect({
|
|
125
118
|
response,
|
|
126
119
|
logCalls: log.mock.calls.filter((args) => args[0].endsWith('stack')),
|
|
127
|
-
codeGranterAuthorizerAuthorizeCalls:
|
|
128
|
-
codeGranter.authorizer.authorize.mock.calls,
|
|
120
|
+
codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
|
|
129
121
|
codeGranterAcknowledgerAcknowledgeCalls:
|
|
130
|
-
codeGranter.
|
|
122
|
+
codeGranter.acknowledge.mock.calls,
|
|
131
123
|
codeGranterAuthenticatorAuthenticateCalls:
|
|
132
|
-
codeGranter.
|
|
133
|
-
tokenGranterAuthorizerAuthorizeCalls:
|
|
134
|
-
tokenGranter.authorizer.authorize.mock.calls,
|
|
124
|
+
codeGranter.authenticate.mock.calls,
|
|
125
|
+
tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
|
|
135
126
|
tokenGranterAcknowledgerAcknowledgeCalls:
|
|
136
|
-
tokenGranter.
|
|
127
|
+
tokenGranter.acknowledge.mock.calls,
|
|
137
128
|
}).toMatchInlineSnapshot(`
|
|
138
129
|
{
|
|
139
130
|
"codeGranterAcknowledgerAcknowledgeCalls": [],
|
|
@@ -145,12 +136,18 @@ describe('getOAuth2WellKnown', () => {
|
|
|
145
136
|
"authorization_endpoint": "https://server.example.com/v0/oauth2/authorize",
|
|
146
137
|
"grant_types_supported": [
|
|
147
138
|
"authorization_code",
|
|
139
|
+
"implicit",
|
|
148
140
|
],
|
|
149
141
|
"issuer": "https://server.example.com",
|
|
150
142
|
"response_types_supported": [
|
|
151
143
|
"code",
|
|
152
144
|
"token",
|
|
153
145
|
],
|
|
146
|
+
"revocation_endpoint": "https://server.example.com/v0/oauth2/revoke",
|
|
147
|
+
"revocation_endpoint_auth_methods_supported": [
|
|
148
|
+
"client_secret_basic",
|
|
149
|
+
"client_secret_post",
|
|
150
|
+
],
|
|
154
151
|
"scopes_supported": [
|
|
155
152
|
"user",
|
|
156
153
|
"admin",
|
|
@@ -158,6 +155,7 @@ describe('getOAuth2WellKnown', () => {
|
|
|
158
155
|
"token_endpoint": "https://server.example.com/v0/oauth2/token",
|
|
159
156
|
"token_endpoint_auth_methods_supported": [
|
|
160
157
|
"client_secret_basic",
|
|
158
|
+
"client_secret_post",
|
|
161
159
|
],
|
|
162
160
|
},
|
|
163
161
|
"status": 200,
|
|
@@ -6,12 +6,18 @@ import {
|
|
|
6
6
|
type WhookRouteDefinition,
|
|
7
7
|
} from '@whook/whook';
|
|
8
8
|
import { autoService, location } from 'knifecycle';
|
|
9
|
-
import {
|
|
10
|
-
|
|
9
|
+
import {
|
|
10
|
+
type WhookOAuth2GranterDefinitions,
|
|
11
|
+
type WhookOAuth2GranterService,
|
|
12
|
+
} from '../services/oAuth2Granters.js';
|
|
13
|
+
import { codeChallengeMethodSchema } from '../services/oAuth2AuthorizationCodeGranter.js';
|
|
14
|
+
import { scopeTokenSchema, scopeTokensSchema } from '../libs/schemas.js';
|
|
11
15
|
import { type LogService } from 'common-services';
|
|
12
16
|
import { type OpenAPI } from 'ya-open-api-types';
|
|
13
17
|
import { collectScopesFromAPI } from '../libs/scopes.js';
|
|
14
18
|
|
|
19
|
+
export { scopeTokenSchema, scopeTokensSchema, codeChallengeMethodSchema };
|
|
20
|
+
|
|
15
21
|
// OAuth Token Endpoint Authentication Methods
|
|
16
22
|
export const endpointAuthenticationMethodsSchema = {
|
|
17
23
|
name: 'OAuthTokenEndpointAuthenticationMethods',
|
|
@@ -89,7 +95,7 @@ export const oAuth2MetadataSchema = {
|
|
|
89
95
|
token_endpoint: refersTo(httpsProtocolURISchema),
|
|
90
96
|
jwks_uri: refersTo(httpsProtocolURISchema),
|
|
91
97
|
registration_endpoint: refersTo(httpsProtocolURISchema),
|
|
92
|
-
scopes_supported:
|
|
98
|
+
scopes_supported: refersTo(scopeTokensSchema),
|
|
93
99
|
response_types_supported: {
|
|
94
100
|
type: 'array',
|
|
95
101
|
items: {
|
|
@@ -295,7 +301,7 @@ async function initGetOAuth2WellKnownMetadata({
|
|
|
295
301
|
API: OpenAPI;
|
|
296
302
|
BASE_URL: string;
|
|
297
303
|
ROUTES_DEFINITIONS: WhookRoutesDefinitionsService;
|
|
298
|
-
oAuth2Granters:
|
|
304
|
+
oAuth2Granters: WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
|
|
299
305
|
log: LogService;
|
|
300
306
|
}) {
|
|
301
307
|
if (!BASE_URL.startsWith('https')) {
|
|
@@ -306,22 +312,31 @@ async function initGetOAuth2WellKnownMetadata({
|
|
|
306
312
|
issuer: `${BASE_URL}`,
|
|
307
313
|
authorization_endpoint: `${BASE_URL}${ROUTES_DEFINITIONS['getOAuth2Authorize']?.module.definition.path || '/oauth2/authorize'}`,
|
|
308
314
|
token_endpoint: `${BASE_URL}${ROUTES_DEFINITIONS['postOAuth2Token']?.module.definition.path || '/oauth2/token'}`,
|
|
309
|
-
|
|
315
|
+
// TODO: Put it behind options (allow none)
|
|
316
|
+
token_endpoint_auth_methods_supported: [
|
|
317
|
+
'client_secret_basic',
|
|
318
|
+
'client_secret_post',
|
|
319
|
+
],
|
|
310
320
|
grant_types_supported: [
|
|
311
321
|
...new Set(
|
|
312
|
-
oAuth2Granters
|
|
313
|
-
.map((granter) => granter.authenticator?.grantType)
|
|
314
|
-
.filter(identity),
|
|
322
|
+
oAuth2Granters.map((granter) => granter.grantType).filter(identity),
|
|
315
323
|
),
|
|
316
324
|
],
|
|
317
325
|
response_types_supported: [
|
|
318
326
|
...new Set(
|
|
319
|
-
oAuth2Granters
|
|
320
|
-
.map((granter) => granter.authorizer?.responseType)
|
|
321
|
-
.filter(identity),
|
|
327
|
+
oAuth2Granters.map((granter) => granter.responseType).filter(identity),
|
|
322
328
|
),
|
|
323
329
|
],
|
|
324
330
|
scopes_supported: collectScopesFromAPI(API),
|
|
331
|
+
...(ROUTES_DEFINITIONS['postOAuth2Revoke']?.module.definition.path
|
|
332
|
+
? {
|
|
333
|
+
revocation_endpoint: `${BASE_URL}${ROUTES_DEFINITIONS['postOAuth2Revoke'].module.definition.path}`,
|
|
334
|
+
revocation_endpoint_auth_methods_supported: [
|
|
335
|
+
'client_secret_basic',
|
|
336
|
+
'client_secret_post',
|
|
337
|
+
],
|
|
338
|
+
}
|
|
339
|
+
: {}),
|
|
325
340
|
};
|
|
326
341
|
|
|
327
342
|
return async () => {
|
|
@@ -11,6 +11,9 @@ import {
|
|
|
11
11
|
import { type LogService } from 'common-services';
|
|
12
12
|
import { type OpenAPI } from 'ya-open-api-types';
|
|
13
13
|
import { collectScopesFromAPI } from '../libs/scopes.js';
|
|
14
|
+
import { scopeTokensSchema } from '../libs/schemas.js';
|
|
15
|
+
|
|
16
|
+
export { scopeTokensSchema };
|
|
14
17
|
|
|
15
18
|
export const bearerMethodSchema = {
|
|
16
19
|
name: 'OAuth2BearerMethod',
|
|
@@ -35,12 +38,7 @@ export const oAuth2ProtectedResourceMetadataSchema = {
|
|
|
35
38
|
items: refersTo(httpsProtocolURISchema),
|
|
36
39
|
},
|
|
37
40
|
jwks_uri: refersTo(httpsProtocolURISchema),
|
|
38
|
-
scopes_supported:
|
|
39
|
-
type: 'array',
|
|
40
|
-
items: {
|
|
41
|
-
type: 'string',
|
|
42
|
-
},
|
|
43
|
-
},
|
|
41
|
+
scopes_supported: refersTo(scopeTokensSchema),
|
|
44
42
|
bearer_methods_supported: {
|
|
45
43
|
type: 'array',
|
|
46
44
|
items: refersTo(bearerMethodSchema),
|