@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,681 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { describe, test, beforeEach, jest, expect } from '@jest/globals';
3
+ import initPostOAuth2PushedAuthorizationRequest from './postOAuth2PushedAuthorizationRequest.js';
4
+ import { YError } from 'yerror';
5
+ import {
6
+ type WhookOAuth2GranterDefinitions,
7
+ type WhookOAuth2Options,
8
+ type WhookOAuth2GranterService,
9
+ type WhookOAuth2ReadClientGrantsService,
10
+ } from '../services/oAuth2Granters.js';
11
+ import { type LogService } from 'common-services';
12
+ import { type WhookOAuth2AuthorizationCodeGranterService } from '../services/oAuth2AuthorizationCodeGranter.js';
13
+ import { type WhookOAuth2ImplicitGranterService } from '../services/oAuth2ImplicitGranter.js';
14
+ import {
15
+ WhookOAuth2AuthorizationRequestsOptions,
16
+ WhookOAuth2AuthorizationRequestsService,
17
+ } from '../services/oAuth2AuthorizationRequests.js';
18
+
19
+ describe('postOAuth2PushedAuthorizationRequest', () => {
20
+ const OAUTH2_PAR: WhookOAuth2AuthorizationRequestsOptions = {
21
+ ttl: 76000,
22
+ mode: 'enabled',
23
+ };
24
+ const OAUTH2: WhookOAuth2Options = {
25
+ rootClientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
26
+ authenticateURL: 'https://auth.example.com/sign_in',
27
+ allowedScopes: ['user'],
28
+ };
29
+ const log = jest.fn<LogService>();
30
+ const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
31
+ const oAuth2AuthorizationRequests = {
32
+ create: jest.fn<WhookOAuth2AuthorizationRequestsService['create']>(),
33
+ };
34
+ const codeGranter = {
35
+ grantType: 'authorization_code',
36
+ responseType: 'code',
37
+ issuesRefreshToken: true,
38
+ authorize:
39
+ jest.fn<
40
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['authorize']>
41
+ >(),
42
+ acknowledge:
43
+ jest.fn<
44
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['acknowledge']>
45
+ >(),
46
+ authenticate:
47
+ jest.fn<
48
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['authenticate']>
49
+ >(),
50
+ } satisfies WhookOAuth2AuthorizationCodeGranterService;
51
+ const tokenGranter = {
52
+ grantType: 'implicit',
53
+ responseType: 'token',
54
+ issuesRefreshToken: false,
55
+ authorize:
56
+ jest.fn<
57
+ NonNullable<NonNullable<WhookOAuth2ImplicitGranterService['authorize']>>
58
+ >(),
59
+ acknowledge:
60
+ jest.fn<NonNullable<WhookOAuth2ImplicitGranterService['acknowledge']>>(),
61
+ } satisfies WhookOAuth2ImplicitGranterService;
62
+ const oAuth2Granters = [
63
+ codeGranter,
64
+ tokenGranter,
65
+ ] as unknown as WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
66
+
67
+ beforeEach(() => {
68
+ log.mockReset();
69
+ [
70
+ codeGranter.authorize,
71
+ codeGranter.acknowledge,
72
+ codeGranter.authenticate,
73
+ tokenGranter.authorize,
74
+ tokenGranter.acknowledge,
75
+ readClientGrants,
76
+ oAuth2AuthorizationRequests.create,
77
+ ].forEach((mock) => mock.mockReset());
78
+ });
79
+
80
+ test('should issue a request URI', async () => {
81
+ [
82
+ codeGranter.acknowledge,
83
+ codeGranter.authenticate,
84
+ tokenGranter.authorize,
85
+ tokenGranter.acknowledge,
86
+ ].forEach((mock: any) =>
87
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
88
+ );
89
+ codeGranter.authorize.mockResolvedValueOnce({
90
+ scopes: ['user'],
91
+ });
92
+
93
+ const postOAuth2PushedAuthorizationRequest =
94
+ await initPostOAuth2PushedAuthorizationRequest({
95
+ OAUTH2,
96
+ OAUTH2_PAR,
97
+ oAuth2Granters,
98
+ readClientGrants,
99
+ oAuth2AuthorizationRequests,
100
+ log,
101
+ });
102
+
103
+ readClientGrants.mockResolvedValueOnce({
104
+ allowedGrantTypes: ['code'],
105
+ allowedScopes: ['user'],
106
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
107
+ isPublicClient: true,
108
+ canAcknowledge: false,
109
+ authenticationData: {
110
+ clientId: 'a_client_id',
111
+ scopes: [],
112
+ userId: '1',
113
+ },
114
+ });
115
+ oAuth2AuthorizationRequests.create.mockResolvedValueOnce({
116
+ requestURI: 'urn:ietf:params:oauth:request_uri:a_request_uri',
117
+ expiresIn: 1000,
118
+ });
119
+
120
+ const response = await postOAuth2PushedAuthorizationRequest({
121
+ body: {
122
+ response_type: 'code',
123
+ client_id: 'a_client_id',
124
+ redirect_uri: 'https://example.com/oauth/cb',
125
+ scope: 'user',
126
+ state: 'bancal',
127
+ },
128
+ });
129
+
130
+ expect({
131
+ response,
132
+ logCalls: log.mock.calls,
133
+ readClientGrantsCalls: readClientGrants.mock.calls,
134
+ oAuth2AuthorizationRequestsCreateCalls:
135
+ oAuth2AuthorizationRequests.create.mock.calls,
136
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
137
+ codeGranterAcknowledgerAcknowledgeCalls:
138
+ codeGranter.acknowledge.mock.calls,
139
+ codeGranterAuthenticatorAuthenticateCalls:
140
+ codeGranter.authenticate.mock.calls,
141
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
142
+ tokenGranterAcknowledgerAcknowledgeCalls:
143
+ tokenGranter.acknowledge.mock.calls,
144
+ }).toMatchInlineSnapshot(`
145
+ {
146
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
147
+ "codeGranterAuthenticatorAuthenticateCalls": [],
148
+ "codeGranterAuthorizerAuthorizeCalls": [
149
+ [
150
+ {
151
+ "clientGrants": {
152
+ "allowedGrantTypes": [
153
+ "code",
154
+ ],
155
+ "allowedRedirectURIS": [
156
+ "https://example.com/oauth/cb",
157
+ ],
158
+ "allowedScopes": [
159
+ "user",
160
+ ],
161
+ "authenticationData": {
162
+ "clientId": "a_client_id",
163
+ "scopes": [],
164
+ "userId": "1",
165
+ },
166
+ "canAcknowledge": false,
167
+ "isPublicClient": true,
168
+ },
169
+ "clientId": "a_client_id",
170
+ "demandedScopes": [
171
+ "user",
172
+ ],
173
+ },
174
+ ],
175
+ ],
176
+ "logCalls": [],
177
+ "oAuth2AuthorizationRequestsCreateCalls": [
178
+ [
179
+ "a_client_id",
180
+ {
181
+ "client_id": "a_client_id",
182
+ "redirect_uri": "https://example.com/oauth/cb",
183
+ "response_type": "code",
184
+ "scope": "user",
185
+ "state": "bancal",
186
+ },
187
+ ],
188
+ ],
189
+ "readClientGrantsCalls": [
190
+ [
191
+ "a_client_id",
192
+ ],
193
+ ],
194
+ "response": {
195
+ "body": {
196
+ "expires_in": 1,
197
+ "request_uri": "urn:ietf:params:oauth:request_uri:a_request_uri",
198
+ },
199
+ "headers": {
200
+ "cache-control": "no-store",
201
+ "pragma": "no-cache",
202
+ },
203
+ "status": 201,
204
+ },
205
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
206
+ "tokenGranterAuthorizerAuthorizeCalls": [],
207
+ }
208
+ `);
209
+ });
210
+
211
+ test('should reject nested request_uri parameter', async () => {
212
+ [
213
+ codeGranter.authorize,
214
+ codeGranter.acknowledge,
215
+ codeGranter.authenticate,
216
+ tokenGranter.authorize,
217
+ tokenGranter.acknowledge,
218
+ oAuth2AuthorizationRequests.create,
219
+ ].forEach((mock: any) =>
220
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
221
+ );
222
+
223
+ const postOAuth2PushedAuthorizationRequest =
224
+ await initPostOAuth2PushedAuthorizationRequest({
225
+ OAUTH2,
226
+ OAUTH2_PAR,
227
+ oAuth2Granters,
228
+ readClientGrants,
229
+ oAuth2AuthorizationRequests,
230
+ log,
231
+ });
232
+
233
+ readClientGrants.mockResolvedValueOnce({
234
+ allowedGrantTypes: ['code'],
235
+ allowedScopes: ['user'],
236
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
237
+ isPublicClient: true,
238
+ canAcknowledge: false,
239
+ authenticationData: {
240
+ clientId: 'a_client_id',
241
+ scopes: [],
242
+ userId: '1',
243
+ },
244
+ });
245
+
246
+ await expect(
247
+ postOAuth2PushedAuthorizationRequest({
248
+ body: {
249
+ response_type: 'code',
250
+ client_id: 'a_client_id',
251
+ redirect_uri: 'https://example.com/oauth/cb',
252
+ scope: 'user',
253
+ state: 'bancal',
254
+ request_uri: 'urn:ietf:params:oauth:request_uri:test',
255
+ },
256
+ }),
257
+ ).rejects.toMatchObject({
258
+ code: 'E_OAUTH2_REQUEST_URI_NOT_ALLOWED',
259
+ });
260
+ });
261
+
262
+ test('should reject client mismatch', async () => {
263
+ [
264
+ codeGranter.authorize,
265
+ codeGranter.acknowledge,
266
+ codeGranter.authenticate,
267
+ tokenGranter.authorize,
268
+ tokenGranter.acknowledge,
269
+ oAuth2AuthorizationRequests.create,
270
+ ].forEach((mock: any) =>
271
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
272
+ );
273
+
274
+ const postOAuth2PushedAuthorizationRequest =
275
+ await initPostOAuth2PushedAuthorizationRequest({
276
+ OAUTH2,
277
+ OAUTH2_PAR,
278
+ oAuth2Granters,
279
+ readClientGrants,
280
+ oAuth2AuthorizationRequests,
281
+ log,
282
+ });
283
+
284
+ readClientGrants.mockResolvedValueOnce({
285
+ allowedGrantTypes: ['code'],
286
+ allowedScopes: ['user'],
287
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
288
+ isPublicClient: true,
289
+ canAcknowledge: false,
290
+ authenticationData: {
291
+ clientId: 'another_client_id',
292
+ scopes: [],
293
+ userId: '1',
294
+ },
295
+ });
296
+
297
+ await expect(
298
+ postOAuth2PushedAuthorizationRequest({
299
+ body: {
300
+ response_type: 'code',
301
+ client_id: 'a_client_id',
302
+ redirect_uri: 'https://example.com/oauth/cb',
303
+ scope: 'user',
304
+ state: 'bancal',
305
+ request_uri: 'urn:ietf:params:oauth:request_uri:test',
306
+ },
307
+ }),
308
+ ).rejects.toMatchObject({
309
+ code: 'E_OAUTH2_CLIENT_GRANTS_MISMATCH',
310
+ });
311
+ });
312
+
313
+ test('should reject not existing clients', async () => {
314
+ [
315
+ codeGranter.authorize,
316
+ codeGranter.acknowledge,
317
+ codeGranter.authenticate,
318
+ tokenGranter.authorize,
319
+ tokenGranter.acknowledge,
320
+ ].forEach((mock: any) =>
321
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
322
+ );
323
+
324
+ const postOAuth2PushedAuthorizationRequest =
325
+ await initPostOAuth2PushedAuthorizationRequest({
326
+ OAUTH2,
327
+ OAUTH2_PAR,
328
+ oAuth2Granters,
329
+ readClientGrants,
330
+ oAuth2AuthorizationRequests,
331
+ log,
332
+ });
333
+
334
+ readClientGrants.mockRejectedValueOnce(
335
+ new YError('E_OAUTH2_CLIENT_NOT_FOUND', ['the_client_app_id']),
336
+ );
337
+
338
+ await expect(
339
+ postOAuth2PushedAuthorizationRequest({
340
+ body: {
341
+ response_type: 'code',
342
+ client_id: 'a_client_id',
343
+ redirect_uri: 'https://example.com/oauth/cb',
344
+ scope: 'user',
345
+ state: 'bancal',
346
+ request_uri: 'urn:ietf:params:oauth:request_uri:test',
347
+ },
348
+ }),
349
+ ).rejects.toMatchObject({
350
+ code: 'E_OAUTH2_CLIENT_NOT_FOUND',
351
+ });
352
+ });
353
+
354
+ test('should reject bad redirect uri', async () => {
355
+ [
356
+ codeGranter.authorize,
357
+ codeGranter.acknowledge,
358
+ codeGranter.authenticate,
359
+ tokenGranter.authorize,
360
+ tokenGranter.acknowledge,
361
+ oAuth2AuthorizationRequests.create,
362
+ ].forEach((mock: any) =>
363
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
364
+ );
365
+
366
+ const postOAuth2PushedAuthorizationRequest =
367
+ await initPostOAuth2PushedAuthorizationRequest({
368
+ OAUTH2,
369
+ OAUTH2_PAR,
370
+ oAuth2Granters,
371
+ readClientGrants,
372
+ oAuth2AuthorizationRequests,
373
+ log,
374
+ });
375
+
376
+ readClientGrants.mockResolvedValueOnce({
377
+ allowedGrantTypes: ['code'],
378
+ allowedScopes: ['user'],
379
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
380
+ isPublicClient: true,
381
+ canAcknowledge: false,
382
+ authenticationData: {
383
+ clientId: 'a_client_id',
384
+ scopes: [],
385
+ userId: '1',
386
+ },
387
+ });
388
+
389
+ await expect(
390
+ postOAuth2PushedAuthorizationRequest({
391
+ body: {
392
+ response_type: 'code',
393
+ client_id: 'a_client_id',
394
+ redirect_uri: 'https://example.com',
395
+ scope: 'user',
396
+ state: 'bancal',
397
+ },
398
+ }),
399
+ ).rejects.toMatchObject({
400
+ code: 'E_OAUTH2_BAD_REDIRECT_URI',
401
+ });
402
+ });
403
+
404
+ test('should reject secret client with no authentication', async () => {
405
+ [
406
+ codeGranter.authorize,
407
+ codeGranter.acknowledge,
408
+ codeGranter.authenticate,
409
+ tokenGranter.authorize,
410
+ tokenGranter.acknowledge,
411
+ oAuth2AuthorizationRequests.create,
412
+ ].forEach((mock: any) =>
413
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
414
+ );
415
+
416
+ const postOAuth2PushedAuthorizationRequest =
417
+ await initPostOAuth2PushedAuthorizationRequest({
418
+ OAUTH2,
419
+ OAUTH2_PAR,
420
+ oAuth2Granters,
421
+ readClientGrants,
422
+ oAuth2AuthorizationRequests,
423
+ log,
424
+ });
425
+
426
+ readClientGrants.mockResolvedValueOnce({
427
+ allowedGrantTypes: ['code'],
428
+ allowedScopes: ['user'],
429
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
430
+ isPublicClient: false,
431
+ canAcknowledge: false,
432
+ authenticationData: {
433
+ clientId: 'a_client_id',
434
+ scopes: [],
435
+ userId: '1',
436
+ },
437
+ });
438
+
439
+ await expect(
440
+ postOAuth2PushedAuthorizationRequest({
441
+ body: {
442
+ response_type: 'code',
443
+ client_id: 'a_client_id',
444
+ redirect_uri: 'https://example.com',
445
+ scope: 'user',
446
+ state: 'bancal',
447
+ },
448
+ }),
449
+ ).rejects.toMatchObject({
450
+ code: 'E_OAUTH2_AUTHENTICATION_REQUIRED',
451
+ });
452
+ });
453
+
454
+ test('should reject secret client id mismatch', async () => {
455
+ [
456
+ codeGranter.authorize,
457
+ codeGranter.acknowledge,
458
+ codeGranter.authenticate,
459
+ tokenGranter.authorize,
460
+ tokenGranter.acknowledge,
461
+ oAuth2AuthorizationRequests.create,
462
+ readClientGrants,
463
+ ].forEach((mock: any) =>
464
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
465
+ );
466
+
467
+ const postOAuth2PushedAuthorizationRequest =
468
+ await initPostOAuth2PushedAuthorizationRequest({
469
+ OAUTH2,
470
+ OAUTH2_PAR,
471
+ oAuth2Granters,
472
+ readClientGrants,
473
+ oAuth2AuthorizationRequests,
474
+ log,
475
+ });
476
+
477
+ await expect(
478
+ postOAuth2PushedAuthorizationRequest({
479
+ body: {
480
+ response_type: 'code',
481
+ client_id: 'a_client_id',
482
+ redirect_uri: 'https://example.com',
483
+ scope: 'user',
484
+ state: 'bancal',
485
+ },
486
+ authenticationData: {
487
+ clientId: 'a_different_client_id',
488
+ scopes: [],
489
+ userId: '1',
490
+ },
491
+ }),
492
+ ).rejects.toMatchObject({
493
+ code: 'E_OAUTH2_CLIENT_MISMATCH',
494
+ });
495
+ });
496
+
497
+ test('should reject bad scopes', async () => {
498
+ [
499
+ codeGranter.authorize,
500
+ codeGranter.acknowledge,
501
+ codeGranter.authenticate,
502
+ tokenGranter.authorize,
503
+ tokenGranter.acknowledge,
504
+ oAuth2AuthorizationRequests.create,
505
+ ].forEach((mock: any) =>
506
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
507
+ );
508
+
509
+ const postOAuth2PushedAuthorizationRequest =
510
+ await initPostOAuth2PushedAuthorizationRequest({
511
+ OAUTH2: {
512
+ ...OAUTH2,
513
+ strictScopesChecks: true,
514
+ },
515
+ OAUTH2_PAR,
516
+ oAuth2Granters,
517
+ readClientGrants,
518
+ oAuth2AuthorizationRequests,
519
+ log,
520
+ });
521
+
522
+ readClientGrants.mockResolvedValueOnce({
523
+ allowedGrantTypes: ['code'],
524
+ allowedScopes: ['user'],
525
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
526
+ isPublicClient: true,
527
+ canAcknowledge: false,
528
+ authenticationData: {
529
+ clientId: 'a_client_id',
530
+ scopes: [],
531
+ userId: '1',
532
+ },
533
+ });
534
+
535
+ await expect(
536
+ postOAuth2PushedAuthorizationRequest({
537
+ body: {
538
+ response_type: 'code',
539
+ client_id: 'a_client_id',
540
+ redirect_uri: 'https://example.com/oauth/cb',
541
+ scope: 'admin',
542
+ state: 'bancal',
543
+ },
544
+ }),
545
+ ).rejects.toMatchObject({
546
+ code: 'E_OAUTH2_BAD_SCOPE',
547
+ });
548
+ });
549
+
550
+ test('should reject bad challenge', async () => {
551
+ [
552
+ codeGranter.authorize,
553
+ codeGranter.acknowledge,
554
+ codeGranter.authenticate,
555
+ tokenGranter.authorize,
556
+ tokenGranter.acknowledge,
557
+ oAuth2AuthorizationRequests.create,
558
+ ].forEach((mock: any) =>
559
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
560
+ );
561
+
562
+ const postOAuth2PushedAuthorizationRequest =
563
+ await initPostOAuth2PushedAuthorizationRequest({
564
+ OAUTH2: {
565
+ ...OAUTH2,
566
+ strictScopesChecks: true,
567
+ },
568
+ OAUTH2_PAR,
569
+ oAuth2Granters,
570
+ readClientGrants,
571
+ oAuth2AuthorizationRequests,
572
+ log,
573
+ });
574
+
575
+ readClientGrants.mockResolvedValueOnce({
576
+ allowedGrantTypes: ['code'],
577
+ allowedScopes: ['user'],
578
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
579
+ isPublicClient: true,
580
+ canAcknowledge: false,
581
+ authenticationData: {
582
+ clientId: 'a_client_id',
583
+ scopes: [],
584
+ userId: '1',
585
+ },
586
+ });
587
+
588
+ await expect(
589
+ postOAuth2PushedAuthorizationRequest({
590
+ body: {
591
+ response_type: 'token',
592
+ client_id: 'a_client_id',
593
+ redirect_uri: 'https://example.com/oauth/cb',
594
+ scope: 'user',
595
+ state: 'bancal',
596
+ code_challenge: 'challenge',
597
+ code_challenge_method: 'S256',
598
+ },
599
+ }),
600
+ ).rejects.toMatchObject({
601
+ code: 'E_OAUTH2_PKCE_NOT_SUPPORTED',
602
+ });
603
+ });
604
+
605
+ test('should reject bad response type', async () => {
606
+ [
607
+ codeGranter.authorize,
608
+ codeGranter.acknowledge,
609
+ codeGranter.authenticate,
610
+ tokenGranter.authorize,
611
+ tokenGranter.acknowledge,
612
+ oAuth2AuthorizationRequests.create,
613
+ ].forEach((mock: any) =>
614
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
615
+ );
616
+
617
+ const postOAuth2PushedAuthorizationRequest =
618
+ await initPostOAuth2PushedAuthorizationRequest({
619
+ OAUTH2: {
620
+ ...OAUTH2,
621
+ strictScopesChecks: true,
622
+ },
623
+ OAUTH2_PAR,
624
+ oAuth2Granters,
625
+ readClientGrants,
626
+ oAuth2AuthorizationRequests,
627
+ log,
628
+ });
629
+
630
+ readClientGrants.mockResolvedValueOnce({
631
+ allowedGrantTypes: ['code'],
632
+ allowedScopes: ['user'],
633
+ allowedRedirectURIS: ['https://example.com/oauth/cb'],
634
+ isPublicClient: true,
635
+ canAcknowledge: false,
636
+ authenticationData: {
637
+ clientId: 'a_client_id',
638
+ scopes: [],
639
+ userId: '1',
640
+ },
641
+ });
642
+
643
+ await expect(
644
+ postOAuth2PushedAuthorizationRequest({
645
+ body: {
646
+ response_type: 'yolo',
647
+ client_id: 'a_client_id',
648
+ redirect_uri: 'https://example.com/oauth/cb',
649
+ scope: 'user',
650
+ state: 'bancal',
651
+ },
652
+ }),
653
+ ).rejects.toMatchObject({
654
+ code: 'E_OAUTH2_UNKNOWN_RESPONSE_TYPE',
655
+ });
656
+ });
657
+
658
+ test('should fail when misconfigured', async () => {
659
+ [
660
+ codeGranter.authorize,
661
+ codeGranter.acknowledge,
662
+ codeGranter.authenticate,
663
+ tokenGranter.authorize,
664
+ tokenGranter.acknowledge,
665
+ ].forEach((mock: any) =>
666
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
667
+ );
668
+
669
+ await expect(
670
+ initPostOAuth2PushedAuthorizationRequest({
671
+ OAUTH2,
672
+ oAuth2Granters,
673
+ readClientGrants,
674
+ oAuth2AuthorizationRequests,
675
+ log,
676
+ }),
677
+ ).rejects.toMatchObject({
678
+ code: 'E_OAUTH2_MISCONFIGURED',
679
+ });
680
+ });
681
+ });