@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,8 +1,12 @@
1
- import initGetOAuth2Authorize from './getOAuth2Authorize';
1
+ import { jest } from '@jest/globals';
2
+ import initGetOAuth2Authorize from './getOAuth2Authorize.js';
2
3
  import { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';
3
- import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters';
4
- import YError from 'yerror';
5
- import type { OAuth2Options, OAuth2GranterService } from '..';
4
+ import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters.js';
5
+ import { YError } from 'yerror';
6
+ import type { OAuth2CodeGranterService } from '../services/oAuth2CodeGranter.js';
7
+ import type { OAuth2TokenGranterService } from '../services/oAuth2TokenGranter.js';
8
+ import type { LogService } from 'common-services';
9
+ import type { OAuth2Options, OAuth2GranterService } from '../index.js';
6
10
 
7
11
  describe('getOAuth2Authorize', () => {
8
12
  const OAUTH2: OAuth2Options = {
@@ -12,22 +16,54 @@ describe('getOAuth2Authorize', () => {
12
16
  ...DEFAULT_ERRORS_DESCRIPTORS,
13
17
  ...OAUTH2_ERRORS_DESCRIPTORS,
14
18
  };
15
- const log = jest.fn();
19
+ const log = jest.fn<LogService>();
16
20
  const codeGranter = {
17
21
  type: 'code',
18
- authorizer: { responseType: 'code', authorize: jest.fn() },
19
- acknowledger: { acknowledgmentType: 'code', acknowledge: jest.fn() },
22
+ authorizer: {
23
+ responseType: 'code',
24
+ authorize:
25
+ jest.fn<
26
+ NonNullable<OAuth2CodeGranterService['authorizer']>['authorize']
27
+ >(),
28
+ },
29
+ acknowledger: {
30
+ acknowledgmentType: 'code',
31
+ acknowledge:
32
+ jest.fn<
33
+ NonNullable<OAuth2CodeGranterService['acknowledger']>['acknowledge']
34
+ >(),
35
+ },
20
36
  authenticator: {
21
37
  grantType: 'authorization_code',
22
- authenticate: jest.fn(),
38
+ authenticate:
39
+ jest.fn<
40
+ NonNullable<OAuth2CodeGranterService['authenticator']>['authenticate']
41
+ >(),
23
42
  },
24
43
  };
25
44
  const tokenGranter = {
26
45
  type: 'token',
27
- authorizer: { responseType: 'token', authorize: jest.fn() },
28
- acknowledger: { acknowledgmentType: 'token', acknowledge: jest.fn() },
46
+ authorizer: {
47
+ responseType: 'token',
48
+ authorize:
49
+ jest.fn<
50
+ NonNullable<
51
+ NonNullable<OAuth2TokenGranterService['authorizer']>['authorize']
52
+ >
53
+ >(),
54
+ },
55
+ acknowledger: {
56
+ acknowledgmentType: 'token',
57
+ acknowledge:
58
+ jest.fn<
59
+ NonNullable<OAuth2TokenGranterService['acknowledger']>['acknowledge']
60
+ >(),
61
+ },
29
62
  };
30
- const oAuth2Granters: OAuth2GranterService[] = [codeGranter, tokenGranter];
63
+ const oAuth2Granters = [
64
+ codeGranter,
65
+ tokenGranter,
66
+ ] as unknown as OAuth2GranterService[];
31
67
 
32
68
  beforeEach(() => {
33
69
  log.mockReset();
@@ -46,7 +82,7 @@ describe('getOAuth2Authorize', () => {
46
82
  codeGranter.authenticator.authenticate,
47
83
  tokenGranter.authorizer.authorize,
48
84
  tokenGranter.acknowledger.acknowledge,
49
- ].forEach((mock) =>
85
+ ].forEach((mock: any) =>
50
86
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
51
87
  );
52
88
  codeGranter.authorizer.authorize.mockResolvedValueOnce({
@@ -99,7 +135,7 @@ describe('getOAuth2Authorize', () => {
99
135
  codeGranter.authenticator.authenticate,
100
136
  tokenGranter.authorizer.authorize,
101
137
  tokenGranter.acknowledger.acknowledge,
102
- ].forEach((mock) =>
138
+ ].forEach((mock: any) =>
103
139
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
104
140
  );
105
141
 
@@ -1,6 +1,6 @@
1
1
  import { autoHandler } from 'knifecycle';
2
2
  import camelCase from 'camelcase';
3
- import YError from 'yerror';
3
+ import { YError } from 'yerror';
4
4
  import { refersTo } from '@whook/whook';
5
5
  import type {
6
6
  WhookAPIHandlerDefinition,
@@ -12,7 +12,7 @@ import type {
12
12
  import type {
13
13
  OAuth2Options,
14
14
  OAuth2GranterService,
15
- } from '../services/oAuth2Granters';
15
+ } from '../services/oAuth2Granters.js';
16
16
  import type { LogService } from 'common-services';
17
17
 
18
18
  /* Architecture Note #1: OAuth2 authorize
@@ -132,7 +132,7 @@ async function getOAuth2Authorize(
132
132
  scope?: string;
133
133
  state: string;
134
134
  } & Record<string, unknown>,
135
- ): Promise<WhookResponse> {
135
+ ): Promise<WhookResponse<302, { location: string }>> {
136
136
  const url = new URL(OAUTH2.authenticateURL);
137
137
 
138
138
  try {
@@ -145,29 +145,30 @@ async function getOAuth2Authorize(
145
145
  throw new YError('E_UNKNOWN_AUTHORIZER_TYPE', responseType);
146
146
  }
147
147
 
148
- const { applicationId, redirectURI, scope } =
149
- await granter.authorizer.authorize(
150
- {
151
- clientId,
152
- redirectURI: demandedRedirectURI,
153
- scope: demandedScope,
154
- },
155
- camelCaseObjectProperties(authorizeParameters),
156
- );
148
+ const { applicationId, redirectURI, scope } = await (
149
+ granter.authorizer as NonNullable<OAuth2GranterService['authorizer']>
150
+ ).authorize(
151
+ {
152
+ clientId,
153
+ redirectURI: demandedRedirectURI,
154
+ scope: demandedScope,
155
+ },
156
+ camelCaseObjectProperties(authorizeParameters),
157
+ );
157
158
 
158
159
  url.searchParams.set('type', responseType);
159
160
  url.searchParams.set('redirect_uri', redirectURI);
160
161
  url.searchParams.set('scope', scope);
161
162
  url.searchParams.set('client_id', applicationId);
162
163
  } catch (err) {
163
- log('debug', '👫 - OAuth2 initialization error', err.code);
164
- log('debug-stack', err.stack);
164
+ log('debug', '👫 - OAuth2 initialization error', (err as YError).code);
165
+ log('debug-stack', (err as Error).stack || 'no_stack_trace');
165
166
 
166
167
  url.searchParams.set('redirect_uri', demandedRedirectURI);
167
168
  setURLError(
168
169
  url,
169
- err,
170
- ERRORS_DESCRIPTORS[err.code] || ERRORS_DESCRIPTORS.E_OAUTH2,
170
+ err as YError,
171
+ ERRORS_DESCRIPTORS[(err as YError).code] || ERRORS_DESCRIPTORS.E_OAUTH2,
171
172
  );
172
173
  }
173
174
 
@@ -1,11 +1,13 @@
1
- import initPostAuthLogin from './postAuthLogin';
1
+ import { jest } from '@jest/globals';
2
+ import initPostAuthLogin from './postAuthLogin.js';
3
+ import type { AuthCookiesService } from '../services/authCookies.js';
2
4
 
3
5
  describe('postAuthLogin', () => {
4
6
  test('should work', async () => {
5
7
  const authCookies = {
6
- build: jest.fn(),
8
+ build: jest.fn<AuthCookiesService['build']>(),
7
9
  };
8
- const postOAuth2Token = jest.fn();
10
+ const postOAuth2Token = jest.fn<any>();
9
11
  const postAuthLogin = await initPostAuthLogin({
10
12
  ROOT_AUTHENTICATION_DATA: {
11
13
  scope: 'user',
@@ -15,7 +17,7 @@ describe('postAuthLogin', () => {
15
17
  postOAuth2Token,
16
18
  });
17
19
 
18
- authCookies.build.mockReturnValueOnce('the_build_cookies');
20
+ authCookies.build.mockReturnValueOnce(['the_build_cookies']);
19
21
  postOAuth2Token.mockResolvedValueOnce({
20
22
  status: 200,
21
23
  body: {
@@ -78,7 +80,9 @@ describe('postAuthLogin', () => {
78
80
  "token_type": "bearer",
79
81
  },
80
82
  "headers": Object {
81
- "Set-Cookie": "the_build_cookies",
83
+ "Set-Cookie": Array [
84
+ "the_build_cookies",
85
+ ],
82
86
  },
83
87
  "status": 200,
84
88
  },
@@ -1,12 +1,12 @@
1
1
  import { autoHandler } from 'knifecycle';
2
- import YHTTPError from 'yhttperror';
3
- import initPostOauth2Token from './postOAuth2Token';
4
- import { AUTH_API_PREFIX } from '../services/authCookies';
2
+ import { YHTTPError } from 'yhttperror';
3
+ import initPostOauth2Token from './postOAuth2Token.js';
4
+ import { AUTH_API_PREFIX } from '../services/authCookies.js';
5
5
  import type { WhookAPIHandlerDefinition } from '@whook/whook';
6
6
  import type {
7
7
  AuthCookiesService,
8
8
  AuthHandlersConfig,
9
- } from '../services/authCookies';
9
+ } from '../services/authCookies.js';
10
10
  import type { AsyncReturnType } from 'type-fest';
11
11
  import type { BaseAuthenticationData } from '@whook/authorization';
12
12
 
@@ -111,11 +111,12 @@ async function postAuthLogin<
111
111
  },
112
112
  };
113
113
  } catch (err) {
114
- const newErr = YHTTPError.wrap(err);
114
+ const newErr = YHTTPError.wrap(err as Error);
115
115
 
116
116
  newErr.headers = {
117
- ...(err.headers || {}),
118
- 'Set-Cookie': authCookies.build(),
117
+ ...((err as YHTTPError).headers || {}),
118
+ // TODO: Allow string[] in YHTTPError
119
+ 'Set-Cookie': authCookies.build() as unknown as string,
119
120
  };
120
121
 
121
122
  throw newErr;
@@ -1,15 +1,17 @@
1
- import initPostAuthLogout from './postAuthLogout';
1
+ import { jest } from '@jest/globals';
2
+ import initPostAuthLogout from './postAuthLogout.js';
3
+ import type { AuthCookiesService } from '../services/authCookies.js';
2
4
 
3
5
  describe('postAuthLogout', () => {
4
6
  test('should work', async () => {
5
7
  const authCookies = {
6
- build: jest.fn(),
8
+ build: jest.fn<AuthCookiesService['build']>(),
7
9
  };
8
10
  const postAuthLogout = await initPostAuthLogout({
9
11
  authCookies,
10
12
  });
11
13
 
12
- authCookies.build.mockReturnValueOnce('the_build_cookies');
14
+ authCookies.build.mockReturnValueOnce(['the_build_cookies']);
13
15
 
14
16
  const response = await postAuthLogout({});
15
17
 
@@ -23,7 +25,9 @@ describe('postAuthLogout', () => {
23
25
  ],
24
26
  "response": Object {
25
27
  "headers": Object {
26
- "Set-Cookie": "the_build_cookies",
28
+ "Set-Cookie": Array [
29
+ "the_build_cookies",
30
+ ],
27
31
  },
28
32
  "status": 204,
29
33
  },
@@ -1,7 +1,7 @@
1
1
  import { autoHandler } from 'knifecycle';
2
- import { AUTH_API_PREFIX } from '../services/authCookies';
2
+ import { AUTH_API_PREFIX } from '../services/authCookies.js';
3
3
  import type { WhookAPIHandlerDefinition } from '@whook/whook';
4
- import type { AuthCookiesService } from '../services/authCookies';
4
+ import type { AuthCookiesService } from '../services/authCookies.js';
5
5
 
6
6
  export const definition: WhookAPIHandlerDefinition = {
7
7
  method: 'post',
@@ -1,12 +1,14 @@
1
- import initPostAuthRefresh from './postAuthRefresh';
1
+ import { jest } from '@jest/globals';
2
+ import initPostAuthRefresh from './postAuthRefresh.js';
3
+ import type { AuthCookiesService } from '../services/authCookies.js';
2
4
 
3
5
  describe('postAuthRefresh', () => {
4
6
  test('should work', async () => {
5
7
  const authCookies = {
6
- parse: jest.fn(),
7
- build: jest.fn(),
8
+ parse: jest.fn<AuthCookiesService['parse']>(),
9
+ build: jest.fn<AuthCookiesService['build']>(),
8
10
  };
9
- const postOAuth2Token = jest.fn();
11
+ const postOAuth2Token = jest.fn<any>();
10
12
  const postAuthRefresh = await initPostAuthRefresh({
11
13
  ROOT_AUTHENTICATION_DATA: {
12
14
  scope: 'user',
@@ -20,7 +22,7 @@ describe('postAuthRefresh', () => {
20
22
  access_token: 'an_access_token',
21
23
  refresh_token: 'a_refresh_token',
22
24
  });
23
- authCookies.build.mockReturnValueOnce('the_build_cookies');
25
+ authCookies.build.mockReturnValueOnce(['the_build_cookies']);
24
26
  postOAuth2Token.mockResolvedValueOnce({
25
27
  status: 200,
26
28
  body: {
@@ -87,7 +89,9 @@ describe('postAuthRefresh', () => {
87
89
  "token_type": "bearer",
88
90
  },
89
91
  "headers": Object {
90
- "Set-Cookie": "the_build_cookies",
92
+ "Set-Cookie": Array [
93
+ "the_build_cookies",
94
+ ],
91
95
  },
92
96
  "status": 200,
93
97
  },
@@ -1,8 +1,8 @@
1
1
  import { autoHandler } from 'knifecycle';
2
- import YHTTPError from 'yhttperror';
2
+ import { YHTTPError } from 'yhttperror';
3
3
  import { refersTo } from '@whook/whook';
4
- import initPostOauth2Token from './postOAuth2Token';
5
- import { AUTH_API_PREFIX } from '../services/authCookies';
4
+ import initPostOauth2Token from './postOAuth2Token.js';
5
+ import { AUTH_API_PREFIX } from '../services/authCookies.js';
6
6
  import type {
7
7
  WhookAPIHandlerDefinition,
8
8
  WhookAPIParameterDefinition,
@@ -10,7 +10,7 @@ import type {
10
10
  import type {
11
11
  AuthCookiesService,
12
12
  AuthHandlersConfig,
13
- } from '../services/authCookies';
13
+ } from '../services/authCookies.js';
14
14
  import type { AsyncReturnType } from 'type-fest';
15
15
  import type { BaseAuthenticationData } from '@whook/authorization';
16
16
 
@@ -131,11 +131,12 @@ async function postAuthRefresh<
131
131
  },
132
132
  };
133
133
  } catch (err) {
134
- const newErr = YHTTPError.wrap(err);
134
+ const newErr = YHTTPError.wrap(err as Error);
135
135
 
136
136
  newErr.headers = {
137
- ...(err.headers || {}),
138
- 'Set-Cookie': authCookies.build(),
137
+ ...((err as YHTTPError).headers || {}),
138
+ // TODO: Allow string[] in YHTTPError
139
+ 'Set-Cookie': authCookies.build() as unknown as string,
139
140
  };
140
141
 
141
142
  throw newErr;
@@ -1,32 +1,86 @@
1
- import initPostOAuth2Acknowledge from './postOAuth2Acknowledge';
2
- import YError from 'yerror';
1
+ import { jest } from '@jest/globals';
2
+ import initPostOAuth2Acknowledge from './postOAuth2Acknowledge.js';
3
+ import { YError } from 'yerror';
3
4
  import { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';
4
- import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters';
5
- import type { OAuth2GranterService } from '../services/oAuth2Granters';
5
+ import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters.js';
6
+ import type { CheckApplicationService } from '../services/oAuth2Granters.js';
7
+ import type { LogService } from 'common-services';
8
+ import type { OAuth2GranterService } from '../services/oAuth2Granters.js';
6
9
  import type { BaseAuthenticationData } from '@whook/authorization';
10
+ import type { OAuth2CodeGranterService } from '../services/oAuth2CodeGranter.js';
11
+ import type { OAuth2TokenGranterService } from '../services/oAuth2TokenGranter.js';
12
+
13
+ type CustomAuthenticationData = BaseAuthenticationData & {
14
+ userId: string;
15
+ };
7
16
 
8
17
  describe('postOAuth2Acknowledge', () => {
9
18
  const ERRORS_DESCRIPTORS = {
10
19
  ...DEFAULT_ERRORS_DESCRIPTORS,
11
20
  ...OAUTH2_ERRORS_DESCRIPTORS,
12
21
  };
13
- const checkApplication = jest.fn();
14
- const log = jest.fn();
22
+ const checkApplication = jest.fn<CheckApplicationService>();
23
+ const log = jest.fn<LogService>();
15
24
  const codeGranter = {
16
25
  type: 'code',
17
- authorizer: { responseType: 'code', authorize: jest.fn() },
18
- acknowledger: { acknowledgmentType: 'code', acknowledge: jest.fn() },
26
+ authorizer: {
27
+ responseType: 'code',
28
+ authorize:
29
+ jest.fn<
30
+ NonNullable<
31
+ OAuth2CodeGranterService<CustomAuthenticationData>['authorizer']
32
+ >['authorize']
33
+ >(),
34
+ },
35
+ acknowledger: {
36
+ acknowledgmentType: 'code',
37
+ acknowledge:
38
+ jest.fn<
39
+ NonNullable<
40
+ OAuth2CodeGranterService<CustomAuthenticationData>['acknowledger']
41
+ >['acknowledge']
42
+ >(),
43
+ },
19
44
  authenticator: {
20
45
  grantType: 'authorization_code',
21
- authenticate: jest.fn(),
46
+ authenticate:
47
+ jest.fn<
48
+ NonNullable<
49
+ OAuth2CodeGranterService<CustomAuthenticationData>['authenticator']
50
+ >['authenticate']
51
+ >(),
22
52
  },
23
53
  };
24
54
  const tokenGranter = {
25
55
  type: 'token',
26
- authorizer: { responseType: 'token', authorize: jest.fn() },
27
- acknowledger: { acknowledgmentType: 'token', acknowledge: jest.fn() },
56
+ authorizer: {
57
+ responseType: 'token',
58
+ authorize:
59
+ jest.fn<
60
+ NonNullable<
61
+ OAuth2TokenGranterService<CustomAuthenticationData>['authenticator']
62
+ >['authenticate']
63
+ >(),
64
+ },
65
+ acknowledger: {
66
+ acknowledgmentType: 'token',
67
+ acknowledge:
68
+ jest.fn<
69
+ NonNullable<
70
+ OAuth2TokenGranterService<CustomAuthenticationData>['acknowledger']
71
+ >['acknowledge']
72
+ >(),
73
+ },
28
74
  };
29
- const oAuth2Granters: OAuth2GranterService[] = [codeGranter, tokenGranter];
75
+ const oAuth2Granters = [
76
+ codeGranter,
77
+ tokenGranter,
78
+ ] as unknown as OAuth2GranterService<
79
+ Record<string, unknown>,
80
+ Record<string, unknown>,
81
+ Record<string, unknown>,
82
+ CustomAuthenticationData
83
+ >[];
30
84
 
31
85
  beforeEach(() => {
32
86
  checkApplication.mockReset();
@@ -53,24 +107,21 @@ describe('postOAuth2Acknowledge', () => {
53
107
  applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
54
108
  redirectURI: 'http://lol',
55
109
  scope: 'user',
56
- userId: 1,
110
+ userId: '1',
57
111
  });
58
112
 
59
- const postOAuth2Acknowledge = await initPostOAuth2Acknowledge<
60
- BaseAuthenticationData & {
61
- userId: number;
62
- }
63
- >({
64
- ERRORS_DESCRIPTORS,
65
- oAuth2Granters,
66
- checkApplication,
67
- log,
68
- });
113
+ const postOAuth2Acknowledge =
114
+ await initPostOAuth2Acknowledge<CustomAuthenticationData>({
115
+ ERRORS_DESCRIPTORS,
116
+ oAuth2Granters,
117
+ checkApplication,
118
+ log,
119
+ });
69
120
  const response = await postOAuth2Acknowledge({
70
121
  authenticationData: {
71
122
  applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
72
123
  scope: 'auth',
73
- userId: 1,
124
+ userId: '1',
74
125
  },
75
126
  body: {
76
127
  responseType: 'code',
@@ -120,21 +171,18 @@ describe('postOAuth2Acknowledge', () => {
120
171
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
121
172
  );
122
173
 
123
- const postOAuth2Acknowledge = await initPostOAuth2Acknowledge<
124
- BaseAuthenticationData & {
125
- userId: number;
126
- }
127
- >({
128
- ERRORS_DESCRIPTORS,
129
- oAuth2Granters,
130
- checkApplication,
131
- log,
132
- });
174
+ const postOAuth2Acknowledge =
175
+ await initPostOAuth2Acknowledge<CustomAuthenticationData>({
176
+ ERRORS_DESCRIPTORS,
177
+ oAuth2Granters,
178
+ checkApplication,
179
+ log,
180
+ });
133
181
  const response = await postOAuth2Acknowledge({
134
182
  authenticationData: {
135
183
  applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
136
184
  scope: 'auth',
137
- userId: 1,
185
+ userId: '1',
138
186
  },
139
187
  body: {
140
188
  responseType: 'yolo',
@@ -1,15 +1,14 @@
1
1
  import { autoHandler } from 'knifecycle';
2
- import YError from 'yerror';
3
- import { setURLError } from './getOAuth2Authorize';
2
+ import { YError } from 'yerror';
3
+ import { setURLError } from './getOAuth2Authorize.js';
4
4
  import type {
5
5
  WhookAPIHandlerDefinition,
6
6
  WhookErrorsDescriptors,
7
- WhookAPIOperationConfig,
8
7
  } from '@whook/whook';
9
8
  import type {
10
9
  CheckApplicationService,
11
10
  OAuth2GranterService,
12
- } from '../services/oAuth2Granters';
11
+ } from '../services/oAuth2Granters.js';
13
12
  import type { LogService } from 'common-services';
14
13
  import type { BaseAuthenticationData } from '@whook/authorization';
15
14
 
@@ -28,7 +27,7 @@ export const definition: WhookAPIHandlerDefinition = {
28
27
  tags: ['oauth2'],
29
28
  'x-whook': {
30
29
  private: true,
31
- } as WhookAPIOperationConfig,
30
+ },
32
31
  requestBody: {
33
32
  required: true,
34
33
  content: {
@@ -96,7 +95,39 @@ export const definition: WhookAPIHandlerDefinition = {
96
95
  },
97
96
  };
98
97
 
99
- export default autoHandler(postOAuth2Acknowledge);
98
+ export type HandlerDependencies<
99
+ AUTHENTICATION_DATA extends BaseAuthenticationData<string, string>,
100
+ > = {
101
+ ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
102
+ oAuth2Granters: OAuth2GranterService<
103
+ Record<string, unknown>,
104
+ Record<string, unknown>,
105
+ Record<string, unknown>,
106
+ AUTHENTICATION_DATA
107
+ >[];
108
+ checkApplication: CheckApplicationService;
109
+ log: LogService;
110
+ };
111
+ type HandlerParameters<AUTHENTICATION_DATA> = {
112
+ authenticationData: AUTHENTICATION_DATA;
113
+ body: {
114
+ responseType: string;
115
+ clientId: string;
116
+ redirectURI: string;
117
+ scope: string;
118
+ state: string;
119
+ acknowledged: boolean;
120
+ [name: string]: unknown;
121
+ };
122
+ };
123
+
124
+ export default autoHandler(postOAuth2Acknowledge) as unknown as <
125
+ AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
126
+ >(
127
+ dependencies: HandlerDependencies<AUTHENTICATION_DATA>,
128
+ ) => (
129
+ parameters: HandlerParameters<AUTHENTICATION_DATA>,
130
+ ) => ReturnType<typeof postOAuth2Acknowledge>;
100
131
 
101
132
  async function postOAuth2Acknowledge<
102
133
  AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
@@ -106,12 +137,7 @@ async function postOAuth2Acknowledge<
106
137
  oAuth2Granters,
107
138
  checkApplication,
108
139
  log,
109
- }: {
110
- ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
111
- oAuth2Granters: OAuth2GranterService[];
112
- checkApplication: CheckApplicationService;
113
- log: LogService;
114
- },
140
+ }: HandlerDependencies<AUTHENTICATION_DATA>,
115
141
  {
116
142
  authenticationData,
117
143
  body: {
@@ -123,18 +149,7 @@ async function postOAuth2Acknowledge<
123
149
  acknowledged = false,
124
150
  ...additionalParameters
125
151
  },
126
- }: {
127
- authenticationData: AUTHENTICATION_DATA;
128
- body: {
129
- responseType: string;
130
- clientId: string;
131
- redirectURI: string;
132
- scope: string;
133
- state: string;
134
- acknowledged: boolean;
135
- [name: string]: unknown;
136
- };
137
- },
152
+ }: HandlerParameters<AUTHENTICATION_DATA>,
138
153
  ) {
139
154
  if (!authenticationData) {
140
155
  throw new YError('E_UNAUTHORIZED');
@@ -161,7 +176,7 @@ async function postOAuth2Acknowledge<
161
176
  granter.acknowledger.acknowledgmentType === responseType,
162
177
  );
163
178
 
164
- if (!granter) {
179
+ if (!granter || !granter.acknowledger) {
165
180
  throw new YError('E_UNKNOWN_ACKNOWLEDGOR_TYPE', responseType);
166
181
  }
167
182
 
@@ -180,16 +195,19 @@ async function postOAuth2Acknowledge<
180
195
  url.searchParams.set('scope', scope);
181
196
  url.searchParams.set('state', state);
182
197
  Object.keys(additionalProperties).forEach((key) =>
183
- url.searchParams.set(snakeCase(key), additionalProperties[key] as string),
198
+ url.searchParams.set(
199
+ snakeCase(key),
200
+ additionalProperties[key] as unknown as string,
201
+ ),
184
202
  );
185
203
  } catch (err) {
186
- log('debug', '👫 - OAuth2 acknowledge error', err.code);
187
- log('debug-stack', err.stack);
204
+ log('debug', '👫 - OAuth2 acknowledge error', (err as YError).code);
205
+ log('debug-stack', (err as Error).stack || 'no_stack_trace');
188
206
 
189
207
  setURLError(
190
208
  url,
191
- err,
192
- ERRORS_DESCRIPTORS[err.code] || ERRORS_DESCRIPTORS.E_OAUTH2,
209
+ err as YError,
210
+ ERRORS_DESCRIPTORS[(err as YError).code] || ERRORS_DESCRIPTORS.E_OAUTH2,
193
211
  );
194
212
  }
195
213