@whook/oauth2 24.1.1 → 25.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.
Files changed (64) hide show
  1. package/README.md +12 -10
  2. package/dist/index.d.ts +7 -6
  3. package/dist/index.js +5 -4
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.test.js +173 -168
  6. package/dist/index.test.js.map +1 -1
  7. package/dist/routes/getOAuth2Authorize.d.ts +39 -2
  8. package/dist/routes/getOAuth2Authorize.js +53 -2
  9. package/dist/routes/getOAuth2Authorize.js.map +1 -1
  10. package/dist/routes/getOAuth2WellKnownMetadata.d.ts +239 -0
  11. package/dist/routes/getOAuth2WellKnownMetadata.js +299 -0
  12. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -0
  13. package/dist/routes/getOAuth2WellKnownMetadata.test.d.ts +1 -0
  14. package/dist/routes/getOAuth2WellKnownMetadata.test.js +100 -0
  15. package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -0
  16. package/dist/routes/postOAuth2Acknowledge.d.ts +1 -1
  17. package/dist/routes/postOAuth2Acknowledge.js +12 -10
  18. package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
  19. package/dist/routes/postOAuth2Acknowledge.test.js +26 -21
  20. package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -1
  21. package/dist/routes/postOAuth2Token.d.ts +10 -0
  22. package/dist/routes/postOAuth2Token.js +10 -0
  23. package/dist/routes/postOAuth2Token.js.map +1 -1
  24. package/dist/services/authCookies.d.ts +10 -9
  25. package/dist/services/authCookies.js.map +1 -1
  26. package/dist/services/authCookies.test.js.map +1 -1
  27. package/dist/services/oAuth2ClientCredentialsGranter.d.ts +5 -5
  28. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
  29. package/dist/services/oAuth2CodeGranter.d.ts +31 -6
  30. package/dist/services/oAuth2CodeGranter.js +37 -9
  31. package/dist/services/oAuth2CodeGranter.js.map +1 -1
  32. package/dist/services/oAuth2CodeGranter.test.js +56 -20
  33. package/dist/services/oAuth2CodeGranter.test.js.map +1 -1
  34. package/dist/services/oAuth2Granters.d.ts +30 -26
  35. package/dist/services/oAuth2Granters.js +14 -0
  36. package/dist/services/oAuth2Granters.js.map +1 -1
  37. package/dist/services/oAuth2PasswordGranter.d.ts +5 -5
  38. package/dist/services/oAuth2PasswordGranter.js.map +1 -1
  39. package/dist/services/oAuth2RefreshTokenGranter.d.ts +5 -5
  40. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
  41. package/dist/services/oAuth2TokenGranter.d.ts +13 -5
  42. package/dist/services/oAuth2TokenGranter.js +11 -7
  43. package/dist/services/oAuth2TokenGranter.js.map +1 -1
  44. package/package.json +9 -9
  45. package/src/__snapshots__/index.test.ts.snap +3 -0
  46. package/src/index.test.ts +177 -167
  47. package/src/index.ts +34 -2
  48. package/src/routes/__snapshots__/getOAuth2Authorize.test.ts.snap +1 -2
  49. package/src/routes/getOAuth2Authorize.ts +67 -2
  50. package/src/routes/getOAuth2WellKnownMetadata.test.ts +132 -0
  51. package/src/routes/getOAuth2WellKnownMetadata.ts +335 -0
  52. package/src/routes/postOAuth2Acknowledge.test.ts +26 -21
  53. package/src/routes/postOAuth2Acknowledge.ts +19 -18
  54. package/src/routes/postOAuth2Token.ts +12 -1
  55. package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +3 -0
  56. package/src/services/authCookies.test.ts +5 -5
  57. package/src/services/authCookies.ts +13 -9
  58. package/src/services/oAuth2ClientCredentialsGranter.ts +5 -4
  59. package/src/services/oAuth2CodeGranter.test.ts +97 -30
  60. package/src/services/oAuth2CodeGranter.ts +89 -15
  61. package/src/services/oAuth2Granters.ts +60 -64
  62. package/src/services/oAuth2PasswordGranter.ts +5 -4
  63. package/src/services/oAuth2RefreshTokenGranter.ts +5 -4
  64. package/src/services/oAuth2TokenGranter.ts +27 -15
