@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,411 @@
1
+ import {
2
+ DEFAULT_ERROR_URI,
3
+ DEFAULT_HELP_URI,
4
+ type WhookErrorsDescriptors,
5
+ } from '@whook/whook';
6
+ import { type WhookOAuth2ClientId } from '../services/oAuth2Granters.js';
7
+
8
+ /**
9
+ * OAuth2 errors registry
10
+ */
11
+ export interface OAuth2YErrorRegistry {
12
+ E_OAUTH2_MISCONFIGURED: [];
13
+
14
+ E_OAUTH2_UNKNOWN_RESPONSE_TYPE: [responseType: string | undefined];
15
+
16
+ E_OAUTH2_UNKNOWN_ACKNOWLEDGER_TYPE: [responseType: string];
17
+
18
+ E_OAUTH2_UNKNOWN_GRANT_TYPE: [grantType: string];
19
+
20
+ /**
21
+ * Thrown when the refresh token is expired/invalid
22
+ */
23
+ E_OAUTH2_BAD_REFRESH_TOKEN: [];
24
+
25
+ /**
26
+ * Thrown when application has no access allowed
27
+ */
28
+ E_OAUTH2_ACCESS_DENIED: [clientId: WhookOAuth2ClientId];
29
+
30
+ /**
31
+ * Thrown when application requires authentication
32
+ */
33
+ E_OAUTH2_AUTHENTICATION_REQUIRED: [clientId: WhookOAuth2ClientId];
34
+
35
+ /**
36
+ * Thrown when the OAuth2 server had unexpected errors
37
+ */
38
+ E_OAUTH2_UNEXPECTED_ERROR: [];
39
+
40
+ /**
41
+ * Thrown when refresh_token cookie is absent and required
42
+ */
43
+ E_AUTH_REFRESH_COOKIE: [cookie: string];
44
+
45
+ /**
46
+ * Thrown when server requires PKCE
47
+ */
48
+ E_OAUTH2_PKCE_REQUIRED: [responseType: string];
49
+
50
+ /**
51
+ * Thrown when server does not support PKCE
52
+ */
53
+ E_OAUTH2_PKCE_NOT_SUPPORTED: [responseType: string];
54
+
55
+ /**
56
+ * Thrown when Pushed Authorization Requests are required
57
+ */
58
+ E_OAUTH2_PAR_REQUIRED: [];
59
+
60
+ /**
61
+ * Thrown when Pushed Authorization Requests are used but disabled
62
+ */
63
+ E_OAUTH2_PAR_NOT_SUPPORTED: [];
64
+
65
+ /**
66
+ * Thrown when a request URI has unexpected additional
67
+ * parameters
68
+ */
69
+ E_OAUTH2_BAD_REQUEST_URI_PARAMETERS: string[];
70
+
71
+ /**
72
+ * Thrown when a request URI is invalid, unknown or expired
73
+ */
74
+ E_OAUTH2_BAD_REQUEST_URI: [
75
+ requestURI: string | undefined,
76
+ expiresAt: number | undefined,
77
+ currentTime: number | undefined,
78
+ ];
79
+
80
+ /**
81
+ * Thrown when a request URI is provided where forbidden
82
+ */
83
+ E_OAUTH2_REQUEST_URI_NOT_ALLOWED: [];
84
+
85
+ /**
86
+ * Thrown when a not supported scope is asked
87
+ */
88
+ E_OAUTH2_BAD_SCOPE: [actualScope: string];
89
+
90
+ /**
91
+ * Thrown when a not allowed grant type is asked
92
+ */
93
+ E_OAUTH2_GRANT_TYPE_NOT_ALLOWED: [
94
+ grantType: string,
95
+ allowedGrantTypes: string[],
96
+ ];
97
+
98
+ /**
99
+ * Thrown when the authorization code is expired
100
+ */
101
+ E_OAUTH2_EXPIRED_CODE: [expiredAt: number, currentTime: number];
102
+
103
+ /**
104
+ * Thrown when the authorization code is evicted
105
+ */
106
+ E_OAUTH2_EVICTED_CODE: [evictedAt: number, currentTime: number];
107
+
108
+ /**
109
+ * Thrown when a code verifier is provided but the code
110
+ * verification returned no challenge
111
+ */
112
+ E_OAUTH2_BAD_AUTHORIZATION_CODE_VERIFIER_CONTEXT: [];
113
+
114
+ /**
115
+ * Thrown when a code verifier is expected
116
+ */
117
+ E_OAUTH2_AUTHORIZATION_CODE_VERIFIER_EXPECTED: [
118
+ codeChallenge: string | undefined,
119
+ codeChallengeMethod: string | undefined,
120
+ ];
121
+
122
+ /**
123
+ * Thrown when a code verifier is not expected
124
+ */
125
+ E_OAUTH2_CODE_VERIFIER_UNEXPECTED: [];
126
+
127
+ /**
128
+ * Thrown when the code verifier is invalid
129
+ */
130
+ E_OAUTH2_BAD_CODE_VERIFIER: [
131
+ codeVerifier: string,
132
+ computedCodeChallenge: string,
133
+ ];
134
+
135
+ /**
136
+ * Thrown when the code challenge method is not supported
137
+ */
138
+ E_OAUTH2_UNSUPPORTED_CODE_CHALLENGE_METHOD: [actualMethod: string];
139
+
140
+ /**
141
+ * Thrown when a code challenge is required
142
+ * (authorization code grant type and a public client)
143
+ */
144
+ E_OAUTH2_AUTHORIZATION_CODE_CHALLENGE_REQUIRED: [];
145
+
146
+ /**
147
+ * Thrown when a code verifier is required
148
+ * (authorization code grant type and a public client)
149
+ */
150
+ E_OAUTH2_AUTHORIZATION_CODE_VERIFIER_REQUIRED: [];
151
+
152
+ /**
153
+ * Thrown when the client does not exist
154
+ */
155
+ E_OAUTH2_CLIENT_NOT_FOUND: [WhookOAuth2ClientId];
156
+
157
+ /**
158
+ * Thrown when the authenticated client does
159
+ * not match the token/query/code clientId
160
+ */
161
+ E_OAUTH2_CLIENT_MISMATCH: WhookOAuth2ClientId[];
162
+
163
+ /**
164
+ * Thrown when the client grants returned by
165
+ * custom implementation does not match the clientId
166
+ */
167
+ E_OAUTH2_CLIENT_GRANTS_MISMATCH: WhookOAuth2ClientId[];
168
+
169
+ /**
170
+ * Thrown when no client was specified
171
+ */
172
+ E_OAUTH2_EMPTY_CLIENT: [];
173
+
174
+ /**
175
+ * Thrown when the redirect URI is not valid
176
+ */
177
+ E_OAUTH2_BAD_REDIRECT_URI: [requestedURI: string, allowedURIS: string[]];
178
+ }
179
+
180
+ export const OAUTH2_ERRORS_DESCRIPTORS: Record<
181
+ keyof OAuth2YErrorRegistry,
182
+ WhookErrorsDescriptors[string]
183
+ > = {
184
+ E_OAUTH2_UNKNOWN_RESPONSE_TYPE: {
185
+ code: 'unsupported_response_type',
186
+ status: 400,
187
+ description: `The response type "$0" is not supported.`,
188
+ uri: DEFAULT_ERROR_URI,
189
+ help: DEFAULT_HELP_URI,
190
+ },
191
+ E_OAUTH2_MISCONFIGURED: {
192
+ code: 'server_error',
193
+ status: 500,
194
+ description: `Server configuration error.`,
195
+ uri: DEFAULT_ERROR_URI,
196
+ help: DEFAULT_HELP_URI,
197
+ },
198
+ E_OAUTH2_UNKNOWN_ACKNOWLEDGER_TYPE: {
199
+ code: 'unsupported_response_type',
200
+ status: 400,
201
+ description: `Type "$0" not supported.`,
202
+ uri: DEFAULT_ERROR_URI,
203
+ help: DEFAULT_HELP_URI,
204
+ },
205
+ E_OAUTH2_UNKNOWN_GRANT_TYPE: {
206
+ code: 'unsupported_grant_type',
207
+ status: 400,
208
+ description: `Grant type "$0" not supported.`,
209
+ uri: DEFAULT_ERROR_URI,
210
+ help: DEFAULT_HELP_URI,
211
+ },
212
+ E_OAUTH2_BAD_REFRESH_TOKEN: {
213
+ code: 'invalid_grant',
214
+ status: 400,
215
+ description: 'The refresh token is expired/invalid.',
216
+ uri: DEFAULT_ERROR_URI,
217
+ help: DEFAULT_HELP_URI,
218
+ },
219
+ E_OAUTH2_ACCESS_DENIED: {
220
+ code: 'access_denied',
221
+ status: 403,
222
+ description: 'The user denied access to your application (id: "$0").',
223
+ uri: DEFAULT_ERROR_URI,
224
+ help: DEFAULT_HELP_URI,
225
+ },
226
+ E_OAUTH2_EMPTY_CLIENT: {
227
+ code: 'invalid_client',
228
+ status: 400,
229
+ description: 'No identifiable client found in the request.',
230
+ uri: DEFAULT_ERROR_URI,
231
+ help: DEFAULT_HELP_URI,
232
+ },
233
+ E_OAUTH2_AUTHENTICATION_REQUIRED: {
234
+ code: 'invalid_client',
235
+ status: 401,
236
+ description: 'The provided client requires authentication (id: "$0").',
237
+ uri: DEFAULT_ERROR_URI,
238
+ help: DEFAULT_HELP_URI,
239
+ },
240
+ E_OAUTH2_UNEXPECTED_ERROR: {
241
+ code: 'server_error',
242
+ status: 500,
243
+ uri: DEFAULT_ERROR_URI,
244
+ help: DEFAULT_HELP_URI,
245
+ },
246
+ E_AUTH_REFRESH_COOKIE: {
247
+ code: 'invalid_request',
248
+ status: 401,
249
+ description: `Could not find any refresh_token value in the cookie header ($0).`,
250
+ uri: DEFAULT_ERROR_URI,
251
+ help: DEFAULT_HELP_URI,
252
+ },
253
+ E_OAUTH2_PKCE_REQUIRED: {
254
+ code: 'invalid_request',
255
+ status: 400,
256
+ description: 'Code challenge required for this response type ($0).',
257
+ uri: DEFAULT_ERROR_URI,
258
+ help: DEFAULT_HELP_URI,
259
+ },
260
+ E_OAUTH2_PKCE_NOT_SUPPORTED: {
261
+ code: 'invalid_request',
262
+ status: 400,
263
+ description: 'Code challenge is not supported for this response type ($0).',
264
+ uri: DEFAULT_ERROR_URI,
265
+ help: DEFAULT_HELP_URI,
266
+ },
267
+ E_OAUTH2_PAR_REQUIRED: {
268
+ code: 'invalid_request',
269
+ status: 400,
270
+ description: 'Pushed authorization requests are required for this server.',
271
+ uri: DEFAULT_ERROR_URI,
272
+ help: DEFAULT_HELP_URI,
273
+ },
274
+ E_OAUTH2_PAR_NOT_SUPPORTED: {
275
+ code: 'invalid_request',
276
+ status: 400,
277
+ description: 'Pushed authorization requests are not enabled for this server.',
278
+ uri: DEFAULT_ERROR_URI,
279
+ help: DEFAULT_HELP_URI,
280
+ },
281
+ E_OAUTH2_BAD_REQUEST_URI: {
282
+ code: 'invalid_request_uri',
283
+ status: 400,
284
+ description: 'The request URI is invalid, expired or unknown ($0).',
285
+ uri: DEFAULT_ERROR_URI,
286
+ help: DEFAULT_HELP_URI,
287
+ },
288
+ E_OAUTH2_BAD_REQUEST_URI_PARAMETERS: {
289
+ code: 'invalid_request',
290
+ status: 400,
291
+ description: 'The request URI should not have additional parameters ($0).',
292
+ uri: DEFAULT_ERROR_URI,
293
+ help: DEFAULT_HELP_URI,
294
+ },
295
+ E_OAUTH2_REQUEST_URI_NOT_ALLOWED: {
296
+ code: 'invalid_request',
297
+ status: 400,
298
+ description: 'The request_uri parameter is not allowed on this endpoint.',
299
+ uri: DEFAULT_ERROR_URI,
300
+ help: DEFAULT_HELP_URI,
301
+ },
302
+ E_OAUTH2_BAD_SCOPE: {
303
+ code: 'invalid_scope',
304
+ status: 400,
305
+ description: 'This scope is not supported ($0).',
306
+ uri: DEFAULT_ERROR_URI,
307
+ help: DEFAULT_HELP_URI,
308
+ },
309
+ E_OAUTH2_GRANT_TYPE_NOT_ALLOWED: {
310
+ code: 'unauthorized_client',
311
+ status: 400,
312
+ description: 'This grant type is not supported ($0).',
313
+ uri: DEFAULT_ERROR_URI,
314
+ help: DEFAULT_HELP_URI,
315
+ },
316
+ E_OAUTH2_EXPIRED_CODE: {
317
+ code: 'invalid_request',
318
+ status: 400,
319
+ description: 'The authorization code is expired (expired at "$0").',
320
+ uri: DEFAULT_ERROR_URI,
321
+ help: DEFAULT_HELP_URI,
322
+ },
323
+ E_OAUTH2_EVICTED_CODE: {
324
+ code: 'invalid_request',
325
+ status: 400,
326
+ description: 'The code has been cancelled.',
327
+ uri: DEFAULT_ERROR_URI,
328
+ help: DEFAULT_HELP_URI,
329
+ },
330
+ E_OAUTH2_BAD_AUTHORIZATION_CODE_VERIFIER_CONTEXT: {
331
+ code: 'invalid_request',
332
+ status: 400,
333
+ description:
334
+ 'A code verifier is provided but the authorization code has no code challenge to compare it with.',
335
+ uri: DEFAULT_ERROR_URI,
336
+ help: DEFAULT_HELP_URI,
337
+ },
338
+ E_OAUTH2_AUTHORIZATION_CODE_VERIFIER_EXPECTED: {
339
+ code: 'invalid_request',
340
+ status: 400,
341
+ description: 'A code verifier is expected for that request.',
342
+ uri: DEFAULT_ERROR_URI,
343
+ help: DEFAULT_HELP_URI,
344
+ },
345
+ E_OAUTH2_CODE_VERIFIER_UNEXPECTED: {
346
+ code: 'invalid_request',
347
+ status: 400,
348
+ description: 'A code verifier is not expected for that request.',
349
+ uri: DEFAULT_ERROR_URI,
350
+ help: DEFAULT_HELP_URI,
351
+ },
352
+ E_OAUTH2_BAD_CODE_VERIFIER: {
353
+ code: 'invalid_request',
354
+ status: 400,
355
+ description: 'The provided code verifier is bad.',
356
+ uri: DEFAULT_ERROR_URI,
357
+ help: DEFAULT_HELP_URI,
358
+ },
359
+ E_OAUTH2_UNSUPPORTED_CODE_CHALLENGE_METHOD: {
360
+ code: 'invalid_request',
361
+ status: 400,
362
+ description:
363
+ 'The authorization code challenge method provided is not supported.',
364
+ uri: DEFAULT_ERROR_URI,
365
+ help: DEFAULT_HELP_URI,
366
+ },
367
+ E_OAUTH2_AUTHORIZATION_CODE_CHALLENGE_REQUIRED: {
368
+ code: 'invalid_request',
369
+ status: 400,
370
+ description:
371
+ 'The authorization code grant type with public client requires a code challenge.',
372
+ uri: DEFAULT_ERROR_URI,
373
+ help: DEFAULT_HELP_URI,
374
+ },
375
+ E_OAUTH2_AUTHORIZATION_CODE_VERIFIER_REQUIRED: {
376
+ code: 'invalid_request',
377
+ status: 400,
378
+ description:
379
+ 'The authorization code grant type with public client requires a code verifier.',
380
+ uri: DEFAULT_ERROR_URI,
381
+ help: DEFAULT_HELP_URI,
382
+ },
383
+ E_OAUTH2_CLIENT_NOT_FOUND: {
384
+ code: 'invalid_client',
385
+ status: 400,
386
+ description: 'The client provided does not exist ($0).',
387
+ uri: DEFAULT_ERROR_URI,
388
+ help: DEFAULT_HELP_URI,
389
+ },
390
+ E_OAUTH2_CLIENT_MISMATCH: {
391
+ code: 'invalid_request',
392
+ status: 400,
393
+ description: 'The client used is not matching the request.',
394
+ uri: DEFAULT_ERROR_URI,
395
+ help: DEFAULT_HELP_URI,
396
+ },
397
+ E_OAUTH2_CLIENT_GRANTS_MISMATCH: {
398
+ code: 'server_error',
399
+ status: 500,
400
+ description: 'An implementation error occurred.',
401
+ uri: DEFAULT_ERROR_URI,
402
+ help: DEFAULT_HELP_URI,
403
+ },
404
+ E_OAUTH2_BAD_REDIRECT_URI: {
405
+ code: 'invalid_request',
406
+ status: 400,
407
+ description: 'The client does not accept that redirect URI.',
408
+ uri: DEFAULT_ERROR_URI,
409
+ help: DEFAULT_HELP_URI,
410
+ },
411
+ };
@@ -0,0 +1,15 @@
1
+ import { YError } from 'yerror';
2
+
3
+ export type WhookOAuth2GrantType = string;
4
+
5
+ export function checkGrantType(
6
+ allowedGrantTypes: WhookOAuth2GrantType[],
7
+ demandedGrantType: WhookOAuth2GrantType,
8
+ ) {
9
+ if (!allowedGrantTypes.includes(demandedGrantType)) {
10
+ throw new YError('E_OAUTH2_GRANT_TYPE_NOT_ALLOWED', [
11
+ demandedGrantType,
12
+ allowedGrantTypes,
13
+ ]);
14
+ }
15
+ }
@@ -0,0 +1,142 @@
1
+ import { type WhookErrorDescriptor } from '@whook/whook';
2
+ import { YError } from 'yerror';
3
+
4
+ export function getUsableRedirectURI(
5
+ allowedURIS: string[],
6
+ requestedURI?: string,
7
+ httpsOnly = false,
8
+ ) {
9
+ if (requestedURI) {
10
+ checkRedirectURI(allowedURIS, requestedURI, httpsOnly);
11
+ }
12
+ requestedURI ||= allowedURIS[0];
13
+
14
+ try {
15
+ new URL(requestedURI);
16
+ } catch (err) {
17
+ throw YError.wrap(err as Error, 'E_OAUTH2_BAD_REDIRECT_URI', [
18
+ requestedURI,
19
+ allowedURIS,
20
+ ]);
21
+ }
22
+
23
+ return requestedURI;
24
+ }
25
+
26
+ export function checkRedirectURI(
27
+ allowedURIS: string[],
28
+ requestedURI: string,
29
+ httpsOnly = false,
30
+ ) {
31
+ for (const allowedURI of allowedURIS) {
32
+ if (isExactOrAllowedMatch(allowedURI, requestedURI, httpsOnly)) {
33
+ return;
34
+ }
35
+ }
36
+
37
+ throw new YError('E_OAUTH2_BAD_REDIRECT_URI', [requestedURI, allowedURIS]);
38
+ }
39
+
40
+ export function isExactOrAllowedMatch(
41
+ allowedURI: string,
42
+ requestedURI: string,
43
+ httpsOnly = false,
44
+ ): boolean {
45
+ try {
46
+ const allowed = new URL(allowedURI);
47
+ const requested = new URL(requestedURI);
48
+
49
+ if (httpsOnly && allowed.protocol !== 'https:') {
50
+ return false;
51
+ }
52
+
53
+ if (allowedURI === requestedURI) {
54
+ return true;
55
+ } else if (allowed.protocol !== 'http:') {
56
+ return false;
57
+ }
58
+
59
+ if (
60
+ isLoopbackHost(allowed.hostname) &&
61
+ isLoopbackHost(requested.hostname)
62
+ ) {
63
+ return (
64
+ (requested.hostname === allowed.hostname ||
65
+ requested.hostname.endsWith(`.${allowed.hostname}`)) &&
66
+ allowed.protocol === requested.protocol &&
67
+ allowed.pathname === requested.pathname &&
68
+ allowed.search === requested.search
69
+ );
70
+ }
71
+ } catch {
72
+ return false;
73
+ }
74
+
75
+ return false;
76
+ }
77
+
78
+ function isLoopbackHost(hostname: string): boolean {
79
+ return (
80
+ hostname === 'localhost' ||
81
+ hostname.endsWith('.localhost') ||
82
+ hostname === '127.0.0.1' ||
83
+ hostname === '[::1]' ||
84
+ hostname === '::1'
85
+ );
86
+ }
87
+
88
+ export function readURLParams(url: URL, transport: 'query' | 'fragment') {
89
+ const searchParams =
90
+ transport === 'query'
91
+ ? url.searchParams
92
+ : new URLSearchParams(url.hash.slice(1));
93
+
94
+ const paramsHash: Record<string, string> = {};
95
+
96
+ for (const [key, value] of searchParams.entries()) {
97
+ paramsHash[key] = value;
98
+ }
99
+
100
+ return paramsHash;
101
+ }
102
+
103
+ export function addParamsToURL(
104
+ url: URL,
105
+ paramsHash: Record<string, string>,
106
+ transport: 'query' | 'fragment',
107
+ ) {
108
+ if (transport === 'query') {
109
+ for (const [key, value] of Object.entries(paramsHash)) {
110
+ url.searchParams.set(key, value);
111
+ }
112
+ } else {
113
+ const searchParams = new URLSearchParams();
114
+
115
+ for (const [key, value] of Object.entries(paramsHash)) {
116
+ searchParams.set(key, value);
117
+ }
118
+ url.hash = searchParams.toString();
119
+ }
120
+ }
121
+
122
+ export function buildParamsFromError(
123
+ err: YError | Error,
124
+ oAuth2Error: WhookErrorDescriptor,
125
+ ) {
126
+ const paramsHash: Record<string, string> = {};
127
+
128
+ paramsHash.error = oAuth2Error.code || 'unexpected_error';
129
+
130
+ if (oAuth2Error.description) {
131
+ paramsHash.error_description = oAuth2Error.description.replace(
132
+ /\$([0-9]+)/g,
133
+ (_: string, paramIndex: string): string => {
134
+ return ((err as YError).debug || [])[
135
+ parseInt(paramIndex, 10)
136
+ ] as string;
137
+ },
138
+ );
139
+ }
140
+
141
+ return paramsHash;
142
+ }
@@ -0,0 +1,37 @@
1
+ import { refersTo, type WhookAPISchemaDefinition } from '@whook/whook';
2
+
3
+ export const scopeTokenSchema = {
4
+ name: 'ScopeToken',
5
+ schema: {
6
+ description:
7
+ 'OAuth 2.1 scope token, see https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-15.html#name-access-token-scope',
8
+ type: 'string',
9
+ pattern: '^[!#-[\\]-~]+$',
10
+ },
11
+ } as const satisfies WhookAPISchemaDefinition;
12
+ export const scopeTokensSchema = {
13
+ name: 'ScopeTokens',
14
+ schema: {
15
+ type: 'array',
16
+ items: refersTo(scopeTokenSchema),
17
+ },
18
+ } as const satisfies WhookAPISchemaDefinition;
19
+ export const scopeSchema = {
20
+ name: 'Scope',
21
+ schema: {
22
+ description:
23
+ 'OAuth 2.1 scope, see https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-15.html#name-access-token-scope',
24
+ type: 'string',
25
+ pattern: '^[!#-[\\]-~]+(?: [!#-[\\]-~]+)*$',
26
+ },
27
+ } as const satisfies WhookAPISchemaDefinition;
28
+
29
+ export const requestURISchema = {
30
+ name: 'RequestURI',
31
+ schema: {
32
+ description:
33
+ 'OAuth2 PAR request_uri, see https://datatracker.ietf.org/doc/html/rfc9126#section-2.2.',
34
+ type: 'string',
35
+ pattern: '^urn:ietf:params:oauth:request_uri:',
36
+ },
37
+ } as const satisfies WhookAPISchemaDefinition;
@@ -1,5 +1,54 @@
1
+ import { type WhookAuthenticationScope } from '@whook/authorization';
2
+ import { identity } from '@whook/whook';
1
3
  import { type OpenAPI, PATH_ITEM_METHODS } from 'ya-open-api-types';
