@whook/oauth2 26.0.0 → 26.0.3

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 (85) hide show
  1. package/dist/index.d.ts +6 -2
  2. package/dist/index.js +5 -2
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.test.js +421 -2
  5. package/dist/index.test.js.map +1 -1
  6. package/dist/libs/authorizationRequests.d.ts +6 -0
  7. package/dist/libs/authorizationRequests.js +22 -0
  8. package/dist/libs/authorizationRequests.js.map +1 -0
  9. package/dist/libs/authorizationRequests.test.d.ts +1 -0
  10. package/dist/libs/authorizationRequests.test.js +24 -0
  11. package/dist/libs/authorizationRequests.test.js.map +1 -0
  12. package/dist/libs/errors.d.ts +31 -1
  13. package/dist/libs/errors.js +49 -0
  14. package/dist/libs/errors.js.map +1 -1
  15. package/dist/libs/redirectURI.d.ts +1 -0
  16. package/dist/libs/redirectURI.js +16 -0
  17. package/dist/libs/redirectURI.js.map +1 -1
  18. package/dist/libs/schemas.d.ts +8 -0
  19. package/dist/libs/schemas.js +8 -0
  20. package/dist/libs/schemas.js.map +1 -1
  21. package/dist/libs/verifier.d.ts +3 -0
  22. package/dist/libs/verifier.js +14 -0
  23. package/dist/libs/verifier.js.map +1 -1
  24. package/dist/routes/getOAuth2Authorize.d.ts +30 -14
  25. package/dist/routes/getOAuth2Authorize.js +92 -30
  26. package/dist/routes/getOAuth2Authorize.js.map +1 -1
  27. package/dist/routes/getOAuth2Authorize.test.js +826 -19
  28. package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
  29. package/dist/routes/getOAuth2WellKnownMetadata.d.ts +10 -3
  30. package/dist/routes/getOAuth2WellKnownMetadata.js +30 -5
  31. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  32. package/dist/routes/getOAuth2WellKnownMetadata.test.js +20 -18
  33. package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
  34. package/dist/routes/postOAuth2PushedAuthorizationRequest.d.ts +137 -0
  35. package/dist/routes/postOAuth2PushedAuthorizationRequest.js +187 -0
  36. package/dist/routes/postOAuth2PushedAuthorizationRequest.js.map +1 -0
  37. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.d.ts +1 -0
  38. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js +554 -0
  39. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js.map +1 -0
  40. package/dist/routes/postOAuth2Token.d.ts +12 -0
  41. package/dist/routes/postOAuth2Token.js +12 -0
  42. package/dist/routes/postOAuth2Token.js.map +1 -1
  43. package/dist/services/authCookies.js +8 -4
  44. package/dist/services/authCookies.js.map +1 -1
  45. package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +2 -7
  46. package/dist/services/oAuth2AuthorizationCodeGranter.js +5 -22
  47. package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -1
  48. package/dist/services/oAuth2AuthorizationCodeGranter.test.js +12 -141
  49. package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -1
  50. package/dist/services/oAuth2AuthorizationRequests.d.ts +41 -0
  51. package/dist/services/oAuth2AuthorizationRequests.js +61 -0
  52. package/dist/services/oAuth2AuthorizationRequests.js.map +1 -0
  53. package/dist/services/oAuth2AuthorizationRequests.test.d.ts +1 -0
  54. package/dist/services/oAuth2AuthorizationRequests.test.js +249 -0
  55. package/dist/services/oAuth2AuthorizationRequests.test.js.map +1 -0
  56. package/dist/services/oAuth2Granters.d.ts +10 -12
  57. package/dist/services/oAuth2Granters.js.map +1 -1
  58. package/dist/services/oAuth2ImplicitGranter.js +3 -14
  59. package/dist/services/oAuth2ImplicitGranter.js.map +1 -1
  60. package/dist/services/oAuth2ImplicitGranter.test.js +25 -163
  61. package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -1
  62. package/package.json +7 -6
  63. package/src/index.test.ts +459 -3
  64. package/src/index.ts +17 -0
  65. package/src/libs/authorizationRequests.test.ts +40 -0
  66. package/src/libs/authorizationRequests.ts +42 -0
  67. package/src/libs/errors.ts +87 -1
  68. package/src/libs/redirectURI.ts +22 -0
  69. package/src/libs/schemas.ts +10 -0
  70. package/src/libs/verifier.ts +18 -0
  71. package/src/routes/getOAuth2Authorize.test.ts +940 -23
  72. package/src/routes/getOAuth2Authorize.ts +168 -49
  73. package/src/routes/getOAuth2WellKnownMetadata.test.ts +22 -20
  74. package/src/routes/getOAuth2WellKnownMetadata.ts +44 -8
  75. package/src/routes/postOAuth2PushedAuthorizationRequest.test.ts +681 -0
  76. package/src/routes/postOAuth2PushedAuthorizationRequest.ts +292 -0
  77. package/src/routes/postOAuth2Token.ts +12 -0
  78. package/src/services/authCookies.ts +8 -4
  79. package/src/services/oAuth2AuthorizationCodeGranter.test.ts +12 -169
  80. package/src/services/oAuth2AuthorizationCodeGranter.ts +8 -31
  81. package/src/services/oAuth2AuthorizationRequests.test.ts +282 -0
  82. package/src/services/oAuth2AuthorizationRequests.ts +141 -0
  83. package/src/services/oAuth2Granters.ts +12 -14
  84. package/src/services/oAuth2ImplicitGranter.test.ts +29 -190
  85. package/src/services/oAuth2ImplicitGranter.ts +3 -19
