@whook/oauth2 25.2.0 → 26.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 (173) hide show
  1. package/README.md +147 -98
  2. package/dist/index.d.ts +30 -11
  3. package/dist/index.js +15 -8
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.test.js +1258 -399
  6. package/dist/index.test.js.map +1 -1
  7. package/dist/libs/clients.d.ts +2 -0
  8. package/dist/libs/clients.js +13 -0
  9. package/dist/libs/clients.js.map +1 -0
  10. package/dist/libs/errors.d.ts +113 -0
  11. package/dist/libs/errors.js +178 -0
  12. package/dist/libs/errors.js.map +1 -0
  13. package/dist/libs/grants.d.ts +2 -0
  14. package/dist/libs/grants.js +10 -0
  15. package/dist/libs/grants.js.map +1 -0
  16. package/dist/libs/redirectURI.d.ts +7 -0
  17. package/dist/libs/redirectURI.js +78 -0
  18. package/dist/libs/redirectURI.js.map +1 -0
  19. package/dist/libs/schemas.d.ts +23 -0
  20. package/dist/libs/schemas.js +25 -0
  21. package/dist/libs/schemas.js.map +1 -0
  22. package/dist/libs/scopes.d.ts +16 -0
  23. package/dist/libs/scopes.js +37 -0
  24. package/dist/libs/scopes.js.map +1 -1
  25. package/dist/libs/utils.d.ts +2 -0
  26. package/dist/libs/utils.js +17 -0
  27. package/dist/libs/utils.js.map +1 -0
  28. package/dist/libs/verifier.d.ts +7 -0
  29. package/dist/libs/verifier.js +37 -0
  30. package/dist/libs/verifier.js.map +1 -0
  31. package/dist/libs/verifier.test.js +21 -0
  32. package/dist/libs/verifier.test.js.map +1 -0
  33. package/dist/routes/getOAuth2Authorize.d.ts +13 -50
  34. package/dist/routes/getOAuth2Authorize.js +60 -86
  35. package/dist/routes/getOAuth2Authorize.js.map +1 -1
  36. package/dist/routes/getOAuth2Authorize.test.js +147 -68
  37. package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
  38. package/dist/routes/getOAuth2WellKnownMetadata.d.ts +8 -8
  39. package/dist/routes/getOAuth2WellKnownMetadata.js +20 -9
  40. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  41. package/dist/routes/getOAuth2WellKnownMetadata.test.js +38 -37
  42. package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
  43. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.d.ts +3 -6
  44. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js +3 -6
  45. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js.map +1 -1
  46. package/dist/routes/postAuthLogin.d.ts +14 -9
  47. package/dist/routes/postAuthLogin.js +10 -5
  48. package/dist/routes/postAuthLogin.js.map +1 -1
  49. package/dist/routes/postAuthLogin.test.js +79 -55
  50. package/dist/routes/postAuthLogin.test.js.map +1 -1
  51. package/dist/routes/postAuthLogout.d.ts +5 -2
  52. package/dist/routes/postAuthLogout.js +12 -7
  53. package/dist/routes/postAuthLogout.js.map +1 -1
  54. package/dist/routes/postAuthLogout.test.js +35 -17
  55. package/dist/routes/postAuthLogout.test.js.map +1 -1
  56. package/dist/routes/postAuthRefresh.d.ts +14 -9
  57. package/dist/routes/postAuthRefresh.js +10 -6
  58. package/dist/routes/postAuthRefresh.js.map +1 -1
  59. package/dist/routes/postAuthRefresh.test.js +90 -59
  60. package/dist/routes/postAuthRefresh.test.js.map +1 -1
  61. package/dist/routes/postOAuth2Acknowledge.d.ts +20 -10
  62. package/dist/routes/postOAuth2Acknowledge.js +54 -38
  63. package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
  64. package/dist/routes/postOAuth2Acknowledge.test.js +137 -72
  65. package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -1
  66. package/dist/routes/postOAuth2Revoke.d.ts +99 -0
  67. package/dist/routes/postOAuth2Revoke.js +102 -0
  68. package/dist/routes/postOAuth2Revoke.js.map +1 -0
  69. package/dist/routes/postOAuth2Revoke.test.d.ts +1 -0
  70. package/dist/routes/postOAuth2Revoke.test.js +161 -0
  71. package/dist/routes/postOAuth2Revoke.test.js.map +1 -0
  72. package/dist/routes/postOAuth2Token.d.ts +35 -108
  73. package/dist/routes/postOAuth2Token.js +53 -114
  74. package/dist/routes/postOAuth2Token.js.map +1 -1
  75. package/dist/routes/postOAuth2Token.test.js +493 -61
  76. package/dist/routes/postOAuth2Token.test.js.map +1 -1
  77. package/dist/services/authCookies.d.ts +9 -13
  78. package/dist/services/authCookies.js +1 -1
  79. package/dist/services/authCookies.js.map +1 -1
  80. package/dist/services/authCookies.test.js.map +1 -1
  81. package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +129 -0
  82. package/dist/services/oAuth2AuthorizationCodeGranter.js +221 -0
  83. package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -0
  84. package/dist/services/oAuth2AuthorizationCodeGranter.test.d.ts +1 -0
  85. package/dist/services/oAuth2AuthorizationCodeGranter.test.js +474 -0
  86. package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -0
  87. package/dist/services/oAuth2ClientCredentialsGranter.d.ts +28 -12
  88. package/dist/services/oAuth2ClientCredentialsGranter.js +39 -21
  89. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
  90. package/dist/services/oAuth2ClientCredentialsGranter.test.d.ts +1 -0
  91. package/dist/services/oAuth2ClientCredentialsGranter.test.js +265 -0
  92. package/dist/services/oAuth2ClientCredentialsGranter.test.js.map +1 -0
  93. package/dist/services/oAuth2Granters.d.ts +81 -86
  94. package/dist/services/oAuth2Granters.js +2 -67
  95. package/dist/services/oAuth2Granters.js.map +1 -1
  96. package/dist/services/oAuth2ImplicitGranter.d.ts +38 -0
  97. package/dist/services/oAuth2ImplicitGranter.js +72 -0
  98. package/dist/services/oAuth2ImplicitGranter.js.map +1 -0
  99. package/dist/services/oAuth2ImplicitGranter.test.d.ts +1 -0
  100. package/dist/services/oAuth2ImplicitGranter.test.js +727 -0
  101. package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -0
  102. package/dist/services/oAuth2PasswordGranter.d.ts +43 -10
  103. package/dist/services/oAuth2PasswordGranter.js +53 -18
  104. package/dist/services/oAuth2PasswordGranter.js.map +1 -1
  105. package/dist/services/oAuth2PasswordGranter.test.d.ts +1 -0
  106. package/dist/services/oAuth2PasswordGranter.test.js +455 -0
  107. package/dist/services/oAuth2PasswordGranter.test.js.map +1 -0
  108. package/dist/services/oAuth2RefreshTokenGranter.d.ts +42 -9
  109. package/dist/services/oAuth2RefreshTokenGranter.js +60 -21
  110. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
  111. package/dist/services/oAuth2RefreshTokenGranter.test.d.ts +1 -0
  112. package/dist/services/oAuth2RefreshTokenGranter.test.js +403 -0
  113. package/dist/services/oAuth2RefreshTokenGranter.test.js.map +1 -0
  114. package/package.json +11 -11
  115. package/src/index.test.ts +1314 -404
  116. package/src/index.ts +46 -53
  117. package/src/libs/clients.ts +22 -0
  118. package/src/libs/errors.ts +325 -0
  119. package/src/libs/grants.ts +15 -0
  120. package/src/libs/redirectURI.ts +120 -0
  121. package/src/libs/schemas.ts +27 -0
  122. package/src/libs/scopes.ts +49 -0
  123. package/src/libs/utils.ts +24 -0
  124. package/src/libs/verifier.test.ts +37 -0
  125. package/src/libs/verifier.ts +57 -0
  126. package/src/routes/getOAuth2Authorize.test.ts +179 -96
  127. package/src/routes/getOAuth2Authorize.ts +97 -124
  128. package/src/routes/getOAuth2WellKnownMetadata.test.ts +58 -60
  129. package/src/routes/getOAuth2WellKnownMetadata.ts +26 -11
  130. package/src/routes/getOAuth2WellKnownProtectedResourceMetadata.ts +4 -6
  131. package/src/routes/postAuthLogin.test.ts +86 -57
  132. package/src/routes/postAuthLogin.ts +24 -14
  133. package/src/routes/postAuthLogout.test.ts +42 -18
  134. package/src/routes/postAuthLogout.ts +22 -8
  135. package/src/routes/postAuthRefresh.test.ts +98 -61
  136. package/src/routes/postAuthRefresh.ts +25 -13
  137. package/src/routes/postOAuth2Acknowledge.test.ts +169 -110
  138. package/src/routes/postOAuth2Acknowledge.ts +101 -61
  139. package/src/routes/postOAuth2Revoke.test.ts +179 -0
  140. package/src/routes/postOAuth2Revoke.ts +156 -0
  141. package/src/routes/postOAuth2Token.test.ts +570 -108
  142. package/src/routes/postOAuth2Token.ts +92 -136
  143. package/src/services/authCookies.test.ts +5 -5
  144. package/src/services/authCookies.ts +16 -18
  145. package/src/services/oAuth2AuthorizationCodeGranter.test.ts +564 -0
  146. package/src/services/oAuth2AuthorizationCodeGranter.ts +388 -0
  147. package/src/services/oAuth2ClientCredentialsGranter.test.ts +303 -0
  148. package/src/services/oAuth2ClientCredentialsGranter.ts +68 -43
  149. package/src/services/oAuth2Granters.ts +101 -195
  150. package/src/services/oAuth2ImplicitGranter.test.ts +811 -0
  151. package/src/services/oAuth2ImplicitGranter.ts +163 -0
  152. package/src/services/oAuth2PasswordGranter.test.ts +493 -0
  153. package/src/services/oAuth2PasswordGranter.ts +116 -40
  154. package/src/services/oAuth2RefreshTokenGranter.test.ts +441 -0
  155. package/src/services/oAuth2RefreshTokenGranter.ts +118 -51
  156. package/src/types.d.ts +1 -0
  157. package/dist/services/oAuth2CodeGranter.d.ts +0 -41
  158. package/dist/services/oAuth2CodeGranter.js +0 -105
  159. package/dist/services/oAuth2CodeGranter.js.map +0 -1
  160. package/dist/services/oAuth2CodeGranter.test.js +0 -123
  161. package/dist/services/oAuth2CodeGranter.test.js.map +0 -1
  162. package/dist/services/oAuth2TokenGranter.d.ts +0 -22
  163. package/dist/services/oAuth2TokenGranter.js +0 -64
  164. package/dist/services/oAuth2TokenGranter.js.map +0 -1
  165. package/src/__snapshots__/index.test.ts.snap +0 -401
  166. package/src/routes/__snapshots__/getOAuth2Authorize.test.ts.snap +0 -37
  167. package/src/routes/__snapshots__/postOAuth2Acknowledge.test.ts.snap +0 -61
  168. package/src/routes/__snapshots__/postOAuth2Token.test.ts.snap +0 -52
  169. package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +0 -52
  170. package/src/services/oAuth2CodeGranter.test.ts +0 -176
  171. package/src/services/oAuth2CodeGranter.ts +0 -210
  172. package/src/services/oAuth2TokenGranter.ts +0 -119
  173. /package/dist/{services/oAuth2CodeGranter.test.d.ts → libs/verifier.test.d.ts} +0 -0
