@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,474 @@
1
+ import { describe, test, beforeEach, jest, expect } from '@jest/globals';
2
+ import initOAuth2AuthorizationCodeGranter from './oAuth2AuthorizationCodeGranter.js';
3
+ import { YError } from 'yerror';
4
+ describe('OAuth2AuthorizationCodeGranter', () => {
5
+ const OAUTH2 = {
6
+ rootClientId: 'the_root_app_id',
7
+ authenticateURL: 'https://auth.example.com/sign_in',
8
+ allowedScopes: ['user', 'admin', 'root'],
9
+ };
10
+ const oAuth2AuthorizationCode = {
11
+ create: jest.fn(),
12
+ check: jest.fn(),
13
+ };
14
+ const readClientGrants = jest.fn();
15
+ const log = jest.fn();
16
+ beforeEach(() => {
17
+ oAuth2AuthorizationCode.create.mockReset();
18
+ oAuth2AuthorizationCode.check.mockReset();
19
+ readClientGrants.mockReset();
20
+ log.mockReset();
21
+ });
22
+ describe('.authorize()', () => {
23
+ test('should work with a valid request', async () => {
24
+ const oAuth2AuthorizationCodeGranter = await initOAuth2AuthorizationCodeGranter({
25
+ OAUTH2,
26
+ readClientGrants,
27
+ oAuth2AuthorizationCode,
28
+ log,
29
+ });
30
+ readClientGrants.mockResolvedValue({
31
+ allowedScopes: ['user', 'admin'],
32
+ allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
33
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
34
+ authenticationData: {
35
+ clientId: 'the_client_app_id',
36
+ scopes: ['user', 'admin'],
37
+ userId: 'user_id',
38
+ },
39
+ isPublicClient: false,
40
+ });
41
+ oAuth2AuthorizationCode.create.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE'));
42
+ oAuth2AuthorizationCode.check.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE'));
43
+ const authorizerResult = await oAuth2AuthorizationCodeGranter.authorize?.({
44
+ clientId: 'the_client_app_id',
45
+ demandedRedirectURI: 'https://www.example.com/oauth2/code',
46
+ demandedScopes: ['user'],
47
+ }, {
48
+ codeChallenge: 'my_code_challenge',
49
+ codeChallengeMethod: 'plain',
50
+ });
51
+ expect({
52
+ authorizerResult,
53
+ oAuth2AuthorizationCodeCreateCalls: oAuth2AuthorizationCode.create.mock.calls,
54
+ oAuth2AuthorizationCodeCheckCalls: oAuth2AuthorizationCode.check.mock.calls,
55
+ readClientGrantsCalls: readClientGrants.mock.calls,
56
+ logCalls: log.mock.calls,
57
+ }).toMatchInlineSnapshot(`
58
+ {
59
+ "authorizerResult": {
60
+ "clientId": "the_client_app_id",
61
+ "redirectURI": "https://www.example.com/oauth2/code",
62
+ "scopes": [
63
+ "user",
64
+ ],
65
+ },
66
+ "logCalls": [
67
+ [
68
+ "debug",
69
+ "👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
70
+ ],
71
+ ],
72
+ "oAuth2AuthorizationCodeCheckCalls": [],
73
+ "oAuth2AuthorizationCodeCreateCalls": [],
74
+ "readClientGrantsCalls": [
75
+ [
76
+ "the_client_app_id",
77
+ ],
78
+ ],
79
+ }
80
+ `);
81
+ });
82
+ test('should fail with a bad uri', async () => {
83
+ const oAuth2AuthorizationCodeGranter = await initOAuth2AuthorizationCodeGranter({
84
+ OAUTH2,
85
+ readClientGrants,
86
+ oAuth2AuthorizationCode,
87
+ log,
88
+ });
89
+ readClientGrants.mockResolvedValue({
90
+ allowedScopes: ['user', 'admin'],
91
+ allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
92
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
93
+ authenticationData: {
94
+ clientId: 'the_client_app_id',
95
+ scopes: ['user', 'admin'],
96
+ userId: 'user_id',
97
+ },
98
+ isPublicClient: false,
99
+ });
100
+ oAuth2AuthorizationCode.create.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE'));
101
+ oAuth2AuthorizationCode.check.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE'));
102
+ try {
103
+ await oAuth2AuthorizationCodeGranter.authorize?.({
104
+ clientId: 'the_client_app_id',
105
+ demandedRedirectURI: 'https://www.example.com/admin',
106
+ demandedScopes: ['user'],
107
+ }, {
108
+ codeChallenge: 'my_code_challenge',
109
+ codeChallengeMethod: 'plain',
110
+ });
111
+ throw new YError('E_UNEXPECTED_SUCCESS');
112
+ }
113
+ catch (err) {
114
+ expect({
115
+ err,
116
+ oAuth2AuthorizationCodeCreateCalls: oAuth2AuthorizationCode.create.mock.calls,
117
+ oAuth2AuthorizationCodeCheckCalls: oAuth2AuthorizationCode.check.mock.calls,
118
+ readClientGrantsCalls: readClientGrants.mock.calls,
119
+ logCalls: log.mock.calls,
120
+ }).toMatchInlineSnapshot(`
121
+ {
122
+ "err": [YError: E_OAUTH2_BAD_REDIRECT_URI (["https://www.example.com/admin",["https://www.example.com/oauth2/code"]]): E_OAUTH2_BAD_REDIRECT_URI],
123
+ "logCalls": [
124
+ [
125
+ "debug",
126
+ "👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
127
+ ],
128
+ ],
129
+ "oAuth2AuthorizationCodeCheckCalls": [],
130
+ "oAuth2AuthorizationCodeCreateCalls": [],
131
+ "readClientGrantsCalls": [
132
+ [
133
+ "the_client_app_id",
134
+ ],
135
+ ],
136
+ }
137
+ `);
138
+ }
139
+ });
140
+ test('should fail with a bad scope', async () => {
141
+ const oAuth2AuthorizationCodeGranter = await initOAuth2AuthorizationCodeGranter({
142
+ OAUTH2: {
143
+ ...OAUTH2,
144
+ strictScopesChecks: true,
145
+ },
146
+ readClientGrants,
147
+ oAuth2AuthorizationCode,
148
+ log,
149
+ });
150
+ readClientGrants.mockResolvedValue({
151
+ allowedScopes: ['user', 'admin'],
152
+ allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
153
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
154
+ authenticationData: {
155
+ clientId: 'the_client_app_id',
156
+ scopes: ['user', 'admin'],
157
+ userId: 'user_id',
158
+ },
159
+ isPublicClient: false,
160
+ });
161
+ oAuth2AuthorizationCode.create.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE'));
162
+ oAuth2AuthorizationCode.check.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE'));
163
+ try {
164
+ await oAuth2AuthorizationCodeGranter.authorize?.({
165
+ clientId: 'the_client_app_id',
166
+ demandedRedirectURI: 'https://www.example.com/admin',
167
+ demandedScopes: ['god'],
168
+ }, {
169
+ codeChallenge: 'my_code_challenge',
170
+ codeChallengeMethod: 'plain',
171
+ });
172
+ throw new YError('E_UNEXPECTED_SUCCESS');
173
+ }
174
+ catch (err) {
175
+ expect({
176
+ err,
177
+ oAuth2AuthorizationCodeCreateCalls: oAuth2AuthorizationCode.create.mock.calls,
178
+ oAuth2AuthorizationCodeCheckCalls: oAuth2AuthorizationCode.check.mock.calls,
179
+ readClientGrantsCalls: readClientGrants.mock.calls,
180
+ logCalls: log.mock.calls,
181
+ }).toMatchInlineSnapshot(`
182
+ {
183
+ "err": [YError: E_OAUTH2_BAD_SCOPE (["god"]): E_OAUTH2_BAD_SCOPE],
184
+ "logCalls": [
185
+ [
186
+ "debug",
187
+ "👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
188
+ ],
189
+ ],
190
+ "oAuth2AuthorizationCodeCheckCalls": [],
191
+ "oAuth2AuthorizationCodeCreateCalls": [],
192
+ "readClientGrantsCalls": [
193
+ [
194
+ "the_client_app_id",
195
+ ],
196
+ ],
197
+ }
198
+ `);
199
+ }
200
+ });
201
+ });
202
+ describe('.acknowledge()', () => {
203
+ test('should work', async () => {
204
+ const oAuth2AuthorizationCodeGranter = await initOAuth2AuthorizationCodeGranter({
205
+ OAUTH2,
206
+ readClientGrants,
207
+ oAuth2AuthorizationCode,
208
+ log,
209
+ });
210
+ readClientGrants.mockResolvedValue({
211
+ allowedScopes: ['user', 'admin'],
212
+ allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
213
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
214
+ authenticationData: {
215
+ clientId: 'an_app_id',
216
+ scopes: ['user', 'admin'],
217
+ userId: 'user_id',
218
+ },
219
+ isPublicClient: false,
220
+ });
221
+ oAuth2AuthorizationCode.create.mockResolvedValueOnce('yolo');
222
+ oAuth2AuthorizationCode.check.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE'));
223
+ const acknowledgerResult = await oAuth2AuthorizationCodeGranter.acknowledge?.({
224
+ clientId: 'the_root_app_id',
225
+ scopes: ['user', 'admin'],
226
+ userId: 'the_user_id',
227
+ }, {
228
+ clientId: 'the_client_app_id',
229
+ demandedRedirectURI: 'https://www.example.com/oauth2/code',
230
+ demandedScopes: ['user'],
231
+ }, {
232
+ codeChallenge: 'my_code_challenge',
233
+ codeChallengeMethod: 'plain',
234
+ });
235
+ expect({
236
+ acknowledgerResult,
237
+ oAuth2AuthorizationCodeCreateCalls: oAuth2AuthorizationCode.create.mock.calls,
238
+ oAuth2AuthorizationCodeCheckCalls: oAuth2AuthorizationCode.check.mock.calls,
239
+ readClientGrantsCalls: readClientGrants.mock.calls,
240
+ logCalls: log.mock.calls,
241
+ }).toMatchInlineSnapshot(`
242
+ {
243
+ "acknowledgerResult": {
244
+ "acknowledgedAuthenticationData": {
245
+ "clientId": "the_client_app_id",
246
+ "scopes": [
247
+ "user",
248
+ ],
249
+ "userId": "the_user_id",
250
+ },
251
+ "acknowledgedData": {
252
+ "code": "yolo",
253
+ "codeChallenge": "my_code_challenge",
254
+ "codeChallengeMethod": "plain",
255
+ },
256
+ "acknowledgedRedirectURI": "https://www.example.com/oauth2/code",
257
+ "acknowledgedScopes": [
258
+ "user",
259
+ ],
260
+ },
261
+ "logCalls": [
262
+ [
263
+ "debug",
264
+ "👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
265
+ ],
266
+ ],
267
+ "oAuth2AuthorizationCodeCheckCalls": [],
268
+ "oAuth2AuthorizationCodeCreateCalls": [
269
+ [
270
+ {
271
+ "clientId": "the_client_app_id",
272
+ "scopes": [
273
+ "user",
274
+ ],
275
+ "userId": "the_user_id",
276
+ },
277
+ {
278
+ "codeChallenge": "my_code_challenge",
279
+ "codeChallengeMethod": "plain",
280
+ "demandedRedirectURI": "https://www.example.com/oauth2/code",
281
+ "demandedScopes": [
282
+ "user",
283
+ ],
284
+ "filteredScopes": [
285
+ "user",
286
+ ],
287
+ },
288
+ ],
289
+ ],
290
+ "readClientGrantsCalls": [
291
+ [
292
+ "the_client_app_id",
293
+ ],
294
+ ],
295
+ }
296
+ `);
297
+ });
298
+ });
299
+ describe('.authenticate()', () => {
300
+ test('should work with plain code verifier', async () => {
301
+ const oAuth2AuthorizationCodeGranter = await initOAuth2AuthorizationCodeGranter({
302
+ OAUTH2,
303
+ readClientGrants,
304
+ oAuth2AuthorizationCode,
305
+ log,
306
+ });
307
+ readClientGrants.mockResolvedValue({
308
+ allowedScopes: ['user', 'admin'],
309
+ allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
310
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
311
+ authenticationData: {
312
+ clientId: 'the_client_app_id',
313
+ scopes: ['user', 'admin'],
314
+ userId: 'user_id',
315
+ },
316
+ isPublicClient: false,
317
+ });
318
+ oAuth2AuthorizationCode.create.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE'));
319
+ oAuth2AuthorizationCode.check.mockResolvedValueOnce({
320
+ codeAuthenticationData: {
321
+ clientId: 'the_client_app_id',
322
+ scopes: ['user', 'admin'],
323
+ userId: 'the_user_id',
324
+ },
325
+ context: {
326
+ demandedRedirectURI: 'https://www.example.com/oauth2/code',
327
+ demandedScopes: ['user'],
328
+ filteredScopes: ['user'],
329
+ codeChallenge: 'a_code_verifier',
330
+ codeChallengeMethod: 'plain',
331
+ },
332
+ });
333
+ const authenticatorResult = await oAuth2AuthorizationCodeGranter.authenticate?.({
334
+ code: 'a_code',
335
+ redirectURI: 'https://www.example.com/oauth2/code',
336
+ clientId: 'the_client_app_id',
337
+ codeVerifier: 'a_code_verifier',
338
+ }, {
339
+ clientId: 'the_client_app_id',
340
+ scopes: ['user', 'admin'],
341
+ userId: 'the_user_id',
342
+ });
343
+ expect({
344
+ authenticatorResult,
345
+ oAuth2AuthorizationCodeCreateCalls: oAuth2AuthorizationCode.create.mock.calls,
346
+ oAuth2AuthorizationCodeCheckCalls: oAuth2AuthorizationCode.check.mock.calls,
347
+ readClientGrantsCalls: readClientGrants.mock.calls,
348
+ logCalls: log.mock.calls,
349
+ }).toMatchInlineSnapshot(`
350
+ {
351
+ "authenticatorResult": {
352
+ "clientId": "the_client_app_id",
353
+ "scopes": [
354
+ "user",
355
+ ],
356
+ "userId": "the_user_id",
357
+ },
358
+ "logCalls": [
359
+ [
360
+ "debug",
361
+ "👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
362
+ ],
363
+ ],
364
+ "oAuth2AuthorizationCodeCheckCalls": [
365
+ [
366
+ {
367
+ "clientId": "the_client_app_id",
368
+ "scopes": [
369
+ "user",
370
+ "admin",
371
+ ],
372
+ "userId": "the_user_id",
373
+ },
374
+ "a_code",
375
+ ],
376
+ ],
377
+ "oAuth2AuthorizationCodeCreateCalls": [],
378
+ "readClientGrantsCalls": [
379
+ [
380
+ "the_client_app_id",
381
+ ],
382
+ ],
383
+ }
384
+ `);
385
+ });
386
+ test('should work with plain S256 verifier', async () => {
387
+ const oAuth2AuthorizationCodeGranter = await initOAuth2AuthorizationCodeGranter({
388
+ OAUTH2,
389
+ readClientGrants,
390
+ oAuth2AuthorizationCode,
391
+ log,
392
+ });
393
+ readClientGrants.mockResolvedValue({
394
+ allowedScopes: ['user', 'admin'],
395
+ allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
396
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
397
+ authenticationData: {
398
+ clientId: 'the_client_app_id',
399
+ scopes: ['user', 'admin'],
400
+ userId: 'user_id',
401
+ },
402
+ isPublicClient: false,
403
+ });
404
+ oAuth2AuthorizationCode.create.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE'));
405
+ oAuth2AuthorizationCode.check.mockResolvedValueOnce({
406
+ codeAuthenticationData: {
407
+ clientId: 'the_client_app_id',
408
+ scopes: ['user', 'admin'],
409
+ userId: 'the_user_id',
410
+ },
411
+ context: {
412
+ demandedRedirectURI: 'https://www.example.com/oauth2/code',
413
+ demandedScopes: ['user'],
414
+ filteredScopes: ['user'],
415
+ codeChallenge: 'pOX5Ly27TpE8bQ0ZRggD_gN3gS_Q6LZDdR7DDP9wwKU',
416
+ codeChallengeMethod: 'S256',
417
+ },
418
+ });
419
+ const authenticatorResult = await oAuth2AuthorizationCodeGranter.authenticate?.({
420
+ code: 'a_code',
421
+ redirectURI: 'https://www.example.com/oauth2/code',
422
+ clientId: 'the_client_app_id',
423
+ codeVerifier: 'a_code_verifier',
424
+ }, {
425
+ clientId: 'the_client_app_id',
426
+ scopes: ['user', 'admin'],
427
+ userId: 'the_user_id',
428
+ });
429
+ expect({
430
+ authenticatorResult,
431
+ oAuth2AuthorizationCodeCreateCalls: oAuth2AuthorizationCode.create.mock.calls,
432
+ oAuth2AuthorizationCodeCheckCalls: oAuth2AuthorizationCode.check.mock.calls,
433
+ readClientGrantsCalls: readClientGrants.mock.calls,
434
+ logCalls: log.mock.calls,
435
+ }).toMatchInlineSnapshot(`
436
+ {
437
+ "authenticatorResult": {
438
+ "clientId": "the_client_app_id",
439
+ "scopes": [
440
+ "user",
441
+ ],
442
+ "userId": "the_user_id",
443
+ },
444
+ "logCalls": [
445
+ [
446
+ "debug",
447
+ "👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
448
+ ],
449
+ ],
450
+ "oAuth2AuthorizationCodeCheckCalls": [
451
+ [
452
+ {
453
+ "clientId": "the_client_app_id",
454
+ "scopes": [
455
+ "user",
456
+ "admin",
457
+ ],
458
+ "userId": "the_user_id",
459
+ },
460
+ "a_code",
461
+ ],
462
+ ],
463
+ "oAuth2AuthorizationCodeCreateCalls": [],
464
+ "readClientGrantsCalls": [
465
+ [
466
+ "the_client_app_id",
467
+ ],
468
+ ],
469
+ }
470
+ `);
471
+ });
472
+ });
473
+ });
474
+ //# sourceMappingURL=oAuth2AuthorizationCodeGranter.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"oAuth2AuthorizationCodeGranter.test.js","sourceRoot":"","sources":["../../src/services/oAuth2AuthorizationCodeGranter.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAKzE,OAAO,kCAEN,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,QAAQ,CAAC,gCAAgC,EAAE,GAAG,EAAE;IAC9C,MAAM,MAAM,GAAuB;QACjC,YAAY,EAAE,iBAAiB;QAC/B,eAAe,EAAE,kCAAkC;QACnD,aAAa,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC;KACzC,CAAC;IACF,MAAM,uBAAuB,GAAG;QAC9B,MAAM,EAAE,IAAI,CAAC,EAAE,EAAiD;QAChE,KAAK,EAAE,IAAI,CAAC,EAAE,EAAgD;KAC/D,CAAC;IACF,MAAM,gBAAgB,GAAG,IAAI,CAAC,EAAE,EAAsC,CAAC;IACvE,MAAM,GAAG,GAAG,IAAI,CAAC,EAAE,EAAE,CAAC;IAEtB,UAAU,CAAC,GAAG,EAAE;QACd,uBAAuB,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;QAC3C,uBAAuB,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC;QAC1C,gBAAgB,CAAC,SAAS,EAAE,CAAC;QAC7B,GAAG,CAAC,SAAS,EAAE,CAAC;IAClB,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,cAAc,EAAE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E,CACjD;gBACE,IAAI,EAAE,QAAQ;gBACd,WAAW,EAAE,qCAAqC;gBAClD,QAAQ,EAAE,mBAAmB;gBAC7B,YAAY,EAAE,iBAAiB;aAChC,EACD;gBACE,QAAQ,EAAE,mBAAmB;gBAC7B,MAAM,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC;gBACzB,MAAM,EAAE,aAAa;aACtB,CACF,CAAC;YAEJ,MAAM,CAAC;gBACL,mBAAmB;gBACnB,kCAAkC,EAChC,uBAAuB,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK;gBAC3C,iCAAiC,EAC/B,uBAAuB,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK;gBAC1C,qBAAqB,EAAE,gBAAgB,CAAC,IAAI,CAAC,KAAK;gBAClD,QAAQ,EAAE,GAAG,CAAC,IAAI,CAAC,KAAK;aACzB,CAAC,CAAC,qBAAqB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAmCxB,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -1,17 +1,33 @@
1
1
  import { type LogService } from 'common-services';
