@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,94 @@
1
+ import { YError } from 'yerror';
2
+ export function getUsableRedirectURI(allowedURIS, requestedURI, httpsOnly = false) {
3
+ if (requestedURI) {
4
+ checkRedirectURI(allowedURIS, requestedURI, httpsOnly);
5
+ }
6
+ requestedURI ||= allowedURIS[0];
7
+ try {
8
+ new URL(requestedURI);
9
+ }
10
+ catch (err) {
11
+ throw YError.wrap(err, 'E_OAUTH2_BAD_REDIRECT_URI', [
12
+ requestedURI,
13
+ allowedURIS,
14
+ ]);
15
+ }
16
+ return requestedURI;
17
+ }
18
+ export function checkRedirectURI(allowedURIS, requestedURI, httpsOnly = false) {
19
+ for (const allowedURI of allowedURIS) {
20
+ if (isExactOrAllowedMatch(allowedURI, requestedURI, httpsOnly)) {
21
+ return;
22
+ }
23
+ }
24
+ throw new YError('E_OAUTH2_BAD_REDIRECT_URI', [requestedURI, allowedURIS]);
25
+ }
26
+ export function isExactOrAllowedMatch(allowedURI, requestedURI, httpsOnly = false) {
27
+ try {
28
+ const allowed = new URL(allowedURI);
29
+ const requested = new URL(requestedURI);
30
+ if (httpsOnly && allowed.protocol !== 'https:') {
31
+ return false;
32
+ }
33
+ if (allowedURI === requestedURI) {
34
+ return true;
35
+ }
36
+ else if (allowed.protocol !== 'http:') {
37
+ return false;
38
+ }
39
+ if (isLoopbackHost(allowed.hostname) &&
40
+ isLoopbackHost(requested.hostname)) {
41
+ return ((requested.hostname === allowed.hostname ||
42
+ requested.hostname.endsWith(`.${allowed.hostname}`)) &&
43
+ allowed.protocol === requested.protocol &&
44
+ allowed.pathname === requested.pathname &&
45
+ allowed.search === requested.search);
46
+ }
47
+ }
48
+ catch {
49
+ return false;
50
+ }
51
+ return false;
52
+ }
53
+ function isLoopbackHost(hostname) {
54
+ return (hostname === 'localhost' ||
55
+ hostname.endsWith('.localhost') ||
56
+ hostname === '127.0.0.1' ||
57
+ hostname === '[::1]' ||
58
+ hostname === '::1');
59
+ }
60
+ export function readURLParams(url, transport) {
61
+ const searchParams = transport === 'query'
62
+ ? url.searchParams
63
+ : new URLSearchParams(url.hash.slice(1));
64
+ const paramsHash = {};
65
+ for (const [key, value] of searchParams.entries()) {
66
+ paramsHash[key] = value;
67
+ }
68
+ return paramsHash;
69
+ }
70
+ export function addParamsToURL(url, paramsHash, transport) {
71
+ if (transport === 'query') {
72
+ for (const [key, value] of Object.entries(paramsHash)) {
73
+ url.searchParams.set(key, value);
74
+ }
75
+ }
76
+ else {
77
+ const searchParams = new URLSearchParams();
78
+ for (const [key, value] of Object.entries(paramsHash)) {
79
+ searchParams.set(key, value);
80
+ }
81
+ url.hash = searchParams.toString();
82
+ }
83
+ }
84
+ export function buildParamsFromError(err, oAuth2Error) {
85
+ const paramsHash = {};
86
+ paramsHash.error = oAuth2Error.code || 'unexpected_error';
87
+ if (oAuth2Error.description) {
88
+ paramsHash.error_description = oAuth2Error.description.replace(/\$([0-9]+)/g, (_, paramIndex) => {
89
+ return (err.debug || [])[parseInt(paramIndex, 10)];
90
+ });
91
+ }
92
+ return paramsHash;
93
+ }
94
+ //# sourceMappingURL=redirectURI.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redirectURI.js","sourceRoot":"","sources":["../../src/libs/redirectURI.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,MAAM,UAAU,oBAAoB,CAClC,WAAqB,EACrB,YAAqB,EACrB,SAAS,GAAG,KAAK;IAEjB,IAAI,YAAY,EAAE,CAAC;QACjB,gBAAgB,CAAC,WAAW,EAAE,YAAY,EAAE,SAAS,CAAC,CAAC;IACzD,CAAC;IACD,YAAY,KAAK,WAAW,CAAC,CAAC,CAAC,CAAC;IAEhC,IAAI,CAAC;QACH,IAAI,GAAG,CAAC,YAAY,CAAC,CAAC;IACxB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,MAAM,CAAC,IAAI,CAAC,GAAY,EAAE,2BAA2B,EAAE;YAC3D,YAAY;YACZ,WAAW;SACZ,CAAC,CAAC;IACL,CAAC;IAED,OAAO,YAAY,CAAC;AACtB,CAAC;AAED,MAAM,UAAU,gBAAgB,CAC9B,WAAqB,EACrB,YAAoB,EACp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KAAK,CAAC,CAAC;QAC/B,CAAC;QACD,GAAG,CAAC,IAAI,GAAG,YAAY,CAAC,QAAQ,EAAE,CAAC;IACrC,CAAC;AACH,CAAC;AAED,MAAM,UAAU,oBAAoB,CAClC,GAAmB,EACnB,WAAiC;IAEjC,MAAM,UAAU,GAA2B,EAAE,CAAC;IAE9C,UAAU,CAAC,KAAK,GAAG,WAAW,CAAC,IAAI,IAAI,kBAAkB,CAAC;IAE1D,IAAI,WAAW,CAAC,WAAW,EAAE,CAAC;QAC5B,UAAU,CAAC,iBAAiB,GAAG,WAAW,CAAC,WAAW,CAAC,OAAO,CAC5D,aAAa,EACb,CAAC,CAAS,EAAE,UAAkB,EAAU,EAAE;YACxC,OAAO,CAAE,GAAc,CAAC,KAAK,IAAI,EAAE,CAAC,CAClC,QAAQ,CAAC,UAAU,EAAE,EAAE,CAAC,CACf,CAAC;QACd,CAAC,CACF,CAAC;IACJ,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC"}
@@ -0,0 +1,31 @@
1
+ export declare const scopeTokenSchema: {
2
+ readonly name: "ScopeToken";
3
+ readonly schema: {
4
+ readonly description: "OAuth 2.1 scope token, see https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-15.html#name-access-token-scope";
5
+ readonly type: "string";
6
+ readonly pattern: "^[!#-[\\]-~]+$";
7
+ };
8
+ };
9
+ export declare const scopeTokensSchema: {
10
+ readonly name: "ScopeTokens";
11
+ readonly schema: {
12
+ readonly type: "array";
13
+ readonly items: import("ya-json-schema-types").ExpressiveJSONSchema;
14
+ };
15
+ };
16
+ export declare const scopeSchema: {
17
+ readonly name: "Scope";
18
+ readonly schema: {
19
+ readonly description: "OAuth 2.1 scope, see https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-15.html#name-access-token-scope";
20
+ readonly type: "string";
21
+ readonly pattern: "^[!#-[\\]-~]+(?: [!#-[\\]-~]+)*$";
22
+ };
23
+ };
24
+ export declare const requestURISchema: {
25
+ readonly name: "RequestURI";
26
+ readonly schema: {
27
+ readonly description: "OAuth2 PAR request_uri, see https://datatracker.ietf.org/doc/html/rfc9126#section-2.2.";
28
+ readonly type: "string";
29
+ readonly pattern: "^urn:ietf:params:oauth:request_uri:";
30
+ };
31
+ };
@@ -0,0 +1,33 @@
1
+ import { refersTo } from '@whook/whook';
2
+ export const scopeTokenSchema = {
3
+ name: 'ScopeToken',
4
+ schema: {
5
+ description: 'OAuth 2.1 scope token, see https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-15.html#name-access-token-scope',
6
+ type: 'string',
7
+ pattern: '^[!#-[\\]-~]+$',
8
+ },
9
+ };
10
+ export const scopeTokensSchema = {
11
+ name: 'ScopeTokens',
12
+ schema: {
13
+ type: 'array',
14
+ items: refersTo(scopeTokenSchema),
15
+ },
16
+ };
17
+ export const scopeSchema = {
18
+ name: 'Scope',
19
+ schema: {
20
+ description: 'OAuth 2.1 scope, see https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-15.html#name-access-token-scope',
21
+ type: 'string',
22
+ pattern: '^[!#-[\\]-~]+(?: [!#-[\\]-~]+)*$',
23
+ },
24
+ };
25
+ export const requestURISchema = {
26
+ name: 'RequestURI',
27
+ schema: {
28
+ description: 'OAuth2 PAR request_uri, see https://datatracker.ietf.org/doc/html/rfc9126#section-2.2.',
29
+ type: 'string',
30
+ pattern: '^urn:ietf:params:oauth:request_uri:',
31
+ },
32
+ };
33
+ //# sourceMappingURL=schemas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schemas.js","sourceRoot":"","sources":["../../src/libs/schemas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAiC,MAAM,cAAc,CAAC;AAEvE,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,IAAI,EAAE,YAAY;IAClB,MAAM,EAAE;QACN,WAAW,EACT,kHAAkH;QACpH,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,gBAAgB;KAC1B;CAC0C,CAAC;AAC9C,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,IAAI,EAAE,aAAa;IACnB,MAAM,EAAE;QACN,IAAI,EAAE,OAAO;QACb,KAAK,EAAE,QAAQ,CAAC,gBAAgB,CAAC;KAClC;CAC0C,CAAC;AAC9C,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,IAAI,EAAE,OAAO;IACb,MAAM,EAAE;QACN,WAAW,EACT,4GAA4G;QAC9G,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,kCAAkC;KAC5C;CAC0C,CAAC;AAE9C,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,IAAI,EAAE,YAAY;IAClB,MAAM,EAAE;QACN,WAAW,EACT,wFAAwF;QAC1F,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,qCAAqC;KAC/C;CAC0C,CAAC"}
@@ -1,2 +1,18 @@
1
+ import { type WhookAuthenticationScope } from '@whook/authorization';
1
2
  import { type OpenAPI } from 'ya-open-api-types';
