@whook/oauth2 8.5.1 → 10.0.0

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 (133) hide show
  1. package/README.md +106 -70
  2. package/dist/handlers/getOAuth2Authorize.d.ts +8 -6
  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 +3 -3
  8. package/dist/handlers/postAuthLogin.js +83 -120
  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 +4 -4
  18. package/dist/handlers/postAuthRefresh.js +93 -131
  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 +11 -8
  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 +4 -4
  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 +16 -16
  33. package/dist/index.js +14 -229
  34. package/dist/index.js.map +1 -1
  35. package/dist/index.test.js +697 -620
  36. package/dist/index.test.js.map +1 -1
  37. package/dist/services/authCookies.d.ts +8 -4
  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 -64
  49. package/dist/services/oAuth2CodeGranter.test.js.map +1 -1
  50. package/dist/services/oAuth2Granters.d.ts +22 -19
  51. package/dist/services/oAuth2Granters.js +131 -138
  52. package/dist/services/oAuth2Granters.js.map +1 -1
  53. package/dist/services/oAuth2PasswordGranter.d.ts +1 -1
  54. package/dist/services/oAuth2PasswordGranter.js +27 -52
  55. package/dist/services/oAuth2PasswordGranter.js.map +1 -1
  56. package/dist/services/oAuth2RefreshTokenGranter.d.ts +1 -1
  57. package/dist/services/oAuth2RefreshTokenGranter.js +34 -58
  58. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
  59. package/dist/services/oAuth2TokenGranter.d.ts +1 -1
  60. package/dist/services/oAuth2TokenGranter.js +50 -86
  61. package/dist/services/oAuth2TokenGranter.js.map +1 -1
  62. package/package.json +54 -96
  63. package/src/__snapshots__/index.test.ts.snap +23 -22
  64. package/src/handlers/__snapshots__/postOAuth2Acknowledge.test.ts.snap +1 -1
  65. package/src/handlers/getOAuth2Authorize.test.ts +49 -13
  66. package/src/handlers/getOAuth2Authorize.ts +17 -16
  67. package/src/handlers/postAuthLogin.test.ts +9 -5
  68. package/src/handlers/postAuthLogin.ts +8 -7
  69. package/src/handlers/postAuthLogout.test.ts +8 -4
  70. package/src/handlers/postAuthLogout.ts +2 -2
  71. package/src/handlers/postAuthRefresh.test.ts +10 -6
  72. package/src/handlers/postAuthRefresh.ts +8 -7
  73. package/src/handlers/postOAuth2Acknowledge.test.ts +83 -35
  74. package/src/handlers/postOAuth2Acknowledge.ts +48 -30
  75. package/src/handlers/postOAuth2Token.test.ts +79 -19
  76. package/src/handlers/postOAuth2Token.ts +7 -7
  77. package/src/index.test.ts +116 -72
  78. package/src/index.ts +21 -19
  79. package/src/services/authCookies.test.ts +2 -2
  80. package/src/services/authCookies.ts +10 -5
  81. package/src/services/oAuth2ClientCredentialsGranter.ts +28 -24
  82. package/src/services/oAuth2CodeGranter.test.ts +42 -18
  83. package/src/services/oAuth2CodeGranter.ts +68 -62
  84. package/src/services/oAuth2Granters.ts +59 -29
  85. package/src/services/oAuth2PasswordGranter.ts +25 -24
  86. package/src/services/oAuth2RefreshTokenGranter.ts +30 -26
  87. package/src/services/oAuth2TokenGranter.ts +45 -43
  88. package/dist/handlers/getOAuth2Authorize.mjs +0 -160
  89. package/dist/handlers/getOAuth2Authorize.mjs.map +0 -1
  90. package/dist/handlers/getOAuth2Authorize.test.mjs +0 -114
  91. package/dist/handlers/getOAuth2Authorize.test.mjs.map +0 -1
  92. package/dist/handlers/postAuthLogin.mjs +0 -108
  93. package/dist/handlers/postAuthLogin.mjs.map +0 -1
  94. package/dist/handlers/postAuthLogin.test.mjs +0 -85
  95. package/dist/handlers/postAuthLogin.test.mjs.map +0 -1
  96. package/dist/handlers/postAuthLogout.mjs +0 -33
  97. package/dist/handlers/postAuthLogout.mjs.map +0 -1
  98. package/dist/handlers/postAuthLogout.test.mjs +0 -30
  99. package/dist/handlers/postAuthLogout.test.mjs.map +0 -1
  100. package/dist/handlers/postAuthRefresh.mjs +0 -121
  101. package/dist/handlers/postAuthRefresh.mjs.map +0 -1
  102. package/dist/handlers/postAuthRefresh.test.mjs +0 -94
  103. package/dist/handlers/postAuthRefresh.test.mjs.map +0 -1
  104. package/dist/handlers/postOAuth2Acknowledge.mjs +0 -150
  105. package/dist/handlers/postOAuth2Acknowledge.mjs.map +0 -1
  106. package/dist/handlers/postOAuth2Acknowledge.test.mjs +0 -140
  107. package/dist/handlers/postOAuth2Acknowledge.test.mjs.map +0 -1
  108. package/dist/handlers/postOAuth2Token.mjs +0 -231
  109. package/dist/handlers/postOAuth2Token.mjs.map +0 -1
  110. package/dist/handlers/postOAuth2Token.test.mjs +0 -160
  111. package/dist/handlers/postOAuth2Token.test.mjs.map +0 -1
  112. package/dist/index.mjs +0 -15
  113. package/dist/index.mjs.map +0 -1
  114. package/dist/index.test.mjs +0 -748
  115. package/dist/index.test.mjs.map +0 -1
  116. package/dist/services/authCookies.mjs +0 -58
  117. package/dist/services/authCookies.mjs.map +0 -1
  118. package/dist/services/authCookies.test.mjs +0 -77
  119. package/dist/services/authCookies.test.mjs.map +0 -1
  120. package/dist/services/oAuth2ClientCredentialsGranter.mjs +0 -41
  121. package/dist/services/oAuth2ClientCredentialsGranter.mjs.map +0 -1
  122. package/dist/services/oAuth2CodeGranter.mjs +0 -98
  123. package/dist/services/oAuth2CodeGranter.mjs.map +0 -1
  124. package/dist/services/oAuth2CodeGranter.test.mjs +0 -81
  125. package/dist/services/oAuth2CodeGranter.test.mjs.map +0 -1
  126. package/dist/services/oAuth2Granters.mjs +0 -127
  127. package/dist/services/oAuth2Granters.mjs.map +0 -1
  128. package/dist/services/oAuth2PasswordGranter.mjs +0 -40
  129. package/dist/services/oAuth2PasswordGranter.mjs.map +0 -1
  130. package/dist/services/oAuth2RefreshTokenGranter.mjs +0 -47
  131. package/dist/services/oAuth2RefreshTokenGranter.mjs.map +0 -1
  132. package/dist/services/oAuth2TokenGranter.mjs +0 -78
  133. package/dist/services/oAuth2TokenGranter.mjs.map +0 -1
