@whook/oauth2 9.0.1 → 10.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 (131) hide show
  1. package/README.md +20 -15
  2. package/dist/handlers/getOAuth2Authorize.d.ts +4 -4
  3. package/dist/handlers/getOAuth2Authorize.js +122 -165
  4. package/dist/handlers/getOAuth2Authorize.js.map +1 -1
  5. package/dist/handlers/getOAuth2Authorize.test.js +117 -103
  6. package/dist/handlers/getOAuth2Authorize.test.js.map +1 -1
  7. package/dist/handlers/postAuthLogin.d.ts +1 -1
  8. package/dist/handlers/postAuthLogin.js +83 -121
  9. package/dist/handlers/postAuthLogin.js.map +1 -1
  10. package/dist/handlers/postAuthLogin.test.js +42 -44
  11. package/dist/handlers/postAuthLogin.test.js.map +1 -1
  12. package/dist/handlers/postAuthLogout.d.ts +1 -1
  13. package/dist/handlers/postAuthLogout.js +26 -42
  14. package/dist/handlers/postAuthLogout.js.map +1 -1
  15. package/dist/handlers/postAuthLogout.test.js +19 -21
  16. package/dist/handlers/postAuthLogout.test.js.map +1 -1
  17. package/dist/handlers/postAuthRefresh.d.ts +1 -1
  18. package/dist/handlers/postAuthRefresh.js +93 -132
  19. package/dist/handlers/postAuthRefresh.js.map +1 -1
  20. package/dist/handlers/postAuthRefresh.test.js +47 -49
  21. package/dist/handlers/postAuthRefresh.test.js.map +1 -1
  22. package/dist/handlers/postOAuth2Acknowledge.d.ts +5 -5
  23. package/dist/handlers/postOAuth2Acknowledge.js +119 -151
  24. package/dist/handlers/postOAuth2Acknowledge.js.map +1 -1
  25. package/dist/handlers/postOAuth2Acknowledge.test.js +139 -125
  26. package/dist/handlers/postOAuth2Acknowledge.test.js.map +1 -1
  27. package/dist/handlers/postOAuth2Token.d.ts +1 -1
  28. package/dist/handlers/postOAuth2Token.js +200 -232
  29. package/dist/handlers/postOAuth2Token.js.map +1 -1
  30. package/dist/handlers/postOAuth2Token.test.js +156 -153
  31. package/dist/handlers/postOAuth2Token.test.js.map +1 -1
  32. package/dist/index.d.ts +15 -15
  33. package/dist/index.js +14 -229
  34. package/dist/index.js.map +1 -1
  35. package/dist/index.test.js +697 -619
  36. package/dist/index.test.js.map +1 -1
  37. package/dist/services/authCookies.d.ts +1 -0
  38. package/dist/services/authCookies.js +40 -71
  39. package/dist/services/authCookies.js.map +1 -1
  40. package/dist/services/authCookies.test.js +57 -62
  41. package/dist/services/authCookies.test.js.map +1 -1
  42. package/dist/services/oAuth2ClientCredentialsGranter.d.ts +2 -2
  43. package/dist/services/oAuth2ClientCredentialsGranter.js +34 -52
  44. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
  45. package/dist/services/oAuth2CodeGranter.d.ts +1 -1
  46. package/dist/services/oAuth2CodeGranter.js +66 -109
  47. package/dist/services/oAuth2CodeGranter.js.map +1 -1
  48. package/dist/services/oAuth2CodeGranter.test.js +65 -66
  49. package/dist/services/oAuth2CodeGranter.test.js.map +1 -1
  50. package/dist/services/oAuth2Granters.js +131 -138
  51. package/dist/services/oAuth2Granters.js.map +1 -1
  52. package/dist/services/oAuth2PasswordGranter.d.ts +1 -1
  53. package/dist/services/oAuth2PasswordGranter.js +27 -52
  54. package/dist/services/oAuth2PasswordGranter.js.map +1 -1
  55. package/dist/services/oAuth2RefreshTokenGranter.d.ts +1 -1
  56. package/dist/services/oAuth2RefreshTokenGranter.js +34 -58
  57. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
  58. package/dist/services/oAuth2TokenGranter.d.ts +1 -1
  59. package/dist/services/oAuth2TokenGranter.js +50 -86
  60. package/dist/services/oAuth2TokenGranter.js.map +1 -1
  61. package/package.json +46 -88
  62. package/src/__snapshots__/index.test.ts.snap +23 -22
  63. package/src/handlers/__snapshots__/postOAuth2Acknowledge.test.ts.snap +1 -1
  64. package/src/handlers/getOAuth2Authorize.test.ts +49 -13
  65. package/src/handlers/getOAuth2Authorize.ts +2 -2
  66. package/src/handlers/postAuthLogin.test.ts +9 -5
  67. package/src/handlers/postAuthLogin.ts +4 -4
  68. package/src/handlers/postAuthLogout.test.ts +8 -4
  69. package/src/handlers/postAuthLogout.ts +2 -2
  70. package/src/handlers/postAuthRefresh.test.ts +10 -6
  71. package/src/handlers/postAuthRefresh.ts +4 -4
  72. package/src/handlers/postOAuth2Acknowledge.test.ts +83 -35
  73. package/src/handlers/postOAuth2Acknowledge.ts +18 -8
  74. package/src/handlers/postOAuth2Token.test.ts +77 -17
  75. package/src/handlers/postOAuth2Token.ts +3 -3
  76. package/src/index.test.ts +110 -65
  77. package/src/index.ts +15 -15
  78. package/src/services/authCookies.test.ts +2 -2
  79. package/src/services/authCookies.ts +2 -1
  80. package/src/services/oAuth2ClientCredentialsGranter.ts +3 -3
  81. package/src/services/oAuth2CodeGranter.test.ts +27 -4
  82. package/src/services/oAuth2CodeGranter.ts +2 -2
  83. package/src/services/oAuth2PasswordGranter.ts +2 -2
  84. package/src/services/oAuth2RefreshTokenGranter.ts +2 -2
  85. package/src/services/oAuth2TokenGranter.ts +1 -1
  86. package/dist/handlers/getOAuth2Authorize.mjs +0 -160
  87. package/dist/handlers/getOAuth2Authorize.mjs.map +0 -1
  88. package/dist/handlers/getOAuth2Authorize.test.mjs +0 -114
  89. package/dist/handlers/getOAuth2Authorize.test.mjs.map +0 -1
  90. package/dist/handlers/postAuthLogin.mjs +0 -109
  91. package/dist/handlers/postAuthLogin.mjs.map +0 -1
  92. package/dist/handlers/postAuthLogin.test.mjs +0 -85
  93. package/dist/handlers/postAuthLogin.test.mjs.map +0 -1
  94. package/dist/handlers/postAuthLogout.mjs +0 -33
  95. package/dist/handlers/postAuthLogout.mjs.map +0 -1
  96. package/dist/handlers/postAuthLogout.test.mjs +0 -30
  97. package/dist/handlers/postAuthLogout.test.mjs.map +0 -1
  98. package/dist/handlers/postAuthRefresh.mjs +0 -122
  99. package/dist/handlers/postAuthRefresh.mjs.map +0 -1
  100. package/dist/handlers/postAuthRefresh.test.mjs +0 -94
  101. package/dist/handlers/postAuthRefresh.test.mjs.map +0 -1
  102. package/dist/handlers/postOAuth2Acknowledge.mjs +0 -150
  103. package/dist/handlers/postOAuth2Acknowledge.mjs.map +0 -1
  104. package/dist/handlers/postOAuth2Acknowledge.test.mjs +0 -140
  105. package/dist/handlers/postOAuth2Acknowledge.test.mjs.map +0 -1
  106. package/dist/handlers/postOAuth2Token.mjs +0 -231
  107. package/dist/handlers/postOAuth2Token.mjs.map +0 -1
  108. package/dist/handlers/postOAuth2Token.test.mjs +0 -160
  109. package/dist/handlers/postOAuth2Token.test.mjs.map +0 -1
  110. package/dist/index.mjs +0 -15
  111. package/dist/index.mjs.map +0 -1
  112. package/dist/index.test.mjs +0 -747
  113. package/dist/index.test.mjs.map +0 -1
  114. package/dist/services/authCookies.mjs +0 -58
  115. package/dist/services/authCookies.mjs.map +0 -1
  116. package/dist/services/authCookies.test.mjs +0 -77
  117. package/dist/services/authCookies.test.mjs.map +0 -1
  118. package/dist/services/oAuth2ClientCredentialsGranter.mjs +0 -41
  119. package/dist/services/oAuth2ClientCredentialsGranter.mjs.map +0 -1
  120. package/dist/services/oAuth2CodeGranter.mjs +0 -98
  121. package/dist/services/oAuth2CodeGranter.mjs.map +0 -1
  122. package/dist/services/oAuth2CodeGranter.test.mjs +0 -83
  123. package/dist/services/oAuth2CodeGranter.test.mjs.map +0 -1
  124. package/dist/services/oAuth2Granters.mjs +0 -127
  125. package/dist/services/oAuth2Granters.mjs.map +0 -1
  126. package/dist/services/oAuth2PasswordGranter.mjs +0 -40
  127. package/dist/services/oAuth2PasswordGranter.mjs.map +0 -1
  128. package/dist/services/oAuth2RefreshTokenGranter.mjs +0 -47
  129. package/dist/services/oAuth2RefreshTokenGranter.mjs.map +0 -1
  130. package/dist/services/oAuth2TokenGranter.mjs +0 -78
  131. package/dist/services/oAuth2TokenGranter.mjs.map +0 -1
