@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
@@ -2,102 +2,95 @@
2
2
  import { describe, test, beforeEach, jest, expect } from '@jest/globals';
3
3
  import initGetOAuth2WellKnown from './getOAuth2WellKnownMetadata.js';
4
4
  import { YError } from 'yerror';
5
- import { type OAuth2CodeGranterService } from '../services/oAuth2CodeGranter.js';
6
- import { type OAuth2TokenGranterService } from '../services/oAuth2TokenGranter.js';
5
+ import { type WhookOAuth2AuthorizationCodeGranterService } from '../services/oAuth2AuthorizationCodeGranter.js';
6
+ import { type WhookOAuth2ImplicitGranterService } from '../services/oAuth2ImplicitGranter.js';
7
7
  import { type LogService } from 'common-services';
8
- import { type OAuth2GranterService } from '../index.js';
9
- import { type WhookRoutesDefinitionsService } from '@whook/whook';
8
+ import {
9
+ type WhookOAuth2GranterDefinitions,
10
+ type WhookOAuth2GranterService,
11
+ } from '../index.js';
12
+ import { type WhookDefinitions } from '@whook/whook';
10
13
 
11
14
  describe('getOAuth2WellKnown', () => {
12
15
  const log = jest.fn<LogService>();
13
16
  const codeGranter = {
14
- type: 'code',
15
- authorizer: {
16
- responseType: 'code',
17
- authorize:
18
- jest.fn<
19
- NonNullable<OAuth2CodeGranterService['authorizer']>['authorize']
20
- >(),
21
- },
22
- acknowledger: {
23
- acknowledgmentType: 'code',
24
- acknowledge:
25
- jest.fn<
26
- NonNullable<OAuth2CodeGranterService['acknowledger']>['acknowledge']
27
- >(),
28
- },
29
- authenticator: {
30
- grantType: 'authorization_code',
31
- authenticate:
32
- jest.fn<
33
- NonNullable<OAuth2CodeGranterService['authenticator']>['authenticate']
34
- >(),
35
- },
17
+ grantType: 'authorization_code',
18
+ responseType: 'code',
19
+ authorize:
20
+ jest.fn<
21
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['authorize']>
22
+ >(),
23
+ acknowledge:
24
+ jest.fn<
25
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['acknowledge']>
26
+ >(),
27
+ authenticate:
28
+ jest.fn<
29
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['authenticate']>
30
+ >(),
36
31
  };
37
32
  const tokenGranter = {
38
- type: 'token',
39
- authorizer: {
40
- responseType: 'token',
41
- authorize:
42
- jest.fn<
43
- NonNullable<
44
- NonNullable<OAuth2TokenGranterService['authorizer']>['authorize']
45
- >
46
- >(),
47
- },
48
- acknowledger: {
49
- acknowledgmentType: 'token',
50
- acknowledge:
51
- jest.fn<
52
- NonNullable<OAuth2TokenGranterService['acknowledger']>['acknowledge']
53
- >(),
54
- },
33
+ grantType: 'implicit',
34
+ responseType: 'token',
35
+ authorize:
36
+ jest.fn<
37
+ NonNullable<NonNullable<WhookOAuth2ImplicitGranterService['authorize']>>
38
+ >(),
39
+ acknowledge:
40
+ jest.fn<NonNullable<WhookOAuth2ImplicitGranterService['acknowledge']>>(),
55
41
  };
56
42
  const oAuth2Granters = [
57
43
  codeGranter,
58
44
  tokenGranter,
59
- ] as unknown as OAuth2GranterService[];
45
+ ] as unknown as WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
60
46
 
61
47
  beforeEach(() => {
62
48
  log.mockReset();
63
49
  [
64
- codeGranter.authorizer.authorize,
65
- codeGranter.acknowledger.acknowledge,
66
- codeGranter.authenticator.authenticate,
67
- tokenGranter.authorizer.authorize,
68
- tokenGranter.acknowledger.acknowledge,
50
+ codeGranter.authorize,
51
+ codeGranter.acknowledge,
52
+ codeGranter.authenticate,
53
+ tokenGranter.authorize,
54
+ tokenGranter.acknowledge,
69
55
  ].forEach((mock) => mock.mockReset());
70
56
  });
71
57
 
72
58
  test('should return OAuth2 metadata', async () => {
73
59
  [
74
- codeGranter.authorizer.authorize,
75
- codeGranter.acknowledger.acknowledge,
76
- codeGranter.authenticator.authenticate,
77
- tokenGranter.authorizer.authorize,
78
- tokenGranter.acknowledger.acknowledge,
60
+ codeGranter.authorize,
61
+ codeGranter.acknowledge,
62
+ codeGranter.authenticate,
63
+ tokenGranter.authorize,
64
+ tokenGranter.acknowledge,
79
65
  ].forEach((mock: any) =>
80
66
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
81
67
  );
82
68
 
83
69
  const getOAuth2WellKnown = await initGetOAuth2WellKnown({
84
70
  BASE_URL: 'https://server.example.com',
85
- ROUTES_DEFINITIONS: {
86
- getOAuth2Authorize: {
87
- module: {
88
- definition: {
89
- path: '/v0/oauth2/authorize',
90
- },
71
+ OAUTH2_PAR: {
72
+ mode: 'required',
73
+ },
74
+ DEFINITIONS: {
75
+ configs: {
76
+ getOAuth2Authorize: {
77
+ type: 'route',
78
+ path: '/v0/oauth2/authorize',
91
79
  },
92
- },
93
- postOAuth2Token: {
94
- module: {
95
- definition: {
96
- path: '/v0/oauth2/token',
97
- },
80
+ postOAuth2Token: {
81
+ type: 'route',
82
+ path: '/v0/oauth2/token',
83
+ },
84
+ postOAuth2Revoke: {
85
+ type: 'route',
86
+ path: '/v0/oauth2/revoke',
87
+ },
88
+ postOAuth2PushedAuthorizationRequest: {
89
+ type: 'route',
90
+ path: '/v0/oauth2/par',
98
91
  },
99
92
  },
100
- } as unknown as WhookRoutesDefinitionsService,
93
+ } as unknown as WhookDefinitions,
101
94
  API: {
102
95
  openapi: '3.2',
103
96
  info: {
@@ -124,16 +117,14 @@ describe('getOAuth2WellKnown', () => {
124
117
  expect({
125
118
  response,
126
119
  logCalls: log.mock.calls.filter((args) => args[0].endsWith('stack')),
127
- codeGranterAuthorizerAuthorizeCalls:
128
- codeGranter.authorizer.authorize.mock.calls,
120
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
129
121
  codeGranterAcknowledgerAcknowledgeCalls:
130
- codeGranter.acknowledger.acknowledge.mock.calls,
122
+ codeGranter.acknowledge.mock.calls,
131
123
  codeGranterAuthenticatorAuthenticateCalls:
132
- codeGranter.authenticator.authenticate.mock.calls,
133
- tokenGranterAuthorizerAuthorizeCalls:
134
- tokenGranter.authorizer.authorize.mock.calls,
124
+ codeGranter.authenticate.mock.calls,
125
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
135
126
  tokenGranterAcknowledgerAcknowledgeCalls:
136
- tokenGranter.acknowledger.acknowledge.mock.calls,
127
+ tokenGranter.acknowledge.mock.calls,
137
128
  }).toMatchInlineSnapshot(`
138
129
  {
139
130
  "codeGranterAcknowledgerAcknowledgeCalls": [],
@@ -145,12 +136,20 @@ describe('getOAuth2WellKnown', () => {
145
136
  "authorization_endpoint": "https://server.example.com/v0/oauth2/authorize",
146
137
  "grant_types_supported": [
147
138
  "authorization_code",
139
+ "implicit",
148
140
  ],
149
141
  "issuer": "https://server.example.com",
142
+ "pushed_authorization_request_endpoint": "https://server.example.com/v0/oauth2/par",
143
+ "require_pushed_authorization_requests": true,
150
144
  "response_types_supported": [
151
145
  "code",
152
146
  "token",
153
147
  ],
148
+ "revocation_endpoint": "https://server.example.com/v0/oauth2/revoke",
149
+ "revocation_endpoint_auth_methods_supported": [
150
+ "client_secret_basic",
151
+ "client_secret_post",
152
+ ],
154
153
  "scopes_supported": [
155
154
  "user",
156
155
  "admin",
@@ -1,16 +1,27 @@
1
1
  import {
2
2
  identity,
3
3
  refersTo,
4
- type WhookRoutesDefinitionsService,
4
+ type WhookDefinitions,
5
5
  type WhookAPISchemaDefinition,
6
6
  type WhookRouteDefinition,
7
7
  } from '@whook/whook';
8
8
  import { autoService, location } from 'knifecycle';
9
- import { type OAuth2GranterService } from '../services/oAuth2Granters.js';
10
- import { codeChallengeMethodSchema } from './getOAuth2Authorize.js';
9
+ import {
10
+ type WhookOAuth2GranterDefinitions,
11
+ type WhookOAuth2GranterService,
12
+ } from '../services/oAuth2Granters.js';
13
+ import { codeChallengeMethodSchema } from '../services/oAuth2AuthorizationCodeGranter.js';
14
+ import { scopeTokenSchema, scopeTokensSchema } from '../libs/schemas.js';
11
15
  import { type LogService } from 'common-services';
12
16
  import { type OpenAPI } from 'ya-open-api-types';
13
17
  import { collectScopesFromAPI } from '../libs/scopes.js';
18
+ import {
19
+ DEFAULT_OAUTH2_PAR,
20
+ type WhookOAuth2AuthorizationRequestsConfig,
21
+ } from '../services/oAuth2AuthorizationRequests.js';
22
+ import { YError } from 'yerror';
23
+
24
+ export { scopeTokenSchema, scopeTokensSchema, codeChallengeMethodSchema };
14
25
 
15
26
  // OAuth Token Endpoint Authentication Methods
16
27
  export const endpointAuthenticationMethodsSchema = {
@@ -87,9 +98,10 @@ export const oAuth2MetadataSchema = {
87
98
  issuer: refersTo(httpsProtocolURISchema),
88
99
  authorization_endpoint: refersTo(httpsProtocolURISchema),
89
100
  token_endpoint: refersTo(httpsProtocolURISchema),
101
+ pushed_authorization_request_endpoint: refersTo(httpsProtocolURISchema),
90
102
  jwks_uri: refersTo(httpsProtocolURISchema),
91
103
  registration_endpoint: refersTo(httpsProtocolURISchema),
92
- scopes_supported: { type: 'array', items: { type: 'string' } },
104
+ scopes_supported: refersTo(scopeTokensSchema),
93
105
  response_types_supported: {
94
106
  type: 'array',
95
107
  items: {
@@ -249,6 +261,9 @@ export const oAuth2MetadataSchema = {
249
261
  request_uri_parameter_supported: {
250
262
  type: 'boolean',
251
263
  },
264
+ require_pushed_authorization_requests: {
265
+ type: 'boolean',
266
+ },
252
267
  require_request_uri_registration: {
253
268
  type: 'boolean',
254
269
  },
@@ -288,24 +303,41 @@ export const definition = {
288
303
  async function initGetOAuth2WellKnownMetadata({
289
304
  API,
290
305
  BASE_URL,
291
- ROUTES_DEFINITIONS,
306
+ DEFINITIONS,
307
+ OAUTH2_PAR = DEFAULT_OAUTH2_PAR,
292
308
  oAuth2Granters,
293
309
  log,
294
- }: {
310
+ }: WhookOAuth2AuthorizationRequestsConfig & {
295
311
  API: OpenAPI;
296
312
  BASE_URL: string;
297
- ROUTES_DEFINITIONS: WhookRoutesDefinitionsService;
298
- oAuth2Granters: OAuth2GranterService[];
313
+ DEFINITIONS: WhookDefinitions;
314
+ oAuth2Granters: WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
299
315
  log: LogService;
300
316
  }) {
301
317
  if (!BASE_URL.startsWith('https')) {
302
318
  log('warning', `⚠️ - OAuth2 issuer must start with HTTPS (${BASE_URL}).`);
303
319
  }
304
320
 
321
+ if (
322
+ OAUTH2_PAR.mode !== 'disabled' &&
323
+ !DEFINITIONS.configs['postOAuth2PushedAuthorizationRequest']
324
+ ) {
325
+ log('error', `💥 - OAuth2 PAR endpoint required to enable PAR.`);
326
+ throw new YError('E_OAUTH2_MISCONFIGURED');
327
+ }
328
+
305
329
  const body = {
306
330
  issuer: `${BASE_URL}`,
307
- authorization_endpoint: `${BASE_URL}${ROUTES_DEFINITIONS['getOAuth2Authorize']?.module.definition.path || '/oauth2/authorize'}`,
308
- token_endpoint: `${BASE_URL}${ROUTES_DEFINITIONS['postOAuth2Token']?.module.definition.path || '/oauth2/token'}`,
331
+ authorization_endpoint: `${BASE_URL}${
332
+ DEFINITIONS.configs['getOAuth2Authorize']?.type === 'route'
333
+ ? DEFINITIONS.configs['getOAuth2Authorize'].path
334
+ : '/oauth2/authorize'
335
+ }`,
336
+ token_endpoint: `${BASE_URL}${
337
+ DEFINITIONS.configs['postOAuth2Token']?.type === 'route'
338
+ ? DEFINITIONS.configs['postOAuth2Token'].path
339
+ : '/oauth2/token'
340
+ }`,
309
341
  // TODO: Put it behind options (allow none)
310
342
  token_endpoint_auth_methods_supported: [
311
343
  'client_secret_basic',
@@ -313,19 +345,34 @@ async function initGetOAuth2WellKnownMetadata({
313
345
  ],
314
346
  grant_types_supported: [
315
347
  ...new Set(
316
- oAuth2Granters
317
- .map((granter) => granter.authenticator?.grantType)
318
- .filter(identity),
348
+ oAuth2Granters.map((granter) => granter.grantType).filter(identity),
319
349
  ),
320
350
  ],
321
351
  response_types_supported: [
322
352
  ...new Set(
323
- oAuth2Granters
324
- .map((granter) => granter.authorizer?.responseType)
325
- .filter(identity),
353
+ oAuth2Granters.map((granter) => granter.responseType).filter(identity),
326
354
  ),
327
355
  ],
328
356
  scopes_supported: collectScopesFromAPI(API),
357
+ ...(DEFINITIONS.configs['postOAuth2Revoke']?.type === 'route' &&
358
+ DEFINITIONS.configs['postOAuth2Revoke'].path
359
+ ? {
360
+ revocation_endpoint: `${BASE_URL}${DEFINITIONS.configs['postOAuth2Revoke'].path}`,
361
+ revocation_endpoint_auth_methods_supported: [
362
+ 'client_secret_basic',
363
+ 'client_secret_post',
364
+ ],
365
+ }
366
+ : {}),
367
+ ...(DEFINITIONS.configs['postOAuth2PushedAuthorizationRequest']?.type ===
368
+ 'route' &&
369
+ DEFINITIONS.configs['postOAuth2PushedAuthorizationRequest'].path &&
370
+ OAUTH2_PAR.mode !== 'disabled'
371
+ ? {
372
+ pushed_authorization_request_endpoint: `${BASE_URL}${DEFINITIONS.configs['postOAuth2PushedAuthorizationRequest'].path}`,
373
+ require_pushed_authorization_requests: OAUTH2_PAR.mode === 'required',
374
+ }
375
+ : {}),
329
376
  };
330
377
 
331
378
  return async () => {
@@ -11,6 +11,9 @@ import {
11
11
  import { type LogService } from 'common-services';
12
12
  import { type OpenAPI } from 'ya-open-api-types';
13
13
  import { collectScopesFromAPI } from '../libs/scopes.js';
14
+ import { scopeTokensSchema } from '../libs/schemas.js';
15
+
16
+ export { scopeTokensSchema };
14
17
 
15
18
  export const bearerMethodSchema = {
16
19
  name: 'OAuth2BearerMethod',
@@ -35,12 +38,7 @@ export const oAuth2ProtectedResourceMetadataSchema = {
35
38
  items: refersTo(httpsProtocolURISchema),
36
39
  },
37
40
  jwks_uri: refersTo(httpsProtocolURISchema),
38
- scopes_supported: {
39
- type: 'array',
40
- items: {
41
- type: 'string',
42
- },
43
- },
41
+ scopes_supported: refersTo(scopeTokensSchema),
44
42
  bearer_methods_supported: {
45
43
  type: 'array',
46
44
  items: refersTo(bearerMethodSchema),
@@ -1,20 +1,45 @@
1
- import { describe, test, jest, expect } from '@jest/globals';
1
+ import { describe, test, beforeEach, jest, expect } from '@jest/globals';
2
2
  import initPostAuthLogin from './postAuthLogin.js';
3
- import { type AuthCookiesService } from '../services/authCookies.js';
4
- import { type WhookAuthenticationData } from '@whook/authorization';
3
+ import { type WhookAuthCookiesService } from '../services/authCookies.js';
4
+ import {
5
+ type WhookOAuth2ReadClientGrantsService,
6
+ type WhookOAuth2Options,
7
+ } from '../services/oAuth2Granters.js';
5
8
 
6
9
  describe('postAuthLogin', () => {
10
+ const OAUTH2: WhookOAuth2Options = {
11
+ authenticateURL: 'https://auth.example.com/sign_in',
12
+ allowedScopes: ['user', 'admin'],
13
+ rootClientId: 'root_app_id',
14
+ };
15
+ const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
16
+ const authCookies = {
17
+ build: jest.fn<WhookAuthCookiesService['build']>(),
18
+ };
19
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
+ const postOAuth2Token = jest.fn<any>();
21
+
22
+ beforeEach(() => {
23
+ readClientGrants.mockReset();
24
+ postOAuth2Token.mockReset();
25
+ authCookies.build.mockReset();
26
+ readClientGrants.mockResolvedValue({
27
+ allowedScopes: ['user', 'admin'],
28
+ allowedRedirectURIS: ['https://www.example.com'],
29
+ allowedGrantTypes: ['code'],
30
+ authenticationData: {
31
+ clientId: 'root_app_id',
32
+ scopes: ['user', 'admin'],
33
+ userId: 'user_id',
34
+ },
35
+ isPublicClient: true,
36
+ });
37
+ });
38
+
7
39
  test('should work', async () => {
8
- const authCookies = {
9
- build: jest.fn<AuthCookiesService['build']>(),
10
- };
11
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
- const postOAuth2Token = jest.fn<any>();
13
40
  const postAuthLogin = await initPostAuthLogin({
14
- ROOT_AUTHENTICATION_DATA: {
15
- scope: 'user',
16
- applicationId: 'root_app_id',
17
- } as WhookAuthenticationData,
41
+ OAUTH2,
42
+ readClientGrants,
18
43
  authCookies,
19
44
  postOAuth2Token,
20
45
  });
@@ -44,51 +69,55 @@ describe('postAuthLogin', () => {
44
69
  authCookiesBuildCalls: authCookies.build.mock.calls,
45
70
  postOAuth2TokenCalls: postOAuth2Token.mock.calls,
46
71
  }).toMatchInlineSnapshot(`
47
- {
48
- "authCookiesBuildCalls": [
49
- [
50
- {
51
- "access_token": "an_access_token",
52
- "expiration_date": "2020-02-02T20:22:02Z",
53
- "expires_in": 3600,
54
- "token_type": "bearer",
55
- },
56
- {
57
- "session": true,
58
- },
59
- ],
60
- ],
61
- "postOAuth2TokenCalls": [
62
- [
63
- {
64
- "authenticationData": {
65
- "applicationId": "root_app_id",
66
- "scope": "user",
67
- },
68
- "body": {
69
- "grant_type": "password",
70
- "password": "a_password",
71
- "scope": "user",
72
- "username": "a_username",
73
- },
74
- },
75
- ],
76
- ],
77
- "response": {
78
- "body": {
79
- "access_token": "an_access_token",
80
- "expiration_date": "2020-02-02T20:22:02Z",
81
- "expires_in": 3600,
82
- "token_type": "bearer",
83
- },
84
- "headers": {
85
- "Set-Cookie": [
86
- "the_build_cookies",
87
- ],
88
- },
89
- "status": 200,
90
- },
91
- }
72
+ {
73
+ "authCookiesBuildCalls": [
74
+ [
75
+ {
76
+ "access_token": "an_access_token",
77
+ "expiration_date": "2020-02-02T20:22:02Z",
78
+ "expires_in": 3600,
79
+ "token_type": "bearer",
80
+ },
81
+ {
82
+ "session": true,
83
+ },
84
+ ],
85
+ ],
86
+ "postOAuth2TokenCalls": [
87
+ [
88
+ {
89
+ "authenticationData": {
90
+ "clientId": "root_app_id",
91
+ "scopes": [
92
+ "user",
93
+ "admin",
94
+ ],
95
+ "userId": "user_id",
96
+ },
97
+ "body": {
98
+ "grant_type": "password",
99
+ "password": "a_password",
100
+ "scope": "user",
101
+ "username": "a_username",
102
+ },
103
+ },
104
+ ],
105
+ ],
106
+ "response": {
107
+ "body": {
108
+ "access_token": "an_access_token",
109
+ "expiration_date": "2020-02-02T20:22:02Z",
110
+ "expires_in": 3600,
111
+ "token_type": "bearer",
112
+ },
113
+ "headers": {
114
+ "Set-Cookie": [
115
+ "the_build_cookies",
116
+ ],
117
+ },
118
+ "status": 200,
119
+ },
120
+ }
92
121
  `);
93
122
  });
94
123
  });
@@ -1,12 +1,17 @@
1
1
  import { autoService, location } from 'knifecycle';
2
+ import {
3
+ type WhookOAuth2ReadClientGrantsService,
4
+ type WhookOAuth2Options,
5
+ } from '../services/oAuth2Granters.js';
2
6
  import { YHTTPError } from 'yhttperror';
3
- import initPostOauth2Token from './postOAuth2Token.js';
7
+ import initPostOAuth2Token from './postOAuth2Token.js';
4
8
  import { AUTH_API_PREFIX } from '../services/authCookies.js';
5
- import { type WhookRouteDefinition } from '@whook/whook';
6
- import {
7
- type AuthCookiesService,
8
- type AuthHandlersConfig,
9
- } from '../services/authCookies.js';
9
+ import { refersTo, type WhookRouteDefinition } from '@whook/whook';
10
+ import { type WhookAuthCookiesService } from '../services/authCookies.js';
11
+ import { scopeSchema } from '../libs/schemas.js';
12
+ import { PASSWORD_GRANT_TYPE } from '../services/oAuth2PasswordGranter.js';
13
+
14
+ export { scopeSchema };
10
15
 
11
16
  export const definition = {
12
17
  method: 'post',
@@ -29,7 +34,7 @@ export const definition = {
29
34
  properties: {
30
35
  username: { type: 'string' },
31
36
  password: { type: 'string' },
32
- scope: { type: 'string' },
37
+ scope: refersTo(scopeSchema),
33
38
  remember: { type: 'boolean' },
34
39
  },
35
40
  },
@@ -46,7 +51,7 @@ export const definition = {
46
51
  properties: {
47
52
  access_token: { type: 'string' },
48
53
  expiration_date: { type: 'string' },
49
- expires_in: { type: 'string' },
54
+ expires_in: { type: 'number' },
50
55
  token_type: { type: 'string' },
51
56
  },
52
57
  },
@@ -58,12 +63,15 @@ export const definition = {
58
63
  } as const satisfies WhookRouteDefinition;
59
64
 
60
65
  async function initPostAuthLogin({
61
- ROOT_AUTHENTICATION_DATA,
66
+ OAUTH2,
67
+ readClientGrants,
62
68
  authCookies,
63
69
  postOAuth2Token,
64
- }: AuthHandlersConfig & {
65
- authCookies: Pick<AuthCookiesService, 'build'>;
66
- postOAuth2Token: Awaited<ReturnType<typeof initPostOauth2Token>>;
70
+ }: {
71
+ OAUTH2: WhookOAuth2Options;
72
+ readClientGrants: WhookOAuth2ReadClientGrantsService;
73
+ authCookies: Pick<WhookAuthCookiesService, 'build'>;
74
+ postOAuth2Token: Awaited<ReturnType<typeof initPostOAuth2Token>>;
67
75
  }) {
68
76
  return async ({
69
77
  body,
@@ -75,15 +83,17 @@ async function initPostAuthLogin({
75
83
  remember: boolean;
76
84
  };
77
85
  }) => {
86
+ const { authenticationData } = await readClientGrants(OAUTH2.rootClientId);
87
+
78
88
  try {
79
89
  const response = await postOAuth2Token({
80
90
  body: {
81
- grant_type: 'password',
91
+ grant_type: PASSWORD_GRANT_TYPE,
82
92
  scope: body.scope,
83
93
  username: body.username,
84
94
  password: body.password,
85
95
  },
86
- authenticationData: ROOT_AUTHENTICATION_DATA,
96
+ authenticationData,
87
97
  });
88
98
 
89
99
  return {