@whook/oauth2 26.0.0 → 26.0.3

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 (85) hide show
  1. package/dist/index.d.ts +6 -2
  2. package/dist/index.js +5 -2
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.test.js +421 -2
  5. package/dist/index.test.js.map +1 -1
  6. package/dist/libs/authorizationRequests.d.ts +6 -0
  7. package/dist/libs/authorizationRequests.js +22 -0
  8. package/dist/libs/authorizationRequests.js.map +1 -0
  9. package/dist/libs/authorizationRequests.test.d.ts +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/errors.d.ts +31 -1
  13. package/dist/libs/errors.js +49 -0
  14. package/dist/libs/errors.js.map +1 -1
  15. package/dist/libs/redirectURI.d.ts +1 -0
  16. package/dist/libs/redirectURI.js +16 -0
  17. package/dist/libs/redirectURI.js.map +1 -1
  18. package/dist/libs/schemas.d.ts +8 -0
  19. package/dist/libs/schemas.js +8 -0
  20. package/dist/libs/schemas.js.map +1 -1
  21. package/dist/libs/verifier.d.ts +3 -0
  22. package/dist/libs/verifier.js +14 -0
  23. package/dist/libs/verifier.js.map +1 -1
  24. package/dist/routes/getOAuth2Authorize.d.ts +30 -14
  25. package/dist/routes/getOAuth2Authorize.js +92 -30
  26. package/dist/routes/getOAuth2Authorize.js.map +1 -1
  27. package/dist/routes/getOAuth2Authorize.test.js +826 -19
  28. package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
  29. package/dist/routes/getOAuth2WellKnownMetadata.d.ts +10 -3
  30. package/dist/routes/getOAuth2WellKnownMetadata.js +30 -5
  31. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  32. package/dist/routes/getOAuth2WellKnownMetadata.test.js +20 -18
  33. package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
  34. package/dist/routes/postOAuth2PushedAuthorizationRequest.d.ts +137 -0
  35. package/dist/routes/postOAuth2PushedAuthorizationRequest.js +187 -0
  36. package/dist/routes/postOAuth2PushedAuthorizationRequest.js.map +1 -0
  37. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.d.ts +1 -0
  38. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js +554 -0
  39. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js.map +1 -0
  40. package/dist/routes/postOAuth2Token.d.ts +12 -0
  41. package/dist/routes/postOAuth2Token.js +12 -0
  42. package/dist/routes/postOAuth2Token.js.map +1 -1
  43. package/dist/services/authCookies.js +8 -4
  44. package/dist/services/authCookies.js.map +1 -1
  45. package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +2 -7
  46. package/dist/services/oAuth2AuthorizationCodeGranter.js +5 -22
  47. package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -1
  48. package/dist/services/oAuth2AuthorizationCodeGranter.test.js +12 -141
  49. package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -1
  50. package/dist/services/oAuth2AuthorizationRequests.d.ts +41 -0
  51. package/dist/services/oAuth2AuthorizationRequests.js +61 -0
  52. package/dist/services/oAuth2AuthorizationRequests.js.map +1 -0
  53. package/dist/services/oAuth2AuthorizationRequests.test.d.ts +1 -0
  54. package/dist/services/oAuth2AuthorizationRequests.test.js +249 -0
  55. package/dist/services/oAuth2AuthorizationRequests.test.js.map +1 -0
  56. package/dist/services/oAuth2Granters.d.ts +10 -12
  57. package/dist/services/oAuth2Granters.js.map +1 -1
  58. package/dist/services/oAuth2ImplicitGranter.js +3 -14
  59. package/dist/services/oAuth2ImplicitGranter.js.map +1 -1
  60. package/dist/services/oAuth2ImplicitGranter.test.js +25 -163
  61. package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -1
  62. package/package.json +7 -6
  63. package/src/index.test.ts +459 -3
  64. package/src/index.ts +17 -0
  65. package/src/libs/authorizationRequests.test.ts +40 -0
  66. package/src/libs/authorizationRequests.ts +42 -0
  67. package/src/libs/errors.ts +87 -1
  68. package/src/libs/redirectURI.ts +22 -0
  69. package/src/libs/schemas.ts +10 -0
  70. package/src/libs/verifier.ts +18 -0
  71. package/src/routes/getOAuth2Authorize.test.ts +940 -23
  72. package/src/routes/getOAuth2Authorize.ts +168 -49
  73. package/src/routes/getOAuth2WellKnownMetadata.test.ts +22 -20
  74. package/src/routes/getOAuth2WellKnownMetadata.ts +44 -8
  75. package/src/routes/postOAuth2PushedAuthorizationRequest.test.ts +681 -0
  76. package/src/routes/postOAuth2PushedAuthorizationRequest.ts +292 -0
  77. package/src/routes/postOAuth2Token.ts +12 -0
  78. package/src/services/authCookies.ts +8 -4
  79. package/src/services/oAuth2AuthorizationCodeGranter.test.ts +12 -169
  80. package/src/services/oAuth2AuthorizationCodeGranter.ts +8 -31
  81. package/src/services/oAuth2AuthorizationRequests.test.ts +282 -0
  82. package/src/services/oAuth2AuthorizationRequests.ts +141 -0
  83. package/src/services/oAuth2Granters.ts +12 -14
  84. package/src/services/oAuth2ImplicitGranter.test.ts +29 -190
  85. package/src/services/oAuth2ImplicitGranter.ts +3 -19
