@whook/oauth2 25.2.1 → 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 +15 -8
  40. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  41. package/dist/routes/getOAuth2WellKnownMetadata.test.js +37 -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 +11 -10
  62. package/dist/routes/postOAuth2Acknowledge.js +43 -37
  63. package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
  64. package/dist/routes/postOAuth2Acknowledge.test.js +135 -70
  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 +57 -60
  129. package/src/routes/getOAuth2WellKnownMetadata.ts +21 -10
  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 +167 -108
  138. package/src/routes/postOAuth2Acknowledge.ts +90 -64
  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,811 @@
1
+ import { describe, test, beforeEach, jest, expect } from '@jest/globals';
2
+ import {
3
+ type WhookOAuth2Options,
4
+ type WhookOAuth2ReadClientGrantsService,
5
+ } from './oAuth2Granters.js';
6
+ import initOAuth2ImplicitGranter, {
7
+ type WhookOAuth2AccessTokenService,
8
+ } from './oAuth2ImplicitGranter.js';
9
+ import { YError } from 'yerror';
10
+ import { type LogService, type TimeService } from 'common-services';
11
+
12
+ describe('OAuth2ImplicitGranter', () => {
13
+ const OAUTH2: WhookOAuth2Options = {
14
+ rootClientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
15
+ authenticateURL: 'https://auth.example.com/sign_in',
16
+ allowedScopes: ['user', 'admin', 'root'],
17
+ };
18
+ const oAuth2AccessToken = {
19
+ create: jest.fn<WhookOAuth2AccessTokenService['create']>(),
20
+ };
21
+ const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
22
+ const time = jest.fn<TimeService>();
23
+ const log = jest.fn<LogService>();
24
+
25
+ beforeEach(() => {
26
+ oAuth2AccessToken.create.mockReset();
27
+ readClientGrants.mockReset();
28
+ log.mockReset();
29
+ time.mockReset();
30
+ });
31
+
32
+ describe('authorizer.authorize()', () => {
33
+ test('should work with a valid request', async () => {
34
+ const oAuth2AccessTokenGranter = await initOAuth2ImplicitGranter({
35
+ OAUTH2,
36
+ readClientGrants,
37
+ oAuth2AccessToken,
38
+ time,
39
+ log,
40
+ });
41
+
42
+ readClientGrants.mockResolvedValue({
43
+ allowedRedirectURIS: ['https://www.example.com'],
44
+ allowedGrantTypes: ['implicit', 'authorization_code'],
45
+ allowedScopes: ['user', 'admin'],
46
+ authenticationData: {
47
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
48
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
49
+ scopes: [],
50
+ },
51
+ isPublicClient: true,
52
+ });
53
+ oAuth2AccessToken.create.mockRejectedValueOnce(
54
+ new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
55
+ );
56
+
57
+ const authorizerAuthenticateResult =
58
+ await oAuth2AccessTokenGranter.authorize?.(
59
+ {
60
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
61
+ demandedRedirectURI: 'https://www.example.com',
62
+ demandedScopes: ['user', 'root'],
63
+ },
64
+ {},
65
+ );
66
+
67
+ expect({
68
+ authorizerAuthenticateResult,
69
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.create.mock.calls,
70
+ readClientGrantsCalls: readClientGrants.mock.calls,
71
+ logCalls: log.mock.calls,
72
+ timeCalls: time.mock.calls,
73
+ }).toMatchInlineSnapshot(`
74
+ {
75
+ "authorizerAuthenticateResult": {
76
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
77
+ "redirectURI": "https://www.example.com",
78
+ "scopes": [
79
+ "user",
80
+ ],
81
+ "userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
82
+ },
83
+ "logCalls": [
84
+ [
85
+ "warning",
86
+ "⚠️ - Using the token flow is deprecated and not recommended.",
87
+ ],
88
+ [
89
+ "debug",
90
+ "👫 - OAuth2ImplicitGranter Service Initialized!",
91
+ ],
92
+ ],
93
+ "oAuth2AccessTokenCheckCalls": [],
94
+ "readClientGrantsCalls": [
95
+ [
96
+ "abbacaca-abba-caca-abba-cacaabbacaca",
97
+ ],
98
+ ],
99
+ "timeCalls": [],
100
+ }
101
+ `);
102
+ });
103
+
104
+ test('should work with a valid request and filter scopes', async () => {
105
+ const oAuth2AccessTokenGranter = await initOAuth2ImplicitGranter({
106
+ OAUTH2,
107
+ readClientGrants,
108
+ oAuth2AccessToken,
109
+ time,
110
+ log,
111
+ });
112
+
113
+ readClientGrants.mockResolvedValue({
114
+ allowedRedirectURIS: ['https://www.example.com'],
115
+ allowedGrantTypes: ['implicit', 'authorization_code'],
116
+ allowedScopes: ['user', 'admin'],
117
+ authenticationData: {
118
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
119
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
120
+ scopes: [],
121
+ },
122
+ isPublicClient: true,
123
+ });
124
+ oAuth2AccessToken.create.mockRejectedValueOnce(
125
+ new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
126
+ );
127
+ time.mockReturnValueOnce(Date.parse('2026-07-22T10:00:00Z'));
128
+
129
+ const authorizerAuthenticateResult =
130
+ await oAuth2AccessTokenGranter.authorize?.(
131
+ {
132
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
133
+ demandedRedirectURI: 'https://www.example.com',
134
+ demandedScopes: ['user', 'admin', 'oauth2', 'a_scope'],
135
+ },
136
+ {},
137
+ );
138
+
139
+ expect({
140
+ authorizerAuthenticateResult,
141
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.create.mock.calls,
142
+ readClientGrantsCalls: readClientGrants.mock.calls,
143
+ logCalls: log.mock.calls,
144
+ timeCalls: time.mock.calls,
145
+ }).toMatchInlineSnapshot(`
146
+ {
147
+ "authorizerAuthenticateResult": {
148
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
149
+ "redirectURI": "https://www.example.com",
150
+ "scopes": [
151
+ "user",
152
+ "admin",
153
+ ],
154
+ "userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
155
+ },
156
+ "logCalls": [
157
+ [
158
+ "warning",
159
+ "⚠️ - Using the token flow is deprecated and not recommended.",
160
+ ],
161
+ [
162
+ "debug",
163
+ "👫 - OAuth2ImplicitGranter Service Initialized!",
164
+ ],
165
+ ],
166
+ "oAuth2AccessTokenCheckCalls": [],
167
+ "readClientGrantsCalls": [
168
+ [
169
+ "abbacaca-abba-caca-abba-cacaabbacaca",
170
+ ],
171
+ ],
172
+ "timeCalls": [],
173
+ }
174
+ `);
175
+ });
176
+
177
+ test('should fail with a client without token grant', async () => {
178
+ const oAuth2AccessTokenGranter = await initOAuth2ImplicitGranter({
179
+ OAUTH2,
180
+ readClientGrants,
181
+ oAuth2AccessToken,
182
+ time,
183
+ log,
184
+ });
185
+
186
+ readClientGrants.mockResolvedValue({
187
+ allowedRedirectURIS: ['https://www.example.com'],
188
+ allowedGrantTypes: ['authorization_code'],
189
+ allowedScopes: ['user', 'admin'],
190
+ authenticationData: {
191
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
192
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
193
+ scopes: [],
194
+ },
195
+ isPublicClient: true,
196
+ });
197
+ oAuth2AccessToken.create.mockRejectedValueOnce(
198
+ new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
199
+ );
200
+ time.mockReturnValueOnce(Date.parse('2026-07-22T10:00:00Z'));
201
+
202
+ try {
203
+ await oAuth2AccessTokenGranter.authorize?.(
204
+ {
205
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
206
+ demandedRedirectURI: 'https://www.example.com',
207
+ demandedScopes: ['user'],
208
+ },
209
+ {},
210
+ );
211
+ throw new YError('E_UNEXPECTED_SUCCESS');
212
+ } catch (err) {
213
+ expect({
214
+ err,
215
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.create.mock.calls,
216
+ readClientGrantsCalls: readClientGrants.mock.calls,
217
+ logCalls: log.mock.calls,
218
+ timeCalls: time.mock.calls,
219
+ }).toMatchInlineSnapshot(`
220
+ {
221
+ "err": [YError: E_OAUTH2_GRANT_TYPE_NOT_ALLOWED (["implicit",["authorization_code"]]): E_OAUTH2_GRANT_TYPE_NOT_ALLOWED],
222
+ "logCalls": [
223
+ [
224
+ "warning",
225
+ "⚠️ - Using the token flow is deprecated and not recommended.",
226
+ ],
227
+ [
228
+ "debug",
229
+ "👫 - OAuth2ImplicitGranter Service Initialized!",
230
+ ],
231
+ ],
232
+ "oAuth2AccessTokenCheckCalls": [],
233
+ "readClientGrantsCalls": [
234
+ [
235
+ "abbacaca-abba-caca-abba-cacaabbacaca",
236
+ ],
237
+ ],
238
+ "timeCalls": [],
239
+ }
240
+ `);
241
+ }
242
+ });
243
+
244
+ test('should fail with a client with a bad scope for the client', async () => {
245
+ const oAuth2AccessTokenGranter = await initOAuth2ImplicitGranter({
246
+ OAUTH2: {
247
+ ...OAUTH2,
248
+ strictScopesChecks: true,
249
+ },
250
+ readClientGrants,
251
+ oAuth2AccessToken,
252
+ log,
253
+ });
254
+
255
+ readClientGrants.mockResolvedValue({
256
+ allowedRedirectURIS: ['https://www.example.com'],
257
+ allowedGrantTypes: ['implicit'],
258
+ allowedScopes: ['user'],
259
+ authenticationData: {
260
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
261
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
262
+ scopes: [],
263
+ },
264
+ isPublicClient: true,
265
+ });
266
+ oAuth2AccessToken.create.mockRejectedValueOnce(
267
+ new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
268
+ );
269
+ time.mockReturnValueOnce(Date.parse('2026-07-22T10:00:00Z'));
270
+
271
+ try {
272
+ await oAuth2AccessTokenGranter.authorize?.(
273
+ {
274
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
275
+ demandedRedirectURI: 'https://www.example.com',
276
+ demandedScopes: ['user', 'admin'],
277
+ },
278
+ {},
279
+ );
280
+ throw new YError('E_UNEXPECTED_SUCCESS');
281
+ } catch (err) {
282
+ expect({
283
+ err,
284
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.create.mock.calls,
285
+ readClientGrantsCalls: readClientGrants.mock.calls,
286
+ logCalls: log.mock.calls,
287
+ timeCalls: time.mock.calls,
288
+ }).toMatchInlineSnapshot(`
289
+ {
290
+ "err": [YError: E_OAUTH2_BAD_SCOPE (["admin"]): E_OAUTH2_BAD_SCOPE],
291
+ "logCalls": [
292
+ [
293
+ "warning",
294
+ "⚠️ - Using the token flow is deprecated and not recommended.",
295
+ ],
296
+ [
297
+ "debug",
298
+ "👫 - OAuth2ImplicitGranter Service Initialized!",
299
+ ],
300
+ ],
301
+ "oAuth2AccessTokenCheckCalls": [],
302
+ "readClientGrantsCalls": [
303
+ [
304
+ "abbacaca-abba-caca-abba-cacaabbacaca",
305
+ ],
306
+ ],
307
+ "timeCalls": [],
308
+ }
309
+ `);
310
+ }
311
+ });
312
+ });
313
+
314
+ describe('acknowledger.acknowledge()', () => {
315
+ test('should work with a complete valid flow', async () => {
316
+ const oAuth2AccessTokenGranter = await initOAuth2ImplicitGranter({
317
+ OAUTH2,
318
+ readClientGrants,
319
+ oAuth2AccessToken,
320
+ time,
321
+ log,
322
+ });
323
+
324
+ readClientGrants.mockResolvedValue({
325
+ allowedRedirectURIS: ['https://www.example.com'],
326
+ allowedGrantTypes: ['implicit', 'authorization_code'],
327
+ allowedScopes: ['user', 'admin'],
328
+ authenticationData: {
329
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
330
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
331
+ scopes: ['user'],
332
+ },
333
+ isPublicClient: true,
334
+ });
335
+ oAuth2AccessToken.create.mockResolvedValueOnce({
336
+ token: 'a_new_token',
337
+ expiresAt: Date.parse('2026-07-22T12:49:00Z'),
338
+ });
339
+ time.mockReturnValueOnce(Date.parse('2026-07-22T10:00:00Z'));
340
+
341
+ const authorizerAcknowledgeResult =
342
+ await oAuth2AccessTokenGranter.acknowledge?.(
343
+ {
344
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
345
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
346
+ scopes: ['user'],
347
+ },
348
+ {
349
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
350
+ demandedRedirectURI: 'https://www.example.com',
351
+ demandedScopes: ['user', 'root'],
352
+ },
353
+ {},
354
+ );
355
+
356
+ expect({
357
+ authorizerAcknowledgeResult,
358
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.create.mock.calls,
359
+ readClientGrantsCalls: readClientGrants.mock.calls,
360
+ logCalls: log.mock.calls,
361
+ timeCalls: time.mock.calls,
362
+ }).toMatchInlineSnapshot(`
363
+ {
364
+ "authorizerAcknowledgeResult": {
365
+ "acknowledgedAuthenticationData": {
366
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
367
+ "scopes": [
368
+ "user",
369
+ ],
370
+ "userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
371
+ },
372
+ "acknowledgedData": {
373
+ "accessToken": "a_new_token",
374
+ "expiresIn": 10140,
375
+ "tokenType": "bearer",
376
+ },
377
+ "acknowledgedRedirectURI": "https://www.example.com",
378
+ "acknowledgedScopes": [
379
+ "user",
380
+ ],
381
+ },
382
+ "logCalls": [
383
+ [
384
+ "warning",
385
+ "⚠️ - Using the token flow is deprecated and not recommended.",
386
+ ],
387
+ [
388
+ "debug",
389
+ "👫 - OAuth2ImplicitGranter Service Initialized!",
390
+ ],
391
+ ],
392
+ "oAuth2AccessTokenCheckCalls": [
393
+ [
394
+ {
395
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
396
+ "scopes": [
397
+ "user",
398
+ ],
399
+ "userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
400
+ },
401
+ ],
402
+ ],
403
+ "readClientGrantsCalls": [
404
+ [
405
+ "abbacaca-abba-caca-abba-cacaabbacaca",
406
+ ],
407
+ ],
408
+ "timeCalls": [
409
+ [],
410
+ ],
411
+ }
412
+ `);
413
+ });
414
+
415
+ test('should work with a complete valid flow and filter scopes', async () => {
416
+ const oAuth2AccessTokenGranter = await initOAuth2ImplicitGranter({
417
+ OAUTH2,
418
+ readClientGrants,
419
+ oAuth2AccessToken,
420
+ time,
421
+ log,
422
+ });
423
+
424
+ readClientGrants.mockResolvedValue({
425
+ allowedRedirectURIS: ['https://www.example.com'],
426
+ allowedGrantTypes: ['implicit', 'authorization_code'],
427
+ allowedScopes: ['user', 'admin'],
428
+ authenticationData: {
429
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
430
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
431
+ scopes: [],
432
+ },
433
+ isPublicClient: true,
434
+ });
435
+ oAuth2AccessToken.create.mockResolvedValueOnce({
436
+ token: 'a_new_token',
437
+ expiresAt: Date.parse('2026-07-22T12:49:00Z'),
438
+ });
439
+ time.mockReturnValueOnce(Date.parse('2026-07-22T10:00:00Z'));
440
+
441
+ const authorizerAcknowledgeResult =
442
+ await oAuth2AccessTokenGranter.acknowledge?.(
443
+ {
444
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
445
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
446
+ scopes: ['user'],
447
+ },
448
+ {
449
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
450
+ demandedRedirectURI: 'https://www.example.com',
451
+ demandedScopes: ['user', 'admin', 'oauth2', 'a_scope'],
452
+ },
453
+ {},
454
+ );
455
+
456
+ expect({
457
+ authorizerAcknowledgeResult,
458
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.create.mock.calls,
459
+ readClientGrantsCalls: readClientGrants.mock.calls,
460
+ logCalls: log.mock.calls,
461
+ timeCalls: time.mock.calls,
462
+ }).toMatchInlineSnapshot(`
463
+ {
464
+ "authorizerAcknowledgeResult": {
465
+ "acknowledgedAuthenticationData": {
466
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
467
+ "scopes": [
468
+ "user",
469
+ ],
470
+ "userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
471
+ },
472
+ "acknowledgedData": {
473
+ "accessToken": "a_new_token",
474
+ "expiresIn": 10140,
475
+ "tokenType": "bearer",
476
+ },
477
+ "acknowledgedRedirectURI": "https://www.example.com",
478
+ "acknowledgedScopes": [
479
+ "user",
480
+ ],
481
+ },
482
+ "logCalls": [
483
+ [
484
+ "warning",
485
+ "⚠️ - Using the token flow is deprecated and not recommended.",
486
+ ],
487
+ [
488
+ "debug",
489
+ "👫 - OAuth2ImplicitGranter Service Initialized!",
490
+ ],
491
+ ],
492
+ "oAuth2AccessTokenCheckCalls": [
493
+ [
494
+ {
495
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
496
+ "scopes": [
497
+ "user",
498
+ ],
499
+ "userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
500
+ },
501
+ ],
502
+ ],
503
+ "readClientGrantsCalls": [
504
+ [
505
+ "abbacaca-abba-caca-abba-cacaabbacaca",
506
+ ],
507
+ ],
508
+ "timeCalls": [
509
+ [],
510
+ ],
511
+ }
512
+ `);
513
+ });
514
+
515
+ test('should fail with a secret client and no authentication', async () => {
516
+ const oAuth2AccessTokenGranter = await initOAuth2ImplicitGranter({
517
+ OAUTH2,
518
+ readClientGrants,
519
+ oAuth2AccessToken,
520
+ time,
521
+ log,
522
+ });
523
+
524
+ readClientGrants.mockResolvedValue({
525
+ allowedRedirectURIS: ['https://www.example.com'],
526
+ allowedGrantTypes: ['implicit', 'authorization_code'],
527
+ allowedScopes: ['user', 'admin'],
528
+ authenticationData: {
529
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
530
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
531
+ scopes: [],
532
+ },
533
+ isPublicClient: false,
534
+ });
535
+ oAuth2AccessToken.create.mockResolvedValueOnce({
536
+ token: 'a_new_token',
537
+ expiresAt: Date.parse('2026-07-22T12:49:00Z'),
538
+ });
539
+ time.mockReturnValueOnce(Date.parse('2026-07-22T10:00:00Z'));
540
+
541
+ try {
542
+ await oAuth2AccessTokenGranter.acknowledge?.(
543
+ {
544
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
545
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
546
+ scopes: ['user'],
547
+ },
548
+ {
549
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
550
+ demandedRedirectURI: 'https://www.example.com',
551
+ demandedScopes: ['user'],
552
+ },
553
+ {},
554
+ );
555
+ throw new YError('E_UNEXPECTED_SUCCESS');
556
+ } catch (err) {
557
+ expect({
558
+ err,
559
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.create.mock.calls,
560
+ readClientGrantsCalls: readClientGrants.mock.calls,
561
+ logCalls: log.mock.calls,
562
+ timeCalls: time.mock.calls,
563
+ }).toMatchInlineSnapshot(`
564
+ {
565
+ "err": [YError: E_OAUTH2_AUTHENTICATION_REQUIRED (["abbacaca-abba-caca-abba-cacaabbacaca"]): E_OAUTH2_AUTHENTICATION_REQUIRED],
566
+ "logCalls": [
567
+ [
568
+ "warning",
569
+ "⚠️ - Using the token flow is deprecated and not recommended.",
570
+ ],
571
+ [
572
+ "debug",
573
+ "👫 - OAuth2ImplicitGranter Service Initialized!",
574
+ ],
575
+ ],
576
+ "oAuth2AccessTokenCheckCalls": [],
577
+ "readClientGrantsCalls": [
578
+ [
579
+ "abbacaca-abba-caca-abba-cacaabbacaca",
580
+ ],
581
+ ],
582
+ "timeCalls": [],
583
+ }
584
+ `);
585
+ }
586
+ });
587
+
588
+ test('should fail with a client with not token grant', async () => {
589
+ const oAuth2AccessTokenGranter = await initOAuth2ImplicitGranter({
590
+ OAUTH2,
591
+ readClientGrants,
592
+ oAuth2AccessToken,
593
+ time,
594
+ log,
595
+ });
596
+
597
+ readClientGrants.mockResolvedValue({
598
+ allowedRedirectURIS: ['https://www.example.com'],
599
+ allowedGrantTypes: ['authorization_code'],
600
+ allowedScopes: ['user', 'admin'],
601
+ authenticationData: {
602
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
603
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
604
+ scopes: [],
605
+ },
606
+ isPublicClient: true,
607
+ });
608
+ oAuth2AccessToken.create.mockResolvedValueOnce({
609
+ token: 'a_new_token',
610
+ expiresAt: Date.parse('2026-07-22T12:49:00Z'),
611
+ });
612
+ time.mockReturnValueOnce(Date.parse('2026-07-22T10:00:00Z'));
613
+
614
+ try {
615
+ await oAuth2AccessTokenGranter.acknowledge?.(
616
+ {
617
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
618
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
619
+ scopes: ['user'],
620
+ },
621
+ {
622
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
623
+ demandedRedirectURI: 'http://example.com',
624
+ demandedScopes: ['user'],
625
+ },
626
+ {},
627
+ );
628
+ throw new YError('E_UNEXPECTED_SUCCESS');
629
+ } catch (err) {
630
+ expect({
631
+ err,
632
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.create.mock.calls,
633
+ readClientGrantsCalls: readClientGrants.mock.calls,
634
+ logCalls: log.mock.calls,
635
+ timeCalls: time.mock.calls,
636
+ }).toMatchInlineSnapshot(`
637
+ {
638
+ "err": [YError: E_OAUTH2_GRANT_TYPE_NOT_ALLOWED (["implicit",["authorization_code"]]): E_OAUTH2_GRANT_TYPE_NOT_ALLOWED],
639
+ "logCalls": [
640
+ [
641
+ "warning",
642
+ "⚠️ - Using the token flow is deprecated and not recommended.",
643
+ ],
644
+ [
645
+ "debug",
646
+ "👫 - OAuth2ImplicitGranter Service Initialized!",
647
+ ],
648
+ ],
649
+ "oAuth2AccessTokenCheckCalls": [],
650
+ "readClientGrantsCalls": [
651
+ [
652
+ "abbacaca-abba-caca-abba-cacaabbacaca",
653
+ ],
654
+ ],
655
+ "timeCalls": [],
656
+ }
657
+ `);
658
+ }
659
+ });
660
+
661
+ test('should fail with a client with a bad scope for the client', async () => {
662
+ const oAuth2AccessTokenGranter = await initOAuth2ImplicitGranter({
663
+ OAUTH2: {
664
+ ...OAUTH2,
665
+ strictScopesChecks: true,
666
+ },
667
+ readClientGrants,
668
+ oAuth2AccessToken,
669
+ log,
670
+ });
671
+
672
+ readClientGrants.mockResolvedValue({
673
+ allowedRedirectURIS: ['https://www.example.com'],
674
+ allowedGrantTypes: ['implicit'],
675
+ allowedScopes: ['user'],
676
+ authenticationData: {
677
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
678
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
679
+ scopes: [],
680
+ },
681
+ isPublicClient: true,
682
+ });
683
+ oAuth2AccessToken.create.mockResolvedValueOnce({
684
+ token: 'a_new_token',
685
+ expiresAt: Date.parse('2026-07-22T12:49:00Z'),
686
+ });
687
+ time.mockReturnValueOnce(Date.parse('2026-07-22T10:00:00Z'));
688
+
689
+ try {
690
+ await oAuth2AccessTokenGranter.acknowledge?.(
691
+ {
692
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
693
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
694
+ scopes: ['user'],
695
+ },
696
+ {
697
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
698
+ demandedRedirectURI: 'https://example.com/',
699
+ demandedScopes: ['user', 'admin'],
700
+ },
701
+ {},
702
+ );
703
+ throw new YError('E_UNEXPECTED_SUCCESS');
704
+ } catch (err) {
705
+ expect({
706
+ err,
707
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.create.mock.calls,
708
+ readClientGrantsCalls: readClientGrants.mock.calls,
709
+ logCalls: log.mock.calls,
710
+ timeCalls: time.mock.calls,
711
+ }).toMatchInlineSnapshot(`
712
+ {
713
+ "err": [YError: E_OAUTH2_BAD_SCOPE (["admin"]): E_OAUTH2_BAD_SCOPE],
714
+ "logCalls": [
715
+ [
716
+ "warning",
717
+ "⚠️ - Using the token flow is deprecated and not recommended.",
718
+ ],
719
+ [
720
+ "debug",
721
+ "👫 - OAuth2ImplicitGranter Service Initialized!",
722
+ ],
723
+ ],
724
+ "oAuth2AccessTokenCheckCalls": [],
725
+ "readClientGrantsCalls": [
726
+ [
727
+ "abbacaca-abba-caca-abba-cacaabbacaca",
728
+ ],
729
+ ],
730
+ "timeCalls": [],
731
+ }
732
+ `);
733
+ }
734
+ });
735
+
736
+ test('should fail with a client with a bad scope for the user', async () => {
737
+ const oAuth2AccessTokenGranter = await initOAuth2ImplicitGranter({
738
+ OAUTH2: {
739
+ ...OAUTH2,
740
+ strictScopesChecks: true,
741
+ },
742
+ readClientGrants,
743
+ oAuth2AccessToken,
744
+ log,
745
+ });
746
+
747
+ readClientGrants.mockResolvedValue({
748
+ allowedRedirectURIS: ['https://www.example.com'],
749
+ allowedGrantTypes: ['implicit'],
750
+ allowedScopes: ['user', 'admin'],
751
+ authenticationData: {
752
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
753
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
754
+ scopes: [],
755
+ },
756
+ isPublicClient: true,
757
+ });
758
+ oAuth2AccessToken.create.mockResolvedValueOnce({
759
+ token: 'a_new_token',
760
+ expiresAt: Date.parse('2026-07-22T12:49:00Z'),
761
+ });
762
+ time.mockReturnValueOnce(Date.parse('2026-07-22T10:00:00Z'));
763
+
764
+ try {
765
+ await oAuth2AccessTokenGranter.acknowledge?.(
766
+ {
767
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
768
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
769
+ scopes: ['user'],
770
+ },
771
+ {
772
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
773
+ demandedRedirectURI: 'https://example.com',
774
+ demandedScopes: ['user', 'admin'],
775
+ },
776
+ {},
777
+ );
778
+ throw new YError('E_UNEXPECTED_SUCCESS');
779
+ } catch (err) {
780
+ expect({
781
+ err,
782
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.create.mock.calls,
783
+ readClientGrantsCalls: readClientGrants.mock.calls,
784
+ logCalls: log.mock.calls,
785
+ timeCalls: time.mock.calls,
786
+ }).toMatchInlineSnapshot(`
787
+ {
788
+ "err": [YError: E_OAUTH2_BAD_SCOPE (["admin"]): E_OAUTH2_BAD_SCOPE],
789
+ "logCalls": [
790
+ [
791
+ "warning",
792
+ "⚠️ - Using the token flow is deprecated and not recommended.",
793
+ ],
794
+ [
795
+ "debug",
796
+ "👫 - OAuth2ImplicitGranter Service Initialized!",
797
+ ],
798
+ ],
799
+ "oAuth2AccessTokenCheckCalls": [],
800
+ "readClientGrantsCalls": [
801
+ [
802
+ "abbacaca-abba-caca-abba-cacaabbacaca",
803
+ ],
804
+ ],
805
+ "timeCalls": [],
806
+ }
807
+ `);
808
+ }
809
+ });
810
+ });
811
+ });