@@ -10,6 +10,7 @@ import {
10
10
  type WhookOAuth2Options,
11
11
  type WhookOAuth2GranterService,
12
12
  type WhookOAuth2GranterDefinitions,
13
+ type WhookOAuth2ReadClientGrantsService,
13
14
  } from '../services/oAuth2Granters.js';
14
15
  import { type LogService } from 'common-services';
15
16
  import {
@@ -18,10 +19,10 @@ import {
18
19
  stringifyScopes,
19
20
  } from '../libs/scopes.js';
20
21
  import {
22
+ checkCodeChallengeParameters,
21
23
  type CodeChallengeMethod,
22
24
  PLAIN_CODE_CHALLENGE_METHOD,
23
25
  } from '../libs/verifier.js';
24
- import { scopeSchema } from '../libs/schemas.js';
25
26
  import {
26
27
  AUTHORIZATION_CODE_RESPONSE_TYPE,
27
28
  codeChallengeMethodSchema,
@@ -30,10 +31,22 @@ import {
30
31
  codeChallengeParameter,
31
32
  } from '../services/oAuth2AuthorizationCodeGranter.js';
32
33
  import { IMPLICIT_RESPONSE_TYPE } from '../services/oAuth2ImplicitGranter.js';
33
- import { camelCaseObjectProperties } from '../libs/utils.js';
34
- import { addParamsToURL, buildParamsFromError } from '../libs/redirectURI.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';
35
47
 
36
48
  export {
49
+ requestURISchema,
37
50
  scopeSchema,
38
51
  codeChallengeMethodSchema,
39
52
  codeChallengeMethodParameter,
@@ -41,6 +54,16 @@ export {
41
54
  codeChallengeParameter,
42
55
  };
43
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
+ }
66
+
44
67
  /* Architecture Note #1: OAuth2 authorize
45
68
 
46
69
  This endpoint simply redirects the user to the authentication
@@ -54,7 +77,7 @@ export const responseTypeParameter = {
54
77
  parameter: {
55
78
  in: 'query',
56
79
  name: 'response_type',
57
- required: true,
80
+ required: false,
58
81
  schema: {
59
82
  type: 'string',
60
83
  enum: [AUTHORIZATION_CODE_RESPONSE_TYPE, IMPLICIT_RESPONSE_TYPE],
@@ -105,6 +128,15 @@ export const stateParameter = {
105
128
  },
106
129
  },
107
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;
108
140
 
109
141
  export const definition = {
110
142
  method: 'get',
@@ -120,6 +152,7 @@ export const definition = {
120
152
  refersTo(redirectURIParameter),
121
153
  refersTo(scopeParameter),
122
154
  refersTo(stateParameter),
155
+ refersTo(requestURIParameter),
123
156
  refersTo(codeChallengeParameter),
124
157
  refersTo(codeChallengeMethodParameter),
125
158
  ],
@@ -133,42 +166,130 @@ export const definition = {
133
166
 
134
167
  async function initGetOAuth2Authorize({
135
168
  OAUTH2,
169
+ OAUTH2_PAR = DEFAULT_OAUTH2_PAR,
136
170
  ERRORS_DESCRIPTORS,
137
171
  oAuth2Granters,
172
+ oAuth2AuthorizationRequests = undefined,
173
+ readClientGrants,
138
174
  log,
139
- }: {
175
+ }: WhookOAuth2AuthorizationRequestsConfig & {
140
176
  OAUTH2: WhookOAuth2Options;
141
177
  ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
142
178
  oAuth2Granters: WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
179
+ readClientGrants: WhookOAuth2ReadClientGrantsService;
180
+ oAuth2AuthorizationRequests?: Pick<
181
+ WhookOAuth2AuthorizationRequestsService,
182
+ 'check'
183
+ >;
143
184
  log: LogService;
144
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
+
145
191
  return async ({
146
- query: {
147
- response_type: responseType,
148
- client_id: givenClientId,
149
- redirect_uri: demandedRedirectURI = '',
150
- scope: demandedScope = '',
151
- state,
152
- code_challenge: codeChallenge,
153
- code_challenge_method: codeChallengeMethod,
154
- ...authorizeParameters
155
- },
192
+ query: { client_id: givenClientId, request_uri: requestURI, ...query },
156
193
  }: {
157
194
  query: {
158
- response_type: string;
159
195
  client_id: string;
160
- redirect_uri?: string;
161
- scope?: string;
162
- state: string;
163
- code_challenge?: string;
164
- code_challenge_method?: CodeChallengeMethod;
165
- } & Record<string, string>;
196
+ request_uri?: string;
197
+ } & Partial<Omit<WhookOAuth2AuthorizeRequestParameters, 'client_id'>>;
166
198
  }) => {
199
+ let usableRedirectURI: string | undefined = undefined;
200
+ let finalQuery: WhookOAuth2AuthorizeRequestParameters | undefined =
201
+ undefined;
202
+
167
203
  // If everything goes well we proxy the request
168
204
  // to the authentication server for acknowledgment
169
205
  let url = new URL(OAUTH2.authenticateURL);
170
206
 
171
207
  try {
208
+ const clientGrants = await readClientGrants(givenClientId);
209
+
210
+ if (givenClientId !== clientGrants.authenticationData.clientId) {
211
+ throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
212
+ givenClientId,
213
+ clientGrants.authenticationData.clientId,
214
+ ]);
215
+ }
216
+
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
+ );
242
+ }
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
+
172
293
  const granter = oAuth2Granters.find(
173
294
  (granter) => granter.responseType === responseType,
174
295
  );
@@ -177,36 +298,33 @@ async function initGetOAuth2Authorize({
177
298
  throw new YError('E_OAUTH2_UNKNOWN_RESPONSE_TYPE', [responseType]);
178
299
  }
179
300
 
180
- if (responseType === 'code') {
181
- if (!codeChallenge) {
182
- if (OAUTH2.forcePKCE) {
183
- throw new YError('E_OAUTH2_PKCE_REQUIRED', [responseType]);
184
- }
185
- }
186
- } else if (codeChallenge) {
187
- throw new YError('E_OAUTH2_PKCE_NOT_SUPPORTED', [responseType]);
188
- }
301
+ checkCodeChallengeParameters(
302
+ responseType,
303
+ codeChallenge,
304
+ OAUTH2.forcePKCE,
305
+ );
189
306
 
190
307
  const demandedScopes = filterScopes(
191
- parseOAuth2Scope(demandedScope),
192
- OAUTH2.allowedScopes,
308
+ filterScopes(
309
+ parseOAuth2Scope(demandedScope),
310
+ OAUTH2.allowedScopes,
311
+ !!OAUTH2.strictScopesChecks,
312
+ ),
313
+ clientGrants.allowedScopes,
193
314
  !!OAUTH2.strictScopesChecks,
194
315
  );
195
316
 
196
- const { clientId, redirectURI, scopes } = await granter.authorize(
197
- {
198
- clientId: givenClientId,
199
- demandedRedirectURI,
200
- demandedScopes,
201
- },
202
- camelCaseObjectProperties(authorizeParameters),
203
- );
317
+ const { scopes } = await granter.authorize({
318
+ clientId: givenClientId,
319
+ clientGrants,
320
+ demandedScopes,
321
+ });
204
322
 
205
323
  const paramsHash: Record<string, string> = {
206
324
  type: responseType,
207
- redirect_uri: redirectURI,
325
+ redirect_uri: usableRedirectURI,
208
326
  scope: stringifyScopes(scopes),
209
- client_id: clientId,
327
+ client_id: givenClientId,
210
328
  };
211
329
 
212
330
  if (responseType === 'code' && codeChallenge) {
@@ -225,13 +343,14 @@ async function initGetOAuth2Authorize({
225
343
  log('error-stack', printStackTrace(err));
226
344
 
227
345
  // If errors happen we try to directly redirect
228
- // to the demanded redirect URI
346
+ // to the demanded redirect URI when valid
229
347
  try {
230
- url = new URL(demandedRedirectURI);
348
+ if (usableRedirectURI) {
349
+ url = new URL(usableRedirectURI);
350
+ }
231
351
  } catch (err) {
232
352
  log('debug', `💥 - Could not redirect to demanded uri.`);
233
353
  log('debug-stack', printStackTrace(err));
234
- url = new URL(OAUTH2.authenticateURL);
235
354
  }
236
355
 
237
356
  const paramsHash = buildParamsFromError(
@@ -240,14 +359,14 @@ async function initGetOAuth2Authorize({
240
359
  ERRORS_DESCRIPTORS.E_OAUTH2_UNEXPECTED_ERROR,
241
360
  );
242
361
 
243
- if (state) {
244
- paramsHash.state = state;
362
+ if (finalQuery && finalQuery.state) {
363
+ paramsHash.state = finalQuery.state;
245
364
  }
246
365
 
247
366
  addParamsToURL(
248
367
  url,
249
368
  paramsHash,
250
- responseType === 'token' ? 'fragment' : 'query',
369
+ finalQuery?.response_type === 'token' ? 'fragment' : 'query',
251
370
  );
252
371
  }
253
372
 
@@ -9,7 +9,7 @@ import {
9
9
  type WhookOAuth2GranterDefinitions,
10
10
  type WhookOAuth2GranterService,
11
11
  } from '../index.js';
12
- import { type WhookRoutesDefinitionsService } from '@whook/whook';
12
+ import { type WhookDefinitions } from '@whook/whook';
13
13
 
14
14
  describe('getOAuth2WellKnown', () => {
15
15
  const log = jest.fn<LogService>();
@@ -68,29 +68,29 @@ describe('getOAuth2WellKnown', () => {
68
68
 
69
69
  const getOAuth2WellKnown = await initGetOAuth2WellKnown({
70
70
  BASE_URL: 'https://server.example.com',
71
- ROUTES_DEFINITIONS: {
72
- getOAuth2Authorize: {
73
- module: {
74
- definition: {
75
- path: '/v0/oauth2/authorize',
76
- },
71
+ OAUTH2_PAR: {
72
+ mode: 'required',
73
+ },
74
+ DEFINITIONS: {
75
+ configs: {
76
+ getOAuth2Authorize: {
77
+ type: 'route',
78
+ path: '/v0/oauth2/authorize',
77
79
  },
78
- },
79
- postOAuth2Token: {
80
- module: {
81
- definition: {
82
- path: '/v0/oauth2/token',
83
- },
80
+ postOAuth2Token: {
81
+ type: 'route',
82
+ path: '/v0/oauth2/token',
84
83
  },
85
- },
86
- postOAuth2Revoke: {
87
- module: {
88
- definition: {
89
- path: '/v0/oauth2/revoke',
90
- },
84
+ postOAuth2Revoke: {
85
+ type: 'route',
86
+ path: '/v0/oauth2/revoke',
87
+ },
88
+ postOAuth2PushedAuthorizationRequest: {
89
+ type: 'route',
90
+ path: '/v0/oauth2/par',
91
91
  },
92
92
  },
93
- } as unknown as WhookRoutesDefinitionsService,
93
+ } as unknown as WhookDefinitions,
94
94
  API: {
95
95
  openapi: '3.2',
96
96
  info: {
@@ -139,6 +139,8 @@ describe('getOAuth2WellKnown', () => {
139
139
  "implicit",
140
140
  ],
141
141
  "issuer": "https://server.example.com",
142
+ "pushed_authorization_request_endpoint": "https://server.example.com/v0/oauth2/par",
143
+ "require_pushed_authorization_requests": true,
142
144
  "response_types_supported": [
143
145
  "code",
144
146
  "token",
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  identity,
3
3
  refersTo,
4
- type WhookRoutesDefinitionsService,
4
+ type WhookDefinitions,
5
5
  type WhookAPISchemaDefinition,
6
6
  type WhookRouteDefinition,
7
7
  } from '@whook/whook';
@@ -15,6 +15,11 @@ import { scopeTokenSchema, scopeTokensSchema } from '../libs/schemas.js';
15
15
  import { type LogService } from 'common-services';
16
16
  import { type OpenAPI } from 'ya-open-api-types';
17
17
  import { collectScopesFromAPI } from '../libs/scopes.js';
18
+ import {
19
+ DEFAULT_OAUTH2_PAR,
20
+ type WhookOAuth2AuthorizationRequestsConfig,
21
+ } from '../services/oAuth2AuthorizationRequests.js';
22
+ import { YError } from 'yerror';
18
23
 
19
24
  export { scopeTokenSchema, scopeTokensSchema, codeChallengeMethodSchema };
20
25
 
@@ -93,6 +98,7 @@ export const oAuth2MetadataSchema = {
93
98
  issuer: refersTo(httpsProtocolURISchema),
94
99
  authorization_endpoint: refersTo(httpsProtocolURISchema),
95
100
  token_endpoint: refersTo(httpsProtocolURISchema),
101
+ pushed_authorization_request_endpoint: refersTo(httpsProtocolURISchema),
96
102
  jwks_uri: refersTo(httpsProtocolURISchema),
97
103
  registration_endpoint: refersTo(httpsProtocolURISchema),
98
104
  scopes_supported: refersTo(scopeTokensSchema),
@@ -255,6 +261,9 @@ export const oAuth2MetadataSchema = {
255
261
  request_uri_parameter_supported: {
256
262
  type: 'boolean',
257
263
  },
264
+ require_pushed_authorization_requests: {
265
+ type: 'boolean',
266
+ },
258
267
  require_request_uri_registration: {
259
268
  type: 'boolean',
260
269
  },
@@ -294,13 +303,14 @@ export const definition = {
294
303
  async function initGetOAuth2WellKnownMetadata({
295
304
  API,
296
305
  BASE_URL,
297
- ROUTES_DEFINITIONS,
306
+ DEFINITIONS,
307
+ OAUTH2_PAR = DEFAULT_OAUTH2_PAR,
298
308
  oAuth2Granters,
299
309
  log,
300
- }: {
310
+ }: WhookOAuth2AuthorizationRequestsConfig & {
301
311
  API: OpenAPI;
302
312
  BASE_URL: string;
303
- ROUTES_DEFINITIONS: WhookRoutesDefinitionsService;
313
+ DEFINITIONS: WhookDefinitions;
304
314
  oAuth2Granters: WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
305
315
  log: LogService;
306
316
  }) {
@@ -308,10 +318,26 @@ async function initGetOAuth2WellKnownMetadata({
308
318
  log('warning', `⚠️ - OAuth2 issuer must start with HTTPS (${BASE_URL}).`);
309
319
  }
310
320
 
321
+ if (
322
+ OAUTH2_PAR.mode !== 'disabled' &&
323
+ !DEFINITIONS.configs['postOAuth2PushedAuthorizationRequest']
324
+ ) {
325
+ log('error', `💥 - OAuth2 PAR endpoint required to enable PAR.`);
326
+ throw new YError('E_OAUTH2_MISCONFIGURED');
327
+ }
328
+
311
329
  const body = {
312
330
  issuer: `${BASE_URL}`,
313
- authorization_endpoint: `${BASE_URL}${ROUTES_DEFINITIONS['getOAuth2Authorize']?.module.definition.path || '/oauth2/authorize'}`,
314
- token_endpoint: `${BASE_URL}${ROUTES_DEFINITIONS['postOAuth2Token']?.module.definition.path || '/oauth2/token'}`,
331
+ authorization_endpoint: `${BASE_URL}${
332
+ DEFINITIONS.configs['getOAuth2Authorize']?.type === 'route'
333
+ ? DEFINITIONS.configs['getOAuth2Authorize'].path
334
+ : '/oauth2/authorize'
335
+ }`,
336
+ token_endpoint: `${BASE_URL}${
337
+ DEFINITIONS.configs['postOAuth2Token']?.type === 'route'
338
+ ? DEFINITIONS.configs['postOAuth2Token'].path
339
+ : '/oauth2/token'
340
+ }`,
315
341
  // TODO: Put it behind options (allow none)
316
342
  token_endpoint_auth_methods_supported: [
317
343
  'client_secret_basic',
@@ -328,15 +354,25 @@ async function initGetOAuth2WellKnownMetadata({
328
354
  ),
329
355
  ],
330
356
  scopes_supported: collectScopesFromAPI(API),
331
- ...(ROUTES_DEFINITIONS['postOAuth2Revoke']?.module.definition.path
357
+ ...(DEFINITIONS.configs['postOAuth2Revoke']?.type === 'route' &&
358
+ DEFINITIONS.configs['postOAuth2Revoke'].path
332
359
  ? {
333
- revocation_endpoint: `${BASE_URL}${ROUTES_DEFINITIONS['postOAuth2Revoke'].module.definition.path}`,
360
+ revocation_endpoint: `${BASE_URL}${DEFINITIONS.configs['postOAuth2Revoke'].path}`,
334
361
  revocation_endpoint_auth_methods_supported: [
335
362
  'client_secret_basic',
336
363
  'client_secret_post',
337
364
  ],
338
365
  }
339
366
  : {}),
367
+ ...(DEFINITIONS.configs['postOAuth2PushedAuthorizationRequest']?.type ===
368
+ 'route' &&
369
+ DEFINITIONS.configs['postOAuth2PushedAuthorizationRequest'].path &&
370
+ OAUTH2_PAR.mode !== 'disabled'
371
+ ? {
372
+ pushed_authorization_request_endpoint: `${BASE_URL}${DEFINITIONS.configs['postOAuth2PushedAuthorizationRequest'].path}`,
373
+ require_pushed_authorization_requests: OAUTH2_PAR.mode === 'required',
374
+ }
375
+ : {}),
340
376
  };
341
377
 
342
378
  return async () => {