@@ -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;
@@ -39,30 +39,34 @@ async function initOAuth2ClientCredentialsGranter({
39
39
  checkApplication,
40
40
  log = noop,
41
41
  }: OAuth2ClientCredentialsGranterDependencies): Promise<OAuth2ClientCredentialsGranterService> {
42
- const authenticateWithClientCredentials: OAuth2ClientCredentialsGranterService['authenticator']['authenticate'] =
43
- async ({ scope: demandedScope = '' }, authenticationData) => {
44
- // The client must be authenticated
45
- if (!authenticationData) {
46
- throw new YError('E_UNAUTHORIZED');
47
- }
42
+ const authenticateWithClientCredentials: NonNullable<
43
+ OAuth2ClientCredentialsGranterService['authenticator']
44
+ >['authenticate'] = async (
45
+ { scope: demandedScope = '' },
46
+ authenticationData,
47
+ ) => {
48
+ // The client must be authenticated
49
+ if (!authenticationData) {
50
+ throw new YError('E_UNAUTHORIZED');
51
+ }
48
52
 
49
- // Checking the scope and availability of the demanded
50
- // grant type
51
- await checkApplication({
52
- applicationId: authenticationData.applicationId,
53
- type: 'client_credentials',
54
- scope: demandedScope,
55
- });
53
+ // Checking the scope and availability of the demanded
54
+ // grant type
55
+ await checkApplication({
56
+ applicationId: authenticationData.applicationId,
57
+ type: 'client_credentials',
58
+ scope: demandedScope,
59
+ });
56
60
 
57
- return {
58
- ...authenticationData,
59
- scope: demandedScope
60
- ? demandedScope
61
- : OAUTH2.defaultToClientScope
62
- ? authenticationData.scope
63
- : '',
64
- };
61
+ return {
62
+ ...authenticationData,
63
+ scope: demandedScope
64
+ ? demandedScope
65
+ : OAUTH2.defaultToClientScope
66
+ ? authenticationData.scope
67
+ : '',
65
68
  };
69
+ };
66
70
 
67
71
  log('debug', '👫 - OAuth2ClientCredentialsGranter Service Initialized!');
68
72
 
@@ -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,32 +42,37 @@ 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
  });
