@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
@@ -0,0 +1,681 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { describe, test, beforeEach, jest, expect } from '@jest/globals';
3
+ import initPostOAuth2PushedAuthorizationRequest from './postOAuth2PushedAuthorizationRequest.js';
4
+ import { YError } from 'yerror';
5
+ import {
6
+ type WhookOAuth2GranterDefinitions,
7
+ type WhookOAuth2Options,
8
+ type WhookOAuth2GranterService,
9
+ type WhookOAuth2ReadClientGrantsService,
10
+ } from '../services/oAuth2Granters.js';
11
+ import { type LogService } from 'common-services';
12
+ import { type WhookOAuth2AuthorizationCodeGranterService } from '../services/oAuth2AuthorizationCodeGranter.js';
13
+ import { type WhookOAuth2ImplicitGranterService } from '../services/oAuth2ImplicitGranter.js';
14
+ import {
15
+ WhookOAuth2AuthorizationRequestsOptions,
16
+ WhookOAuth2AuthorizationRequestsService,
17
+ } from '../services/oAuth2AuthorizationRequests.js';
18
+
19
+ describe('postOAuth2PushedAuthorizationRequest', () => {
20
+ const OAUTH2_PAR: WhookOAuth2AuthorizationRequestsOptions = {
21
+ ttl: 76000,
22
+ mode: 'enabled',
23
+ };
24
+ const OAUTH2: WhookOAuth2Options = {
25
+ rootClientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
26
+ authenticateURL: 'https://auth.example.com/sign_in',
27
+ allowedScopes: ['user'],
28
+ };
29
+ const log = jest.fn<LogService>();
30
+ const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
31
+ const oAuth2AuthorizationRequests = {
32
+ create: jest.fn<WhookOAuth2AuthorizationRequestsService['create']>(),
33
+ };
34
+ const codeGranter = {
35
+ grantType: 'authorization_code',
36
+ responseType: 'code',
37
+ issuesRefreshToken: true,
38
+ authorize:
39
+ jest.fn<
40
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['authorize']>
41
+ >(),
42
+ acknowledge:
43
+ jest.fn<
44
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['acknowledge']>
45
+ >(),
46
+ authenticate:
47
+ jest.fn<
48
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['authenticate']>
49
+ >(),
50
+ } satisfies WhookOAuth2AuthorizationCodeGranterService;
51
+ const tokenGranter = {
52
+ grantType: 'implicit',
53
+ responseType: 'token',
54
+ issuesRefreshToken: false,
55
+ authorize:
56
+ jest.fn<
57
+ NonNullable<NonNullable<WhookOAuth2ImplicitGranterService['authorize']>>
58
+ >(),
59
+ acknowledge:
60
+ jest.fn<NonNullable<WhookOAuth2ImplicitGranterService['acknowledge']>>(),
61
+ } satisfies WhookOAuth2ImplicitGranterService;
62
+ const oAuth2Granters = [
63
+ codeGranter,
64
+ tokenGranter,
65
+ ] as unknown as WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
66
+
67
+ beforeEach(() => {
68
+ log.mockReset();
69
+ [
70
+ codeGranter.authorize,
71
+ codeGranter.acknowledge,
72
+ codeGranter.authenticate,
73
+ tokenGranter.authorize,
74
+ tokenGranter.acknowledge,
75
+ readClientGrants,
76
+ oAuth2AuthorizationRequests.create,
77
+ ].forEach((mock) => mock.mockReset());
78
+ });
79
+
80
+ test('should issue a request URI', async () => {
81
+ [
82
+ codeGranter.acknowledge,
83
+ codeGranter.authenticate,
84
+ tokenGranter.authorize,
85
+ tokenGranter.acknowledge,
86
+ ].forEach((mock: any) =>
87
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
88
+ );
89
+ codeGranter.authorize.mockResolvedValueOnce({
90
+ scopes: ['user'],
91
+ });
92
+
93
+ const postOAuth2PushedAuthorizationRequest =
94
+ await initPostOAuth2PushedAuthorizationRequest({
95
+ OAUTH2,
96
+ OAUTH2_PAR,
97
+ oAuth2Granters,
98
+ readClientGrants,
99
+ oAuth2AuthorizationRequests,
100
+ log,
101
+ });
102
+
103
+ readClientGrants.mockResolvedValueOnce({
104
+ allowedGrantTypes: ['code'],
105
+ allowedScopes: ['user'],
106
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
107
+ isPublicClient: true,
108
+ canAcknowledge: false,
109
+ authenticationData: {
110
+ clientId: 'a_client_id',
111
+ scopes: [],
112
+ userId: '1',
113
+ },
114
+ });
115
+ oAuth2AuthorizationRequests.create.mockResolvedValueOnce({
116
+ requestURI: 'urn:ietf:params:oauth:request_uri:a_request_uri',
117
+ expiresIn: 1000,
118
+ });
119
+
120
+ const response = await postOAuth2PushedAuthorizationRequest({
121
+ body: {
122
+ response_type: 'code',
123
+ client_id: 'a_client_id',
124
+ redirect_uri: 'https://example.com/oauth/cb',
125
+ scope: 'user',
126
+ state: 'bancal',
127
+ },
128
+ });
129
+
130
+ expect({
131
+ response,
132
+ logCalls: log.mock.calls,
133
+ readClientGrantsCalls: readClientGrants.mock.calls,
134
+ oAuth2AuthorizationRequestsCreateCalls:
135
+ oAuth2AuthorizationRequests.create.mock.calls,
136
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
137
+ codeGranterAcknowledgerAcknowledgeCalls:
138
+ codeGranter.acknowledge.mock.calls,
139
+ codeGranterAuthenticatorAuthenticateCalls:
140
+ codeGranter.authenticate.mock.calls,
141
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
142
+ tokenGranterAcknowledgerAcknowledgeCalls:
143
+ tokenGranter.acknowledge.mock.calls,
144
+ }).toMatchInlineSnapshot(`
145
+ {
146
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
147
+ "codeGranterAuthenticatorAuthenticateCalls": [],
148
+ "codeGranterAuthorizerAuthorizeCalls": [
149
+ [
150
+ {
151
+ "clientGrants": {
152
+ "allowedGrantTypes": [
153
+ "code",
154
+ ],
155
+ "allowedRedirectURIS": [
156
+ "https://example.com/oauth/cb",
157
+ ],
158
+ "allowedScopes": [
159
+ "user",
160
+ ],
161
+ "authenticationData": {
162
+ "clientId": "a_client_id",
163
+ "scopes": [],
164
+ "userId": "1",
165
+ },
166
+ "canAcknowledge": false,
167
+ "isPublicClient": true,
168
+ },
169
+ "clientId": "a_client_id",
170
+ "demandedScopes": [
171
+ "user",
172
+ ],
173
+ },
174
+ ],
175
+ ],
176
+ "logCalls": [],
177
+ "oAuth2AuthorizationRequestsCreateCalls": [
178
+ [
179
+ "a_client_id",
180
+ {
181
+ "client_id": "a_client_id",
182
+ "redirect_uri": "https://example.com/oauth/cb",
183
+ "response_type": "code",
184
+ "scope": "user",
185
+ "state": "bancal",
186
+ },
187
+ ],
188
+ ],
189
+ "readClientGrantsCalls": [
190
+ [
191
+ "a_client_id",
192
+ ],
193
+ ],
194
+ "response": {
195
+ "body": {
196
+ "expires_in": 1,
197
+ "request_uri": "urn:ietf:params:oauth:request_uri:a_request_uri",
198
+ },
199
+ "headers": {
200
+ "cache-control": "no-store",
201
+ "pragma": "no-cache",
202
+ },
203
+ "status": 201,
204
+ },
205
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
206
+ "tokenGranterAuthorizerAuthorizeCalls": [],
207
+ }
208
+ `);
209
+ });
210
+
211
+ test('should reject nested request_uri parameter', async () => {
212
+ [
213
+ codeGranter.authorize,
214
+ codeGranter.acknowledge,
215
+ codeGranter.authenticate,
216
+ tokenGranter.authorize,
217
+ tokenGranter.acknowledge,
218
+ oAuth2AuthorizationRequests.create,
219
+ ].forEach((mock: any) =>
220
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
221
+ );
222
+
223
+ const postOAuth2PushedAuthorizationRequest =
224
+ await initPostOAuth2PushedAuthorizationRequest({
225
+ OAUTH2,
226
+ OAUTH2_PAR,
227
+ oAuth2Granters,
228
+ readClientGrants,
229
+ oAuth2AuthorizationRequests,
230
+ log,
231
+ });
232
+
233
+ readClientGrants.mockResolvedValueOnce({
234
+ allowedGrantTypes: ['code'],
235
+ allowedScopes: ['user'],
236
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
237
+ isPublicClient: true,
238
+ canAcknowledge: false,
239
+ authenticationData: {
240
+ clientId: 'a_client_id',
241
+ scopes: [],
242
+ userId: '1',
243
+ },
244
+ });
245
+
246
+ await expect(
247
+ postOAuth2PushedAuthorizationRequest({
248
+ body: {
249
+ response_type: 'code',
250
+ client_id: 'a_client_id',
251
+ redirect_uri: 'https://example.com/oauth/cb',
252
+ scope: 'user',
253
+ state: 'bancal',
254
+ request_uri: 'urn:ietf:params:oauth:request_uri:test',
255
+ },
256
+ }),
257
+ ).rejects.toMatchObject({
258
+ code: 'E_OAUTH2_REQUEST_URI_NOT_ALLOWED',
259
+ });
260
+ });
261
+
262
+ test('should reject client mismatch', async () => {
263
+ [
264
+ codeGranter.authorize,
265
+ codeGranter.acknowledge,
266
+ codeGranter.authenticate,
267
+ tokenGranter.authorize,
268
+ tokenGranter.acknowledge,
269
+ oAuth2AuthorizationRequests.create,
270
+ ].forEach((mock: any) =>
271
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
272
+ );
273
+
274
+ const postOAuth2PushedAuthorizationRequest =
275
+ await initPostOAuth2PushedAuthorizationRequest({
276
+ OAUTH2,
277
+ OAUTH2_PAR,
278
+ oAuth2Granters,
279
+ readClientGrants,
280
+ oAuth2AuthorizationRequests,
281
+ log,
282
+ });
283
+
284
+ readClientGrants.mockResolvedValueOnce({
285
+ allowedGrantTypes: ['code'],
286
+ allowedScopes: ['user'],
287
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
288
+ isPublicClient: true,
289
+ canAcknowledge: false,
290
+ authenticationData: {
291
+ clientId: 'another_client_id',
292
+ scopes: [],
293
+ userId: '1',
294
+ },
295
+ });
296
+
297
+ await expect(
298
+ postOAuth2PushedAuthorizationRequest({
299
+ body: {
300
+ response_type: 'code',
301
+ client_id: 'a_client_id',
302
+ redirect_uri: 'https://example.com/oauth/cb',
303
+ scope: 'user',
304
+ state: 'bancal',
305
+ request_uri: 'urn:ietf:params:oauth:request_uri:test',
306
+ },
307
+ }),
308
+ ).rejects.toMatchObject({
309
+ code: 'E_OAUTH2_CLIENT_GRANTS_MISMATCH',
310
+ });
311
+ });
312
+
313
+ test('should reject not existing clients', async () => {
314
+ [
315
+ codeGranter.authorize,
316
+ codeGranter.acknowledge,
317
+ codeGranter.authenticate,
318
+ tokenGranter.authorize,
319
+ tokenGranter.acknowledge,
320
+ ].forEach((mock: any) =>
321
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
322
+ );
323
+
324
+ const postOAuth2PushedAuthorizationRequest =
325
+ await initPostOAuth2PushedAuthorizationRequest({
326
+ OAUTH2,
327
+ OAUTH2_PAR,
328
+ oAuth2Granters,
329
+ readClientGrants,
330
+ oAuth2AuthorizationRequests,
331
+ log,
332
+ });
333
+
334
+ readClientGrants.mockRejectedValueOnce(
335
+ new YError('E_OAUTH2_CLIENT_NOT_FOUND', ['the_client_app_id']),
336
+ );
337
+
338
+ await expect(
339
+ postOAuth2PushedAuthorizationRequest({
340
+ body: {
341
+ response_type: 'code',
342
+ client_id: 'a_client_id',
343
+ redirect_uri: 'https://example.com/oauth/cb',
344
+ scope: 'user',
345
+ state: 'bancal',
346
+ request_uri: 'urn:ietf:params:oauth:request_uri:test',
347
+ },
348
+ }),
349
+ ).rejects.toMatchObject({
350
+ code: 'E_OAUTH2_CLIENT_NOT_FOUND',
351
+ });
352
+ });
353
+
354
+ test('should reject bad redirect uri', async () => {
355
+ [
356
+ codeGranter.authorize,
357
+ codeGranter.acknowledge,
358
+ codeGranter.authenticate,
359
+ tokenGranter.authorize,
360
+ tokenGranter.acknowledge,
361
+ oAuth2AuthorizationRequests.create,
362
+ ].forEach((mock: any) =>
363
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
364
+ );
365
+
366
+ const postOAuth2PushedAuthorizationRequest =
367
+ await initPostOAuth2PushedAuthorizationRequest({
368
+ OAUTH2,
369
+ OAUTH2_PAR,
370
+ oAuth2Granters,
371
+ readClientGrants,
372
+ oAuth2AuthorizationRequests,
373
+ log,
374
+ });
375
+
376
+ readClientGrants.mockResolvedValueOnce({
377
+ allowedGrantTypes: ['code'],
378
+ allowedScopes: ['user'],
379
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
380
+ isPublicClient: true,
381
+ canAcknowledge: false,
382
+ authenticationData: {
383
+ clientId: 'a_client_id',
384
+ scopes: [],
385
+ userId: '1',
386
+ },
387
+ });
388
+
389
+ await expect(
390
+ postOAuth2PushedAuthorizationRequest({
391
+ body: {
392
+ response_type: 'code',
393
+ client_id: 'a_client_id',
394
+ redirect_uri: 'https://example.com',
395
+ scope: 'user',
396
+ state: 'bancal',
397
+ },
398
+ }),
399
+ ).rejects.toMatchObject({
400
+ code: 'E_OAUTH2_BAD_REDIRECT_URI',
401
+ });
402
+ });
403
+
404
+ test('should reject secret client with no authentication', async () => {
405
+ [
406
+ codeGranter.authorize,
407
+ codeGranter.acknowledge,
408
+ codeGranter.authenticate,
409
+ tokenGranter.authorize,
410
+ tokenGranter.acknowledge,
411
+ oAuth2AuthorizationRequests.create,
412
+ ].forEach((mock: any) =>
413
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
414
+ );
415
+
416
+ const postOAuth2PushedAuthorizationRequest =
417
+ await initPostOAuth2PushedAuthorizationRequest({
418
+ OAUTH2,
419
+ OAUTH2_PAR,
420
+ oAuth2Granters,
421
+ readClientGrants,
422
+ oAuth2AuthorizationRequests,
423
+ log,
424
+ });
425
+
426
+ readClientGrants.mockResolvedValueOnce({
427
+ allowedGrantTypes: ['code'],
428
+ allowedScopes: ['user'],
429
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
430
+ isPublicClient: false,
431
+ canAcknowledge: false,
432
+ authenticationData: {
433
+ clientId: 'a_client_id',
434
+ scopes: [],
435
+ userId: '1',
436
+ },
437
+ });
438
+
439
+ await expect(
440
+ postOAuth2PushedAuthorizationRequest({
441
+ body: {
442
+ response_type: 'code',
443
+ client_id: 'a_client_id',
444
+ redirect_uri: 'https://example.com',
445
+ scope: 'user',
446
+ state: 'bancal',
447
+ },
448
+ }),
449
+ ).rejects.toMatchObject({
450
+ code: 'E_OAUTH2_AUTHENTICATION_REQUIRED',
451
+ });
452
+ });
453
+
454
+ test('should reject secret client id mismatch', async () => {
455
+ [
456
+ codeGranter.authorize,
457
+ codeGranter.acknowledge,
458
+ codeGranter.authenticate,
459
+ tokenGranter.authorize,
460
+ tokenGranter.acknowledge,
461
+ oAuth2AuthorizationRequests.create,
462
+ readClientGrants,
463
+ ].forEach((mock: any) =>
464
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
465
+ );
466
+
467
+ const postOAuth2PushedAuthorizationRequest =
468
+ await initPostOAuth2PushedAuthorizationRequest({
469
+ OAUTH2,
470
+ OAUTH2_PAR,
471
+ oAuth2Granters,
472
+ readClientGrants,
473
+ oAuth2AuthorizationRequests,
474
+ log,
475
+ });
476
+
477
+ await expect(
478
+ postOAuth2PushedAuthorizationRequest({
479
+ body: {
480
+ response_type: 'code',
481
+ client_id: 'a_client_id',
482
+ redirect_uri: 'https://example.com',
483
+ scope: 'user',
484
+ state: 'bancal',
485
+ },
486
+ authenticationData: {
487
+ clientId: 'a_different_client_id',
488
+ scopes: [],
489
+ userId: '1',
490
+ },
491
+ }),
492
+ ).rejects.toMatchObject({
493
+ code: 'E_OAUTH2_CLIENT_MISMATCH',
494
+ });
495
+ });
496
+
497
+ test('should reject bad scopes', async () => {
498
+ [
499
+ codeGranter.authorize,
500
+ codeGranter.acknowledge,
501
+ codeGranter.authenticate,
502
+ tokenGranter.authorize,
503
+ tokenGranter.acknowledge,
504
+ oAuth2AuthorizationRequests.create,
505
+ ].forEach((mock: any) =>
506
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
507
+ );
508
+
509
+ const postOAuth2PushedAuthorizationRequest =
510
+ await initPostOAuth2PushedAuthorizationRequest({
511
+ OAUTH2: {
512
+ ...OAUTH2,
513
+ strictScopesChecks: true,
514
+ },
515
+ OAUTH2_PAR,
516
+ oAuth2Granters,
517
+ readClientGrants,
518
+ oAuth2AuthorizationRequests,
519
+ log,
520
+ });
521
+
522
+ readClientGrants.mockResolvedValueOnce({
523
+ allowedGrantTypes: ['code'],
524
+ allowedScopes: ['user'],
525
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
526
+ isPublicClient: true,
527
+ canAcknowledge: false,
528
+ authenticationData: {
529
+ clientId: 'a_client_id',
530
+ scopes: [],
531
+ userId: '1',
532
+ },
533
+ });
534
+
535
+ await expect(
536
+ postOAuth2PushedAuthorizationRequest({
537
+ body: {
538
+ response_type: 'code',
539
+ client_id: 'a_client_id',
540
+ redirect_uri: 'https://example.com/oauth/cb',
541
+ scope: 'admin',
542
+ state: 'bancal',
543
+ },
544
+ }),
545
+ ).rejects.toMatchObject({
546
+ code: 'E_OAUTH2_BAD_SCOPE',
547
+ });
548
+ });
549
+
550
+ test('should reject bad challenge', async () => {
551
+ [
552
+ codeGranter.authorize,
553
+ codeGranter.acknowledge,
554
+ codeGranter.authenticate,
555
+ tokenGranter.authorize,
556
+ tokenGranter.acknowledge,
557
+ oAuth2AuthorizationRequests.create,
558
+ ].forEach((mock: any) =>
559
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
560
+ );
561
+
562
+ const postOAuth2PushedAuthorizationRequest =
563
+ await initPostOAuth2PushedAuthorizationRequest({
564
+ OAUTH2: {
565
+ ...OAUTH2,
566
+ strictScopesChecks: true,
567
+ },
568
+ OAUTH2_PAR,
569
+ oAuth2Granters,
570
+ readClientGrants,
571
+ oAuth2AuthorizationRequests,
572
+ log,
573
+ });
574
+
575
+ readClientGrants.mockResolvedValueOnce({
576
+ allowedGrantTypes: ['code'],
577
+ allowedScopes: ['user'],
578
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
579
+ isPublicClient: true,
580
+ canAcknowledge: false,
581
+ authenticationData: {
582
+ clientId: 'a_client_id',
583
+ scopes: [],
584
+ userId: '1',
585
+ },
586
+ });
587
+
588
+ await expect(
589
+ postOAuth2PushedAuthorizationRequest({
590
+ body: {
591
+ response_type: 'token',
592
+ client_id: 'a_client_id',
593
+ redirect_uri: 'https://example.com/oauth/cb',
594
+ scope: 'user',
595
+ state: 'bancal',
596
+ code_challenge: 'challenge',
597
+ code_challenge_method: 'S256',
598
+ },
599
+ }),
600
+ ).rejects.toMatchObject({
601
+ code: 'E_OAUTH2_PKCE_NOT_SUPPORTED',
602
+ });
603
+ });
604
+
605
+ test('should reject bad response type', async () => {
606
+ [
607
+ codeGranter.authorize,
608
+ codeGranter.acknowledge,
609
+ codeGranter.authenticate,
610
+ tokenGranter.authorize,
611
+ tokenGranter.acknowledge,
612
+ oAuth2AuthorizationRequests.create,
613
+ ].forEach((mock: any) =>
614
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
615
+ );
616
+
617
+ const postOAuth2PushedAuthorizationRequest =
618
+ await initPostOAuth2PushedAuthorizationRequest({
619
+ OAUTH2: {
620
+ ...OAUTH2,
621
+ strictScopesChecks: true,
622
+ },
623
+ OAUTH2_PAR,
624
+ oAuth2Granters,
625
+ readClientGrants,
626
+ oAuth2AuthorizationRequests,
627
+ log,
628
+ });
629
+
630
+ readClientGrants.mockResolvedValueOnce({
631
+ allowedGrantTypes: ['code'],
632
+ allowedScopes: ['user'],
633
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
634
+ isPublicClient: true,
635
+ canAcknowledge: false,
636
+ authenticationData: {
637
+ clientId: 'a_client_id',
638
+ scopes: [],
639
+ userId: '1',
640
+ },
641
+ });
642
+
643
+ await expect(
644
+ postOAuth2PushedAuthorizationRequest({
645
+ body: {
646
+ response_type: 'yolo',
647
+ client_id: 'a_client_id',
648
+ redirect_uri: 'https://example.com/oauth/cb',
649
+ scope: 'user',
650
+ state: 'bancal',
651
+ },
652
+ }),
653
+ ).rejects.toMatchObject({
654
+ code: 'E_OAUTH2_UNKNOWN_RESPONSE_TYPE',
655
+ });
656
+ });
657
+
658
+ test('should fail when misconfigured', async () => {
659
+ [
660
+ codeGranter.authorize,
661
+ codeGranter.acknowledge,
662
+ codeGranter.authenticate,
663
+ tokenGranter.authorize,
664
+ tokenGranter.acknowledge,
665
+ ].forEach((mock: any) =>
666
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
667
+ );
668
+
669
+ await expect(
670
+ initPostOAuth2PushedAuthorizationRequest({
671
+ OAUTH2,
672
+ oAuth2Granters,
673
+ readClientGrants,
674
+ oAuth2AuthorizationRequests,
675
+ log,
676
+ }),
677
+ ).rejects.toMatchObject({
678
+ code: 'E_OAUTH2_MISCONFIGURED',
679
+ });
680
+ });
681
+ });