@whook/oauth2 26.0.0 → 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.
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
@@ -2,7 +2,10 @@
2
2
  import { describe, test, beforeEach, jest, expect } from '@jest/globals';
3
3
  import initGetOAuth2Authorize from './getOAuth2Authorize.js';
4
4
  import { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';
5
- import { type WhookOAuth2GranterDefinitions } from '../services/oAuth2Granters.js';
5
+ import {
6
+ type WhookOAuth2ReadClientGrantsService,
7
+ type WhookOAuth2GranterDefinitions,
8
+ } from '../services/oAuth2Granters.js';
6
9
  import { YError } from 'yerror';
7
10
  import { type WhookOAuth2AuthorizationCodeGranterService } from '../services/oAuth2AuthorizationCodeGranter.js';
8
11
  import { type WhookOAuth2ImplicitGranterService } from '../services/oAuth2ImplicitGranter.js';
@@ -12,6 +15,7 @@ import {
12
15
  type WhookOAuth2GranterService,
13
16
  } from '../index.js';
14
17
  import { OAUTH2_ERRORS_DESCRIPTORS } from '../libs/errors.js';
18
+ import { type WhookOAuth2AuthorizationRequestsService } from '../services/oAuth2AuthorizationRequests.js';
15
19
 
16
20
  describe('getOAuth2Authorize', () => {
17
21
  const OAUTH2: WhookOAuth2Options = {
@@ -24,6 +28,9 @@ describe('getOAuth2Authorize', () => {
24
28
  ...OAUTH2_ERRORS_DESCRIPTORS,
25
29
  };
26
30
  const log = jest.fn<LogService>();
31
+ const oAuth2AuthorizationRequests = {
32
+ check: jest.fn<WhookOAuth2AuthorizationRequestsService['check']>(),
33
+ };
27
34
  const codeGranter = {
28
35
  grantType: 'authorization_code',
29
36
  responseType: 'code',
@@ -52,19 +59,22 @@ describe('getOAuth2Authorize', () => {
52
59
  acknowledge:
53
60
  jest.fn<NonNullable<WhookOAuth2ImplicitGranterService['acknowledge']>>(),
54
61
  } satisfies WhookOAuth2ImplicitGranterService;
62
+ const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
55
63
  const oAuth2Granters = [
56
64
  codeGranter,
57
65
  tokenGranter,
58
66
  ] as unknown as WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
59
67
 
60
68
  beforeEach(() => {
61
- log.mockReset();
62
69
  [
70
+ log,
63
71
  codeGranter.authorize,
64
72
  codeGranter.acknowledge,
65
73
  codeGranter.authenticate,
66
74
  tokenGranter.authorize,
67
75
  tokenGranter.acknowledge,
76
+ readClientGrants,
77
+ oAuth2AuthorizationRequests.check,
68
78
  ].forEach((mock) => mock.mockReset());
69
79
  });
70
80
 
@@ -74,12 +84,22 @@ describe('getOAuth2Authorize', () => {
74
84
  codeGranter.authenticate,
75
85
  tokenGranter.authorize,
76
86
  tokenGranter.acknowledge,
77
- ].forEach((mock: any) =>
87
+ oAuth2AuthorizationRequests.check,
88
+ ].forEach((mock) =>
78
89
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
79
90
  );
91
+ readClientGrants.mockResolvedValue({
92
+ allowedScopes: ['user', 'admin'],
93
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
94
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
95
+ authenticationData: {
96
+ clientId: 'the_client_app_id',
97
+ scopes: ['user', 'admin'],
98
+ userId: 'user_id',
99
+ },
100
+ isPublicClient: false,
101
+ });
80
102
  codeGranter.authorize.mockResolvedValueOnce({
81
- clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
82
- redirectURI: 'http://lol',
83
103
  scopes: ['user'],
84
104
  });
85
105
 
@@ -87,16 +107,141 @@ describe('getOAuth2Authorize', () => {
87
107
  OAUTH2,
88
108
  ERRORS_DESCRIPTORS,
89
109
  oAuth2Granters,
110
+ readClientGrants,
111
+ oAuth2AuthorizationRequests,
90
112
  log,
91
113
  });
92
114
  const response = await getOAuth2Authorize({
93
115
  query: {
94
116
  response_type: 'code',
95
- client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
96
- redirect_uri: 'https://www.example.com',
117
+ client_id: 'the_client_app_id',
118
+ redirect_uri: 'https://www.example.com/oauth2/cb',
119
+ scope: 'user',
120
+ state: 'bancal',
121
+ },
122
+ });
123
+
124
+ expect({
125
+ response,
126
+ logCalls: log.mock.calls.filter((args) => args[0].endsWith('stack')),
127
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
128
+ codeGranterAcknowledgerAcknowledgeCalls:
129
+ codeGranter.acknowledge.mock.calls,
130
+ codeGranterAuthenticatorAuthenticateCalls:
131
+ codeGranter.authenticate.mock.calls,
132
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
133
+ tokenGranterAcknowledgerAcknowledgeCalls:
134
+ tokenGranter.acknowledge.mock.calls,
135
+ readClientGrantsCalls: readClientGrants.mock.calls,
136
+ oAuth2AuthorizationRequestsCheckCalls:
137
+ oAuth2AuthorizationRequests.check.mock.calls,
138
+ }).toMatchInlineSnapshot(`
139
+ {
140
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
141
+ "codeGranterAuthenticatorAuthenticateCalls": [],
142
+ "codeGranterAuthorizerAuthorizeCalls": [
143
+ [
144
+ {
145
+ "clientGrants": {
146
+ "allowedGrantTypes": [
147
+ "authorization_code",
148
+ "refresh_token",
149
+ ],
150
+ "allowedRedirectURIS": [
151
+ "https://www.example.com/oauth2/cb",
152
+ ],
153
+ "allowedScopes": [
154
+ "user",
155
+ "admin",
156
+ ],
157
+ "authenticationData": {
158
+ "clientId": "the_client_app_id",
159
+ "scopes": [
160
+ "user",
161
+ "admin",
162
+ ],
163
+ "userId": "user_id",
164
+ },
165
+ "isPublicClient": false,
166
+ },
167
+ "clientId": "the_client_app_id",
168
+ "demandedScopes": [
169
+ "user",
170
+ ],
171
+ },
172
+ ],
173
+ ],
174
+ "logCalls": [],
175
+ "oAuth2AuthorizationRequestsCheckCalls": [],
176
+ "readClientGrantsCalls": [
177
+ [
178
+ "the_client_app_id",
179
+ ],
180
+ ],
181
+ "response": {
182
+ "headers": {
183
+ "location": "https://auth.example.com/sign_in?type=code&redirect_uri=https%3A%2F%2Fwww.example.com%2Foauth2%2Fcb&scope=user&client_id=the_client_app_id&state=bancal",
184
+ },
185
+ "status": 302,
186
+ },
187
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
188
+ "tokenGranterAuthorizerAuthorizeCalls": [],
189
+ }
190
+ `);
191
+ });
192
+
193
+ test('should redirect with a pushed authorization request URI', async () => {
194
+ [
195
+ codeGranter.acknowledge,
196
+ codeGranter.authenticate,
197
+ tokenGranter.authorize,
198
+ tokenGranter.acknowledge,
199
+ ].forEach((mock: any) =>
200
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
201
+ );
202
+ readClientGrants.mockResolvedValue({
203
+ allowedScopes: ['user', 'admin'],
204
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
205
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
206
+ authenticationData: {
207
+ clientId: 'the_client_app_id',
208
+ scopes: ['user', 'admin'],
209
+ userId: 'user_id',
210
+ },
211
+ isPublicClient: false,
212
+ });
213
+ codeGranter.authorize.mockResolvedValueOnce({
214
+ scopes: ['user'],
215
+ });
216
+
217
+ oAuth2AuthorizationRequests.check.mockResolvedValueOnce({
218
+ clientId: 'the_client_app_id',
219
+ parameters: {
220
+ response_type: 'code',
221
+ client_id: 'the_client_app_id',
222
+ redirect_uri: 'https://www.example.com/oauth2/cb',
97
223
  scope: 'user',
98
224
  state: 'bancal',
99
225
  },
226
+ expiresAt: Date.parse('1983-04-28T00:00:00Z'),
227
+ });
228
+
229
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
230
+ OAUTH2,
231
+ OAUTH2_PAR: {
232
+ mode: 'enabled',
233
+ },
234
+ ERRORS_DESCRIPTORS,
235
+ oAuth2Granters,
236
+ readClientGrants,
237
+ oAuth2AuthorizationRequests,
238
+ log,
239
+ });
240
+ const response = await getOAuth2Authorize({
241
+ query: {
242
+ client_id: 'the_client_app_id',
243
+ request_uri: 'urn:ietf:params:oauth:request_uri:a_request_ui',
244
+ },
100
245
  });
101
246
 
102
247
  expect({
@@ -110,6 +255,9 @@ describe('getOAuth2Authorize', () => {
110
255
  tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
111
256
  tokenGranterAcknowledgerAcknowledgeCalls:
112
257
  tokenGranter.acknowledge.mock.calls,
258
+ readClientGrantsCalls: readClientGrants.mock.calls,
259
+ oAuth2AuthorizationRequestsCheckCalls:
260
+ oAuth2AuthorizationRequests.check.mock.calls,
113
261
  }).toMatchInlineSnapshot(`
114
262
  {
115
263
  "codeGranterAcknowledgerAcknowledgeCalls": [],
@@ -117,19 +265,231 @@ describe('getOAuth2Authorize', () => {
117
265
  "codeGranterAuthorizerAuthorizeCalls": [
118
266
  [
119
267
  {
120
- "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
121
- "demandedRedirectURI": "https://www.example.com",
268
+ "clientGrants": {
269
+ "allowedGrantTypes": [
270
+ "authorization_code",
271
+ "refresh_token",
272
+ ],
273
+ "allowedRedirectURIS": [
274
+ "https://www.example.com/oauth2/cb",
275
+ ],
276
+ "allowedScopes": [
277
+ "user",
278
+ "admin",
279
+ ],
280
+ "authenticationData": {
281
+ "clientId": "the_client_app_id",
282
+ "scopes": [
283
+ "user",
284
+ "admin",
285
+ ],
286
+ "userId": "user_id",
287
+ },
288
+ "isPublicClient": false,
289
+ },
290
+ "clientId": "the_client_app_id",
122
291
  "demandedScopes": [
123
292
  "user",
124
293
  ],
125
294
  },
126
- {},
127
295
  ],
128
296
  ],
129
297
  "logCalls": [],
298
+ "oAuth2AuthorizationRequestsCheckCalls": [
299
+ [
300
+ "the_client_app_id",
301
+ "urn:ietf:params:oauth:request_uri:a_request_ui",
302
+ ],
303
+ ],
304
+ "readClientGrantsCalls": [
305
+ [
306
+ "the_client_app_id",
307
+ ],
308
+ ],
309
+ "response": {
310
+ "headers": {
311
+ "location": "https://auth.example.com/sign_in?type=code&redirect_uri=https%3A%2F%2Fwww.example.com%2Foauth2%2Fcb&scope=user&client_id=the_client_app_id&state=bancal",
312
+ },
313
+ "status": 302,
314
+ },
315
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
316
+ "tokenGranterAuthorizerAuthorizeCalls": [],
317
+ }
318
+ `);
319
+ });
320
+
321
+ test('should reject unknown pushed authorization request URIs', async () => {
322
+ [
323
+ codeGranter.acknowledge,
324
+ codeGranter.authenticate,
325
+ tokenGranter.authorize,
326
+ tokenGranter.acknowledge,
327
+ ].forEach((mock: any) =>
328
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
329
+ );
330
+ readClientGrants.mockResolvedValue({
331
+ allowedScopes: ['user', 'admin'],
332
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
333
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
334
+ authenticationData: {
335
+ clientId: 'the_client_app_id',
336
+ scopes: ['user', 'admin'],
337
+ userId: 'user_id',
338
+ },
339
+ isPublicClient: false,
340
+ });
341
+ codeGranter.authorize.mockResolvedValueOnce({
342
+ scopes: ['user'],
343
+ });
344
+
345
+ oAuth2AuthorizationRequests.check.mockResolvedValueOnce(undefined);
346
+
347
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
348
+ OAUTH2,
349
+ OAUTH2_PAR: {
350
+ mode: 'enabled',
351
+ },
352
+ ERRORS_DESCRIPTORS,
353
+ oAuth2Granters,
354
+ readClientGrants,
355
+ oAuth2AuthorizationRequests,
356
+ log,
357
+ });
358
+
359
+ const response = await getOAuth2Authorize({
360
+ query: {
361
+ client_id: 'the_client_app_id',
362
+ request_uri: 'urn:ietf:params:oauth:request_uri:a_request_ui',
363
+ },
364
+ });
365
+
366
+ expect({
367
+ response,
368
+ logCalls: log.mock.calls.filter((args) => !args[0].endsWith('stack')),
369
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
370
+ codeGranterAcknowledgerAcknowledgeCalls:
371
+ codeGranter.acknowledge.mock.calls,
372
+ codeGranterAuthenticatorAuthenticateCalls:
373
+ codeGranter.authenticate.mock.calls,
374
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
375
+ tokenGranterAcknowledgerAcknowledgeCalls:
376
+ tokenGranter.acknowledge.mock.calls,
377
+ readClientGrantsCalls: readClientGrants.mock.calls,
378
+ oAuth2AuthorizationRequestsCheckCalls:
379
+ oAuth2AuthorizationRequests.check.mock.calls,
380
+ }).toMatchInlineSnapshot(`
381
+ {
382
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
383
+ "codeGranterAuthenticatorAuthenticateCalls": [],
384
+ "codeGranterAuthorizerAuthorizeCalls": [],
385
+ "logCalls": [
386
+ [
387
+ "debug",
388
+ "👫 - OAuth2 authorize error.",
389
+ ],
390
+ ],
391
+ "oAuth2AuthorizationRequestsCheckCalls": [
392
+ [
393
+ "the_client_app_id",
394
+ "urn:ietf:params:oauth:request_uri:a_request_ui",
395
+ ],
396
+ ],
397
+ "readClientGrantsCalls": [
398
+ [
399
+ "the_client_app_id",
400
+ ],
401
+ ],
402
+ "response": {
403
+ "headers": {
404
+ "location": "https://www.example.com/oauth2/cb?error=invalid_request_uri&error_description=The+request+URI+is+invalid%2C+expired+or+unknown+%28urn%3Aietf%3Aparams%3Aoauth%3Arequest_uri%3Aa_request_ui%29.",
405
+ },
406
+ "status": 302,
407
+ },
408
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
409
+ "tokenGranterAuthorizerAuthorizeCalls": [],
410
+ }
411
+ `);
412
+ });
413
+
414
+ test('should reject pushed authorization request when disabled', async () => {
415
+ [
416
+ codeGranter.acknowledge,
417
+ codeGranter.authenticate,
418
+ tokenGranter.authorize,
419
+ tokenGranter.acknowledge,
420
+ ].forEach((mock: any) =>
421
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
422
+ );
423
+ readClientGrants.mockResolvedValue({
424
+ allowedScopes: ['user', 'admin'],
425
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
426
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
427
+ authenticationData: {
428
+ clientId: 'the_client_app_id',
429
+ scopes: ['user', 'admin'],
430
+ userId: 'user_id',
431
+ },
432
+ isPublicClient: false,
433
+ });
434
+ codeGranter.authorize.mockResolvedValueOnce({
435
+ scopes: ['user'],
436
+ });
437
+
438
+ oAuth2AuthorizationRequests.check.mockResolvedValueOnce(undefined);
439
+
440
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
441
+ OAUTH2,
442
+ OAUTH2_PAR: {
443
+ mode: 'disabled',
444
+ },
445
+ ERRORS_DESCRIPTORS,
446
+ oAuth2Granters,
447
+ readClientGrants,
448
+ oAuth2AuthorizationRequests,
449
+ log,
450
+ });
451
+
452
+ const response = await getOAuth2Authorize({
453
+ query: {
454
+ client_id: 'the_client_app_id',
455
+ request_uri: 'urn:ietf:params:oauth:request_uri:a_request_ui',
456
+ },
457
+ });
458
+
459
+ expect({
460
+ response,
461
+ logCalls: log.mock.calls.filter((args) => !args[0].endsWith('stack')),
462
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
463
+ codeGranterAcknowledgerAcknowledgeCalls:
464
+ codeGranter.acknowledge.mock.calls,
465
+ codeGranterAuthenticatorAuthenticateCalls:
466
+ codeGranter.authenticate.mock.calls,
467
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
468
+ tokenGranterAcknowledgerAcknowledgeCalls:
469
+ tokenGranter.acknowledge.mock.calls,
470
+ readClientGrantsCalls: readClientGrants.mock.calls,
471
+ oAuth2AuthorizationRequestsCheckCalls:
472
+ oAuth2AuthorizationRequests.check.mock.calls,
473
+ }).toMatchInlineSnapshot(`
474
+ {
475
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
476
+ "codeGranterAuthenticatorAuthenticateCalls": [],
477
+ "codeGranterAuthorizerAuthorizeCalls": [],
478
+ "logCalls": [
479
+ [
480
+ "debug",
481
+ "👫 - OAuth2 authorize error.",
482
+ ],
483
+ ],
484
+ "oAuth2AuthorizationRequestsCheckCalls": [],
485
+ "readClientGrantsCalls": [
486
+ [
487
+ "the_client_app_id",
488
+ ],
489
+ ],
130
490
  "response": {
131
491
  "headers": {
132
- "location": "https://auth.example.com/sign_in?type=code&redirect_uri=http%3A%2F%2Flol&scope=user&client_id=abbacaca-abba-caca-abba-cacaabbacaca&state=bancal",
492
+ "location": "https://www.example.com/oauth2/cb?error=invalid_request&error_description=Pushed+authorization+requests+are+not+enabled+for+this+server.",
133
493
  },
134
494
  "status": 302,
135
495
  },
@@ -139,7 +499,7 @@ describe('getOAuth2Authorize', () => {
139
499
  `);
140
500
  });
141
501
 
142
- test('should redirect with an error when some', async () => {
502
+ test('should redirect with an error for bad response type', async () => {
143
503
  [
144
504
  codeGranter.authorize,
145
505
  codeGranter.acknowledge,
@@ -154,13 +514,28 @@ describe('getOAuth2Authorize', () => {
154
514
  OAUTH2,
155
515
  ERRORS_DESCRIPTORS,
156
516
  oAuth2Granters,
517
+ readClientGrants,
518
+ oAuth2AuthorizationRequests,
157
519
  log,
158
520
  });
521
+
522
+ readClientGrants.mockResolvedValue({
523
+ allowedScopes: ['user', 'admin'],
524
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
525
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
526
+ authenticationData: {
527
+ clientId: 'the_client_app_id',
528
+ scopes: ['user', 'admin'],
529
+ userId: 'user_id',
530
+ },
531
+ isPublicClient: false,
532
+ });
533
+
159
534
  const response = await getOAuth2Authorize({
160
535
  query: {
161
536
  response_type: 'yolo',
162
- client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
163
- redirect_uri: 'https://www.example.com',
537
+ client_id: 'the_client_app_id',
538
+ redirect_uri: 'https://www.example.com/oauth2/cb',
164
539
  scope: 'user',
165
540
  state: 'bancal',
166
541
  },
@@ -177,6 +552,9 @@ describe('getOAuth2Authorize', () => {
177
552
  tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
178
553
  tokenGranterAcknowledgerAcknowledgeCalls:
179
554
  tokenGranter.acknowledge.mock.calls,
555
+ readClientGrantsCalls: readClientGrants.mock.calls,
556
+ oAuth2AuthorizationRequestsCheckCalls:
557
+ oAuth2AuthorizationRequests.check.mock.calls,
180
558
  }).toMatchInlineSnapshot(`
181
559
  {
182
560
  "codeGranterAcknowledgerAcknowledgeCalls": [],
@@ -188,9 +566,15 @@ describe('getOAuth2Authorize', () => {
188
566
  "👫 - OAuth2 authorize error.",
189
567
  ],
190
568
  ],
569
+ "oAuth2AuthorizationRequestsCheckCalls": [],
570
+ "readClientGrantsCalls": [
571
+ [
572
+ "the_client_app_id",
573
+ ],
574
+ ],
191
575
  "response": {
192
576
  "headers": {
193
- "location": "https://www.example.com/?error=unsupported_response_type&error_description=The+response+type+%22yolo%22+is+not+supported.&state=bancal",
577
+ "location": "https://www.example.com/oauth2/cb?error=unsupported_response_type&error_description=The+response+type+%22yolo%22+is+not+supported.&state=bancal",
194
578
  },
195
579
  "status": 302,
196
580
  },
@@ -200,7 +584,7 @@ describe('getOAuth2Authorize', () => {
200
584
  `);
201
585
  });
202
586
 
203
- test('should redirect with an error with bad scopes', async () => {
587
+ test('should redirect with an error with bad redirect uri', async () => {
204
588
  [
205
589
  codeGranter.authorize,
206
590
  codeGranter.acknowledge,
@@ -212,20 +596,32 @@ describe('getOAuth2Authorize', () => {
212
596
  );
213
597
 
214
598
  const getOAuth2Authorize = await initGetOAuth2Authorize({
215
- OAUTH2: {
216
- ...OAUTH2,
217
- strictScopesChecks: true,
218
- },
599
+ OAUTH2,
219
600
  ERRORS_DESCRIPTORS,
220
601
  oAuth2Granters,
602
+ readClientGrants,
603
+ oAuth2AuthorizationRequests,
221
604
  log,
222
605
  });
606
+
607
+ readClientGrants.mockResolvedValue({
608
+ allowedScopes: ['user', 'admin'],
609
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
610
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
611
+ authenticationData: {
612
+ clientId: 'the_client_app_id',
613
+ scopes: ['user', 'admin'],
614
+ userId: 'user_id',
615
+ },
616
+ isPublicClient: false,
617
+ });
618
+
223
619
  const response = await getOAuth2Authorize({
224
620
  query: {
225
621
  response_type: 'code',
226
- client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
622
+ client_id: 'the_client_app_id',
227
623
  redirect_uri: 'https://www.example.com',
228
- scope: 'god',
624
+ scope: 'user',
229
625
  state: 'bancal',
230
626
  },
231
627
  });
@@ -241,6 +637,9 @@ describe('getOAuth2Authorize', () => {
241
637
  tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
242
638
  tokenGranterAcknowledgerAcknowledgeCalls:
243
639
  tokenGranter.acknowledge.mock.calls,
640
+ readClientGrantsCalls: readClientGrants.mock.calls,
641
+ oAuth2AuthorizationRequestsCheckCalls:
642
+ oAuth2AuthorizationRequests.check.mock.calls,
244
643
  }).toMatchInlineSnapshot(`
245
644
  {
246
645
  "codeGranterAcknowledgerAcknowledgeCalls": [],
@@ -252,9 +651,527 @@ describe('getOAuth2Authorize', () => {
252
651
  "👫 - OAuth2 authorize error.",
253
652
  ],
254
653
  ],
654
+ "oAuth2AuthorizationRequestsCheckCalls": [],
655
+ "readClientGrantsCalls": [
656
+ [
657
+ "the_client_app_id",
658
+ ],
659
+ ],
660
+ "response": {
661
+ "headers": {
662
+ "location": "https://www.example.com/oauth2/cb?error=invalid_request&error_description=The+client+does+not+accept+that+redirect+URI.&state=bancal",
663
+ },
664
+ "status": 302,
665
+ },
666
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
667
+ "tokenGranterAuthorizerAuthorizeCalls": [],
668
+ }
669
+ `);
670
+ });
671
+
672
+ test('should redirect with an error with bad scope and strict scopes', async () => {
673
+ [
674
+ codeGranter.authorize,
675
+ codeGranter.acknowledge,
676
+ codeGranter.authenticate,
677
+ tokenGranter.acknowledge,
678
+ ].forEach((mock: any) =>
679
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
680
+ );
681
+
682
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
683
+ OAUTH2: {
684
+ ...OAUTH2,
685
+ strictScopesChecks: true,
686
+ },
687
+ ERRORS_DESCRIPTORS,
688
+ oAuth2Granters,
689
+ readClientGrants,
690
+ oAuth2AuthorizationRequests,
691
+ log,
692
+ });
693
+
694
+ tokenGranter.authorize.mockResolvedValueOnce({ scopes: ['user'] });
695
+ readClientGrants.mockResolvedValue({
696
+ allowedScopes: ['user'],
697
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
698
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
699
+ authenticationData: {
700
+ clientId: 'the_client_app_id',
701
+ scopes: ['user'],
702
+ userId: 'user_id',
703
+ },
704
+ isPublicClient: false,
705
+ });
706
+
707
+ const response = await getOAuth2Authorize({
708
+ query: {
709
+ response_type: 'token',
710
+ client_id: 'the_client_app_id',
711
+ redirect_uri: 'https://www.example.com/oauth2/cb',
712
+ scope: 'admin',
713
+ state: 'bancal',
714
+ },
715
+ });
716
+
717
+ expect({
718
+ response,
719
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
720
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
721
+ codeGranterAcknowledgerAcknowledgeCalls:
722
+ codeGranter.acknowledge.mock.calls,
723
+ codeGranterAuthenticatorAuthenticateCalls:
724
+ codeGranter.authenticate.mock.calls,
725
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
726
+ tokenGranterAcknowledgerAcknowledgeCalls:
727
+ tokenGranter.acknowledge.mock.calls,
728
+ readClientGrantsCalls: readClientGrants.mock.calls,
729
+ oAuth2AuthorizationRequestsCheckCalls:
730
+ oAuth2AuthorizationRequests.check.mock.calls,
731
+ }).toMatchInlineSnapshot(`
732
+ {
733
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
734
+ "codeGranterAuthenticatorAuthenticateCalls": [],
735
+ "codeGranterAuthorizerAuthorizeCalls": [],
736
+ "logCalls": [
737
+ [
738
+ "debug",
739
+ "👫 - OAuth2 authorize error.",
740
+ ],
741
+ ],
742
+ "oAuth2AuthorizationRequestsCheckCalls": [],
743
+ "readClientGrantsCalls": [
744
+ [
745
+ "the_client_app_id",
746
+ ],
747
+ ],
748
+ "response": {
749
+ "headers": {
750
+ "location": "https://www.example.com/oauth2/cb#error=invalid_scope&error_description=This+scope+is+not+supported+%28admin%29.&state=bancal",
751
+ },
752
+ "status": 302,
753
+ },
754
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
755
+ "tokenGranterAuthorizerAuthorizeCalls": [],
756
+ }
757
+ `);
758
+ });
759
+
760
+ test('should redirect with an error with bad client', async () => {
761
+ [
762
+ codeGranter.authorize,
763
+ codeGranter.acknowledge,
764
+ codeGranter.authenticate,
765
+ tokenGranter.authorize,
766
+ tokenGranter.acknowledge,
767
+ ].forEach((mock: any) =>
768
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
769
+ );
770
+
771
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
772
+ OAUTH2,
773
+ ERRORS_DESCRIPTORS,
774
+ oAuth2Granters,
775
+ readClientGrants,
776
+ oAuth2AuthorizationRequests,
777
+ log,
778
+ });
779
+
780
+ readClientGrants.mockRejectedValueOnce(
781
+ new YError('E_OAUTH2_CLIENT_NOT_FOUND', ['the_client_app_id']),
782
+ );
783
+
784
+ const response = await getOAuth2Authorize({
785
+ query: {
786
+ response_type: 'yolo',
787
+ client_id: 'the_client_app_id',
788
+ redirect_uri: 'https://www.example.com/oauth2/cb',
789
+ scope: 'user',
790
+ state: 'bancal',
791
+ },
792
+ });
793
+
794
+ expect({
795
+ response,
796
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
797
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
798
+ codeGranterAcknowledgerAcknowledgeCalls:
799
+ codeGranter.acknowledge.mock.calls,
800
+ codeGranterAuthenticatorAuthenticateCalls:
801
+ codeGranter.authenticate.mock.calls,
802
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
803
+ tokenGranterAcknowledgerAcknowledgeCalls:
804
+ tokenGranter.acknowledge.mock.calls,
805
+ readClientGrantsCalls: readClientGrants.mock.calls,
806
+ oAuth2AuthorizationRequestsCheckCalls:
807
+ oAuth2AuthorizationRequests.check.mock.calls,
808
+ }).toMatchInlineSnapshot(`
809
+ {
810
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
811
+ "codeGranterAuthenticatorAuthenticateCalls": [],
812
+ "codeGranterAuthorizerAuthorizeCalls": [],
813
+ "logCalls": [
814
+ [
815
+ "debug",
816
+ "👫 - OAuth2 authorize error.",
817
+ ],
818
+ ],
819
+ "oAuth2AuthorizationRequestsCheckCalls": [],
820
+ "readClientGrantsCalls": [
821
+ [
822
+ "the_client_app_id",
823
+ ],
824
+ ],
825
+ "response": {
826
+ "headers": {
827
+ "location": "https://auth.example.com/sign_in?error=invalid_client&error_description=The+client+provided+does+not+exist+%28the_client_app_id%29.",
828
+ },
829
+ "status": 302,
830
+ },
831
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
832
+ "tokenGranterAuthorizerAuthorizeCalls": [],
833
+ }
834
+ `);
835
+ });
836
+
837
+ test('should redirect with an error with bad request uri', async () => {
838
+ [
839
+ codeGranter.authorize,
840
+ codeGranter.acknowledge,
841
+ codeGranter.authenticate,
842
+ tokenGranter.authorize,
843
+ tokenGranter.acknowledge,
844
+ ].forEach((mock: any) =>
845
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
846
+ );
847
+
848
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
849
+ OAUTH2,
850
+ OAUTH2_PAR: {
851
+ mode: 'enabled',
852
+ },
853
+ ERRORS_DESCRIPTORS,
854
+ oAuth2Granters,
855
+ readClientGrants,
856
+ oAuth2AuthorizationRequests,
857
+ log,
858
+ });
859
+
860
+ readClientGrants.mockResolvedValue({
861
+ allowedScopes: ['user', 'admin'],
862
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
863
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
864
+ authenticationData: {
865
+ clientId: 'the_client_app_id',
866
+ scopes: ['user', 'admin'],
867
+ userId: 'user_id',
868
+ },
869
+ isPublicClient: false,
870
+ });
871
+
872
+ const response = await getOAuth2Authorize({
873
+ query: {
874
+ client_id: 'the_client_app_id',
875
+ request_uri: 'not_a_request_uri',
876
+ },
877
+ });
878
+
879
+ expect({
880
+ response,
881
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
882
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
883
+ codeGranterAcknowledgerAcknowledgeCalls:
884
+ codeGranter.acknowledge.mock.calls,
885
+ codeGranterAuthenticatorAuthenticateCalls:
886
+ codeGranter.authenticate.mock.calls,
887
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
888
+ tokenGranterAcknowledgerAcknowledgeCalls:
889
+ tokenGranter.acknowledge.mock.calls,
890
+ readClientGrantsCalls: readClientGrants.mock.calls,
891
+ oAuth2AuthorizationRequestsCheckCalls:
892
+ oAuth2AuthorizationRequests.check.mock.calls,
893
+ }).toMatchInlineSnapshot(`
894
+ {
895
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
896
+ "codeGranterAuthenticatorAuthenticateCalls": [],
897
+ "codeGranterAuthorizerAuthorizeCalls": [],
898
+ "logCalls": [
899
+ [
900
+ "debug",
901
+ "👫 - OAuth2 authorize error.",
902
+ ],
903
+ ],
904
+ "oAuth2AuthorizationRequestsCheckCalls": [],
905
+ "readClientGrantsCalls": [
906
+ [
907
+ "the_client_app_id",
908
+ ],
909
+ ],
910
+ "response": {
911
+ "headers": {
912
+ "location": "https://www.example.com/oauth2/cb?error=invalid_request_uri&error_description=The+request+URI+is+invalid%2C+expired+or+unknown+%28not_a_request_uri%29.",
913
+ },
914
+ "status": 302,
915
+ },
916
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
917
+ "tokenGranterAuthorizerAuthorizeCalls": [],
918
+ }
919
+ `);
920
+ });
921
+
922
+ test('should redirect with an error with bad request', async () => {
923
+ [
924
+ codeGranter.authorize,
925
+ codeGranter.acknowledge,
926
+ codeGranter.authenticate,
927
+ tokenGranter.authorize,
928
+ tokenGranter.acknowledge,
929
+ ].forEach((mock: any) =>
930
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
931
+ );
932
+
933
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
934
+ OAUTH2,
935
+ OAUTH2_PAR: {
936
+ mode: 'enabled',
937
+ },
938
+ ERRORS_DESCRIPTORS,
939
+ oAuth2Granters,
940
+ readClientGrants,
941
+ oAuth2AuthorizationRequests,
942
+ log,
943
+ });
944
+
945
+ readClientGrants.mockResolvedValue({
946
+ allowedScopes: ['user', 'admin'],
947
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
948
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
949
+ authenticationData: {
950
+ clientId: 'the_client_app_id',
951
+ scopes: ['user', 'admin'],
952
+ userId: 'user_id',
953
+ },
954
+ isPublicClient: false,
955
+ });
956
+
957
+ const response = await getOAuth2Authorize({
958
+ query: {
959
+ client_id: 'the_client_app_id',
960
+ request_uri: 'urn:ietf:params:oauth:request_uri:a_request_ui',
961
+ scope: 'user',
962
+ redirect_uri: 'https://www.example.com/oauth2/cb',
963
+ },
964
+ });
965
+
966
+ expect({
967
+ response,
968
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
969
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
970
+ codeGranterAcknowledgerAcknowledgeCalls:
971
+ codeGranter.acknowledge.mock.calls,
972
+ codeGranterAuthenticatorAuthenticateCalls:
973
+ codeGranter.authenticate.mock.calls,
974
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
975
+ tokenGranterAcknowledgerAcknowledgeCalls:
976
+ tokenGranter.acknowledge.mock.calls,
977
+ readClientGrantsCalls: readClientGrants.mock.calls,
978
+ oAuth2AuthorizationRequestsCheckCalls:
979
+ oAuth2AuthorizationRequests.check.mock.calls,
980
+ }).toMatchInlineSnapshot(`
981
+ {
982
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
983
+ "codeGranterAuthenticatorAuthenticateCalls": [],
984
+ "codeGranterAuthorizerAuthorizeCalls": [],
985
+ "logCalls": [
986
+ [
987
+ "debug",
988
+ "👫 - OAuth2 authorize error.",
989
+ ],
990
+ ],
991
+ "oAuth2AuthorizationRequestsCheckCalls": [],
992
+ "readClientGrantsCalls": [
993
+ [
994
+ "the_client_app_id",
995
+ ],
996
+ ],
997
+ "response": {
998
+ "headers": {
999
+ "location": "https://www.example.com/oauth2/cb?error=invalid_request&error_description=The+request+URI+should+not+have+additional+parameters+%28scope%29.",
1000
+ },
1001
+ "status": 302,
1002
+ },
1003
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
1004
+ "tokenGranterAuthorizerAuthorizeCalls": [],
1005
+ }
1006
+ `);
1007
+ });
1008
+
1009
+ test('should redirect with an error with bad scopes', async () => {
1010
+ [
1011
+ codeGranter.authorize,
1012
+ codeGranter.acknowledge,
1013
+ codeGranter.authenticate,
1014
+ tokenGranter.authorize,
1015
+ tokenGranter.acknowledge,
1016
+ ].forEach((mock: any) =>
1017
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
1018
+ );
1019
+
1020
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
1021
+ OAUTH2: {
1022
+ ...OAUTH2,
1023
+ strictScopesChecks: true,
1024
+ },
1025
+ ERRORS_DESCRIPTORS,
1026
+ oAuth2Granters,
1027
+ readClientGrants,
1028
+ oAuth2AuthorizationRequests,
1029
+ log,
1030
+ });
1031
+
1032
+ readClientGrants.mockResolvedValue({
1033
+ allowedScopes: ['user', 'admin'],
1034
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
1035
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
1036
+ authenticationData: {
1037
+ clientId: 'the_client_app_id',
1038
+ scopes: ['user', 'admin'],
1039
+ userId: 'user_id',
1040
+ },
1041
+ isPublicClient: false,
1042
+ });
1043
+
1044
+ const response = await getOAuth2Authorize({
1045
+ query: {
1046
+ response_type: 'code',
1047
+ client_id: 'the_client_app_id',
1048
+ redirect_uri: 'https://www.example.com/oauth2/cb',
1049
+ scope: 'god',
1050
+ state: 'bancal',
1051
+ },
1052
+ });
1053
+
1054
+ expect({
1055
+ response,
1056
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
1057
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
1058
+ codeGranterAcknowledgerAcknowledgeCalls:
1059
+ codeGranter.acknowledge.mock.calls,
1060
+ codeGranterAuthenticatorAuthenticateCalls:
1061
+ codeGranter.authenticate.mock.calls,
1062
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
1063
+ tokenGranterAcknowledgerAcknowledgeCalls:
1064
+ tokenGranter.acknowledge.mock.calls,
1065
+ readClientGrantsCalls: readClientGrants.mock.calls,
1066
+ oAuth2AuthorizationRequestsCheckCalls:
1067
+ oAuth2AuthorizationRequests.check.mock.calls,
1068
+ }).toMatchInlineSnapshot(`
1069
+ {
1070
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
1071
+ "codeGranterAuthenticatorAuthenticateCalls": [],
1072
+ "codeGranterAuthorizerAuthorizeCalls": [],
1073
+ "logCalls": [
1074
+ [
1075
+ "debug",
1076
+ "👫 - OAuth2 authorize error.",
1077
+ ],
1078
+ ],
1079
+ "oAuth2AuthorizationRequestsCheckCalls": [],
1080
+ "readClientGrantsCalls": [
1081
+ [
1082
+ "the_client_app_id",
1083
+ ],
1084
+ ],
1085
+ "response": {
1086
+ "headers": {
1087
+ "location": "https://www.example.com/oauth2/cb?error=invalid_scope&error_description=This+scope+is+not+supported+%28god%29.&state=bancal",
1088
+ },
1089
+ "status": 302,
1090
+ },
1091
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
1092
+ "tokenGranterAuthorizerAuthorizeCalls": [],
1093
+ }
1094
+ `);
1095
+ });
1096
+ test('should redirect with an error with no PAR when required', async () => {
1097
+ [
1098
+ codeGranter.authorize,
1099
+ codeGranter.acknowledge,
1100
+ codeGranter.authenticate,
1101
+ tokenGranter.authorize,
1102
+ tokenGranter.acknowledge,
1103
+ ].forEach((mock: any) =>
1104
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
1105
+ );
1106
+
1107
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
1108
+ OAUTH2,
1109
+ OAUTH2_PAR: {
1110
+ mode: 'required',
1111
+ },
1112
+ ERRORS_DESCRIPTORS,
1113
+ oAuth2Granters,
1114
+ readClientGrants,
1115
+ oAuth2AuthorizationRequests,
1116
+ log,
1117
+ });
1118
+
1119
+ readClientGrants.mockResolvedValue({
1120
+ allowedScopes: ['user', 'admin'],
1121
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
1122
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
1123
+ authenticationData: {
1124
+ clientId: 'the_client_app_id',
1125
+ scopes: ['user', 'admin'],
1126
+ userId: 'user_id',
1127
+ },
1128
+ isPublicClient: false,
1129
+ });
1130
+
1131
+ const response = await getOAuth2Authorize({
1132
+ query: {
1133
+ response_type: 'code',
1134
+ client_id: 'the_client_app_id',
1135
+ redirect_uri: 'https://www.example.com/oauth2/cb',
1136
+ scope: 'god',
1137
+ state: 'bancal',
1138
+ },
1139
+ });
1140
+
1141
+ expect({
1142
+ response,
1143
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
1144
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
1145
+ codeGranterAcknowledgerAcknowledgeCalls:
1146
+ codeGranter.acknowledge.mock.calls,
1147
+ codeGranterAuthenticatorAuthenticateCalls:
1148
+ codeGranter.authenticate.mock.calls,
1149
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
1150
+ tokenGranterAcknowledgerAcknowledgeCalls:
1151
+ tokenGranter.acknowledge.mock.calls,
1152
+ readClientGrantsCalls: readClientGrants.mock.calls,
1153
+ oAuth2AuthorizationRequestsCheckCalls:
1154
+ oAuth2AuthorizationRequests.check.mock.calls,
1155
+ }).toMatchInlineSnapshot(`
1156
+ {
1157
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
1158
+ "codeGranterAuthenticatorAuthenticateCalls": [],
1159
+ "codeGranterAuthorizerAuthorizeCalls": [],
1160
+ "logCalls": [
1161
+ [
1162
+ "debug",
1163
+ "👫 - OAuth2 authorize error.",
1164
+ ],
1165
+ ],
1166
+ "oAuth2AuthorizationRequestsCheckCalls": [],
1167
+ "readClientGrantsCalls": [
1168
+ [
1169
+ "the_client_app_id",
1170
+ ],
1171
+ ],
255
1172
  "response": {
256
1173
  "headers": {
257
- "location": "https://www.example.com/?error=invalid_scope&error_description=This+scope+is+not+supported+%28god%29.&state=bancal",
1174
+ "location": "https://www.example.com/oauth2/cb?error=invalid_request&error_description=Pushed+authorization+requests+are+required+for+this+server.",
258
1175
  },
259
1176
  "status": 302,
260
1177
  },