2
- import { type OAuth2GranterService, type CheckApplicationService } from './oAuth2Granters.js';
3
- import { type WhookAuthenticationData } from '@whook/authorization';
4
- import { type OAuth2Options } from '../services/oAuth2Granters.js';
5
- export interface OAuth2ClientCredentialsGranterDependencies {
6
- OAUTH2: OAuth2Options;
7
- checkApplication: CheckApplicationService;
2
+ import { type WhookOAuth2GranterService, type WhookOAuth2ReadClientGrantsService, type WhookOAuth2Options, type WhookOAuth2GranterDefinitions } from './oAuth2Granters.js';
3
+ import { type WhookAuthenticationScope } from '@whook/authorization';
4
+ export declare const CLIENT_CREDENTIALS_GRANT_TYPE = "client_credentials";
5
+ export declare const clientCredentialsTokenRequestBodySchema: {
6
+ readonly name: "ClientCredentialsRequestBody";
7
+ readonly schema: {
8
+ readonly type: "object";
9
+ readonly description: "Client credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.4";
10
+ readonly required: ["grant_type"];
11
+ readonly properties: {
12
+ readonly grant_type: {
13
+ readonly type: "string";
14
+ readonly const: "client_credentials";
15
+ };
16
+ readonly scope: import("ya-json-schema-types").ExpressiveJSONSchema;
17
+ };
18
+ };
19
+ };
20
+ export interface WhookOAuth2ClientCredentialsGranterDependencies {
21
+ OAUTH2: WhookOAuth2Options;
22
+ readClientGrants: WhookOAuth2ReadClientGrantsService;
8
23
  log?: LogService;
9
24
  }
10
- export interface OAuth2ClientCredentialsGranterParameters {
11
- username: string;
12
- password: string;
13
- scope?: WhookAuthenticationData['scope'];
25
+ export interface WhookOAuth2ClientCredentialsGranterDefinitions extends WhookOAuth2GranterDefinitions {
26
+ grantType: typeof CLIENT_CREDENTIALS_GRANT_TYPE;
27
+ authenticateParameters: {
28
+ demandedScopes: WhookAuthenticationScope[];
29
+ };
14
30
  }
15
- export type OAuth2ClientCredentialsGranterService = OAuth2GranterService<Record<string, unknown>, Record<string, unknown>, Record<string, unknown>, OAuth2ClientCredentialsGranterParameters>;
16
- declare const _default: import("knifecycle").ServiceInitializer<OAuth2ClientCredentialsGranterDependencies, OAuth2ClientCredentialsGranterService>;
31
+ export type WhookOAuth2ClientCredentialsGranterService = WhookOAuth2GranterService<WhookOAuth2ClientCredentialsGranterDefinitions>;
32
+ declare const _default: import("knifecycle").ServiceInitializer<WhookOAuth2ClientCredentialsGranterDependencies, WhookOAuth2ClientCredentialsGranterService>;
17
33
  export default _default;
@@ -1,38 +1,56 @@
1
1
  import { autoService, location } from 'knifecycle';
2
- import { noop } from '@whook/whook';
2
+ import { noop, refersTo } from '@whook/whook';
3
3
  import { YError } from 'yerror';
4
+ import { checkGrantType } from '../libs/grants.js';
5
+ import { filterScopes } from '../libs/scopes.js';
6
+ import { scopeSchema } from '../libs/schemas.js';
7
+ export const CLIENT_CREDENTIALS_GRANT_TYPE = 'client_credentials';
8
+ export const clientCredentialsTokenRequestBodySchema = {
9
+ name: 'ClientCredentialsRequestBody',
10
+ schema: {
11
+ type: 'object',
12
+ description: 'Client credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.4',
13
+ required: ['grant_type'],
14
+ properties: {
15
+ grant_type: {
16
+ type: 'string',
17
+ const: CLIENT_CREDENTIALS_GRANT_TYPE,
18
+ },
19
+ scope: refersTo(scopeSchema),
20
+ },
21
+ },
22
+ };
4
23
  export default location(autoService(initOAuth2ClientCredentialsGranter), import.meta.url);
5
24
  // Client Credentials Grant
6
25
  // https://tools.ietf.org/html/rfc6749#section-4.4
7
- async function initOAuth2ClientCredentialsGranter({ OAUTH2, checkApplication, log = noop, }) {
8
- const authenticateWithClientCredentials = async ({ scope: demandedScope = '' }, authenticationData) => {
9
- // The client must be authenticated
26
+ async function initOAuth2ClientCredentialsGranter({ OAUTH2, readClientGrants, log = noop, }) {
27
+ const authenticateWithClientCredentials = async ({ demandedScopes }, authenticationData) => {
10
28
  if (!authenticationData) {
11
29
  throw new YError('E_UNAUTHORIZED');
12
30
  }
13
- // Checking the scope and availability of the demanded
14
- // grant type
15
- await checkApplication({
16
- applicationId: authenticationData.applicationId,
17
- type: 'client_credentials',
18
- scope: demandedScope,
19
- });
31
+ const grants = await readClientGrants(authenticationData.clientId);
32
+ if (authenticationData.clientId !== grants.authenticationData.clientId) {
33
+ throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
34
+ authenticationData.clientId,
35
+ grants.authenticationData.clientId,
36
+ ]);
37
+ }
38
+ checkGrantType(grants.allowedGrantTypes, CLIENT_CREDENTIALS_GRANT_TYPE);
39
+ const filteredScopes = filterScopes(demandedScopes.length
40
+ ? demandedScopes
41
+ : OAUTH2.defaultToClientScope
42
+ ? authenticationData.scopes
43
+ : [], grants.allowedScopes, !!OAUTH2.strictScopesChecks);
20
44
  return {
21
45
  ...authenticationData,
22
- scope: demandedScope
23
- ? demandedScope
24
- : OAUTH2.defaultToClientScope
25
- ? authenticationData.scope
26
- : '',
46
+ scopes: filteredScopes,
27
47
  };
28
48
  };
29
49
  log('debug', '👫 - OAuth2ClientCredentialsGranter Service Initialized!');
30
50
  return {
31
- type: 'client_credentials',
32
- authenticator: {
33
- grantType: 'client_credentials',
34
- authenticate: authenticateWithClientCredentials,
35
- },
51
+ grantType: CLIENT_CREDENTIALS_GRANT_TYPE,
52
+ issuesRefreshToken: false,
53
+ authenticate: authenticateWithClientCredentials,
36
54
  };
37
55
  }
38
56
  //# sourceMappingURL=oAuth2ClientCredentialsGranter.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"oAuth2ClientCredentialsGranter.js","sourceRoot":"","sources":["../../src/services/oAuth2ClientCredentialsGranter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AA0BhC,eAAe,QAAQ,CACrB,WAAW,CAAC,kCAAkC,CAAC,EAC/C,MAAM,CAAC,IAAI,CAAC,GAAG,CAChB,CAAC;AAEF,2BAA2B;AAC3B,kDAAkD;AAClD,KAAK,UAAU,kCAAkC,CAAC,EAChD,MAAM,EACN,gBAAgB,EAChB,GAAG,GAAG,IAAI,GACiC;IAC3C,MAAM,iCAAiC,GAEnB,KAAK,EACvB,EAAE,KAAK,EAAE,aAAa,GAAG,EAAE,EAAE,EAC7B,kBAAkB,EAClB,EAAE;QACF,mCAAmC;QACnC,IAAI,CAAC,kBAAkB,EAAE,CAAC;YACxB,MAAM,IAAI,MAAM,CAAC,gBAAgB,CAAC,CAAC;QACrC,CAAC;QAED,sDAAsD;QACtD,aAAa;QACb,MAAM,gBAAgB,CAAC;YACrB,aAAa,EAAE,kBAAkB,CAAC,aAAa;YAC/C,IAAI,EAAE,oBAAoB;YAC1B,KAAK,EAAE,aAAa;SACrB,CAAC,CAAC;QAEH,OAAO;YACL,GAAG,kBAAkB;YACrB,KAAK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