@@ -7,12 +7,18 @@ import {
7
7
  type WhookAPIParameterDefinition,
8
8
  type WhookErrorsDescriptors,
9
9
  type WhookErrorDescriptor,
10
+ type WhookAPISchemaDefinition,
10
11
  } from '@whook/whook';
11
12
  import {
12
13
  type OAuth2Options,
13
14
  type OAuth2GranterService,
14
15
  } from '../services/oAuth2Granters.js';
15
16
  import { type LogService } from 'common-services';
17
+ import {
18
+ CODE_CHALLENGE_METHODS,
19
+ CodeChallengeMethod,
20
+ PLAIN_CODE_CHALLENGE_METHOD,
21
+ } from '../services/oAuth2CodeGranter.js';
16
22
 
17
23
  /* Architecture Note #1: OAuth2 authorize
18
24
  This endpoint simply redirect the user to the authentication
@@ -20,6 +26,41 @@ This endpoint simply redirect the user to the authentication
20
26
  fine.
21
27
  */
22
28
 
29
+ export const codeChallengeSchema = {
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;
63
+
23
64
  export const responseTypeParameter = {
24
65
  name: 'responseType',
25
66
  parameter: {
@@ -94,6 +135,8 @@ export const definition = {
94
135
  refersTo(redirectURIParameter),
95
136
  refersTo(scopeParameter),
96
137
  refersTo(stateParameter),
138
+ refersTo(codeChallengeParameter),
139
+ refersTo(codeChallengeMethodParameter),
97
140
  ],
98
141
  responses: {
99
142
  '302': {
@@ -158,6 +201,8 @@ async function initGetOAuth2Authorize({
158
201
  redirect_uri: demandedRedirectURI = '',
159
202
  scope: demandedScope = '',
160
203
  state,
204
+ code_challenge: codeChallenge,
205
+ code_challenge_method: codeChallengeMethod,
161
206
  ...authorizeParameters
162
207
  },
163
208
  }: {
@@ -167,6 +212,8 @@ async function initGetOAuth2Authorize({
167
212
  redirect_uri?: string;
168
213
  scope?: string;
169
214
  state: string;
215
+ code_challenge?: string;
216
+ code_challenge_method?: CodeChallengeMethod;
170
217
  } & Record<string, unknown>;
171
218
  }) => {
172
219
  const url = new URL(OAUTH2.authenticateURL);
@@ -182,6 +229,16 @@ async function initGetOAuth2Authorize({
182
229
  throw new YError('E_UNKNOWN_AUTHORIZER_TYPE', [responseType]);
183
230
  }
184
231
 
232
+ if (responseType === 'code') {
233
+ if (!codeChallenge) {
234
+ if (OAUTH2.forcePKCE) {
235
+ throw new YError('E_PKCE_REQUIRED', [responseType]);
236
+ }
237
+ }
238
+ } else if (codeChallenge) {
239
+ throw new YError('E_PKCE_NOT_SUPPORTED', [responseType]);
240
+ }
241
+
185
242
  const { applicationId, redirectURI, scope } = await (
186
243
  granter.authorizer as NonNullable<OAuth2GranterService['authorizer']>
187
244
  ).authorize(
@@ -197,9 +254,17 @@ async function initGetOAuth2Authorize({
197
254
  url.searchParams.set('redirect_uri', redirectURI);
198
255
  url.searchParams.set('scope', scope);
199
256
  url.searchParams.set('client_id', applicationId);
257
+
258
+ if (responseType === 'code' && codeChallenge) {
259
+ url.searchParams.set('code_challenge', codeChallenge);
260
+ url.searchParams.set(
261
+ 'code_challenge_method',
262
+ codeChallengeMethod || PLAIN_CODE_CHALLENGE_METHOD,
263
+ );
264
+ }
200
265
  } catch (err) {
201
- log('debug', '👫 - OAuth2 initialization error', (err as YError).code);
202
- log('error-stack', printStackTrace(err as Error));
266
+ log('debug', '👫 - OAuth2 authorize error.');
267
+ log('error-stack', printStackTrace(err));
203
268
 
204
269
  url.searchParams.set('redirect_uri', demandedRedirectURI);
205
270
  setURLError(
@@ -0,0 +1,132 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { describe, test, beforeEach, jest, expect } from '@jest/globals';
3
+ import initGetOAuth2WellKnown from './getOAuth2WellKnownMetadata.js';
4
+ import { YError } from 'yerror';
5
+ import { type OAuth2CodeGranterService } from '../services/oAuth2CodeGranter.js';
6
+ import { type OAuth2TokenGranterService } from '../services/oAuth2TokenGranter.js';
7
+ import { type LogService } from 'common-services';
8
+ import { type OAuth2GranterService } from '../index.js';
9
+
10
+ describe('getOAuth2WellKnown', () => {
11
+ const log = jest.fn<LogService>();
12
+ const codeGranter = {
13
+ type: 'code',
14
+ authorizer: {
15
+ responseType: 'code',
16
+ authorize:
17
+ jest.fn<
18
+ NonNullable<OAuth2CodeGranterService['authorizer']>['authorize']
19
+ >(),
20
+ },
21
+ acknowledger: {
22
+ acknowledgmentType: 'code',
23
+ acknowledge:
24
+ jest.fn<
25
+ NonNullable<OAuth2CodeGranterService['acknowledger']>['acknowledge']
26
+ >(),
27
+ },
28
+ authenticator: {
29
+ grantType: 'authorization_code',
30
+ authenticate:
31
+ jest.fn<
32
+ NonNullable<OAuth2CodeGranterService['authenticator']>['authenticate']
33
+ >(),
34
+ },
35
+ };
36
+ const tokenGranter = {
37
+ type: 'token',
38
+ authorizer: {
39
+ responseType: 'token',
40
+ authorize:
41
+ jest.fn<
42
+ NonNullable<
43
+ NonNullable<OAuth2TokenGranterService['authorizer']>['authorize']
44
+ >
45
+ >(),
46
+ },
47
+ acknowledger: {
48
+ acknowledgmentType: 'token',
49
+ acknowledge:
50
+ jest.fn<
51
+ NonNullable<OAuth2TokenGranterService['acknowledger']>['acknowledge']
52
+ >(),
53
+ },
54
+ };
55
+ const oAuth2Granters = [
56
+ codeGranter,
57
+ tokenGranter,
58
+ ] as unknown as OAuth2GranterService[];
59
+
60
+ beforeEach(() => {
61
+ log.mockReset();
62
+ [
63
+ codeGranter.authorizer.authorize,
64
+ codeGranter.acknowledger.acknowledge,
65
+ codeGranter.authenticator.authenticate,
66
+ tokenGranter.authorizer.authorize,
67
+ tokenGranter.acknowledger.acknowledge,
68
+ ].forEach((mock) => mock.mockReset());
69
+ });
70
+
71
+ test('should return OAuth2 metadata', async () => {
72
+ [
73
+ codeGranter.authorizer.authorize,
74
+ codeGranter.acknowledger.acknowledge,
75
+ codeGranter.authenticator.authenticate,
76
+ tokenGranter.authorizer.authorize,
77
+ tokenGranter.acknowledger.acknowledge,
78
+ ].forEach((mock: any) =>
79
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
80
+ );
81
+
82
+ const getOAuth2WellKnown = await initGetOAuth2WellKnown({
83
+ BASE_URL: 'https://server.example.com',
84
+ BASE_PATH: '/v0',
85
+ oAuth2Granters,
86
+ log,
87
+ });
88
+ const response = await getOAuth2WellKnown();
89
+
90
+ expect({
91
+ response,
92
+ logCalls: log.mock.calls.filter((args) => args[0].endsWith('stack')),
93
+ codeGranterAuthorizerAuthorizeCalls:
94
+ codeGranter.authorizer.authorize.mock.calls,
95
+ codeGranterAcknowledgerAcknowledgeCalls:
96
+ codeGranter.acknowledger.acknowledge.mock.calls,
97
+ codeGranterAuthenticatorAuthenticateCalls:
98
+ codeGranter.authenticator.authenticate.mock.calls,
99
+ tokenGranterAuthorizerAuthorizeCalls:
100
+ tokenGranter.authorizer.authorize.mock.calls,
101
+ tokenGranterAcknowledgerAcknowledgeCalls:
102
+ tokenGranter.acknowledger.acknowledge.mock.calls,
103
+ }).toMatchInlineSnapshot(`
104
+ {
105
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
106
+ "codeGranterAuthenticatorAuthenticateCalls": [],
107
+ "codeGranterAuthorizerAuthorizeCalls": [],
108
+ "logCalls": [],
109
+ "response": {
110
+ "body": {
111
+ "authorization_endpoint": "https://server.example.com/v0/oauth2/authorize",
112
+ "grant_types_supported": [
113
+ "authorization_code",
114
+ ],
115
+ "issuer": "https://server.example.com/v0",
116
+ "response_types_supported": [
117
+ "code",
118
+ "token",
119
+ ],
120
+ "token_endpoint": "https://server.example.com/v0/oauth2/token",
121
+ "token_endpoint_auth_methods_supported": [
122
+ "client_secret_basic",
123
+ ],
124
+ },
125
+ "status": 200,
126
+ },
127
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
128
+ "tokenGranterAuthorizerAuthorizeCalls": [],
129
+ }
130
+ `);
131
+ });
132
+ });
@@ -0,0 +1,335 @@
1
+ import {
2
+ identity,
3
+ refersTo,
4
+ type WhookAPISchemaDefinition,
5
+ type WhookRouteDefinition,
6
+ } from '@whook/whook';
7
+ import { autoService, location } from 'knifecycle';
8
+ import { type OAuth2GranterService } from '../services/oAuth2Granters.js';
9
+ import { codeChallengeMethodSchema } from './getOAuth2Authorize.js';
10
+ import { type LogService } from 'common-services';
11
+
12
+ // OAuth Token Endpoint Authentication Methods
13
+ export const endpointAuthenticationMethodsSchema = {
14
+ name: 'OAuthTokenEndpointAuthenticationMethods',
15
+ schema: {
16
+ type: 'string',
17
+ enum: [
18
+ 'none',
19
+ 'client_secret_post',
20
+ 'client_secret_basic',
21
+ 'client_secret_jwt',
22
+ 'private_key_jwt',
23
+ 'tls_client_auth',
24
+ 'self_signed_tls_client_auth',
25
+ ],
26
+ },
27
+ } as const satisfies WhookAPISchemaDefinition;
28
+
29
+ // JSON Web Algorithms (JWA) except "none"
30
+ // https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-algorithms
31
+ export const jsonWebAlgorithmsSchema = {
32
+ name: 'JSONWebAlgorithms',
33
+ schema: {
34
+ type: 'string',
35
+ enum: [
36
+ 'HS256',
37
+ 'HS384',
38
+ 'HS512',
39
+ 'RS256',
40
+ 'RS384',
41
+ 'RS512',
42
+ 'ES256',
43
+ 'ES384',
44
+ 'ES512',
45
+ 'PS256',
46
+ 'PS384',
47
+ 'PS512',
48
+ ],
49
+ },
50
+ } as const satisfies WhookAPISchemaDefinition;
51
+
52
+ // JSON Web Encryptions (JWE)
53
+ // https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-algorithms
54
+ export const jsonWebEncryptionsSchema = {
55
+ name: 'JSONWebEncryptions',
56
+ schema: {
57
+ type: 'string',
58
+ enum: [
59
+ 'A128CBC-HS256',
60
+ 'A192CBC-HS384',
61
+ 'A256CBC-HS512',
62
+ 'A128GCM',
63
+ 'A192GCM',
64
+ 'A256GCM',
65
+ ],
66
+ },
67
+ } as const satisfies WhookAPISchemaDefinition;
68
+
69
+ export const httpsProtocolURISchema = {
70
+ name: 'HTTPSProtocolURI',
71
+ schema: {
72
+ type: 'string',
73
+ format: 'uri',
74
+ pattern: '^https:',
75
+ },
76
+ } as const satisfies WhookAPISchemaDefinition;
77
+
78
+ export const oAuth2MetadataSchema = {
79
+ name: 'OAuth2MetadataSchema',
80
+ schema: {
81
+ type: 'object',
82
+ required: ['issuer', 'response_types_supported'],
83
+ properties: {
84
+ issuer: refersTo(httpsProtocolURISchema),
85
+ authorization_endpoint: refersTo(httpsProtocolURISchema),
86
+ token_endpoint: refersTo(httpsProtocolURISchema),
87
+ jwks_uri: refersTo(httpsProtocolURISchema),
88
+ registration_endpoint: refersTo(httpsProtocolURISchema),
89
+ scopes_supported: { type: 'array', items: { type: 'string' } },
90
+ response_types_supported: {
91
+ type: 'array',
92
+ items: {
93
+ type: 'string',
94
+ enum: [
95
+ 'code',
96
+ 'code id_token',
97
+ 'code id_token token',
98
+ 'code token',
99
+ 'id_token',
100
+ 'id_token token',
101
+ 'none',
102
+ 'token',
103
+ 'vp_token',
104
+ 'vp_token id_token',
105
+ ],
106
+ },
107
+ },
108
+ response_modes_supported: {
109
+ type: 'array',
110
+ items: {
111
+ type: 'string',
112
+ enum: ['query', 'fragment', 'form_post'],
113
+ },
114
+ },
115
+ grant_types_supported: {
116
+ type: 'array',
117
+ items: {
118
+ type: 'string',
119
+ enum: [
120
+ 'authorization_code',
121
+ 'implicit',
122
+ 'refresh_token',
123
+ 'client_credentials',
124
+ 'password',
125
+ 'urn:ietf:params:oauth:grant-type:jwt-bearer',
126
+ 'urn:ietf:params:oauth:grant-type:saml2-bearer',
127
+ ],
128
+ },
129
+ },
130
+ token_endpoint_auth_methods_supported: {
131
+ type: 'array',
132
+ items: refersTo(endpointAuthenticationMethodsSchema),
133
+ },
134
+ service_documentation: refersTo(httpsProtocolURISchema),
135
+ token_endpoint_auth_signing_alg_values_supported: {
136
+ type: 'array',
137
+ items: refersTo(jsonWebAlgorithmsSchema),
138
+ },
139
+ ui_locales_supported: { type: 'array', items: { type: 'string' } },
140
+ op_policy_uri: refersTo(httpsProtocolURISchema),
141
+ op_tos_uri: refersTo(httpsProtocolURISchema),
142
+ revocation_endpoint: refersTo(httpsProtocolURISchema),
143
+ revocation_endpoint_auth_methods_supported: {
144
+ type: 'array',
145
+ items: refersTo(endpointAuthenticationMethodsSchema),
146
+ },
147
+ revocation_endpoint_auth_signing_alg_values_supported: {
148
+ type: 'array',
149
+ items: refersTo(jsonWebAlgorithmsSchema),
150
+ },
151
+ introspection_endpoint: refersTo(httpsProtocolURISchema),
152
+ introspection_endpoint_auth_methods_supported: {
153
+ type: 'array',
154
+ items: refersTo(endpointAuthenticationMethodsSchema),
155
+ },
156
+ introspection_endpoint_auth_signing_alg_values_supported: {
157
+ type: 'array',
158
+ items: refersTo(jsonWebAlgorithmsSchema),
159
+ },
160
+ code_challenge_methods_supported: {
161
+ type: 'array',
162
+ items: refersTo(codeChallengeMethodSchema),
163
+ },
164
+ // OpenID specs: https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata
165
+ userinfo_endpoint: refersTo(httpsProtocolURISchema),
166
+ acr_values_supported: {
167
+ type: 'array',
168
+ items: {
169
+ type: 'string',
170
+ },
171
+ },
172
+ subject_types_supported: {
173
+ type: 'array',
174
+ items: {
175
+ type: 'string',
176
+ enum: ['pairwise', 'public'],
177
+ },
178
+ },
179
+ id_token_signing_alg_values_supported: {
180
+ type: 'array',
181
+ items: refersTo(jsonWebAlgorithmsSchema),
182
+ },
183
+ id_token_encryption_alg_values_supported: {
184
+ type: 'array',
185
+ items: refersTo(jsonWebAlgorithmsSchema),
186
+ },
187
+ id_token_encryption_enc_values_supported: {
188
+ type: 'array',
189
+ items: refersTo(jsonWebEncryptionsSchema),
190
+ },
191
+ userinfo_signing_alg_values_supported: {
192
+ type: 'array',
193
+ items: refersTo(jsonWebAlgorithmsSchema),
194
+ },
195
+ userinfo_encryption_alg_values_supported: {
196
+ type: 'array',
197
+ items: refersTo(jsonWebAlgorithmsSchema),
198
+ },
199
+ userinfo_encryption_enc_values_supported: {
200
+ type: 'array',
201
+ items: refersTo(jsonWebEncryptionsSchema),
202
+ },
203
+ request_object_signing_alg_values_supported: {
204
+ type: 'array',
205
+ items: refersTo(jsonWebAlgorithmsSchema),
206
+ },
207
+ request_object_encryption_alg_values_supported: {
208
+ type: 'array',
209
+ items: refersTo(jsonWebAlgorithmsSchema),
210
+ },
211
+ request_object_encryption_enc_values_supported: {
212
+ type: 'array',
213
+ items: refersTo(jsonWebEncryptionsSchema),
214
+ },
215
+ display_values_supported: {
216
+ type: 'array',
217
+ items: {
218
+ type: 'string',
219
+ },
220
+ },
221
+ claim_types_supported: {
222
+ type: 'array',
223
+ items: {
224
+ type: 'string',
225
+ enum: ['normal', 'aggregated', 'distributed'],
226
+ },
227
+ },
228
+ claims_supported: {
229
+ type: 'array',
230
+ items: {
231
+ type: 'string',
232
+ },
233
+ },
234
+ claims_locales_supported: {
235
+ type: 'array',
236
+ items: {
237
+ type: 'string',
238
+ },
239
+ },
240
+ claims_parameter_supported: {
241
+ type: 'boolean',
242
+ },
243
+ request_parameter_supported: {
244
+ type: 'boolean',
245
+ },
246
+ request_uri_parameter_supported: {
247
+ type: 'boolean',
248
+ },
249
+ require_request_uri_registration: {
250
+ type: 'boolean',
251
+ },
252
+ // https://openid.net/specs/openid-connect-session-1_0.html#OPMetadata
253
+ check_session_iframe: {
254
+ type: 'string',
255
+ format: 'uri',
256
+ },
257
+ },
258
+ },
259
+ } as const satisfies WhookAPISchemaDefinition;
260
+
261
+ export const definition = {
262
+ method: 'get',
263
+ path: '/.well-known/oauth-authorization-server',
264
+ operation: {
265
+ operationId: 'getOAuth2WellKnownMetadata',
266
+ summary: `Provide the [OAuth 2.0 Authorization Server Metadata](https://datatracker.ietf.org/doc/html/rfc8414).`,
267
+ tags: ['oauth2'],
268
+ parameters: [],
269
+ responses: {
270
+ '200': {
271
+ description: 'Well known OAuth2 specs found.',
272
+ content: {
273
+ 'application/json': {
274
+ schema: refersTo(oAuth2MetadataSchema),
275
+ },
276
+ },
277
+ },
278
+ },
279
+ },
280
+ } as const satisfies WhookRouteDefinition;
281
+
282
+ async function initGetOAuth2WellKnownMetadata({
283
+ BASE_URL,
284
+ BASE_PATH = '',
285
+ oAuth2Granters,
286
+ log,
287
+ }: {
288
+ BASE_URL: string;
289
+ BASE_PATH?: string;
290
+ oAuth2Granters: OAuth2GranterService[];
291
+ log: LogService;
292
+ }) {
293
+ if (BASE_PATH) {
294
+ log(
295
+ 'warning',
296
+ `⚠️ - Using a base path (${BASE_PATH}) breaks the well known URI discovery.`,
297
+ );
298
+ }
299
+ if (!BASE_URL.startsWith('https')) {
300
+ log('warning', `⚠️ - OAuth2 issuer must start with HTTPS (${BASE_URL}).`);
301
+ }
302
+
303
+ const body = {
304
+ issuer: `${BASE_URL}${BASE_PATH}`,
305
+ authorization_endpoint: `${BASE_URL}${BASE_PATH}/oauth2/authorize`,
306
+ token_endpoint: `${BASE_URL}${BASE_PATH}/oauth2/token`,
307
+ token_endpoint_auth_methods_supported: ['client_secret_basic'],
308
+ grant_types_supported: [
309
+ ...new Set(
310
+ oAuth2Granters
311
+ .map((granter) => granter.authenticator?.grantType)
312
+ .filter(identity),
313
+ ),
314
+ ],
315
+ response_types_supported: [
316
+ ...new Set(
317
+ oAuth2Granters
318
+ .map((granter) => granter.authorizer?.responseType)
319
+ .filter(identity),
320
+ ),
321
+ ],
322
+ };
323
+
324
+ return async () => {
325
+ return {
326
+ status: 200,
327
+ body,
328
+ };
329
+ };
330
+ }
331
+
332
+ export default location(
333
+ autoService(initGetOAuth2WellKnownMetadata),
334
+ import.meta.url,
335
+ );
@@ -90,10 +90,15 @@ describe('postOAuth2Acknowledge', () => {
90
90
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
91
91
  );
92
92
  codeGranter.acknowledger.acknowledge.mockResolvedValueOnce({
93
- applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
94
- redirectURI: 'http://lol',
95
- scope: 'user',
96
- userId: '1',
93
+ authenticationData: {
94
+ applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
95
+ scope: 'user',
96
+ userId: '1',
97
+ },
98
+ redirectURI: 'https://www.example.com?my_custom_parameter=a_custom_value',
99
+ acknowledgedData: {
100
+ code: 'a_valid_code',
101
+ },
97
102
  });
98
103
 
99
104
  const postOAuth2Acknowledge = await initPostOAuth2Acknowledge({
@@ -120,14 +125,14 @@ describe('postOAuth2Acknowledge', () => {
120
125
  expect({
121
126
  response,
122
127
  }).toMatchInlineSnapshot(`
123
- {
124
- "response": {
125
- "headers": {
126
- "location": "https://www.example.com/?client_id=abbacaca-abba-caca-abba-cacaabbacaca&scope=user&state=bancal&redirect_uri=http%3A%2F%2Flol&user_id=1",
127
- },
128
- "status": 302,
129
- },
130
- }
128
+ {
129
+ "response": {
130
+ "headers": {
131
+ "location": "https://www.example.com/?my_custom_parameter=a_custom_value&client_id=abbacaca-abba-caca-abba-cacaabbacaca&scope=user&state=bancal&code=a_valid_code",
132
+ },
133
+ "status": 302,
134
+ },
135
+ }
131
136
  `);
132
137
  expect({
133
138
  logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
@@ -180,15 +185,15 @@ describe('postOAuth2Acknowledge', () => {
180
185
  expect({
181
186
  response,
182
187
  }).toMatchInlineSnapshot(`
183
- {
184
- "response": {
185
- "headers": {
186
- "location": "https://www.example.com/?error=unsupported_response_type&error_description=Type+%22yolo%22+not+supported.",
187
- },
188
- "status": 302,
189
- },
190
- }
191
- `);
188
+ {
189
+ "response": {
190
+ "headers": {
191
+ "location": "https://www.example.com/?error=unsupported_response_type&error_description=Type+%22yolo%22+not+supported.",
192
+ },
193
+ "status": 302,
194
+ },
195
+ }
196
+ `);
192
197
  expect({
193
198
  logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
194
199
  checkApplicationCalls: checkApplication.mock.calls,