33
55
 
34
- const authorizerResult = await oAuth2CodeGranter.authorizer.authorize({
56
+ const authorizerResult = await oAuth2CodeGranter.authorizer?.authorize({
35
57
  clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
36
58
  redirectURI: 'https://www.example.com/oauth2/code',
37
59
  scope: 'user',
38
60
  });
39
- const acknowledgerResult = await oAuth2CodeGranter.acknowledger.acknowledge(
40
- {
41
- applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
42
- scope: 'user, admin',
43
- },
44
- {
45
- clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
46
- redirectURI: 'https://www.example.com/oauth2/code',
47
- scope: 'user',
48
- },
49
- {},
50
- );
61
+ const acknowledgerResult =
62
+ await oAuth2CodeGranter.acknowledger?.acknowledge(
63
+ {
64
+ applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
65
+ scope: 'user, admin',
66
+ },
67
+ {
68
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
69
+ redirectURI: 'https://www.example.com/oauth2/code',
70
+ scope: 'user',
71
+ },
72
+ {},
73
+ );
51
74
  const authenticatorResult =
52
- await oAuth2CodeGranter.authenticator.authenticate(
75
+ await oAuth2CodeGranter.authenticator?.authenticate(
53
76
  {
54
77
  clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
55
78
  redirectURI: 'https://www.example.com/oauth2/code',
@@ -75,6 +98,7 @@ describe('OAuth2CodeGranter', () => {
75
98
  },
76
99
  "authenticatorResult": Object {
77
100
  "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
101
+ "redirectURI": "https://www.example2.com",
78
102
  "scope": "user",
79
103
  },
80
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';
@@ -39,79 +39,85 @@ async function initOAuth2CodeGranter({
39
39
  checkApplication,
40
40
  log = noop,
41
41
  }: OAuth2CodeGranterDependencies): Promise<OAuth2CodeGranterService> {
42
- const authorizeWithCode: OAuth2CodeGranterService['authorizer']['authorize'] =
43
- async ({ clientId, redirectURI, scope = '' }) => {
44
- const { redirectURI: finalRedirectURI } = await checkApplication({
45
- applicationId: clientId,
46
- type: CODE_GRANTER_TYPE,
47
- redirectURI,
48
- scope,
49
- });
42
+ const authorizeWithCode: NonNullable<
43
+ OAuth2CodeGranterService['authorizer']
44
+ >['authorize'] = async ({ clientId, redirectURI, scope = '' }) => {
45
+ const { redirectURI: finalRedirectURI } = await checkApplication({
46
+ applicationId: clientId,
47
+ type: CODE_GRANTER_TYPE,
48
+ redirectURI,
49
+ scope,
50
+ });
50
51
 
51
- return {
52
- applicationId: clientId,
53
- redirectURI: finalRedirectURI,
54
- scope,
55
- };
52
+ return {
53
+ applicationId: clientId,
54
+ redirectURI: finalRedirectURI,
55
+ scope,
56
56
  };
57
+ };
57
58
 
58
59
  // Authorization Code Response:
59
60
  // https://tools.ietf.org/html/rfc6749#section-4.1.2
60
- const acknowledgeWithCode: OAuth2CodeGranterService['acknowledger']['acknowledge'] =
61
- async (
62
- authenticationData,
63
- { clientId, redirectURI, scope },
61
+ const acknowledgeWithCode: NonNullable<
62
+ OAuth2CodeGranterService['acknowledger']
63
+ >['acknowledge'] = async (
64
+ authenticationData,
65
+ { clientId, redirectURI, scope },
66
+ additionalParameters,
67
+ ) => {
68
+ const code = await oAuth2Code.create(
69
+ { ...authenticationData, applicationId: clientId, scope },
70
+ redirectURI,
64
71
  additionalParameters,
65
- ) => {
66
- const code = await oAuth2Code.create(
67
- { ...authenticationData, applicationId: clientId, scope },
68
- redirectURI,
69
- additionalParameters,
70
- );
72
+ );
71
73
 
72
- return {
73
- applicationId: clientId,
74
- redirectURI,
75
- scope,
76
- code,
77
- };
74
+ return {
75
+ applicationId: clientId,
76
+ redirectURI,
77
+ scope,
78
+ code,
78
79
  };
80
+ };
79
81
 
80
- const authenticateWithCode: OAuth2CodeGranterService['authenticator']['authenticate'] =
81
- async ({ code, clientId, redirectURI }, authenticationData) => {
82
- // The client must be authenticated (for now, see below)
83
- if (!authenticationData) {
84
- throw new YError('E_UNAUTHORIZED');
85
- }
82
+ const authenticateWithCode: NonNullable<
83
+ OAuth2CodeGranterService['authenticator']
84
+ >['authenticate'] = async (
85
+ { code, clientId, redirectURI },
86
+ authenticationData,
87
+ ) => {
88
+ // The client must be authenticated (for now, see below)
89
+ if (!authenticationData) {
90
+ throw new YError('E_UNAUTHORIZED');
91
+ }
86
92
 
87
- // This check is not really necessary atm but it acts
88
- // as a reminder that this grant type could be used
89
- // without authenticating the client. In this
90
- // scenario, the authenticationData should be deducted
91
- // from the clientId and the code given in parameters
92
- // See https://tools.ietf.org/html/rfc6749#section-4.1.3
93
- if (clientId && clientId !== authenticationData.applicationId) {
94
- throw new YError(
95
- 'E_UNAUTHORIZED',
96
- clientId,
97
- authenticationData.applicationId,
98
- );
99
- }
93
+ // This check is not really necessary atm but it acts
94
+ // as a reminder that this grant type could be used
95
+ // without authenticating the client. In this
96
+ // scenario, the authenticationData should be deducted
97
+ // from the clientId and the code given in parameters
98
+ // See https://tools.ietf.org/html/rfc6749#section-4.1.3
99
+ if (clientId && clientId !== authenticationData.applicationId) {
100
+ throw new YError(
101
+ 'E_UNAUTHORIZED',
102
+ clientId,
103
+ authenticationData.applicationId,
104
+ );
105
+ }
100
106
 
101
- await checkApplication({
102
- applicationId: authenticationData.applicationId,
103
- type: CODE_GRANTER_TYPE,
104
- scope: '',
105
- });
107
+ await checkApplication({
108
+ applicationId: authenticationData.applicationId,
109
+ type: CODE_GRANTER_TYPE,
110
+ scope: '',
111
+ });
106
112
 
107
- const newAuthenticationData = await oAuth2Code.check(
108
- authenticationData,
109
- code,
110
- redirectURI,
111
- );
113
+ const newAuthenticationData = await oAuth2Code.check(
114
+ authenticationData,
115
+ code,
116
+ redirectURI,
117
+ );
112
118
 
113
- return newAuthenticationData;
114
- };
119
+ return newAuthenticationData;
120
+ };
115
121
 
116
122
  log('debug', '👫 - OAuth2CodeGranter Service Initialized!');
117
123
 
@@ -193,6 +193,54 @@ export type CheckApplicationService = {
193
193
  }>;
194
194
  };
195
195
 
196
+ export type OAuth2GranterAuthorize<
197
+ AUTHORIZE_PARAMETERS extends Record<string, unknown> = Record<
198
+ string,
199
+ unknown
200
+ >,
201
+ AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
202
+ > = (
203
+ context: {
204
+ clientId: AUTHENTICATION_DATA['applicationId'];
205
+ redirectURI: string;
206
+ scope: AUTHENTICATION_DATA['scope'];
207
+ },
208
+ authorizeParameters?: AUTHORIZE_PARAMETERS,
209
+ ) => Promise<{
210
+ applicationId: AUTHENTICATION_DATA['applicationId'];
211
+ redirectURI: string;
212
+ scope: AUTHENTICATION_DATA['scope'];
213
+ }>;
214
+
215
+ export type Oauth2GranterAuthenticate<
216
+ GRANT_PARAMETERS extends Record<string, unknown> = Record<string, unknown>,
217
+ AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
218
+ > = (
219
+ grantParameters: GRANT_PARAMETERS,
220
+ authenticationData: AUTHENTICATION_DATA,
221
+ ) => Promise<AUTHENTICATION_DATA>;
222
+
223
+ export type OAuth2GranterAcknowledge<
224
+ ACKNOWLEDGE_PARAMETERS extends Record<string, unknown> = Record<
225
+ string,
226
+ unknown
227
+ >,
228
+ AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
229
+ > = (
230
+ authenticationData: AUTHENTICATION_DATA,
231
+ acknowledgeParameters: {
232
+ clientId: AUTHENTICATION_DATA['applicationId'];
233
+ redirectURI: string;
234
+ scope: AUTHENTICATION_DATA['applicationId'];
235
+ },
236
+ additionalParameters: ACKNOWLEDGE_PARAMETERS,
237
+ ) => Promise<
238
+ AUTHENTICATION_DATA & {
239
+ redirectURI: string;
240
+ [name: string]: unknown;
241
+ }
242
+ >;
243
+
196
244
  export type OAuth2GranterService<
197
245
  AUTHORIZE_PARAMETERS extends Record<string, unknown> = Record<
198
246
  string,
@@ -208,42 +256,24 @@ export type OAuth2GranterService<
208
256
  type: string;
209
257
  authorizer?: {
210
258
  responseType: string;
211
- authorize: (
212
- context: {
213
- clientId: AUTHENTICATION_DATA['applicationId'];
214
- redirectURI: string;
215
- scope: AUTHENTICATION_DATA['scope'];
216
- },
217
- authorizeParameters?: AUTHORIZE_PARAMETERS,
218
- ) => Promise<{
219
- applicationId: AUTHENTICATION_DATA['applicationId'];
220
- redirectURI: string;
221
- scope: AUTHENTICATION_DATA['scope'];
222
- }>;
259
+ authorize: OAuth2GranterAuthorize<
260
+ AUTHORIZE_PARAMETERS,
261
+ AUTHENTICATION_DATA
262
+ >;
223
263
  };
224
264
  acknowledger?: {
225
265
  acknowledgmentType: string;
226
- acknowledge: (
227
- authenticationData: AUTHENTICATION_DATA,
228
- acknowledgeParameters: {
229
- clientId: AUTHENTICATION_DATA['applicationId'];
230
- redirectURI: string;
231
- scope: AUTHENTICATION_DATA['applicationId'];
232
- },
233
- additionalParameters: ACKNOWLEDGE_PARAMETERS,
234
- ) => Promise<
235
- AUTHENTICATION_DATA & {
236
- redirectURI: string;
237
- [name: string]: unknown;
238
- }
266
+ acknowledge: OAuth2GranterAcknowledge<
267
+ ACKNOWLEDGE_PARAMETERS,
268
+ AUTHENTICATION_DATA
239
269
  >;
240
270
  };
241
271
  authenticator?: {
242
272
  grantType: string;
243
- authenticate: (
244
- grantParameters: GRANT_PARAMETERS,
245
- authenticationData: AUTHENTICATION_DATA,
246
- ) => Promise<AUTHENTICATION_DATA>;
273
+ authenticate: Oauth2GranterAuthenticate<
274
+ GRANT_PARAMETERS,
275
+ AUTHENTICATION_DATA
276
+ >;
247
277
  };
248
278
  };
249
279
 
@@ -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
  OAuth2PasswordService,
8
8
  CheckApplicationService,
9
- } from './oAuth2Granters';
9
+ } from './oAuth2Granters.js';
10
10
  import type { BaseAuthenticationData } from '@whook/authorization';
11
11
 
12
12
  export type OAuth2PasswordGranterDependencies = {
@@ -37,31 +37,32 @@ async function initOAuth2PasswordGranter({
37
37
  checkApplication,
38
38
  log = noop,
39
39
  }: OAuth2PasswordGranterDependencies): Promise<OAuth2PasswordGranterService> {
40
- const authenticateWithPassword: OAuth2PasswordGranterService['authenticator']['authenticate'] =
41
- async (
42
- { username, password, scope: demandedScope = '' },
43
- authenticationData,
44
- ) => {
45
- // The client must be authenticated
46
- if (!authenticationData) {
47
- throw new YError('E_UNAUTHORIZED');
48
- }
40
+ const authenticateWithPassword: NonNullable<
41
+ OAuth2PasswordGranterService['authenticator']
42
+ >['authenticate'] = async (
43
+ { username, password, scope: demandedScope = '' },
44
+ authenticationData,
45
+ ) => {
46
+ // The client must be authenticated
47
+ if (!authenticationData) {
48
+ throw new YError('E_UNAUTHORIZED');
49
+ }
49
50
 
50
- await checkApplication({
51
- applicationId: authenticationData.applicationId,
52
- type: 'password',
53
- scope: demandedScope,
54
- });
51
+ await checkApplication({
52
+ applicationId: authenticationData.applicationId,
53
+ type: 'password',
54
+ scope: demandedScope,
55
+ });
55
56
 
56
- const finalAuthenticationData = await oAuth2Password.check(
57
- authenticationData,
58
- username,
59
- password,
60
- demandedScope,
61
- );
57
+ const finalAuthenticationData = await oAuth2Password.check(
58
+ authenticationData,
59
+ username,
60
+ password,
61
+ demandedScope,
62
+ );
62
63
 
63
- return finalAuthenticationData;
64
- };
64
+ return finalAuthenticationData;
65
+ };
65
66
 
66
67
  log('debug', '👫 - OAuth2PasswordGranter Service Initialized!');
67
68
 
@@ -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
  CheckApplicationService,
8
8
  OAuth2RefreshTokenService,
9
- } from './oAuth2Granters';
9
+ } from './oAuth2Granters.js';
10
10
  import type { BaseAuthenticationData } from '@whook/authorization';
11
11
 
12
12
  export type OAuth2RefreshTokenGranterDependencies = {
@@ -36,34 +36,38 @@ async function initOAuth2RefreshTokenGranter({
36
36
  oAuth2RefreshToken,
37
37
  log = noop,
38
38
  }: OAuth2RefreshTokenGranterDependencies): Promise<OAuth2RefreshTokenGranterService> {
39
- const authenticateWithRefreshToken: OAuth2RefreshTokenGranterService['authenticator']['authenticate'] =
40
- async ({ refreshToken, scope: demandedScope }, authenticationData) => {
41
- try {
42
- // The client must be authenticated
43
- if (!authenticationData) {
44
- throw new YError('E_UNAUTHORIZED');
45
- }
39
+ const authenticateWithRefreshToken: NonNullable<
40
+ OAuth2RefreshTokenGranterService['authenticator']
41
+ >['authenticate'] = async (
42
+ { refreshToken, scope: demandedScope = '' },
43
+ authenticationData,
44
+ ) => {
45
+ try {
46
+ // The client must be authenticated
47
+ if (!authenticationData) {
48
+ throw new YError('E_UNAUTHORIZED');
49
+ }
46
50
 
47
- await checkApplication({
48
- applicationId: authenticationData.applicationId,
49
- type: 'refresh',
50
- scope: demandedScope,
51
- });
51
+ await checkApplication({
52
+ applicationId: authenticationData.applicationId,
53
+ type: 'refresh',
54
+ scope: demandedScope,
55
+ });
52
56
 
53
- const newAuthenticationData = await oAuth2RefreshToken.check(
54
- authenticationData,
55
- refreshToken,
56
- demandedScope,
57
- );
57
+ const newAuthenticationData = await oAuth2RefreshToken.check(
58
+ authenticationData,
59
+ refreshToken,
60
+ demandedScope,
61
+ );
58
62
 
59
- return newAuthenticationData;
60
- } catch (err) {
61
- if (err.code === 'E_BAD_TOKEN') {
62
- throw YError.wrap(err, 'E_BAD_REFRESH_TOKEN');
63
- }
64
- throw err;
63
+ return newAuthenticationData;
64
+ } catch (err) {
65
+ if ((err as YError).code === 'E_BAD_TOKEN') {
66
+ throw YError.wrap(err as Error, 'E_BAD_REFRESH_TOKEN');
65
67
  }
66
- };
68
+ throw err;
69
+ }
70
+ };
67
71
 
68
72
  log('debug', '👫 - OAuth2RefreshTokenGranter Service Initialized!');
69
73