@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
package/src/index.ts CHANGED
@@ -9,6 +9,7 @@ import initGetOAuth2Authorize, {
9
9
  codeChallengeParameter as getOAuth2AuthorizeCodeChallengeParameter,
10
10
  codeChallengeMethodSchema as getOAuth2AuthorizeCodeChallengeMethodSchema,
11
11
  codeChallengeMethodParameter as getOAuth2AuthorizeCodeChallengeMethodParameter,
12
+ scopeSchema as getOAuth2AuthorizeScopeSchema,
12
13
  } from './routes/getOAuth2Authorize.js';
13
14
  import initPostOAuth2Acknowledge, {
14
15
  definition as postOAuth2AcknowledgeDefinition,
@@ -29,29 +30,34 @@ import initGetOAuth2WellKnown, {
29
30
  jsonWebEncryptionsSchema as getOAuth2WellKnownJsonWebEncryptionsSchema,
30
31
  httpsProtocolURISchema as getOAuth2WellKnownHTTPSProtocolURISchema,
31
32
  oAuth2MetadataSchema as getOAuth2WellKnownOAuth2MetadataSchema,
33
+ scopeTokenSchema as getOAuth2WellKnownScopeTokenSchema,
34
+ scopeTokensSchema as getOAuth2WellKnownScopeTokensSchema,
32
35
  } from './routes/getOAuth2WellKnownMetadata.js';
33
36
  import initGetOAuth2WellKnownProtectedResource, {
34
37
  definition as getOAuth2WellKnownProtectedResourceDefinition,
35
38
  bearerMethodSchema as getOAuth2WellKnownProtectedResourceBearerMethodSchema,
36
39
  oAuth2ProtectedResourceMetadataSchema as getOAuth2WellKnownProtectedResourceSchema,
37
40
  } from './routes/getOAuth2WellKnownProtectedResourceMetadata.js';
38
- import initOAuth2Granters, {
39
- type OAuth2YErrorRegistry,
40
- OAUTH2_ERRORS_DESCRIPTORS,
41
- } from './services/oAuth2Granters.js';
41
+ import initPostOAuth2Revoke, {
42
+ definition as postOAuth2RevokeDefinition,
43
+ tokenTypeHintSchema as postOAuth2RevokeTokenTypeHintSchema,
44
+ revokeTokenRequestBodySchema as postOAuth2RevokeTokenRequestBodySchema,
45
+ } from './routes/postOAuth2Revoke.js';
46
+ export type * from './routes/postOAuth2Revoke.js';
47
+
48
+ import initOAuth2Granters from './services/oAuth2Granters.js';
49
+ export type * from './services/oAuth2Granters.js';
42
50
  import initOAuth2ClientCredentialsGranter from './services/oAuth2ClientCredentialsGranter.js';
51
+ export type * from './services/oAuth2ClientCredentialsGranter.js';
43
52
  import initOAuth2PasswordGranter from './services/oAuth2PasswordGranter.js';
53
+ export type * from './services/oAuth2PasswordGranter.js';
44
54
  import initOAuth2RefreshTokenGranter from './services/oAuth2RefreshTokenGranter.js';
45
- import initOAuth2TokenGranter from './services/oAuth2TokenGranter.js';
46
- import {
47
- type OAuth2PasswordService,
48
- type OAuth2AccessTokenService,
49
- type OAuth2RefreshTokenService,
50
- type CheckApplicationService,
51
- type OAuth2GranterService,
52
- type OAuth2Options,
53
- type OAuth2Config,
54
- } from './services/oAuth2Granters.js';
55
+ export type * from './services/oAuth2RefreshTokenGranter.js';
56
+ import initOAuth2ImplicitGranter from './services/oAuth2ImplicitGranter.js';
57
+ export type * from './services/oAuth2ImplicitGranter.js';
58
+ import initOAuth2AuthorizationCodeGranter from './services/oAuth2AuthorizationCodeGranter.js';
59
+ export type * from './services/oAuth2AuthorizationCodeGranter.js';
60
+
55
61
  import initPostAuthLogin, {
56
62
  definition as postAuthLoginDefinition,
57
63
  } from './routes/postAuthLogin.js';
@@ -62,22 +68,23 @@ import initPostAuthRefresh, {
62
68
  authCookieHeaderParameter,
63
69
  definition as postAuthRefreshDefinition,
64
70
  } from './routes/postAuthRefresh.js';
65
- import initAuthCookiesService, {
66
- AUTH_API_PREFIX,
67
- } from './services/authCookies.js';
68
- import {
69
- type AuthCookiesConfig,
70
- type AuthCookiesService,
71
- type AuthCookiesData,
72
- type AuthHandlersConfig,
73
- } from './services/authCookies.js';
74
- import initOAuth2CodeGranter, {
75
- base64UrlEncode,
76
- hashCodeVerifier,
77
- type CodeChallengeMethod,
78
- type OAuth2CodeService,
79
- } from './services/oAuth2CodeGranter.js';
80
- import { collectScopesFromAPI } from './libs/scopes.js';
71
+ import initAuthCookiesService from './services/authCookies.js';
72
+ import { type OAuth2YErrorRegistry } from './libs/errors.js';
73
+ export * from './services/authCookies.js';
74
+ export type * from './services/authCookies.js';
75
+
76
+ export * from './libs/schemas.js';
77
+ export type * from './libs/schemas.js';
78
+ export * from './libs/errors.js';
79
+ export type * from './libs/errors.js';
80
+ export * from './libs/scopes.js';
81
+ export type * from './libs/scopes.js';
82
+ export * from './libs/grants.js';
83
+ export type * from './libs/grants.js';
84
+ export * from './libs/redirectURI.js';
85
+ export type * from './libs/redirectURI.js';
86
+ export * from './libs/verifier.js';
87
+ export type * from './libs/verifier.js';
81
88
 
82
89
  declare module 'yerror' {
83
90
  interface YErrorRegistry extends OAuth2YErrorRegistry {
@@ -85,22 +92,6 @@ declare module 'yerror' {
85
92
  }
86
93
  }
87
94
 
88
- export type {
89
- OAuth2YErrorRegistry,
90
- CodeChallengeMethod,
91
- OAuth2CodeService,
92
- OAuth2PasswordService,
93
- OAuth2AccessTokenService,
94
- OAuth2RefreshTokenService,
95
- CheckApplicationService,
96
- OAuth2GranterService,
97
- OAuth2Options,
98
- OAuth2Config,
99
- AuthCookiesConfig,
100
- AuthCookiesService,
101
- AuthCookiesData,
102
- AuthHandlersConfig,
103
- };
104
95
  export {
105
96
  initGetOAuth2Authorize,
106
97
  getOAuth2AuthorizeDefinition,
@@ -109,6 +100,7 @@ export {
109
100
  getOAuth2AuthorizeRedirectURIParameter,
110
101
  getOAuth2AuthorizeScopeParameter,
111
102
  getOAuth2AuthorizeStateParameter,
103
+ getOAuth2AuthorizeScopeSchema,
112
104
  getOAuth2AuthorizeCodeChallengeSchema,
113
105
  getOAuth2AuthorizeCodeChallengeMethodSchema,
114
106
  getOAuth2AuthorizeCodeChallengeParameter,
@@ -120,12 +112,16 @@ export {
120
112
  getOAuth2WellKnownJsonWebEncryptionsSchema,
121
113
  getOAuth2WellKnownHTTPSProtocolURISchema,
122
114
  getOAuth2WellKnownOAuth2MetadataSchema,
115
+ getOAuth2WellKnownScopeTokenSchema,
116
+ getOAuth2WellKnownScopeTokensSchema,
123
117
  initGetOAuth2WellKnownProtectedResource,
124
118
  getOAuth2WellKnownProtectedResourceDefinition,
125
119
  getOAuth2WellKnownProtectedResourceBearerMethodSchema,
126
120
  getOAuth2WellKnownProtectedResourceSchema,
127
- base64UrlEncode,
128
- hashCodeVerifier,
121
+ initPostOAuth2Revoke,
122
+ postOAuth2RevokeDefinition,
123
+ postOAuth2RevokeTokenTypeHintSchema,
124
+ postOAuth2RevokeTokenRequestBodySchema,
129
125
  initPostOAuth2Acknowledge,
130
126
  postOAuth2AcknowledgeDefinition,
131
127
  initPostOAuth2Token,
@@ -136,14 +132,12 @@ export {
136
132
  postOAuth2TokenClientCredentialsTokenRequestBodySchema,
137
133
  postOAuth2TokenRefreshTokenRequestBodySchema,
138
134
  postOAuth2TokenTokenBodySchema,
139
- OAUTH2_ERRORS_DESCRIPTORS,
140
135
  initOAuth2Granters,
141
136
  initOAuth2ClientCredentialsGranter,
142
- initOAuth2CodeGranter,
137
+ initOAuth2AuthorizationCodeGranter,
143
138
  initOAuth2PasswordGranter,
144
139
  initOAuth2RefreshTokenGranter,
145
- initOAuth2TokenGranter,
146
- AUTH_API_PREFIX,
140
+ initOAuth2ImplicitGranter,
147
141
  authCookieHeaderParameter,
148
142
  initPostAuthLogin,
149
143
  postAuthLoginDefinition,
@@ -152,5 +146,4 @@ export {
152
146
  initPostAuthRefresh,
153
147
  postAuthRefreshDefinition,
154
148
  initAuthCookiesService,
155
- collectScopesFromAPI,
156
149
  };
@@ -0,0 +1,22 @@
1
+ import { identity } from '@whook/whook';
2
+ import { type WhookOAuth2ClientId } from '../services/oAuth2Granters.js';
3
+ import { YError } from 'yerror';
4
+
5
+ export function toUsableClientId(
6
+ clientsIds: (WhookOAuth2ClientId | undefined)[],
7
+ ): WhookOAuth2ClientId {
8
+ const usableClientId = clientsIds.find(identity);
9
+
10
+ if (!usableClientId) {
11
+ throw new YError('E_OAUTH2_EMPTY_CLIENT', []);
12
+ }
13
+
14
+ if (clientsIds.some((clientId) => clientId && clientId !== usableClientId)) {
15
+ throw new YError(
16
+ 'E_OAUTH2_CLIENT_MISMATCH',
17
+ clientsIds.filter((clientId) => typeof clientId !== 'undefined'),
18
+ );
19
+ }
20
+
21
+ return usableClientId;
22
+ }
@@ -0,0 +1,325 @@
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_UNKNOWN_RESPONSE_TYPE: [responseType: string];
13
+
14
+ E_OAUTH2_UNKNOWN_ACKNOWLEDGER_TYPE: [responseType: string];
15
+
16
+ E_OAUTH2_UNKNOWN_GRANT_TYPE: [grantType: string];
17
+
18
+ /**
19
+ * Thrown when the refresh token is expired/invalid
20
+ */
21
+ E_OAUTH2_BAD_REFRESH_TOKEN: [];
22
+
23
+ /**
24
+ * Thrown when application has no access allowed
25
+ */
26
+ E_OAUTH2_ACCESS_DENIED: [clientId: WhookOAuth2ClientId];
27
+
28
+ /**
29
+ * Thrown when application requires authentication
30
+ */
31
+ E_OAUTH2_AUTHENTICATION_REQUIRED: [clientId: WhookOAuth2ClientId];
32
+
33
+ /**
34
+ * Thrown when the OAuth2 server had unexpected errors
35
+ */
36
+ E_OAUTH2_UNEXPECTED_ERROR: [];
37
+
38
+ /**
39
+ * Thrown when refresh_token cookie is absent and required
40
+ */
41
+ E_AUTH_REFRESH_COOKIE: [cookie: string];
42
+
43
+ /**
44
+ * Thrown when server requires PKCE
45
+ */
46
+ E_OAUTH2_PKCE_REQUIRED: [responseType: string];
47
+
48
+ /**
49
+ * Thrown when server does not support PKCE
50
+ */
51
+ E_OAUTH2_PKCE_NOT_SUPPORTED: [responseType: string];
52
+
53
+ /**
54
+ * Thrown when a not supported scope is asked
55
+ */
56
+ E_OAUTH2_BAD_SCOPE: [actualScope: string];
57
+
58
+ /**
59
+ * Thrown when a not allowed grant type is asked
60
+ */
61
+ E_OAUTH2_GRANT_TYPE_NOT_ALLOWED: [
62
+ grantType: string,
63
+ allowedGrantTypes: string[],
64
+ ];
65
+
66
+ /**
67
+ * Thrown when the authorization code is expired
68
+ */
69
+ E_OAUTH2_EXPIRED_CODE: [expiredAt: number, currentTime: number];
70
+
71
+ /**
72
+ * Thrown when the authorization code is evicted
73
+ */
74
+ E_OAUTH2_EVICTED_CODE: [evictedAt: number, currentTime: number];
75
+
76
+ /**
77
+ * Thrown when a code verifier is provided but the code
78
+ * verification returned no challenge
79
+ */
80
+ E_OAUTH2_BAD_AUTHORIZATION_CODE_VERIFIER_CONTEXT: [];
81
+
82
+ /**
83
+ * Thrown when a code verifier is expected
84
+ */
85
+ E_OAUTH2_AUTHORIZATION_CODE_VERIFIER_EXPECTED: [
86
+ codeChallenge: string | undefined,
87
+ codeChallengeMethod: string | undefined,
88
+ ];
89
+
90
+ /**
91
+ * Thrown when a code verifier is not expected
92
+ */
93
+ E_OAUTH2_CODE_VERIFIER_UNEXPECTED: [];
94
+
95
+ /**
96
+ * Thrown when the code verifier is invalid
97
+ */
98
+ E_OAUTH2_BAD_CODE_VERIFIER: [
99
+ codeVerifier: string,
100
+ computedCodeChallenge: string,
101
+ ];
102
+
103
+ /**
104
+ * Thrown when the code challenge method is not supported
105
+ */
106
+ E_OAUTH2_UNSUPPORTED_CODE_CHALLENGE_METHOD: [actualMethod: string];
107
+
108
+ /**
109
+ * Thrown when a code challenge is required
110
+ * (authorization code grant type and a public client)
111
+ */
112
+ E_OAUTH2_AUTHORIZATION_CODE_CHALLENGE_REQUIRED: [];
113
+
114
+ /**
115
+ * Thrown when a code verifier is required
116
+ * (authorization code grant type and a public client)
117
+ */
118
+ E_OAUTH2_AUTHORIZATION_CODE_VERIFIER_REQUIRED: [];
119
+
120
+ /**
121
+ * Thrown when the authenticated client does
122
+ * not match the token/query/code clientId
123
+ */
124
+ E_OAUTH2_CLIENT_MISMATCH: WhookOAuth2ClientId[];
125
+
126
+ /**
127
+ * Thrown when the client grants returned by
128
+ * custom implementation does not match the clientId
129
+ */
130
+ E_OAUTH2_CLIENT_GRANTS_MISMATCH: WhookOAuth2ClientId[];
131
+
132
+ /**
133
+ * Thrown when no client was specified
134
+ */
135
+ E_OAUTH2_EMPTY_CLIENT: [];
136
+
137
+ /**
138
+ * Thrown when the redirect URI is not valid
139
+ */
140
+ E_OAUTH2_BAD_REDIRECT_URI: [requestedURI: string, allowedURIS: string[]];
141
+ }
142
+
143
+ export const OAUTH2_ERRORS_DESCRIPTORS: Record<
144
+ keyof OAuth2YErrorRegistry,
145
+ WhookErrorsDescriptors[string]
146
+ > = {
147
+ E_OAUTH2_UNKNOWN_RESPONSE_TYPE: {
148
+ code: 'unsupported_response_type',
149
+ status: 400,
150
+ description: `The response type "$0" is not supported.`,
151
+ uri: DEFAULT_ERROR_URI,
152
+ help: DEFAULT_HELP_URI,
153
+ },
154
+ E_OAUTH2_UNKNOWN_ACKNOWLEDGER_TYPE: {
155
+ code: 'unsupported_response_type',
156
+ status: 400,
157
+ description: `Type "$0" not supported.`,
158
+ uri: DEFAULT_ERROR_URI,
159
+ help: DEFAULT_HELP_URI,
160
+ },
161
+ E_OAUTH2_UNKNOWN_GRANT_TYPE: {
162
+ code: 'unsupported_grant_type',
163
+ status: 400,
164
+ description: `Grant type "$0" not supported.`,
165
+ uri: DEFAULT_ERROR_URI,
166
+ help: DEFAULT_HELP_URI,
167
+ },
168
+ E_OAUTH2_BAD_REFRESH_TOKEN: {
169
+ code: 'invalid_grant',
170
+ status: 400,
171
+ description: 'The refresh token is expired/invalid.',
172
+ uri: DEFAULT_ERROR_URI,
173
+ help: DEFAULT_HELP_URI,
174
+ },
175
+ E_OAUTH2_ACCESS_DENIED: {
176
+ code: 'access_denied',
177
+ status: 403,
178
+ description: 'The user denied access to your application (id: "$0").',
179
+ uri: DEFAULT_ERROR_URI,
180
+ help: DEFAULT_HELP_URI,
181
+ },
182
+ E_OAUTH2_EMPTY_CLIENT: {
183
+ code: 'invalid_client',
184
+ status: 400,
185
+ description: 'No identifiable client found in the request.',
186
+ uri: DEFAULT_ERROR_URI,
187
+ help: DEFAULT_HELP_URI,
188
+ },
189
+ E_OAUTH2_AUTHENTICATION_REQUIRED: {
190
+ code: 'invalid_client',
191
+ status: 401,
192
+ description: 'The provided client requires authentication (id: "$0").',
193
+ uri: DEFAULT_ERROR_URI,
194
+ help: DEFAULT_HELP_URI,
195
+ },
196
+ E_OAUTH2_UNEXPECTED_ERROR: {
197
+ code: 'server_error',
198
+ status: 500,
199
+ uri: DEFAULT_ERROR_URI,
200
+ help: DEFAULT_HELP_URI,
201
+ },
202
+ E_AUTH_REFRESH_COOKIE: {
203
+ code: 'invalid_request',
204
+ status: 401,
205
+ description: `Could not find any refresh_token value in the cookie header ($0).`,
206
+ uri: DEFAULT_ERROR_URI,
207
+ help: DEFAULT_HELP_URI,
208
+ },
209
+ E_OAUTH2_PKCE_REQUIRED: {
210
+ code: 'invalid_request',
211
+ status: 400,
212
+ description: 'Code challenge required for this response type ($0).',
213
+ uri: DEFAULT_ERROR_URI,
214
+ help: DEFAULT_HELP_URI,
215
+ },
216
+ E_OAUTH2_PKCE_NOT_SUPPORTED: {
217
+ code: 'invalid_request',
218
+ status: 400,
219
+ description: 'Code challenge is not supported for this response type ($0).',
220
+ uri: DEFAULT_ERROR_URI,
221
+ help: DEFAULT_HELP_URI,
222
+ },
223
+ E_OAUTH2_BAD_SCOPE: {
224
+ code: 'invalid_scope',
225
+ status: 400,
226
+ description: 'This scope is not supported ($0).',
227
+ uri: DEFAULT_ERROR_URI,
228
+ help: DEFAULT_HELP_URI,
229
+ },
230
+ E_OAUTH2_GRANT_TYPE_NOT_ALLOWED: {
231
+ code: 'unauthorized_client',
232
+ status: 400,
233
+ description: 'This grant type is not supported ($0).',
234
+ uri: DEFAULT_ERROR_URI,
235
+ help: DEFAULT_HELP_URI,
236
+ },
237
+ E_OAUTH2_EXPIRED_CODE: {
238
+ code: 'invalid_request',
239
+ status: 400,
240
+ description: 'The authorization code is expired (expired at "$0").',
241
+ uri: DEFAULT_ERROR_URI,
242
+ help: DEFAULT_HELP_URI,
243
+ },
244
+ E_OAUTH2_EVICTED_CODE: {
245
+ code: 'invalid_request',
246
+ status: 400,
247
+ description: 'The code has been cancelled.',
248
+ uri: DEFAULT_ERROR_URI,
249
+ help: DEFAULT_HELP_URI,
250
+ },
251
+ E_OAUTH2_BAD_AUTHORIZATION_CODE_VERIFIER_CONTEXT: {
252
+ code: 'invalid_request',
253
+ status: 400,
254
+ description:
255
+ 'A code verifier is provided but the authorization code has no code challenge to compare it with.',
256
+ uri: DEFAULT_ERROR_URI,
257
+ help: DEFAULT_HELP_URI,
258
+ },
259
+ E_OAUTH2_AUTHORIZATION_CODE_VERIFIER_EXPECTED: {
260
+ code: 'invalid_request',
261
+ status: 400,
262
+ description: 'A code verifier is expected for that request.',
263
+ uri: DEFAULT_ERROR_URI,
264
+ help: DEFAULT_HELP_URI,
265
+ },
266
+ E_OAUTH2_CODE_VERIFIER_UNEXPECTED: {
267
+ code: 'invalid_request',
268
+ status: 400,
269
+ description: 'A code verifier is not expected for that request.',
270
+ uri: DEFAULT_ERROR_URI,
271
+ help: DEFAULT_HELP_URI,
272
+ },
273
+ E_OAUTH2_BAD_CODE_VERIFIER: {
274
+ code: 'invalid_request',
275
+ status: 400,
276
+ description: 'The provided code verifier is bad.',
277
+ uri: DEFAULT_ERROR_URI,
278
+ help: DEFAULT_HELP_URI,
279
+ },
280
+ E_OAUTH2_UNSUPPORTED_CODE_CHALLENGE_METHOD: {
281
+ code: 'invalid_request',
282
+ status: 400,
283
+ description:
284
+ 'The authorization code challenge method provided is not supported.',
285
+ uri: DEFAULT_ERROR_URI,
286
+ help: DEFAULT_HELP_URI,
287
+ },
288
+ E_OAUTH2_AUTHORIZATION_CODE_CHALLENGE_REQUIRED: {
289
+ code: 'invalid_request',
290
+ status: 400,
291
+ description:
292
+ 'The authorization code grant type with public client requires a code challenge.',
293
+ uri: DEFAULT_ERROR_URI,
294
+ help: DEFAULT_HELP_URI,
295
+ },
296
+ E_OAUTH2_AUTHORIZATION_CODE_VERIFIER_REQUIRED: {
297
+ code: 'invalid_request',
298
+ status: 400,
299
+ description:
300
+ 'The authorization code grant type with public client requires a code verifier.',
301
+ uri: DEFAULT_ERROR_URI,
302
+ help: DEFAULT_HELP_URI,
303
+ },
304
+ E_OAUTH2_CLIENT_MISMATCH: {
305
+ code: 'invalid_request',
306
+ status: 400,
307
+ description: 'The client used is not matching the request.',
308
+ uri: DEFAULT_ERROR_URI,
309
+ help: DEFAULT_HELP_URI,
310
+ },
311
+ E_OAUTH2_CLIENT_GRANTS_MISMATCH: {
312
+ code: 'server_error',
313
+ status: 500,
314
+ description: 'An implementation error occurred.',
315
+ uri: DEFAULT_ERROR_URI,
316
+ help: DEFAULT_HELP_URI,
317
+ },
318
+ E_OAUTH2_BAD_REDIRECT_URI: {
319
+ code: 'invalid_request',
320
+ status: 400,
321
+ description: 'The client does not accept that redirect URI.',
322
+ uri: DEFAULT_ERROR_URI,
323
+ help: DEFAULT_HELP_URI,
324
+ },
325
+ };
@@ -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,120 @@
1
+ import { type WhookErrorDescriptor } from '@whook/whook';
2
+ import { YError } from 'yerror';
3
+
4
+ export function checkRedirectURI(
5
+ allowedURIS: string[],
6
+ requestedURI: string,
7
+ httpsOnly = false,
8
+ ) {
9
+ for (const allowedURI of allowedURIS) {
10
+ if (isExactOrAllowedMatch(allowedURI, requestedURI, httpsOnly)) {
11
+ return;
12
+ }
13
+ }
14
+
15
+ throw new YError('E_OAUTH2_BAD_REDIRECT_URI', [requestedURI, allowedURIS]);
16
+ }
17
+
18
+ export function isExactOrAllowedMatch(
19
+ allowedURI: string,
20
+ requestedURI: string,
21
+ httpsOnly = false,
22
+ ): boolean {
23
+ try {
24
+ const allowed = new URL(allowedURI);
25
+ const requested = new URL(requestedURI);
26
+
27
+ if (httpsOnly && allowed.protocol !== 'https:') {
28
+ return false;
29
+ }
30
+
31
+ if (allowedURI === requestedURI) {
32
+ return true;
33
+ } else if (allowed.protocol !== 'http:') {
34
+ return false;
35
+ }
36
+
37
+ if (
38
+ isLoopbackHost(allowed.hostname) &&
39
+ isLoopbackHost(requested.hostname)
40
+ ) {
41
+ return (
42
+ (requested.hostname === allowed.hostname ||
43
+ requested.hostname.endsWith(`.${allowed.hostname}`)) &&
44
+ allowed.protocol === requested.protocol &&
45
+ allowed.pathname === requested.pathname &&
46
+ allowed.search === requested.search
47
+ );
48
+ }
49
+ } catch {
50
+ return false;
51
+ }
52
+
53
+ return false;
54
+ }
55
+
56
+ function isLoopbackHost(hostname: string): boolean {
57
+ return (
58
+ hostname === 'localhost' ||
59
+ hostname.endsWith('.localhost') ||
60
+ hostname === '127.0.0.1' ||
61
+ hostname === '[::1]' ||
62
+ hostname === '::1'
63
+ );
64
+ }
65
+
66
+ export function readURLParams(url: URL, transport: 'query' | 'fragment') {
67
+ const searchParams =
68
+ transport === 'query'
69
+ ? url.searchParams
70
+ : new URLSearchParams(url.hash.slice(1));
71
+
72
+ const paramsHash: Record<string, string> = {};
73
+
74
+ for (const [key, value] of searchParams.entries()) {
75
+ paramsHash[key] = value;
76
+ }
77
+
78
+ return paramsHash;
79
+ }
80
+
81
+ export function addParamsToURL(
82
+ url: URL,
83
+ paramsHash: Record<string, string>,
84
+ transport: 'query' | 'fragment',
85
+ ) {
86
+ if (transport === 'query') {
87
+ for (const [key, value] of Object.entries(paramsHash)) {
88
+ url.searchParams.set(key, value);
89
+ }
90
+ } else {
91
+ const searchParams = new URLSearchParams();
92
+
93
+ for (const [key, value] of Object.entries(paramsHash)) {
94
+ searchParams.set(key, value);
95
+ }
96
+ url.hash = searchParams.toString();
97
+ }
98
+ }
99
+
100
+ export function buildParamsFromError(
101
+ err: YError | Error,
102
+ oAuth2Error: WhookErrorDescriptor,
103
+ ) {
104
+ const paramsHash: Record<string, string> = {};
105
+
106
+ paramsHash.error = oAuth2Error.code || 'unexpected_error';
107
+
108
+ if (oAuth2Error.description) {
109
+ paramsHash.error_description = oAuth2Error.description.replace(
110
+ /\$([0-9]+)/g,
111
+ (_: string, paramIndex: string): string => {
112
+ return ((err as YError).debug || [])[
113
+ parseInt(paramIndex, 10)
114
+ ] as string;
115
+ },
116
+ );
117
+ }
118
+
119
+ return paramsHash;
120
+ }
@@ -0,0 +1,27 @@
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;