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