3
+ /**
4
+ * Splits a scope into its scope tokens
5
+ */
6
+ export declare function parseOAuth2Scope(oAuth2Scope: string): string[];
7
+ /**
8
+ * Join scopes into scope tokens
9
+ */
10
+ export declare function stringifyScopes(scopes: WhookAuthenticationScope[]): string;
11
+ /**
12
+ * Filter scopes tokens to become valid ones
13
+ */
14
+ export declare function filterScopes(scopes: string[], allowedScopes: WhookAuthenticationScope[], strict: boolean): WhookAuthenticationScope[];
15
+ /**
16
+ * Collects all scopes tokens from an Open API
17
+ */
2
18
  export declare function collectScopesFromAPI(API: OpenAPI): string[];
@@ -1,4 +1,41 @@
1
+ import { identity } from '@whook/whook';
1
2
  import { PATH_ITEM_METHODS } from 'ya-open-api-types';
3
+ import { YError } from 'yerror';
4
+ const OAUTH2_SCOPES_SEPARATOR = ' ';
5
+ /**
6
+ * Splits a scope into its scope tokens
7
+ */
8
+ export function parseOAuth2Scope(oAuth2Scope) {
9
+ const scopes = [
10
+ ...new Set(oAuth2Scope.split(OAUTH2_SCOPES_SEPARATOR).filter(identity)),
11
+ ];
12
+ return scopes;
13
+ }
14
+ /**
15
+ * Join scopes into scope tokens
16
+ */
17
+ export function stringifyScopes(scopes) {
18
+ return scopes.join(OAUTH2_SCOPES_SEPARATOR);
19
+ }
20
+ /**
21
+ * Filter scopes tokens to become valid ones
22
+ */
23
+ export function filterScopes(scopes, allowedScopes, strict) {
24
+ const filteredScopes = [];
25
+ for (const scope of scopes) {
26
+ if (allowedScopes.includes(scope)) {
27
+ filteredScopes.push(scope);
28
+ continue;
29
+ }
30
+ if (strict) {
31
+ throw new YError('E_OAUTH2_BAD_SCOPE', [scope]);
32
+ }
33
+ }
34
+ return filteredScopes;
35
+ }
36
+ /**
37
+ * Collects all scopes tokens from an Open API
38
+ */
2
39
  export function collectScopesFromAPI(API) {
3
40
  const scopes = [];
4
41
  if (API?.security) {
@@ -1 +1 @@
1
- {"version":3,"file":"scopes.js","sourceRoot":"","sources":["../../src/libs/scopes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEpE,MAAM,UAAU,oBAAoB,CAAC,GAAY;IAC/C,MAAM,MAAM,GAAa,EAAE,CAAC;IAE5B,IAAI,GAAG,EAAE,QAAQ,EAAE,CAAC;QAClB,KAAK,MAAM,WAAW,IAAI,GAAG,CAAC,QAAQ,EAAE,CAAC;YACvC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;gBACvC,MAAM,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC;YACnC,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED,IAAI,GAAG,CAAC,KAAK,EAAE,CAAC;QACd,KAAK,MAAM,UAAU,IAAI,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;YAClD,KAAK,MAAM,MAAM,IAAI,iBAAiB,EAAE,CAAC;gBACvC,MA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;oBACL,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;AAC9B,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function camelCaseObjectProperties<T>(object: Record<string, T>): Record<string, T>;
2
+ export declare function snakeCase(s: string): string;
@@ -0,0 +1,17 @@
1
+ import camelCase from 'camelcase';
2
+ // The OAuth2 standard uses snake case names so we are
3
+ // converting them to the project standards asap
4
+ export function camelCaseObjectProperties(object) {
5
+ return Object.keys(object).reduce((camelCasedObject, key) => {
6
+ const newKey = key === 'redirect_uri' ? 'redirectURI' : camelCase(key);
7
+ camelCasedObject[newKey] = object[key];
8
+ return camelCasedObject;
9
+ }, {});
10
+ }
11
+ export function snakeCase(s) {
12
+ return s
13
+ .split(/(?=(?<![A-Z])[A-Z])|[^a-zA-Z]+/)
14
+ .map((s) => s.toLowerCase())
15
+ .join('_');
16
+ }
17
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/libs/utils.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,WAAW,CAAC;AAElC,sDAAsD;AACtD,gDAAgD;AAChD,MAAM,UAAU,yBAAyB,CACvC,MAAyB;IAEzB,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,CAC/B,CAAC,gBAAgB,EAAE,GAAG,EAAE,EAAE;QACxB,MAAM,MAAM,GAAG,GAAG,KAAK,cAAc,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QAEvE,gBAAgB,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QACvC,OAAO,gBAAgB,CAAC;IAC1B,CAAC,EACD,EAAuB,CACxB,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,CAAS;IACjC,OAAO,CAAC;SACL,KAAK,CAAC,gCAAgC,CAAC;SACvC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;SAC3B,IAAI,CAAC,GAAG,CAAC,CAAC;AACf,CAAC"}
@@ -0,0 +1,10 @@
1
+ export declare const AUTHORIZATION_CODE_RESPONSE_TYPE = "code";
2
+ export declare const AUTHORIZATION_CODE_GRANT_TYPE = "authorization_code";
3
+ export type CodeChallengeMethod = (typeof CODE_CHALLENGE_METHODS)[number];
4
+ export declare const PLAIN_CODE_CHALLENGE_METHOD = "plain";
5
+ export declare const S256_CODE_CHALLENGE_METHOD = "S256";
6
+ export declare const CODE_CHALLENGE_METHODS: readonly ["plain", "S256"];
7
+ export declare function base64UrlEncode(buf: Buffer): string;
8
+ export declare function checkCodeChallengeParameters(responseType: string, codeChallenge?: string, forcePKCE?: boolean): void;
9
+ export declare function computeCodeChallenge(codeChallengeMethod: CodeChallengeMethod, codeVerifier: string): string;
10
+ export declare function checkCodeChallenge(codeChallengeMethod: CodeChallengeMethod, codeChallenge: string, codeVerifier: string): void;
@@ -0,0 +1,51 @@
1
+ import { createHash } from 'crypto';
2
+ import { YError } from 'yerror';
3
+ export const AUTHORIZATION_CODE_RESPONSE_TYPE = 'code';
4
+ export const AUTHORIZATION_CODE_GRANT_TYPE = 'authorization_code';
5
+ export const PLAIN_CODE_CHALLENGE_METHOD = 'plain';
6
+ export const S256_CODE_CHALLENGE_METHOD = 'S256';
7
+ export const CODE_CHALLENGE_METHODS = [
8
+ PLAIN_CODE_CHALLENGE_METHOD,
9
+ S256_CODE_CHALLENGE_METHOD,
10
+ ];
11
+ // See https://tools.ietf.org/html/rfc7636#appendix-A
12
+ export function base64UrlEncode(buf) {
13
+ let s = buf.toString('base64');
14
+ s = s.split('=')[0];
15
+ s = s.replaceAll('+', '-');
16
+ s = s.replaceAll('/', '_');
17
+ return s;
18
+ }
19
+ export function checkCodeChallengeParameters(responseType, codeChallenge, forcePKCE) {
20
+ if (responseType === AUTHORIZATION_CODE_RESPONSE_TYPE) {
21
+ if (!codeChallenge) {
22
+ if (forcePKCE) {
23
+ throw new YError('E_OAUTH2_PKCE_REQUIRED', [responseType]);
24
+ }
25
+ }
26
+ }
27
+ else if (codeChallenge) {
28
+ throw new YError('E_OAUTH2_PKCE_NOT_SUPPORTED', [responseType]);
29
+ }
30
+ }
31
+ export function computeCodeChallenge(codeChallengeMethod, codeVerifier) {
32
+ if (PLAIN_CODE_CHALLENGE_METHOD === codeChallengeMethod) {
33
+ return codeVerifier;
34
+ }
35
+ if (S256_CODE_CHALLENGE_METHOD === codeChallengeMethod) {
36
+ return base64UrlEncode(createHash('sha256').update(Buffer.from(codeVerifier)).digest());
37
+ }
38
+ throw new YError('E_OAUTH2_UNSUPPORTED_CODE_CHALLENGE_METHOD', [
39
+ codeChallengeMethod,
40
+ ]);
41
+ }
42
+ export function checkCodeChallenge(codeChallengeMethod, codeChallenge, codeVerifier) {
43
+ const computedCodeChallenge = computeCodeChallenge(codeChallengeMethod, codeVerifier);
44
+ if (computedCodeChallenge !== codeChallenge) {
45
+ throw new YError('E_OAUTH2_BAD_CODE_VERIFIER', [
46
+ codeChallenge,
47
+ computedCodeChallenge,
48
+ ]);
49
+ }
50
+ }
51
+ //# sourceMappingURL=verifier.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verifier.js","sourceRoot":"","sources":["../../src/libs/verifier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,MAAM,CAAC,MAAM,gCAAgC,GAAG,MAAM,CAAC;AACvD,MAAM,CAAC,MAAM,6BAA6B,GAAG,oBAAoB,CAAC;AAElE,MAAM,CAAC,MAAM,2BAA2B,GAAG,OAAO,CAAC;AACnD,MAAM,CAAC,MAAM,0BAA0B,GAAG,MAAM,CAAC;AAEjD,MAAM,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4C,EAAE;QAC7D,mBAAmB;KACpB,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,kBAAkB,CAChC,mBAAwC,EACxC,aAAqB,EACrB,YAAoB;IAEpB,MAAM,qBAAqB,GAAG,oBAAoB,CAChD,mBAAmB,EACnB,YAAY,CACb,CAAC;IAEF,IAAI,qBAAqB,KAAK,aAAa,EAAE,CAAC;QAC5C,MAAM,IAAI,MAAM,CAAC,4BAA4B,EAAE;YAC7C,aAAa;YACb,qBAAqB;SACtB,CAAC,CAAC;IACL,CAAC;AACH,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
1
+ import { describe, test, expect } from '@jest/globals';
2
+ import { base64UrlEncode, computeCodeChallenge } from './verifier.js';
3
+ describe('base64UrlEncode()', () => {
4
+ test('should work like here https://tools.ietf.org/html/rfc7636#appendix-A', () => {
5
+ expect(base64UrlEncode(Buffer.from([
6
+ 116, 24, 223, 180, 151, 153, 224, 37, 79, 250, 96, 125, 216, 173, 187,
7
+ 186, 22, 212, 37, 77, 105, 214, 191, 240, 91, 88, 5, 88, 83, 132, 141,
8
+ 121,
9
+ ]))).toEqual('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk');
10
+ });
11
+ });
12
+ describe('computeCodeChallenge()', () => {
13
+ test('should work with plain method', () => {
14
+ expect(computeCodeChallenge('plain', 'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk')).toEqual('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk');
15
+ });
16
+ // See https://tools.ietf.org/html/rfc7636#appendix-A
17
+ test('should work like for the spec example', () => {
18
+ expect(computeCodeChallenge('S256', 'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk')).toEqual('E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM');
19
+ });
20
+ });
21
+ //# sourceMappingURL=verifier.test.js.map
@@ -0,0 +1 @@
1
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CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -1,48 +1,26 @@
1
- import { YError } from 'yerror';
2
- import { type WhookErrorsDescriptors, type WhookErrorDescriptor } from '@whook/whook';
3
- import { type OAuth2Options, type OAuth2GranterService } from '../services/oAuth2Granters.js';
1
+ import { type WhookErrorsDescriptors } from '@whook/whook';
2
+ import { type WhookOAuth2Options, type WhookOAuth2GranterService, type WhookOAuth2GranterDefinitions, type WhookOAuth2ReadClientGrantsService } from '../services/oAuth2Granters.js';
4
3
  import { type LogService } from 'common-services';
5
- import { CodeChallengeMethod } from '../services/oAuth2CodeGranter.js';
6
- export declare const codeChallengeSchema: {
7
- readonly name: "CodeChallenge";
8
- readonly schema: {
9
- readonly type: "string";
10
- readonly minLength: 43;
11
- readonly maxLength: 128;
12
- readonly pattern: "^[A-Za-z0-9\\-._~]{43,128}$";
13
- };
14
- };
15
- export declare const codeChallengeParameter: {
16
- readonly name: "code_challenge";
17
- readonly parameter: {
18
- readonly in: "query";
19
- readonly name: "code_challenge";
20
- readonly required: false;
21
- readonly schema: import("ya-json-schema-types").ExpressiveJSONSchema;
22
- };
23
- };
24
- export declare const codeChallengeMethodSchema: {
25
- readonly name: "CodeChallengeMethod";
26
- readonly schema: {
27
- readonly type: "string";
28
- readonly enum: ("plain" | "S256")[];
29
- };
30
- };
31
- export declare const codeChallengeMethodParameter: {
32
- readonly name: "code_challenge_method";
33
- readonly parameter: {
34
- readonly in: "query";
35
- readonly name: "code_challenge_method";
36
- readonly required: false;
37
- readonly schema: import("ya-json-schema-types").ExpressiveJSONSchema;
38
- };
39
- };
4
+ import { type CodeChallengeMethod } from '../libs/verifier.js';
5
+ import { codeChallengeMethodSchema, codeChallengeMethodParameter, codeChallengeSchema, codeChallengeParameter } from '../services/oAuth2AuthorizationCodeGranter.js';
6
+ import { requestURISchema, scopeSchema } from '../libs/schemas.js';
7
+ import { type WhookOAuth2AuthorizationRequestsConfig, type WhookOAuth2AuthorizationRequestsService } from '../services/oAuth2AuthorizationRequests.js';
8
+ export { requestURISchema, scopeSchema, codeChallengeMethodSchema, codeChallengeMethodParameter, codeChallengeSchema, codeChallengeParameter, };
9
+ export interface WhookOAuth2AuthorizeRequestParameters {
10
+ client_id: string;
11
+ response_type: string;
12
+ redirect_uri?: string;
13
+ scope?: string;
14
+ state?: string;
15
+ code_challenge?: string;
16
+ code_challenge_method?: CodeChallengeMethod;
17
+ }
40
18
  export declare const responseTypeParameter: {
41
19
  readonly name: "responseType";
42
20
  readonly parameter: {
43
21
  readonly in: "query";
44
22
  readonly name: "response_type";
45
- readonly required: true;
23
+ readonly required: false;
46
24
  readonly schema: {
47
25
  readonly type: "string";
48
26
  readonly enum: ["code", "token"];
@@ -78,11 +56,8 @@ export declare const scopeParameter: {
78
56
  readonly parameter: {
79
57
  readonly in: "query";
80
58
  readonly name: "scope";
81
- readonly description: "See https://tools.ietf.org/html/rfc6749#section-3.3";
82
59
  readonly required: false;
83
- readonly schema: {
84
- readonly type: "string";
85
- };
60
+ readonly schema: import("ya-json-schema-types").ExpressiveJSONSchema;
86
61
  };
87
62
  };
88
63
  export declare const stateParameter: {
@@ -90,20 +65,29 @@ export declare const stateParameter: {
90
65
  readonly parameter: {
91
66
  readonly in: "query";
92
67
  readonly name: "state";
93
- readonly required: true;
68
+ readonly required: false;
94
69
  readonly schema: {
95
70
  readonly type: "string";
96
71
  };
97
72
  };
98
73
  };
74
+ export declare const requestURIParameter: {
75
+ readonly name: "requestURI";
76
+ readonly parameter: {
77
+ readonly in: "query";
78
+ readonly name: "request_uri";
79
+ readonly required: false;
80
+ readonly schema: import("ya-json-schema-types").ExpressiveJSONSchema;
81
+ };
82
+ };
99
83
  export declare const definition: {
100
84
  readonly method: "get";
101
85
  readonly path: "/oauth2/authorize";
102
86
  readonly operation: {
103
87
  readonly operationId: "getOAuth2Authorize";
104
- readonly summary: "Implements the [Authorization Endpoint](https://tools.ietf.org/html/rfc6749#section-3.1)\n as defined per the OAuth2 RFC.";
88
+ readonly summary: "Implements the [Authorization Endpoint](https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-15.html#section-3.1)\n as defined per the OAuth2.1 RFC.";
105
89
  readonly tags: ["oauth2"];
106
- readonly parameters: [import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>, import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>, import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>, import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>, import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>, import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>, import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>];
90
+ readonly parameters: [import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>, import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>, import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>, import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>, import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>, import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>, import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>, import("ya-open-api-types").OpenAPIReference<import("ya-open-api-types").OpenAPIParameter<import("ya-json-schema-types").ExpressiveJSONSchema, import("ya-open-api-types").OpenAPIExtension>>];
107
91
  readonly responses: {
108
92
  readonly '302': {
109
93
  readonly description: "Redirects the user to the authorization interface.";
@@ -111,23 +95,18 @@ export declare const definition: {
111
95
  };
112
96
  };
113
97
  };
114
- export declare function camelCaseObjectProperties<T>(object: Record<string, T>): Record<string, T>;
115
- export declare function setURLError(url: URL, err: YError | Error, oAuth2Error: WhookErrorDescriptor): void;
116
- declare const _default: import("knifecycle").ServiceInitializer<{
117
- OAUTH2: OAuth2Options;
98
+ declare const _default: import("knifecycle").ServiceInitializer<WhookOAuth2AuthorizationRequestsConfig & {
99
+ OAUTH2: WhookOAuth2Options;
118
100
  ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
119
- oAuth2Granters: OAuth2GranterService[];
101
+ oAuth2Granters: WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
102
+ readClientGrants: WhookOAuth2ReadClientGrantsService;
103
+ oAuth2AuthorizationRequests?: Pick<WhookOAuth2AuthorizationRequestsService, "check">;
120
104
  log: LogService;
121
- }, ({ query: { response_type: responseType, client_id: clientId, redirect_uri: demandedRedirectURI, scope: demandedScope, state, code_challenge: codeChallenge, code_challenge_method: codeChallengeMethod, ...authorizeParameters }, }: {
105
+ }, ({ query: { client_id: givenClientId, request_uri: requestURI, ...query }, }: {
122
106
  query: {
123
- response_type: string;
124
107
  client_id: string;
125
- redirect_uri?: string;
126
- scope?: string;
127
- state: string;
128
- code_challenge?: string;
129
- code_challenge_method?: CodeChallengeMethod;
130
- } & Record<string, unknown>;
108
+ request_uri?: string;
109
+ } & Partial<Omit<WhookOAuth2AuthorizeRequestParameters, "client_id">>;
131
110
  }) => Promise<{
132
111
  status: number;
133
112
  headers: {