package/src/index.test.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { jest } from '@jest/globals';
1
2
  import {
2
3
  runServer,
3
4
  prepareServer,
@@ -6,11 +7,12 @@ import {
6
7
  } from '@whook/whook';
7
8
  import {
8
9
  AUTHORIZATION_ERRORS_DESCRIPTORS,
10
+ BaseAuthenticationData,
9
11
  wrapHandlerWithAuthorization,
10
12
  } from '@whook/authorization';
11
13
  import { constant, initializer } from 'knifecycle';
12
- import axios from 'axios';
13
- import YError from 'yerror';
14
+ import { default as axios } from 'axios';
15
+ import { YError } from 'yerror';
14
16
  import {
15
17
  BEARER as BEARER_MECHANISM,
16
18
  BASIC as BASIC_MECHANISM,
@@ -39,19 +41,32 @@ import {
39
41
  postOAuth2TokenClientCredentialsTokenRequestBodySchema,
40
42
  postOAuth2TokenTokenBodySchema,
41
43
  postOAuth2TokenRefreshTokenRequestBodySchema,
42
- } from '.';
43
- import type { OAuth2Options } from '.';
44
+ } from './index.js';
45
+ import type {
46
+ OAuth2Options,
47
+ CheckApplicationService,
48
+ OAuth2PasswordService,
49
+ OAuth2CodeService,
50
+ OAuth2RefreshTokenService,
51
+ OAuth2AccessTokenService,
52
+ } from './index.js';
44
53
  import type { Knifecycle } from 'knifecycle';
45
54
  import type { OpenAPIV3 } from 'openapi-types';
55
+ import type { Logger } from 'common-services';
56
+ import type { AuthenticationService } from '@whook/authorization';
57
+
58
+ type CustomAuthenticationData = BaseAuthenticationData & {
59
+ userId: string;
60
+ };
46
61
 
47
62
  describe('OAuth2 server', () => {
48
63
  const BASE_PATH = '/v1';
49
64
  const PORT = 4444;
50
65
  const HOST = 'localhost';
51
66
  const logger = {
52
- output: jest.fn(),
53
- error: jest.fn(),
54
- debug: jest.fn(),
67
+ output: jest.fn<Logger['output']>(),
68
+ error: jest.fn<Logger['error']>(),
69
+ debug: jest.fn<Logger['debug']>(),
55
70
  };
56
71
  const time = jest.fn();
57
72
  const $autoload = jest.fn();
@@ -133,13 +148,30 @@ describe('OAuth2 server', () => {
133
148
  const OAUTH2: OAuth2Options = {
134
149
  authenticateURL: 'https://auth.example.com/sign_in',
135
150
  };
136
- const authentication = { check: jest.fn() };
137
- const checkApplication = jest.fn();
138
- const oAuth2AccessToken = { create: jest.fn(), check: jest.fn() };
139
- const oAuth2RefreshToken = { create: jest.fn(), check: jest.fn() };
140
- const oAuth2ClientCredentials = { check: jest.fn() };
141
- const oAuth2Code = { create: jest.fn(), check: jest.fn() };
142
- const oAuth2Password = { check: jest.fn() };
151
+ const authentication = {
152
+ check:
153
+ jest.fn<AuthenticationService<any, CustomAuthenticationData>['check']>(),
154
+ };
155
+ const checkApplication = jest.fn<CheckApplicationService>();
156
+ const oAuth2AccessToken = {
157
+ create:
158
+ jest.fn<OAuth2AccessTokenService<CustomAuthenticationData>['create']>(),
159
+ check:
160
+ jest.fn<OAuth2AccessTokenService<CustomAuthenticationData>['check']>(),
161
+ };
162
+ const oAuth2RefreshToken = {
163
+ create:
164
+ jest.fn<OAuth2RefreshTokenService<CustomAuthenticationData>['create']>(),
165
+ check:
166
+ jest.fn<OAuth2RefreshTokenService<CustomAuthenticationData>['check']>(),
167
+ };
168
+ const oAuth2Code = {
169
+ create: jest.fn<OAuth2CodeService<CustomAuthenticationData>['create']>(),
170
+ check: jest.fn<OAuth2CodeService<CustomAuthenticationData>['check']>(),
171
+ };
172
+ const oAuth2Password = {
173
+ check: jest.fn<OAuth2PasswordService<CustomAuthenticationData>['check']>(),
174
+ };
143
175
 
144
176
  let $instance;
145
177
 
@@ -165,7 +197,7 @@ describe('OAuth2 server', () => {
165
197
  $.register(constant('DEBUG_NODE_ENVS', []));
166
198
  $.register(constant('NODE_ENVS', ['test']));
167
199
  $.register(constant('MECHANISMS', [BEARER_MECHANISM, BASIC_MECHANISM]));
168
- $.register(constant('logger', logger));
200
+ $.register(constant('logger', logger as Logger));
169
201
  $.register(constant('time', time));
170
202
 
171
203
  // OAuth2 Specifics
@@ -196,7 +228,6 @@ describe('OAuth2 server', () => {
196
228
  $.register(constant('checkApplication', checkApplication));
197
229
  $.register(constant('oAuth2AccessToken', oAuth2AccessToken));
198
230
  $.register(constant('oAuth2RefreshToken', oAuth2RefreshToken));
199
- $.register(constant('oAuth2ClientCredentials', oAuth2ClientCredentials));
200
231
  $.register(constant('oAuth2Code', oAuth2Code));
201
232
  $.register(constant('oAuth2Password', oAuth2Password));
202
233
  [
@@ -245,7 +276,6 @@ describe('OAuth2 server', () => {
245
276
  oAuth2AccessToken.check,
246
277
  oAuth2RefreshToken.create,
247
278
  oAuth2RefreshToken.check,
248
- oAuth2ClientCredentials.check,
249
279
  oAuth2Code.check,
250
280
  oAuth2Code.create,
251
281
  oAuth2Password.check,
@@ -260,24 +290,27 @@ describe('OAuth2 server', () => {
260
290
  [
261
291
  oAuth2AccessToken.check,
262
292
  oAuth2RefreshToken.check,
263
- oAuth2ClientCredentials.check,
264
293
  oAuth2Code.check,
265
294
  oAuth2Code.create,
266
295
  oAuth2RefreshToken.check,
267
- ].forEach((mock) =>
296
+ ].forEach((mock: any) =>
268
297
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
269
298
  );
270
299
  authentication.check.mockResolvedValueOnce({
271
300
  applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
272
301
  scope: 'user,oauth',
302
+ userId: '2',
303
+ });
304
+ checkApplication.mockResolvedValueOnce({
305
+ type: 'code',
306
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
307
+ scope: 'user,oauth',
308
+ redirectURI: 'http://redirect.example.com/yolo',
273
309
  });
274
- checkApplication.mockResolvedValueOnce(undefined);
275
310
  oAuth2Password.check.mockResolvedValueOnce({
276
311
  applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
277
312
  scope: 'user,auth',
278
- authentication: {
279
- userId: 1,
280
- },
313
+ userId: '1',
281
314
  });
282
315
  oAuth2AccessToken.create.mockResolvedValueOnce({
283
316
  token: 'an_access_token',
@@ -312,6 +345,7 @@ describe('OAuth2 server', () => {
312
345
  // Erasing the Date header that may be added by Axios :/
313
346
  date: undefined,
314
347
  },
348
+
315
349
  data,
316
350
  }).toMatchInlineSnapshot(`
317
351
  Object {
@@ -330,7 +364,7 @@ describe('OAuth2 server', () => {
330
364
  "date": undefined,
331
365
  "transaction-id": "0",
332
366
  "transfer-encoding": "chunked",
333
- "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\"}",
367
+ "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\",\\"userId\\":\\"2\\"}",
334
368
  },
335
369
  "status": 200,
336
370
  }
@@ -342,8 +376,6 @@ describe('OAuth2 server', () => {
342
376
  oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
343
377
  oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
344
378
  oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
345
- oAuth2ClientCredentialsCheckCalls:
346
- oAuth2ClientCredentials.check.mock.calls,
347
379
  oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
348
380
  oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
349
381
  oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
@@ -356,7 +388,6 @@ describe('OAuth2 server', () => {
356
388
  time.mockReturnValue(Date.parse('2010-03-06T00:00:00Z'));
357
389
  [
358
390
  oAuth2AccessToken.check,
359
- oAuth2ClientCredentials.check,
360
391
  oAuth2Code.check,
361
392
  oAuth2Code.create,
362
393
  oAuth2Password.check,
@@ -366,14 +397,18 @@ describe('OAuth2 server', () => {
366
397
  authentication.check.mockResolvedValueOnce({
367
398
  applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
368
399
  scope: 'user,oauth',
400
+ userId: '2',
401
+ });
402
+ checkApplication.mockResolvedValueOnce({
403
+ type: 'code',
404
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
405
+ scope: 'user,oauth',
406
+ redirectURI: 'http://redirect.example.com/yolo',
369
407
  });
370
- checkApplication.mockResolvedValueOnce(undefined);
371
408
  oAuth2RefreshToken.check.mockResolvedValueOnce({
372
409
  applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
373
410
  scope: 'user,auth',
374
- authentication: {
375
- userId: 1,
376
- },
411
+ userId: '1',
377
412
  });
378
413
  oAuth2AccessToken.create.mockResolvedValueOnce({
379
414
  token: 'an_access_token',
@@ -407,6 +442,7 @@ describe('OAuth2 server', () => {
407
442
  // Erasing the Date header that may be added by Axios :/
408
443
  date: undefined,
409
444
  },
445
+
410
446
  data,
411
447
  }).toMatchInlineSnapshot(`
412
448
  Object {
@@ -425,7 +461,7 @@ describe('OAuth2 server', () => {
425
461
  "date": undefined,
426
462
  "transaction-id": "1",
427
463
  "transfer-encoding": "chunked",
428
- "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\"}",
464
+ "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\",\\"userId\\":\\"2\\"}",
429
465
  },
430
466
  "status": 200,
431
467
  }
@@ -437,8 +473,6 @@ describe('OAuth2 server', () => {
437
473
  oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
438
474
  oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
439
475
  oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
440
- oAuth2ClientCredentialsCheckCalls:
441
- oAuth2ClientCredentials.check.mock.calls,
442
476
  oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
443
477
  oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
444
478
  oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
@@ -461,14 +495,13 @@ describe('OAuth2 server', () => {
461
495
  authentication.check.mockResolvedValueOnce({
462
496
  applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
463
497
  scope: 'user,oauth',
498
+ userId: '2',
464
499
  });
465
- checkApplication.mockResolvedValueOnce(undefined);
466
- oAuth2ClientCredentials.check.mockResolvedValueOnce({
500
+ checkApplication.mockResolvedValueOnce({
501
+ type: 'code',
467
502
  applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
468
- scope: 'user,auth',
469
- authentication: {
470
- userId: 1,
471
- },
503
+ scope: 'user,oauth',
504
+ redirectURI: 'http://redirect.example.com/yolo',
472
505
  });
473
506
  oAuth2AccessToken.create.mockResolvedValueOnce({
474
507
  token: 'an_access_token',
@@ -501,6 +534,7 @@ describe('OAuth2 server', () => {
501
534
  // Erasing the Date header that may be added by Axios :/
502
535
  date: undefined,
503
536
  },
537
+
504
538
  data,
505
539
  }).toMatchInlineSnapshot(`
506
540
  Object {
@@ -519,7 +553,7 @@ describe('OAuth2 server', () => {
519
553
  "date": undefined,
520
554
  "transaction-id": "2",
521
555
  "transfer-encoding": "chunked",
522
- "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\"}",
556
+ "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\",\\"userId\\":\\"2\\"}",
523
557
  },
524
558
  "status": 200,
525
559
  }
@@ -531,8 +565,6 @@ describe('OAuth2 server', () => {
531
565
  oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
532
566
  oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
533
567
  oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
534
- oAuth2ClientCredentialsCheckCalls:
535
- oAuth2ClientCredentials.check.mock.calls,
536
568
  oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
537
569
  oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
538
570
  oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
@@ -551,12 +583,14 @@ describe('OAuth2 server', () => {
551
583
  oAuth2RefreshToken.create,
552
584
  oAuth2Code.check,
553
585
  oAuth2Code.create,
554
- oAuth2ClientCredentials.check,
555
586
  oAuth2Password.check,
556
587
  ].forEach((mock) =>
557
588
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
558
589
  );
559
590
  checkApplication.mockResolvedValueOnce({
591
+ type: 'code',
592
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
593
+ scope: 'user,oauth',
560
594
  redirectURI:
561
595
  'https://example.com/oauth2/callback?a_param=a_param_value',
562
596
  });
@@ -604,8 +638,6 @@ describe('OAuth2 server', () => {
604
638
  oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
605
639
  oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
606
640
  oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
607
- oAuth2ClientCredentialsCheckCalls:
608
- oAuth2ClientCredentials.check.mock.calls,
609
641
  oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
610
642
  oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
611
643
  oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
@@ -620,7 +652,6 @@ describe('OAuth2 server', () => {
620
652
  oAuth2RefreshToken.check,
621
653
  oAuth2RefreshToken.create,
622
654
  oAuth2Code.check,
623
- oAuth2ClientCredentials.check,
624
655
  oAuth2Password.check,
625
656
  ].forEach((mock) =>
626
657
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
@@ -628,10 +659,13 @@ describe('OAuth2 server', () => {
628
659
  authentication.check.mockResolvedValueOnce({
629
660
  applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
630
661
  scope: 'user,oauth',
662
+ userId: '2',
631
663
  });
632
664
  oAuth2Code.create.mockResolvedValueOnce('a_code');
633
- checkApplication.mockResolvedValueOnce(undefined);
634
665
  checkApplication.mockResolvedValueOnce({
666
+ type: 'code',
667
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
668
+ scope: 'user,oauth',
635
669
  redirectURI: 'http://redirect.example.com/yolo',
636
670
  });
637
671
 
@@ -660,6 +694,7 @@ describe('OAuth2 server', () => {
660
694
  // Erasing the Date header that may be added by Axios :/
661
695
  date: undefined,
662
696
  },
697
+
663
698
  data,
664
699
  }).toMatchInlineSnapshot(`
665
700
  Object {
@@ -670,7 +705,7 @@ describe('OAuth2 server', () => {
670
705
  "location": "http://redirect.example.com/yolo?a_param=a_value&client_id=acdc41ce-acdc-41ce-acdc-41ceacdc41ce&scope=user&state=xyz&redirect_uri=http%3A%2F%2Fredirect.example.com%2Fyolo%3Fa_param%3Da_value&code=a_code",
671
706
  "transaction-id": "4",
672
707
  "transfer-encoding": "chunked",
673
- "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\"}",
708
+ "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\",\\"userId\\":\\"2\\"}",
674
709
  },
675
710
  "status": 302,
676
711
  }
@@ -682,8 +717,6 @@ describe('OAuth2 server', () => {
682
717
  oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
683
718
  oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
684
719
  oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
685
- oAuth2ClientCredentialsCheckCalls:
686
- oAuth2ClientCredentials.check.mock.calls,
687
720
  oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
688
721
  oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
689
722
  oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
@@ -695,7 +728,6 @@ describe('OAuth2 server', () => {
695
728
  [
696
729
  oAuth2AccessToken.check,
697
730
  oAuth2RefreshToken.check,
698
- oAuth2ClientCredentials.check,
699
731
  oAuth2Password.check,
700
732
  ].forEach((mock) =>
701
733
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
@@ -703,12 +735,19 @@ describe('OAuth2 server', () => {
703
735
  authentication.check.mockResolvedValueOnce({
704
736
  applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
705
737
  scope: 'user,oauth',
738
+ userId: '2',
739
+ });
740
+ checkApplication.mockResolvedValueOnce({
741
+ type: 'code',
742
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
743
+ scope: 'user,oauth',
744
+ redirectURI: 'http://redirect.example.com/yolo',
706
745
  });
707
- checkApplication.mockResolvedValueOnce(undefined);
708
746
  oAuth2Code.check.mockResolvedValueOnce({
709
747
  applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
710
748
  scope: 'user,auth',
711
- userId: 1,
749
+ userId: '1',
750
+ redirectURI: 'http://redirect.example.com/yolo',
712
751
  });
713
752
  oAuth2AccessToken.create.mockResolvedValueOnce({
714
753
  token: 'an_access_token',
@@ -742,6 +781,7 @@ describe('OAuth2 server', () => {
742
781
  // Erasing the Date header that may be added by Axios :/
743
782
  date: undefined,
744
783
  },
784
+
745
785
  data,
746
786
  }).toMatchInlineSnapshot(`
747
787
  Object {
@@ -760,7 +800,7 @@ describe('OAuth2 server', () => {
760
800
  "date": undefined,
761
801
  "transaction-id": "5",
762
802
  "transfer-encoding": "chunked",
763
- "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\"}",
803
+ "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\",\\"userId\\":\\"2\\"}",
764
804
  },
765
805
  "status": 200,
766
806
  }
@@ -772,8 +812,6 @@ describe('OAuth2 server', () => {
772
812
  oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
773
813
  oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
774
814
  oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
775
- oAuth2ClientCredentialsCheckCalls:
776
- oAuth2ClientCredentials.check.mock.calls,
777
815
  oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
778
816
  oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
779
817
  oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
@@ -792,12 +830,14 @@ describe('OAuth2 server', () => {
792
830
  oAuth2RefreshToken.create,
793
831
  oAuth2Code.check,
794
832
  oAuth2Code.create,
795
- oAuth2ClientCredentials.check,
796
833
  oAuth2Password.check,
797
834
  ].forEach((mock) =>
798
835
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
799
836
  );
800
837
  checkApplication.mockResolvedValueOnce({
838
+ type: 'implicit',
839
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
840
+ scope: 'user,oauth',
801
841
  redirectURI: 'http://redirect.example.com/yolo',
802
842
  });
803
843
 
@@ -843,8 +883,6 @@ describe('OAuth2 server', () => {
843
883
  oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
844
884
  oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
845
885
  oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
846
- oAuth2ClientCredentialsCheckCalls:
847
- oAuth2ClientCredentials.check.mock.calls,
848
886
  oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
849
887
  oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
850
888
  oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
@@ -859,7 +897,6 @@ describe('OAuth2 server', () => {
859
897
  oAuth2RefreshToken.create,
860
898
  oAuth2Code.check,
861
899
  oAuth2Code.create,
862
- oAuth2ClientCredentials.check,
863
900
  oAuth2Password.check,
864
901
  ].forEach((mock) =>
865
902
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
@@ -867,13 +904,22 @@ describe('OAuth2 server', () => {
867
904
  authentication.check.mockResolvedValueOnce({
868
905
  applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
869
906
  scope: 'user,oauth',
907
+ userId: '2',
870
908
  });
871
909
  oAuth2AccessToken.create.mockResolvedValueOnce({
872
910
  token: 'an_access_token',
873
911
  expiresAt: Date.parse('2010-03-07T00:00:00Z'),
874
912
  });
875
- checkApplication.mockResolvedValueOnce(undefined);
876
913
  checkApplication.mockResolvedValueOnce({
914
+ type: 'implicit',
915
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
916
+ scope: 'user,oauth',
917
+ redirectURI: 'http://redirect.example.com/yolo',
918
+ });
919
+ checkApplication.mockResolvedValueOnce({
920
+ type: 'implicit',
921
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
922
+ scope: 'user,oauth',
877
923
  redirectURI: 'http://redirect.example.com/yolo',
878
924
  });
879
925
 
@@ -902,6 +948,7 @@ describe('OAuth2 server', () => {
902
948
  // Erasing the Date header that may be added by Axios :/
903
949
  date: undefined,
904
950
  },
951
+
905
952
  data,
906
953
  }).toMatchInlineSnapshot(`
907
954
  Object {
@@ -912,7 +959,7 @@ describe('OAuth2 server', () => {
912
959
  "location": "http://redirect.example.com/yolo?a_param=a_value&client_id=acdc41ce-acdc-41ce-acdc-41ceacdc41ce&scope=user&state=xyz&redirect_uri=http%3A%2F%2Fredirect.example.com%2Fyolo&access_token=an_access_token&token_type=bearer&expires_in=86400",
913
960
  "transaction-id": "7",
914
961
  "transfer-encoding": "chunked",
915
- "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\"}",
962
+ "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\",\\"userId\\":\\"2\\"}",
916
963
  },
917
964
  "status": 302,
918
965
  }
@@ -924,8 +971,6 @@ describe('OAuth2 server', () => {
924
971
  oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
925
972
  oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
926
973
  oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
927
- oAuth2ClientCredentialsCheckCalls:
928
- oAuth2ClientCredentials.check.mock.calls,
929
974
  oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
930
975
  oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
931
976
  oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
package/src/index.ts CHANGED
@@ -5,10 +5,10 @@ import initGetOAuth2Authorize, {
5
5
  redirectURIParameter as getOAuth2AuthorizeRedirectURIParameter,
6
6
  scopeParameter as getOAuth2AuthorizeScopeParameter,
7
7
  stateParameter as getOAuth2AuthorizeStateParameter,
8
- } from './handlers/getOAuth2Authorize';
8
+ } from './handlers/getOAuth2Authorize.js';
9
9
  import initPostOAuth2Acknowledge, {
10
10
  definition as postOAuth2AcknowledgeDefinition,
11
- } from './handlers/postOAuth2Acknowledge';
11
+ } from './handlers/postOAuth2Acknowledge.js';
12
12
  import initPostOAuth2Token, {
13
13
  definition as postOAuth2TokenDefinition,
14
14
  authorizationCodeTokenRequestBodySchema as postOAuth2TokenAuthorizationCodeTokenRequestBodySchema,
@@ -16,15 +16,15 @@ import initPostOAuth2Token, {
16
16
  clientCredentialsTokenRequestBodySchema as postOAuth2TokenClientCredentialsTokenRequestBodySchema,
17
17
  refreshTokenRequestBodySchema as postOAuth2TokenRefreshTokenRequestBodySchema,
18
18
  tokenBodySchema as postOAuth2TokenTokenBodySchema,
19
- } from './handlers/postOAuth2Token';
19
+ } from './handlers/postOAuth2Token.js';
20
20
  import initOAuth2Granters, {
21
21
  OAUTH2_ERRORS_DESCRIPTORS,
22
- } from './services/oAuth2Granters';
23
- import initOAuth2ClientCredentialsGranter from './services/oAuth2ClientCredentialsGranter';
24
- import initOAuth2CodeGranter from './services/oAuth2CodeGranter';
25
- import initOAuth2PasswordGranter from './services/oAuth2PasswordGranter';
26
- import initOAuth2RefreshTokenGranter from './services/oAuth2RefreshTokenGranter';
27
- import initOAuth2TokenGranter from './services/oAuth2TokenGranter';
22
+ } from './services/oAuth2Granters.js';
23
+ import initOAuth2ClientCredentialsGranter from './services/oAuth2ClientCredentialsGranter.js';
24
+ import initOAuth2CodeGranter from './services/oAuth2CodeGranter.js';
25
+ import initOAuth2PasswordGranter from './services/oAuth2PasswordGranter.js';
26
+ import initOAuth2RefreshTokenGranter from './services/oAuth2RefreshTokenGranter.js';
27
+ import initOAuth2TokenGranter from './services/oAuth2TokenGranter.js';
28
28
  import type {
29
29
  OAuth2CodeService,
30
30
  OAuth2PasswordService,
@@ -34,27 +34,27 @@ import type {
34
34
  OAuth2GranterService,
35
35
  OAuth2Options,
36
36
  OAuth2Config,
37
- } from './services/oAuth2Granters';
37
+ } from './services/oAuth2Granters.js';
38
38
  import initPostAuthLogin, {
39
39
  definition as postAuthLoginDefinition,
40
- } from './handlers/postAuthLogin';
40
+ } from './handlers/postAuthLogin.js';
41
41
  import initPostAuthLogout, {
42
42
  definition as postAuthLogoutDefinition,
43
- } from './handlers/postAuthLogout';
43
+ } from './handlers/postAuthLogout.js';
44
44
  import initPostAuthRefresh, {
45
45
  authCookieHeaderParameter,
46
46
  definition as postAuthRefreshDefinition,
47
- } from './handlers/postAuthRefresh';
47
+ } from './handlers/postAuthRefresh.js';
48
48
  import initAuthCookiesService, {
49
49
  AUTH_API_PREFIX,
50
- } from './services/authCookies';
50
+ } from './services/authCookies.js';
51
51
  import type {
52
52
  AuthCookiesEnv,
53
53
  AuthCookiesConfig,
54
54
  AuthCookiesService,
55
55
  AuthCookiesData,
56
56
  AuthHandlersConfig,
57
- } from './services/authCookies';
57
+ } from './services/authCookies.js';
58
58
 
59
59
  export type {
60
60
  OAuth2CodeService,
@@ -1,5 +1,5 @@
1
- import initAuthCookies from './authCookies';
2
- import type { AuthCookiesConfig } from './authCookies';
1
+ import initAuthCookies from './authCookies.js';
2
+ import type { AuthCookiesConfig } from './authCookies.js';
3
3
 
4
4
  describe('authCookies', () => {
5
5
  describe('.build()', () => {
@@ -4,6 +4,7 @@ import { autoService } from 'knifecycle';
4
4
  import type { BaseAuthenticationData } from '@whook/authorization';
5
5
 
6
6
  export const AUTH_API_PREFIX = '/auth';
7
+ export const DEFAULT_COOKIES_ENV = {};
7
8
 
8
9
  export type AuthHandlersConfig<
9
10
  AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
@@ -41,7 +42,7 @@ export type AuthCookiesService = {
41
42
  export default autoService(initAuthCookies);
42
43
 
43
44
  async function initAuthCookies({
44
- ENV = {},
45
+ ENV = DEFAULT_COOKIES_ENV,
45
46
  COOKIES,
46
47
  BASE_PATH = '',
47
48
  }: AuthCookiesDependencies): Promise<AuthCookiesService> {
@@ -1,13 +1,13 @@
1
1
  import { autoService } from 'knifecycle';
2
2
  import { noop } from '@whook/whook';
3
- import YError from 'yerror';
3
+ import { YError } from 'yerror';
4
4
  import type { LogService } from 'common-services';
5
5
  import type {
6
6
  OAuth2GranterService,
7
7
  CheckApplicationService,
8
- } from './oAuth2Granters';
8
+ } from './oAuth2Granters.js';
9
9
  import type { BaseAuthenticationData } from '@whook/authorization';
10
- import type { OAuth2Options } from '..';
10
+ import type { OAuth2Options } from '../index.js';
11
11
 
12
12
  export type OAuth2ClientCredentialsGranterDependencies = {
13
13
  OAUTH2: OAuth2Options;
@@ -1,11 +1,29 @@
1
- import initOAuth2CodeGranter from './oAuth2CodeGranter';
1
+ import { jest } from '@jest/globals';
2
+ import { BaseAuthenticationData } from '@whook/authorization';
3
+ import initOAuth2CodeGranter from './oAuth2CodeGranter.js';
4
+ import {
5
+ CheckApplicationService,
6
+ OAuth2CodeService,
7
+ } from './oAuth2Granters.js';
2
8
 
3
9
  describe('OAuth2CodeGranter', () => {
4
10
  const oAuth2Code = {
5
- create: jest.fn(),
6
- check: jest.fn(),
11
+ create:
12
+ jest.fn<
13
+ OAuth2CodeService<
14
+ BaseAuthenticationData<string, string>,
15
+ string
16
+ >['create']
17
+ >(),
18
+ check:
19
+ jest.fn<
20
+ OAuth2CodeService<
21
+ BaseAuthenticationData<string, string>,
22
+ string
23
+ >['check']
24
+ >(),
7
25
  };
8
- const checkApplication = jest.fn();
26
+ const checkApplication = jest.fn<CheckApplicationService>();
9
27
  const log = jest.fn();
10
28
 
11
29
  beforeEach(() => {
@@ -24,9 +42,13 @@ describe('OAuth2CodeGranter', () => {
24
42
 
25
43
  checkApplication.mockResolvedValue({
26
44
  redirectURI: 'https://www.example.com',
45
+ type: 'a_type',
46
+ scope: 'a_scope',
47
+ applicationId: 'abbacaca-abba-caca-abba-cacaabbac0c0',
27
48
  });
28
49
  oAuth2Code.create.mockResolvedValueOnce('yolo');
29
50
  oAuth2Code.check.mockResolvedValueOnce({
51
+ redirectURI: 'https://www.example2.com',
30
52
  applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
31
53
  scope: 'user',
32
54
  });
@@ -76,6 +98,7 @@ describe('OAuth2CodeGranter', () => {
76
98
  },
77
99
  "authenticatorResult": Object {
78
100
  "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
101
+ "redirectURI": "https://www.example2.com",
79
102
  "scope": "user",
80
103
  },
81
104
  "authorizerResult": Object {
@@ -1,12 +1,12 @@
1
1
  import { autoService } from 'knifecycle';
2
2
  import { noop } from '@whook/whook';
3
- import YError from 'yerror';
3
+ import { YError } from 'yerror';
4
4
  import type { LogService } from 'common-services';
5
5
  import type {
6
6
  OAuth2GranterService,
7
7
  OAuth2CodeService,
8
8
  CheckApplicationService,
9
- } from './oAuth2Granters';
9
+ } from './oAuth2Granters.js';
10
10
  import type { BaseAuthenticationData } from '@whook/authorization';
11
11
 
12
12
  export const CODE_GRANTER_TYPE = 'code';