@@ -0,0 +1,564 @@
1
+ import { describe, test, beforeEach, jest, expect } from '@jest/globals';
2
+ import {
3
+ type WhookOAuth2Options,
4
+ type WhookOAuth2ReadClientGrantsService,
5
+ } from './oAuth2Granters.js';
6
+ import initOAuth2AuthorizationCodeGranter, {
7
+ type WhookOAuth2AuthorizationCodeService,
8
+ } from './oAuth2AuthorizationCodeGranter.js';
9
+ import { YError } from 'yerror';
10
+
11
+ describe('OAuth2AuthorizationCodeGranter', () => {
12
+ const OAUTH2: WhookOAuth2Options = {
13
+ rootClientId: 'the_root_app_id',
14
+ authenticateURL: 'https://auth.example.com/sign_in',
15
+ allowedScopes: ['user', 'admin', 'root'],
16
+ };
17
+ const oAuth2AuthorizationCode = {
18
+ create: jest.fn<WhookOAuth2AuthorizationCodeService['create']>(),
19
+ check: jest.fn<WhookOAuth2AuthorizationCodeService['check']>(),
20
+ };
21
+ const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
22
+ const log = jest.fn();
23
+
24
+ beforeEach(() => {
25
+ oAuth2AuthorizationCode.create.mockReset();
26
+ oAuth2AuthorizationCode.check.mockReset();
27
+ readClientGrants.mockReset();
28
+ log.mockReset();
29
+ });
30
+
31
+ describe('.authorize()', () => {
32
+ test('should work with a valid request', async () => {
33
+ const oAuth2AuthorizationCodeGranter =
34
+ await initOAuth2AuthorizationCodeGranter({
35
+ OAUTH2,
36
+ readClientGrants,
37
+ oAuth2AuthorizationCode,
38
+ log,
39
+ });
40
+
41
+ readClientGrants.mockResolvedValue({
42
+ allowedScopes: ['user', 'admin'],
43
+ allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
44
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
45
+ authenticationData: {
46
+ clientId: 'the_client_app_id',
47
+ scopes: ['user', 'admin'],
48
+ userId: 'user_id',
49
+ },
50
+ isPublicClient: false,
51
+ });
52
+
53
+ oAuth2AuthorizationCode.create.mockRejectedValueOnce(
54
+ new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
55
+ );
56
+ oAuth2AuthorizationCode.check.mockRejectedValueOnce(
57
+ new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
58
+ );
59
+
60
+ const authorizerResult = await oAuth2AuthorizationCodeGranter.authorize?.(
61
+ {
62
+ clientId: 'the_client_app_id',
63
+ demandedRedirectURI: 'https://www.example.com/oauth2/code',
64
+ demandedScopes: ['user'],
65
+ },
66
+ {
67
+ codeChallenge: 'my_code_challenge',
68
+ codeChallengeMethod: 'plain',
69
+ },
70
+ );
71
+
72
+ expect({
73
+ authorizerResult,
74
+ oAuth2AuthorizationCodeCreateCalls:
75
+ oAuth2AuthorizationCode.create.mock.calls,
76
+ oAuth2AuthorizationCodeCheckCalls:
77
+ oAuth2AuthorizationCode.check.mock.calls,
78
+ readClientGrantsCalls: readClientGrants.mock.calls,
79
+ logCalls: log.mock.calls,
80
+ }).toMatchInlineSnapshot(`
81
+ {
82
+ "authorizerResult": {
83
+ "clientId": "the_client_app_id",
84
+ "redirectURI": "https://www.example.com/oauth2/code",
85
+ "scopes": [
86
+ "user",
87
+ ],
88
+ },
89
+ "logCalls": [
90
+ [
91
+ "debug",
92
+ "👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
93
+ ],
94
+ ],
95
+ "oAuth2AuthorizationCodeCheckCalls": [],
96
+ "oAuth2AuthorizationCodeCreateCalls": [],
97
+ "readClientGrantsCalls": [
98
+ [
99
+ "the_client_app_id",
100
+ ],
101
+ ],
102
+ }
103
+ `);
104
+ });
105
+
106
+ test('should fail with a bad uri', async () => {
107
+ const oAuth2AuthorizationCodeGranter =
108
+ await initOAuth2AuthorizationCodeGranter({
109
+ OAUTH2,
110
+ readClientGrants,
111
+ oAuth2AuthorizationCode,
112
+ log,
113
+ });
114
+
115
+ readClientGrants.mockResolvedValue({
116
+ allowedScopes: ['user', 'admin'],
117
+ allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
118
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
119
+ authenticationData: {
120
+ clientId: 'the_client_app_id',
121
+ scopes: ['user', 'admin'],
122
+ userId: 'user_id',
123
+ },
124
+ isPublicClient: false,
125
+ });
126
+
127
+ oAuth2AuthorizationCode.create.mockRejectedValueOnce(
128
+ new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
129
+ );
130
+ oAuth2AuthorizationCode.check.mockRejectedValueOnce(
131
+ new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
132
+ );
133
+
134
+ try {
135
+ await oAuth2AuthorizationCodeGranter.authorize?.(
136
+ {
137
+ clientId: 'the_client_app_id',
138
+ demandedRedirectURI: 'https://www.example.com/admin',
139
+ demandedScopes: ['user'],
140
+ },
141
+ {
142
+ codeChallenge: 'my_code_challenge',
143
+ codeChallengeMethod: 'plain',
144
+ },
145
+ );
146
+ throw new YError('E_UNEXPECTED_SUCCESS');
147
+ } catch (err) {
148
+ expect({
149
+ err,
150
+ oAuth2AuthorizationCodeCreateCalls:
151
+ oAuth2AuthorizationCode.create.mock.calls,
152
+ oAuth2AuthorizationCodeCheckCalls:
153
+ oAuth2AuthorizationCode.check.mock.calls,
154
+ readClientGrantsCalls: readClientGrants.mock.calls,
155
+ logCalls: log.mock.calls,
156
+ }).toMatchInlineSnapshot(`
157
+ {
158
+ "err": [YError: E_OAUTH2_BAD_REDIRECT_URI (["https://www.example.com/admin",["https://www.example.com/oauth2/code"]]): E_OAUTH2_BAD_REDIRECT_URI],
159
+ "logCalls": [
160
+ [
161
+ "debug",
162
+ "👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
163
+ ],
164
+ ],
165
+ "oAuth2AuthorizationCodeCheckCalls": [],
166
+ "oAuth2AuthorizationCodeCreateCalls": [],
167
+ "readClientGrantsCalls": [
168
+ [
169
+ "the_client_app_id",
170
+ ],
171
+ ],
172
+ }
173
+ `);
174
+ }
175
+ });
176
+
177
+ test('should fail with a bad scope', async () => {
178
+ const oAuth2AuthorizationCodeGranter =
179
+ await initOAuth2AuthorizationCodeGranter({
180
+ OAUTH2: {
181
+ ...OAUTH2,
182
+ strictScopesChecks: true,
183
+ },
184
+ readClientGrants,
185
+ oAuth2AuthorizationCode,
186
+ log,
187
+ });
188
+
189
+ readClientGrants.mockResolvedValue({
190
+ allowedScopes: ['user', 'admin'],
191
+ allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
192
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
193
+ authenticationData: {
194
+ clientId: 'the_client_app_id',
195
+ scopes: ['user', 'admin'],
196
+ userId: 'user_id',
197
+ },
198
+ isPublicClient: false,
199
+ });
200
+
201
+ oAuth2AuthorizationCode.create.mockRejectedValueOnce(
202
+ new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
203
+ );
204
+ oAuth2AuthorizationCode.check.mockRejectedValueOnce(
205
+ new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
206
+ );
207
+
208
+ try {
209
+ await oAuth2AuthorizationCodeGranter.authorize?.(
210
+ {
211
+ clientId: 'the_client_app_id',
212
+ demandedRedirectURI: 'https://www.example.com/admin',
213
+ demandedScopes: ['god'],
214
+ },
215
+ {
216
+ codeChallenge: 'my_code_challenge',
217
+ codeChallengeMethod: 'plain',
218
+ },
219
+ );
220
+ throw new YError('E_UNEXPECTED_SUCCESS');
221
+ } catch (err) {
222
+ expect({
223
+ err,
224
+ oAuth2AuthorizationCodeCreateCalls:
225
+ oAuth2AuthorizationCode.create.mock.calls,
226
+ oAuth2AuthorizationCodeCheckCalls:
227
+ oAuth2AuthorizationCode.check.mock.calls,
228
+ readClientGrantsCalls: readClientGrants.mock.calls,
229
+ logCalls: log.mock.calls,
230
+ }).toMatchInlineSnapshot(`
231
+ {
232
+ "err": [YError: E_OAUTH2_BAD_SCOPE (["god"]): E_OAUTH2_BAD_SCOPE],
233
+ "logCalls": [
234
+ [
235
+ "debug",
236
+ "👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
237
+ ],
238
+ ],
239
+ "oAuth2AuthorizationCodeCheckCalls": [],
240
+ "oAuth2AuthorizationCodeCreateCalls": [],
241
+ "readClientGrantsCalls": [
242
+ [
243
+ "the_client_app_id",
244
+ ],
245
+ ],
246
+ }
247
+ `);
248
+ }
249
+ });
250
+ });
251
+
252
+ describe('.acknowledge()', () => {
253
+ test('should work', async () => {
254
+ const oAuth2AuthorizationCodeGranter =
255
+ await initOAuth2AuthorizationCodeGranter({
256
+ OAUTH2,
257
+ readClientGrants,
258
+ oAuth2AuthorizationCode,
259
+ log,
260
+ });
261
+
262
+ readClientGrants.mockResolvedValue({
263
+ allowedScopes: ['user', 'admin'],
264
+ allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
265
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
266
+ authenticationData: {
267
+ clientId: 'an_app_id',
268
+ scopes: ['user', 'admin'],
269
+ userId: 'user_id',
270
+ },
271
+ isPublicClient: false,
272
+ });
273
+
274
+ oAuth2AuthorizationCode.create.mockResolvedValueOnce('yolo');
275
+ oAuth2AuthorizationCode.check.mockRejectedValueOnce(
276
+ new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
277
+ );
278
+
279
+ const acknowledgerResult =
280
+ await oAuth2AuthorizationCodeGranter.acknowledge?.(
281
+ {
282
+ clientId: 'the_root_app_id',
283
+ scopes: ['user', 'admin'],
284
+ userId: 'the_user_id',
285
+ },
286
+ {
287
+ clientId: 'the_client_app_id',
288
+ demandedRedirectURI: 'https://www.example.com/oauth2/code',
289
+ demandedScopes: ['user'],
290
+ },
291
+ {
292
+ codeChallenge: 'my_code_challenge',
293
+ codeChallengeMethod: 'plain',
294
+ },
295
+ );
296
+
297
+ expect({
298
+ acknowledgerResult,
299
+ oAuth2AuthorizationCodeCreateCalls:
300
+ oAuth2AuthorizationCode.create.mock.calls,
301
+ oAuth2AuthorizationCodeCheckCalls:
302
+ oAuth2AuthorizationCode.check.mock.calls,
303
+ readClientGrantsCalls: readClientGrants.mock.calls,
304
+ logCalls: log.mock.calls,
305
+ }).toMatchInlineSnapshot(`
306
+ {
307
+ "acknowledgerResult": {
308
+ "acknowledgedAuthenticationData": {
309
+ "clientId": "the_client_app_id",
310
+ "scopes": [
311
+ "user",
312
+ ],
313
+ "userId": "the_user_id",
314
+ },
315
+ "acknowledgedData": {
316
+ "code": "yolo",
317
+ "codeChallenge": "my_code_challenge",
318
+ "codeChallengeMethod": "plain",
319
+ },
320
+ "acknowledgedRedirectURI": "https://www.example.com/oauth2/code",
321
+ "acknowledgedScopes": [
322
+ "user",
323
+ ],
324
+ },
325
+ "logCalls": [
326
+ [
327
+ "debug",
328
+ "👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
329
+ ],
330
+ ],
331
+ "oAuth2AuthorizationCodeCheckCalls": [],
332
+ "oAuth2AuthorizationCodeCreateCalls": [
333
+ [
334
+ {
335
+ "clientId": "the_client_app_id",
336
+ "scopes": [
337
+ "user",
338
+ ],
339
+ "userId": "the_user_id",
340
+ },
341
+ {
342
+ "codeChallenge": "my_code_challenge",
343
+ "codeChallengeMethod": "plain",
344
+ "demandedRedirectURI": "https://www.example.com/oauth2/code",
345
+ "demandedScopes": [
346
+ "user",
347
+ ],
348
+ "filteredScopes": [
349
+ "user",
350
+ ],
351
+ },
352
+ ],
353
+ ],
354
+ "readClientGrantsCalls": [
355
+ [
356
+ "the_client_app_id",
357
+ ],
358
+ ],
359
+ }
360
+ `);
361
+ });
362
+ });
363
+
364
+ describe('.authenticate()', () => {
365
+ test('should work with plain code verifier', async () => {
366
+ const oAuth2AuthorizationCodeGranter =
367
+ await initOAuth2AuthorizationCodeGranter({
368
+ OAUTH2,
369
+ readClientGrants,
370
+ oAuth2AuthorizationCode,
371
+ log,
372
+ });
373
+
374
+ readClientGrants.mockResolvedValue({
375
+ allowedScopes: ['user', 'admin'],
376
+ allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
377
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
378
+ authenticationData: {
379
+ clientId: 'the_client_app_id',
380
+ scopes: ['user', 'admin'],
381
+ userId: 'user_id',
382
+ },
383
+ isPublicClient: false,
384
+ });
385
+
386
+ oAuth2AuthorizationCode.create.mockRejectedValueOnce(
387
+ new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
388
+ );
389
+ oAuth2AuthorizationCode.check.mockResolvedValueOnce({
390
+ codeAuthenticationData: {
391
+ clientId: 'the_client_app_id',
392
+ scopes: ['user', 'admin'],
393
+ userId: 'the_user_id',
394
+ },
395
+ context: {
396
+ demandedRedirectURI: 'https://www.example.com/oauth2/code',
397
+ demandedScopes: ['user'],
398
+ filteredScopes: ['user'],
399
+ codeChallenge: 'a_code_verifier',
400
+ codeChallengeMethod: 'plain',
401
+ },
402
+ });
403
+
404
+ const authenticatorResult =
405
+ await oAuth2AuthorizationCodeGranter.authenticate?.(
406
+ {
407
+ code: 'a_code',
408
+ redirectURI: 'https://www.example.com/oauth2/code',
409
+ clientId: 'the_client_app_id',
410
+ codeVerifier: 'a_code_verifier',
411
+ },
412
+ {
413
+ clientId: 'the_client_app_id',
414
+ scopes: ['user', 'admin'],
415
+ userId: 'the_user_id',
416
+ },
417
+ );
418
+
419
+ expect({
420
+ authenticatorResult,
421
+ oAuth2AuthorizationCodeCreateCalls:
422
+ oAuth2AuthorizationCode.create.mock.calls,
423
+ oAuth2AuthorizationCodeCheckCalls:
424
+ oAuth2AuthorizationCode.check.mock.calls,
425
+ readClientGrantsCalls: readClientGrants.mock.calls,
426
+ logCalls: log.mock.calls,
427
+ }).toMatchInlineSnapshot(`
428
+ {
429
+ "authenticatorResult": {
430
+ "clientId": "the_client_app_id",
431
+ "scopes": [
432
+ "user",
433
+ ],
434
+ "userId": "the_user_id",
435
+ },
436
+ "logCalls": [
437
+ [
438
+ "debug",
439
+ "👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
440
+ ],
441
+ ],
442
+ "oAuth2AuthorizationCodeCheckCalls": [
443
+ [
444
+ {
445
+ "clientId": "the_client_app_id",
446
+ "scopes": [
447
+ "user",
448
+ "admin",
449
+ ],
450
+ "userId": "the_user_id",
451
+ },
452
+ "a_code",
453
+ ],
454
+ ],
455
+ "oAuth2AuthorizationCodeCreateCalls": [],
456
+ "readClientGrantsCalls": [
457
+ [
458
+ "the_client_app_id",
459
+ ],
460
+ ],
461
+ }
462
+ `);
463
+ });
464
+ test('should work with plain S256 verifier', async () => {
465
+ const oAuth2AuthorizationCodeGranter =
466
+ await initOAuth2AuthorizationCodeGranter({
467
+ OAUTH2,
468
+ readClientGrants,
469
+ oAuth2AuthorizationCode,
470
+ log,
471
+ });
472
+
473
+ readClientGrants.mockResolvedValue({
474
+ allowedScopes: ['user', 'admin'],
475
+ allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
476
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
477
+ authenticationData: {
478
+ clientId: 'the_client_app_id',
479
+ scopes: ['user', 'admin'],
480
+ userId: 'user_id',
481
+ },
482
+ isPublicClient: false,
483
+ });
484
+
485
+ oAuth2AuthorizationCode.create.mockRejectedValueOnce(
486
+ new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
487
+ );
488
+ oAuth2AuthorizationCode.check.mockResolvedValueOnce({
489
+ codeAuthenticationData: {
490
+ clientId: 'the_client_app_id',
491
+ scopes: ['user', 'admin'],
492
+ userId: 'the_user_id',
493
+ },
494
+ context: {
495
+ demandedRedirectURI: 'https://www.example.com/oauth2/code',
496
+ demandedScopes: ['user'],
497
+ filteredScopes: ['user'],
498
+ codeChallenge: 'pOX5Ly27TpE8bQ0ZRggD_gN3gS_Q6LZDdR7DDP9wwKU',
499
+ codeChallengeMethod: 'S256',
500
+ },
501
+ });
502
+
503
+ const authenticatorResult =
504
+ await oAuth2AuthorizationCodeGranter.authenticate?.(
505
+ {
506
+ code: 'a_code',
507
+ redirectURI: 'https://www.example.com/oauth2/code',
508
+ clientId: 'the_client_app_id',
509
+ codeVerifier: 'a_code_verifier',
510
+ },
511
+ {
512
+ clientId: 'the_client_app_id',
513
+ scopes: ['user', 'admin'],
514
+ userId: 'the_user_id',
515
+ },
516
+ );
517
+
518
+ expect({
519
+ authenticatorResult,
520
+ oAuth2AuthorizationCodeCreateCalls:
521
+ oAuth2AuthorizationCode.create.mock.calls,
522
+ oAuth2AuthorizationCodeCheckCalls:
523
+ oAuth2AuthorizationCode.check.mock.calls,
524
+ readClientGrantsCalls: readClientGrants.mock.calls,
525
+ logCalls: log.mock.calls,
526
+ }).toMatchInlineSnapshot(`
527
+ {
528
+ "authenticatorResult": {
529
+ "clientId": "the_client_app_id",
530
+ "scopes": [
531
+ "user",
532
+ ],
533
+ "userId": "the_user_id",
534
+ },
535
+ "logCalls": [
536
+ [
537
+ "debug",
538
+ "👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
539
+ ],
540
+ ],
541
+ "oAuth2AuthorizationCodeCheckCalls": [
542
+ [
543
+ {
544
+ "clientId": "the_client_app_id",
545
+ "scopes": [
546
+ "user",
547
+ "admin",
548
+ ],
549
+ "userId": "the_user_id",
550
+ },
551
+ "a_code",
552
+ ],
553
+ ],
554
+ "oAuth2AuthorizationCodeCreateCalls": [],
555
+ "readClientGrantsCalls": [
556
+ [
557
+ "the_client_app_id",
558
+ ],
559
+ ],
560
+ }
561
+ `);
562
+ });
563
+ });
564
+ });