@whook/oauth2 25.2.1 → 26.0.2

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