4
+ import { YError } from 'yerror';
2
5
 
6
+ const OAUTH2_SCOPES_SEPARATOR = ' ';
7
+
8
+ /**
9
+ * Splits a scope into its scope tokens
10
+ */
11
+ export function parseOAuth2Scope(oAuth2Scope: string): string[] {
12
+ const scopes = [
13
+ ...new Set(oAuth2Scope.split(OAUTH2_SCOPES_SEPARATOR).filter(identity)),
14
+ ];
15
+
16
+ return scopes;
17
+ }
18
+
19
+ /**
20
+ * Join scopes into scope tokens
21
+ */
22
+ export function stringifyScopes(scopes: WhookAuthenticationScope[]): string {
23
+ return scopes.join(OAUTH2_SCOPES_SEPARATOR);
24
+ }
25
+
26
+ /**
27
+ * Filter scopes tokens to become valid ones
28
+ */
29
+ export function filterScopes(
30
+ scopes: string[],
31
+ allowedScopes: WhookAuthenticationScope[],
32
+ strict: boolean,
33
+ ): WhookAuthenticationScope[] {
34
+ const filteredScopes: WhookAuthenticationScope[] = [];
35
+
36
+ for (const scope of scopes) {
37
+ if (allowedScopes.includes(scope)) {
38
+ filteredScopes.push(scope);
39
+ continue;
40
+ }
41
+ if (strict) {
42
+ throw new YError('E_OAUTH2_BAD_SCOPE', [scope]);
43
+ }
44
+ }
45
+
46
+ return filteredScopes;
47
+ }
48
+
49
+ /**
50
+ * Collects all scopes tokens from an Open API
51
+ */
3
52
  export function collectScopesFromAPI(API: OpenAPI) {
4
53
  const scopes: string[] = [];
5
54
 
@@ -0,0 +1,24 @@
1
+ import camelCase from 'camelcase';
2
+
3
+ // The OAuth2 standard uses snake case names so we are
4
+ // converting them to the project standards asap
5
+ export function camelCaseObjectProperties<T>(
6
+ object: Record<string, T>,
7
+ ): Record<string, T> {
8
+ return Object.keys(object).reduce(
9
+ (camelCasedObject, key) => {
10
+ const newKey = key === 'redirect_uri' ? 'redirectURI' : camelCase(key);
11
+
12
+ camelCasedObject[newKey] = object[key];
13
+ return camelCasedObject;
14
+ },
15
+ {} as Record<string, T>,
16
+ );
17
+ }
18
+
19
+ export function snakeCase(s: string): string {
20
+ return s
21
+ .split(/(?=(?<![A-Z])[A-Z])|[^a-zA-Z]+/)
22
+ .map((s) => s.toLowerCase())
23
+ .join('_');
24
+ }