@@ -39,102 +39,26 @@ describe('OAuth2ImplicitGranter', () => {
39
39
  log,
40
40
  });
41
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
42
  oAuth2AccessToken.create.mockRejectedValueOnce(
54
43
  new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
55
44
  );
56
45
 
57
46
  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: {
47
+ await oAuth2AccessTokenGranter.authorize?.({
118
48
  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'],
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,
135
59
  },
136
- {},
137
- );
60
+ demandedScopes: ['user'],
61
+ });
138
62
 
139
63
  expect({
140
64
  authorizerAuthenticateResult,
@@ -145,13 +69,9 @@ describe('OAuth2ImplicitGranter', () => {
145
69
  }).toMatchInlineSnapshot(`
146
70
  {
147
71
  "authorizerAuthenticateResult": {
148
- "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
149
- "redirectURI": "https://www.example.com",
150
72
  "scopes": [
151
73
  "user",
152
- "admin",
153
74
  ],
154
- "userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
155
75
  },
156
76
  "logCalls": [
157
77
  [
@@ -164,11 +84,7 @@ describe('OAuth2ImplicitGranter', () => {
164
84
  ],
165
85
  ],
166
86
  "oAuth2AccessTokenCheckCalls": [],
167
- "readClientGrantsCalls": [
168
- [
169
- "abbacaca-abba-caca-abba-cacaabbacaca",
170
- ],
171
- ],
87
+ "readClientGrantsCalls": [],
172
88
  "timeCalls": [],
173
89
  }
174
90
  `);
@@ -183,100 +99,27 @@ describe('OAuth2ImplicitGranter', () => {
183
99
  log,
184
100
  });
185
101
 
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
102
  oAuth2AccessToken.create.mockRejectedValueOnce(
198
103
  new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
199
104
  );
200
105
  time.mockReturnValueOnce(Date.parse('2026-07-22T10:00:00Z'));
201
106
 
202
107
  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: {
108
+ await oAuth2AccessTokenGranter.authorize?.({
260
109
  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'],
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,
277
120
  },
278
- {},
279
- );
121
+ demandedScopes: ['user'],
122
+ });
280
123
  throw new YError('E_UNEXPECTED_SUCCESS');
281
124
  } catch (err) {
282
125
  expect({
@@ -287,7 +130,7 @@ describe('OAuth2ImplicitGranter', () => {
287
130
  timeCalls: time.mock.calls,
288
131
  }).toMatchInlineSnapshot(`
289
132
  {
290
- "err": [YError: E_OAUTH2_BAD_SCOPE (["admin"]): E_OAUTH2_BAD_SCOPE],
133
+ "err": [YError: E_OAUTH2_GRANT_TYPE_NOT_ALLOWED (["implicit",["authorization_code"]]): E_OAUTH2_GRANT_TYPE_NOT_ALLOWED],
291
134
  "logCalls": [
292
135
  [
293
136
  "warning",
@@ -299,11 +142,7 @@ describe('OAuth2ImplicitGranter', () => {
299
142
  ],
300
143
  ],
301
144
  "oAuth2AccessTokenCheckCalls": [],
302
- "readClientGrantsCalls": [
303
- [
304
- "abbacaca-abba-caca-abba-cacaabbacaca",
305
- ],
306
- ],
145
+ "readClientGrantsCalls": [],
307
146
  "timeCalls": [],
308
147
  }
309
148
  `);
@@ -70,27 +70,11 @@ async function initOAuth2ImplicitGranter({
70
70
 
71
71
  const authorizeWithToken: NonNullable<
72
72
  WhookOAuth2ImplicitGranterService['authorize']
73
- > = async ({ clientId, demandedRedirectURI, demandedScopes }) => {
74
- const grants = await readClientGrants(clientId);
75
-
76
- checkGrantType(grants.allowedGrantTypes, IMPLICIT_GRANT_TYPE);
77
- if (demandedRedirectURI) {
78
- checkRedirectURI(grants.allowedRedirectURIS, demandedRedirectURI);
79
- } else {
80
- demandedRedirectURI = grants.allowedRedirectURIS[0];
81
- }
82
-
83
- const filteredScopes = filterScopes(
84
- demandedScopes,
85
- grants.allowedScopes,
86
- !!OAUTH2.strictScopesChecks,
87
- );
73
+ > = async ({ clientGrants, demandedScopes }) => {
74
+ checkGrantType(clientGrants.allowedGrantTypes, IMPLICIT_GRANT_TYPE);
88
75
 
89
76
  return {
90
- ...grants.authenticationData,
91
- clientId,
92
- redirectURI: demandedRedirectURI,
93
- scopes: filteredScopes,
77
+ scopes: demandedScopes,
94
78
  };
95
79
  };
96
80