@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,182 +2,1182 @@
2
2
  import { describe, test, beforeEach, jest, expect } from '@jest/globals';
3
3
  import initGetOAuth2Authorize from './getOAuth2Authorize.js';
4
4
  import { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';
5
- import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters.js';
5
+ import {
6
+ type WhookOAuth2ReadClientGrantsService,
7
+ type WhookOAuth2GranterDefinitions,
8
+ } from '../services/oAuth2Granters.js';
6
9
  import { YError } from 'yerror';
7
- import { type OAuth2CodeGranterService } from '../services/oAuth2CodeGranter.js';
8
- import { type OAuth2TokenGranterService } from '../services/oAuth2TokenGranter.js';
10
+ import { type WhookOAuth2AuthorizationCodeGranterService } from '../services/oAuth2AuthorizationCodeGranter.js';
11
+ import { type WhookOAuth2ImplicitGranterService } from '../services/oAuth2ImplicitGranter.js';
9
12
  import { type LogService } from 'common-services';
10
- import { type OAuth2Options, type OAuth2GranterService } from '../index.js';
13
+ import {
14
+ type WhookOAuth2Options,
15
+ type WhookOAuth2GranterService,
16
+ } from '../index.js';
17
+ import { OAUTH2_ERRORS_DESCRIPTORS } from '../libs/errors.js';
18
+ import { type WhookOAuth2AuthorizationRequestsService } from '../services/oAuth2AuthorizationRequests.js';
11
19
 
12
20
  describe('getOAuth2Authorize', () => {
13
- const OAUTH2: OAuth2Options = {
21
+ const OAUTH2: WhookOAuth2Options = {
22
+ rootClientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
14
23
  authenticateURL: 'https://auth.example.com/sign_in',
24
+ allowedScopes: ['user'],
15
25
  };
16
26
  const ERRORS_DESCRIPTORS = {
17
27
  ...DEFAULT_ERRORS_DESCRIPTORS,
18
28
  ...OAUTH2_ERRORS_DESCRIPTORS,
19
29
  };
20
30
  const log = jest.fn<LogService>();
21
- const codeGranter = {
22
- type: 'code',
23
- authorizer: {
24
- responseType: 'code',
25
- authorize:
26
- jest.fn<
27
- NonNullable<OAuth2CodeGranterService['authorizer']>['authorize']
28
- >(),
29
- },
30
- acknowledger: {
31
- acknowledgmentType: 'code',
32
- acknowledge:
33
- jest.fn<
34
- NonNullable<OAuth2CodeGranterService['acknowledger']>['acknowledge']
35
- >(),
36
- },
37
- authenticator: {
38
- grantType: 'authorization_code',
39
- authenticate:
40
- jest.fn<
41
- NonNullable<OAuth2CodeGranterService['authenticator']>['authenticate']
42
- >(),
43
- },
31
+ const oAuth2AuthorizationRequests = {
32
+ check: jest.fn<WhookOAuth2AuthorizationRequestsService['check']>(),
44
33
  };
34
+ const codeGranter = {
35
+ grantType: 'authorization_code',
36
+ responseType: 'code',
37
+ issuesRefreshToken: true,
38
+ authorize:
39
+ jest.fn<
40
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['authorize']>
41
+ >(),
42
+ acknowledge:
43
+ jest.fn<
44
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['acknowledge']>
45
+ >(),
46
+ authenticate:
47
+ jest.fn<
48
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['authenticate']>
49
+ >(),
50
+ } satisfies WhookOAuth2AuthorizationCodeGranterService;
45
51
  const tokenGranter = {
46
- type: 'token',
47
- authorizer: {
48
- responseType: 'token',
49
- authorize:
50
- jest.fn<
51
- NonNullable<
52
- NonNullable<OAuth2TokenGranterService['authorizer']>['authorize']
53
- >
54
- >(),
55
- },
56
- acknowledger: {
57
- acknowledgmentType: 'token',
58
- acknowledge:
59
- jest.fn<
60
- NonNullable<OAuth2TokenGranterService['acknowledger']>['acknowledge']
61
- >(),
62
- },
63
- };
52
+ grantType: 'implicit',
53
+ responseType: 'token',
54
+ issuesRefreshToken: false,
55
+ authorize:
56
+ jest.fn<
57
+ NonNullable<NonNullable<WhookOAuth2ImplicitGranterService['authorize']>>
58
+ >(),
59
+ acknowledge:
60
+ jest.fn<NonNullable<WhookOAuth2ImplicitGranterService['acknowledge']>>(),
61
+ } satisfies WhookOAuth2ImplicitGranterService;
62
+ const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
64
63
  const oAuth2Granters = [
65
64
  codeGranter,
66
65
  tokenGranter,
67
- ] as unknown as OAuth2GranterService[];
66
+ ] as unknown as WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
68
67
 
69
68
  beforeEach(() => {
70
- log.mockReset();
71
69
  [
72
- codeGranter.authorizer.authorize,
73
- codeGranter.acknowledger.acknowledge,
74
- codeGranter.authenticator.authenticate,
75
- tokenGranter.authorizer.authorize,
76
- tokenGranter.acknowledger.acknowledge,
70
+ log,
71
+ codeGranter.authorize,
72
+ codeGranter.acknowledge,
73
+ codeGranter.authenticate,
74
+ tokenGranter.authorize,
75
+ tokenGranter.acknowledge,
76
+ readClientGrants,
77
+ oAuth2AuthorizationRequests.check,
77
78
  ].forEach((mock) => mock.mockReset());
78
79
  });
79
80
 
80
81
  test('should redirect', async () => {
81
82
  [
82
- codeGranter.acknowledger.acknowledge,
83
- codeGranter.authenticator.authenticate,
84
- tokenGranter.authorizer.authorize,
85
- tokenGranter.acknowledger.acknowledge,
86
- ].forEach((mock: any) =>
83
+ codeGranter.acknowledge,
84
+ codeGranter.authenticate,
85
+ tokenGranter.authorize,
86
+ tokenGranter.acknowledge,
87
+ oAuth2AuthorizationRequests.check,
88
+ ].forEach((mock) =>
87
89
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
88
90
  );
89
- codeGranter.authorizer.authorize.mockResolvedValueOnce({
90
- applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
91
- redirectURI: 'http://lol',
92
- scope: 'user',
91
+ readClientGrants.mockResolvedValue({
92
+ allowedScopes: ['user', 'admin'],
93
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
94
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
95
+ authenticationData: {
96
+ clientId: 'the_client_app_id',
97
+ scopes: ['user', 'admin'],
98
+ userId: 'user_id',
99
+ },
100
+ isPublicClient: false,
101
+ });
102
+ codeGranter.authorize.mockResolvedValueOnce({
103
+ scopes: ['user'],
93
104
  });
94
105
 
95
106
  const getOAuth2Authorize = await initGetOAuth2Authorize({
96
107
  OAUTH2,
97
108
  ERRORS_DESCRIPTORS,
98
109
  oAuth2Granters,
110
+ readClientGrants,
111
+ oAuth2AuthorizationRequests,
99
112
  log,
100
113
  });
101
114
  const response = await getOAuth2Authorize({
102
115
  query: {
103
116
  response_type: 'code',
104
- client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
105
- redirect_uri: 'https://www.example.com',
117
+ client_id: 'the_client_app_id',
118
+ redirect_uri: 'https://www.example.com/oauth2/cb',
106
119
  scope: 'user',
107
120
  state: 'bancal',
108
121
  },
109
122
  });
110
123
 
111
- expect(response).toMatchInlineSnapshot(`
112
- {
113
- "headers": {
114
- "location": "https://auth.example.com/sign_in?type=code&redirect_uri=http%3A%2F%2Flol&scope=user&client_id=abbacaca-abba-caca-abba-cacaabbacaca&state=bancal",
115
- },
116
- "status": 302,
117
- }
124
+ expect({
125
+ response,
126
+ logCalls: log.mock.calls.filter((args) => args[0].endsWith('stack')),
127
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
128
+ codeGranterAcknowledgerAcknowledgeCalls:
129
+ codeGranter.acknowledge.mock.calls,
130
+ codeGranterAuthenticatorAuthenticateCalls:
131
+ codeGranter.authenticate.mock.calls,
132
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
133
+ tokenGranterAcknowledgerAcknowledgeCalls:
134
+ tokenGranter.acknowledge.mock.calls,
135
+ readClientGrantsCalls: readClientGrants.mock.calls,
136
+ oAuth2AuthorizationRequestsCheckCalls:
137
+ oAuth2AuthorizationRequests.check.mock.calls,
138
+ }).toMatchInlineSnapshot(`
139
+ {
140
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
141
+ "codeGranterAuthenticatorAuthenticateCalls": [],
142
+ "codeGranterAuthorizerAuthorizeCalls": [
143
+ [
144
+ {
145
+ "clientGrants": {
146
+ "allowedGrantTypes": [
147
+ "authorization_code",
148
+ "refresh_token",
149
+ ],
150
+ "allowedRedirectURIS": [
151
+ "https://www.example.com/oauth2/cb",
152
+ ],
153
+ "allowedScopes": [
154
+ "user",
155
+ "admin",
156
+ ],
157
+ "authenticationData": {
158
+ "clientId": "the_client_app_id",
159
+ "scopes": [
160
+ "user",
161
+ "admin",
162
+ ],
163
+ "userId": "user_id",
164
+ },
165
+ "isPublicClient": false,
166
+ },
167
+ "clientId": "the_client_app_id",
168
+ "demandedScopes": [
169
+ "user",
170
+ ],
171
+ },
172
+ ],
173
+ ],
174
+ "logCalls": [],
175
+ "oAuth2AuthorizationRequestsCheckCalls": [],
176
+ "readClientGrantsCalls": [
177
+ [
178
+ "the_client_app_id",
179
+ ],
180
+ ],
181
+ "response": {
182
+ "headers": {
183
+ "location": "https://auth.example.com/sign_in?type=code&redirect_uri=https%3A%2F%2Fwww.example.com%2Foauth2%2Fcb&scope=user&client_id=the_client_app_id&state=bancal",
184
+ },
185
+ "status": 302,
186
+ },
187
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
188
+ "tokenGranterAuthorizerAuthorizeCalls": [],
189
+ }
118
190
  `);
191
+ });
192
+
193
+ test('should redirect with a pushed authorization request URI', async () => {
194
+ [
195
+ codeGranter.acknowledge,
196
+ codeGranter.authenticate,
197
+ tokenGranter.authorize,
198
+ tokenGranter.acknowledge,
199
+ ].forEach((mock: any) =>
200
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
201
+ );
202
+ readClientGrants.mockResolvedValue({
203
+ allowedScopes: ['user', 'admin'],
204
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
205
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
206
+ authenticationData: {
207
+ clientId: 'the_client_app_id',
208
+ scopes: ['user', 'admin'],
209
+ userId: 'user_id',
210
+ },
211
+ isPublicClient: false,
212
+ });
213
+ codeGranter.authorize.mockResolvedValueOnce({
214
+ scopes: ['user'],
215
+ });
216
+
217
+ oAuth2AuthorizationRequests.check.mockResolvedValueOnce({
218
+ clientId: 'the_client_app_id',
219
+ parameters: {
220
+ response_type: 'code',
221
+ client_id: 'the_client_app_id',
222
+ redirect_uri: 'https://www.example.com/oauth2/cb',
223
+ scope: 'user',
224
+ state: 'bancal',
225
+ },
226
+ expiresAt: Date.parse('1983-04-28T00:00:00Z'),
227
+ });
228
+
229
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
230
+ OAUTH2,
231
+ OAUTH2_PAR: {
232
+ mode: 'enabled',
233
+ },
234
+ ERRORS_DESCRIPTORS,
235
+ oAuth2Granters,
236
+ readClientGrants,
237
+ oAuth2AuthorizationRequests,
238
+ log,
239
+ });
240
+ const response = await getOAuth2Authorize({
241
+ query: {
242
+ client_id: 'the_client_app_id',
243
+ request_uri: 'urn:ietf:params:oauth:request_uri:a_request_ui',
244
+ },
245
+ });
246
+
119
247
  expect({
248
+ response,
120
249
  logCalls: log.mock.calls.filter((args) => args[0].endsWith('stack')),
121
- codeGranterAuthorizerAuthorizeCalls:
122
- codeGranter.authorizer.authorize.mock.calls,
250
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
123
251
  codeGranterAcknowledgerAcknowledgeCalls:
124
- codeGranter.acknowledger.acknowledge.mock.calls,
252
+ codeGranter.acknowledge.mock.calls,
125
253
  codeGranterAuthenticatorAuthenticateCalls:
126
- codeGranter.authenticator.authenticate.mock.calls,
127
- tokenGranterAuthorizerAuthorizeCalls:
128
- tokenGranter.authorizer.authorize.mock.calls,
254
+ codeGranter.authenticate.mock.calls,
255
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
129
256
  tokenGranterAcknowledgerAcknowledgeCalls:
130
- tokenGranter.acknowledger.acknowledge.mock.calls,
131
- }).toMatchSnapshot();
257
+ tokenGranter.acknowledge.mock.calls,
258
+ readClientGrantsCalls: readClientGrants.mock.calls,
259
+ oAuth2AuthorizationRequestsCheckCalls:
260
+ oAuth2AuthorizationRequests.check.mock.calls,
261
+ }).toMatchInlineSnapshot(`
262
+ {
263
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
264
+ "codeGranterAuthenticatorAuthenticateCalls": [],
265
+ "codeGranterAuthorizerAuthorizeCalls": [
266
+ [
267
+ {
268
+ "clientGrants": {
269
+ "allowedGrantTypes": [
270
+ "authorization_code",
271
+ "refresh_token",
272
+ ],
273
+ "allowedRedirectURIS": [
274
+ "https://www.example.com/oauth2/cb",
275
+ ],
276
+ "allowedScopes": [
277
+ "user",
278
+ "admin",
279
+ ],
280
+ "authenticationData": {
281
+ "clientId": "the_client_app_id",
282
+ "scopes": [
283
+ "user",
284
+ "admin",
285
+ ],
286
+ "userId": "user_id",
287
+ },
288
+ "isPublicClient": false,
289
+ },
290
+ "clientId": "the_client_app_id",
291
+ "demandedScopes": [
292
+ "user",
293
+ ],
294
+ },
295
+ ],
296
+ ],
297
+ "logCalls": [],
298
+ "oAuth2AuthorizationRequestsCheckCalls": [
299
+ [
300
+ "the_client_app_id",
301
+ "urn:ietf:params:oauth:request_uri:a_request_ui",
302
+ ],
303
+ ],
304
+ "readClientGrantsCalls": [
305
+ [
306
+ "the_client_app_id",
307
+ ],
308
+ ],
309
+ "response": {
310
+ "headers": {
311
+ "location": "https://auth.example.com/sign_in?type=code&redirect_uri=https%3A%2F%2Fwww.example.com%2Foauth2%2Fcb&scope=user&client_id=the_client_app_id&state=bancal",
312
+ },
313
+ "status": 302,
314
+ },
315
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
316
+ "tokenGranterAuthorizerAuthorizeCalls": [],
317
+ }
318
+ `);
132
319
  });
133
320
 
134
- test('should redirect with an error when some', async () => {
321
+ test('should reject unknown pushed authorization request URIs', async () => {
135
322
  [
136
- codeGranter.authorizer.authorize,
137
- codeGranter.acknowledger.acknowledge,
138
- codeGranter.authenticator.authenticate,
139
- tokenGranter.authorizer.authorize,
140
- tokenGranter.acknowledger.acknowledge,
323
+ codeGranter.acknowledge,
324
+ codeGranter.authenticate,
325
+ tokenGranter.authorize,
326
+ tokenGranter.acknowledge,
141
327
  ].forEach((mock: any) =>
142
328
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
143
329
  );
330
+ readClientGrants.mockResolvedValue({
331
+ allowedScopes: ['user', 'admin'],
332
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
333
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
334
+ authenticationData: {
335
+ clientId: 'the_client_app_id',
336
+ scopes: ['user', 'admin'],
337
+ userId: 'user_id',
338
+ },
339
+ isPublicClient: false,
340
+ });
341
+ codeGranter.authorize.mockResolvedValueOnce({
342
+ scopes: ['user'],
343
+ });
344
+
345
+ oAuth2AuthorizationRequests.check.mockResolvedValueOnce(undefined);
144
346
 
145
347
  const getOAuth2Authorize = await initGetOAuth2Authorize({
146
348
  OAUTH2,
349
+ OAUTH2_PAR: {
350
+ mode: 'enabled',
351
+ },
147
352
  ERRORS_DESCRIPTORS,
148
353
  oAuth2Granters,
354
+ readClientGrants,
355
+ oAuth2AuthorizationRequests,
149
356
  log,
150
357
  });
358
+
359
+ const response = await getOAuth2Authorize({
360
+ query: {
361
+ client_id: 'the_client_app_id',
362
+ request_uri: 'urn:ietf:params:oauth:request_uri:a_request_ui',
363
+ },
364
+ });
365
+
366
+ expect({
367
+ response,
368
+ logCalls: log.mock.calls.filter((args) => !args[0].endsWith('stack')),
369
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
370
+ codeGranterAcknowledgerAcknowledgeCalls:
371
+ codeGranter.acknowledge.mock.calls,
372
+ codeGranterAuthenticatorAuthenticateCalls:
373
+ codeGranter.authenticate.mock.calls,
374
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
375
+ tokenGranterAcknowledgerAcknowledgeCalls:
376
+ tokenGranter.acknowledge.mock.calls,
377
+ readClientGrantsCalls: readClientGrants.mock.calls,
378
+ oAuth2AuthorizationRequestsCheckCalls:
379
+ oAuth2AuthorizationRequests.check.mock.calls,
380
+ }).toMatchInlineSnapshot(`
381
+ {
382
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
383
+ "codeGranterAuthenticatorAuthenticateCalls": [],
384
+ "codeGranterAuthorizerAuthorizeCalls": [],
385
+ "logCalls": [
386
+ [
387
+ "debug",
388
+ "👫 - OAuth2 authorize error.",
389
+ ],
390
+ ],
391
+ "oAuth2AuthorizationRequestsCheckCalls": [
392
+ [
393
+ "the_client_app_id",
394
+ "urn:ietf:params:oauth:request_uri:a_request_ui",
395
+ ],
396
+ ],
397
+ "readClientGrantsCalls": [
398
+ [
399
+ "the_client_app_id",
400
+ ],
401
+ ],
402
+ "response": {
403
+ "headers": {
404
+ "location": "https://www.example.com/oauth2/cb?error=invalid_request_uri&error_description=The+request+URI+is+invalid%2C+expired+or+unknown+%28urn%3Aietf%3Aparams%3Aoauth%3Arequest_uri%3Aa_request_ui%29.",
405
+ },
406
+ "status": 302,
407
+ },
408
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
409
+ "tokenGranterAuthorizerAuthorizeCalls": [],
410
+ }
411
+ `);
412
+ });
413
+
414
+ test('should reject pushed authorization request when disabled', async () => {
415
+ [
416
+ codeGranter.acknowledge,
417
+ codeGranter.authenticate,
418
+ tokenGranter.authorize,
419
+ tokenGranter.acknowledge,
420
+ ].forEach((mock: any) =>
421
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
422
+ );
423
+ readClientGrants.mockResolvedValue({
424
+ allowedScopes: ['user', 'admin'],
425
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
426
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
427
+ authenticationData: {
428
+ clientId: 'the_client_app_id',
429
+ scopes: ['user', 'admin'],
430
+ userId: 'user_id',
431
+ },
432
+ isPublicClient: false,
433
+ });
434
+ codeGranter.authorize.mockResolvedValueOnce({
435
+ scopes: ['user'],
436
+ });
437
+
438
+ oAuth2AuthorizationRequests.check.mockResolvedValueOnce(undefined);
439
+
440
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
441
+ OAUTH2,
442
+ OAUTH2_PAR: {
443
+ mode: 'disabled',
444
+ },
445
+ ERRORS_DESCRIPTORS,
446
+ oAuth2Granters,
447
+ readClientGrants,
448
+ oAuth2AuthorizationRequests,
449
+ log,
450
+ });
451
+
452
+ const response = await getOAuth2Authorize({
453
+ query: {
454
+ client_id: 'the_client_app_id',
455
+ request_uri: 'urn:ietf:params:oauth:request_uri:a_request_ui',
456
+ },
457
+ });
458
+
459
+ expect({
460
+ response,
461
+ logCalls: log.mock.calls.filter((args) => !args[0].endsWith('stack')),
462
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
463
+ codeGranterAcknowledgerAcknowledgeCalls:
464
+ codeGranter.acknowledge.mock.calls,
465
+ codeGranterAuthenticatorAuthenticateCalls:
466
+ codeGranter.authenticate.mock.calls,
467
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
468
+ tokenGranterAcknowledgerAcknowledgeCalls:
469
+ tokenGranter.acknowledge.mock.calls,
470
+ readClientGrantsCalls: readClientGrants.mock.calls,
471
+ oAuth2AuthorizationRequestsCheckCalls:
472
+ oAuth2AuthorizationRequests.check.mock.calls,
473
+ }).toMatchInlineSnapshot(`
474
+ {
475
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
476
+ "codeGranterAuthenticatorAuthenticateCalls": [],
477
+ "codeGranterAuthorizerAuthorizeCalls": [],
478
+ "logCalls": [
479
+ [
480
+ "debug",
481
+ "👫 - OAuth2 authorize error.",
482
+ ],
483
+ ],
484
+ "oAuth2AuthorizationRequestsCheckCalls": [],
485
+ "readClientGrantsCalls": [
486
+ [
487
+ "the_client_app_id",
488
+ ],
489
+ ],
490
+ "response": {
491
+ "headers": {
492
+ "location": "https://www.example.com/oauth2/cb?error=invalid_request&error_description=Pushed+authorization+requests+are+not+enabled+for+this+server.",
493
+ },
494
+ "status": 302,
495
+ },
496
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
497
+ "tokenGranterAuthorizerAuthorizeCalls": [],
498
+ }
499
+ `);
500
+ });
501
+
502
+ test('should redirect with an error for bad response type', async () => {
503
+ [
504
+ codeGranter.authorize,
505
+ codeGranter.acknowledge,
506
+ codeGranter.authenticate,
507
+ tokenGranter.authorize,
508
+ tokenGranter.acknowledge,
509
+ ].forEach((mock: any) =>
510
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
511
+ );
512
+
513
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
514
+ OAUTH2,
515
+ ERRORS_DESCRIPTORS,
516
+ oAuth2Granters,
517
+ readClientGrants,
518
+ oAuth2AuthorizationRequests,
519
+ log,
520
+ });
521
+
522
+ readClientGrants.mockResolvedValue({
523
+ allowedScopes: ['user', 'admin'],
524
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
525
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
526
+ authenticationData: {
527
+ clientId: 'the_client_app_id',
528
+ scopes: ['user', 'admin'],
529
+ userId: 'user_id',
530
+ },
531
+ isPublicClient: false,
532
+ });
533
+
151
534
  const response = await getOAuth2Authorize({
152
535
  query: {
153
536
  response_type: 'yolo',
154
- client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
537
+ client_id: 'the_client_app_id',
538
+ redirect_uri: 'https://www.example.com/oauth2/cb',
539
+ scope: 'user',
540
+ state: 'bancal',
541
+ },
542
+ });
543
+
544
+ expect({
545
+ response,
546
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
547
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
548
+ codeGranterAcknowledgerAcknowledgeCalls:
549
+ codeGranter.acknowledge.mock.calls,
550
+ codeGranterAuthenticatorAuthenticateCalls:
551
+ codeGranter.authenticate.mock.calls,
552
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
553
+ tokenGranterAcknowledgerAcknowledgeCalls:
554
+ tokenGranter.acknowledge.mock.calls,
555
+ readClientGrantsCalls: readClientGrants.mock.calls,
556
+ oAuth2AuthorizationRequestsCheckCalls:
557
+ oAuth2AuthorizationRequests.check.mock.calls,
558
+ }).toMatchInlineSnapshot(`
559
+ {
560
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
561
+ "codeGranterAuthenticatorAuthenticateCalls": [],
562
+ "codeGranterAuthorizerAuthorizeCalls": [],
563
+ "logCalls": [
564
+ [
565
+ "debug",
566
+ "👫 - OAuth2 authorize error.",
567
+ ],
568
+ ],
569
+ "oAuth2AuthorizationRequestsCheckCalls": [],
570
+ "readClientGrantsCalls": [
571
+ [
572
+ "the_client_app_id",
573
+ ],
574
+ ],
575
+ "response": {
576
+ "headers": {
577
+ "location": "https://www.example.com/oauth2/cb?error=unsupported_response_type&error_description=The+response+type+%22yolo%22+is+not+supported.&state=bancal",
578
+ },
579
+ "status": 302,
580
+ },
581
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
582
+ "tokenGranterAuthorizerAuthorizeCalls": [],
583
+ }
584
+ `);
585
+ });
586
+
587
+ test('should redirect with an error with bad redirect uri', async () => {
588
+ [
589
+ codeGranter.authorize,
590
+ codeGranter.acknowledge,
591
+ codeGranter.authenticate,
592
+ tokenGranter.authorize,
593
+ tokenGranter.acknowledge,
594
+ ].forEach((mock: any) =>
595
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
596
+ );
597
+
598
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
599
+ OAUTH2,
600
+ ERRORS_DESCRIPTORS,
601
+ oAuth2Granters,
602
+ readClientGrants,
603
+ oAuth2AuthorizationRequests,
604
+ log,
605
+ });
606
+
607
+ readClientGrants.mockResolvedValue({
608
+ allowedScopes: ['user', 'admin'],
609
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
610
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
611
+ authenticationData: {
612
+ clientId: 'the_client_app_id',
613
+ scopes: ['user', 'admin'],
614
+ userId: 'user_id',
615
+ },
616
+ isPublicClient: false,
617
+ });
618
+
619
+ const response = await getOAuth2Authorize({
620
+ query: {
621
+ response_type: 'code',
622
+ client_id: 'the_client_app_id',
155
623
  redirect_uri: 'https://www.example.com',
156
624
  scope: 'user',
157
625
  state: 'bancal',
158
626
  },
159
627
  });
160
628
 
161
- expect(response).toMatchInlineSnapshot(`
162
- {
163
- "headers": {
164
- "location": "https://auth.example.com/sign_in?redirect_uri=https%3A%2F%2Fwww.example.com&error=unsupported_response_type&error_description=The+type+%22yolo%22+is+not+supported.&state=bancal",
165
- },
166
- "status": 302,
167
- }
168
- `);
169
629
  expect({
630
+ response,
631
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
632
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
633
+ codeGranterAcknowledgerAcknowledgeCalls:
634
+ codeGranter.acknowledge.mock.calls,
635
+ codeGranterAuthenticatorAuthenticateCalls:
636
+ codeGranter.authenticate.mock.calls,
637
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
638
+ tokenGranterAcknowledgerAcknowledgeCalls:
639
+ tokenGranter.acknowledge.mock.calls,
640
+ readClientGrantsCalls: readClientGrants.mock.calls,
641
+ oAuth2AuthorizationRequestsCheckCalls:
642
+ oAuth2AuthorizationRequests.check.mock.calls,
643
+ }).toMatchInlineSnapshot(`
644
+ {
645
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
646
+ "codeGranterAuthenticatorAuthenticateCalls": [],
647
+ "codeGranterAuthorizerAuthorizeCalls": [],
648
+ "logCalls": [
649
+ [
650
+ "debug",
651
+ "👫 - OAuth2 authorize error.",
652
+ ],
653
+ ],
654
+ "oAuth2AuthorizationRequestsCheckCalls": [],
655
+ "readClientGrantsCalls": [
656
+ [
657
+ "the_client_app_id",
658
+ ],
659
+ ],
660
+ "response": {
661
+ "headers": {
662
+ "location": "https://www.example.com/oauth2/cb?error=invalid_request&error_description=The+client+does+not+accept+that+redirect+URI.&state=bancal",
663
+ },
664
+ "status": 302,
665
+ },
666
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
667
+ "tokenGranterAuthorizerAuthorizeCalls": [],
668
+ }
669
+ `);
670
+ });
671
+
672
+ test('should redirect with an error with bad scope and strict scopes', async () => {
673
+ [
674
+ codeGranter.authorize,
675
+ codeGranter.acknowledge,
676
+ codeGranter.authenticate,
677
+ tokenGranter.acknowledge,
678
+ ].forEach((mock: any) =>
679
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
680
+ );
681
+
682
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
683
+ OAUTH2: {
684
+ ...OAUTH2,
685
+ strictScopesChecks: true,
686
+ },
687
+ ERRORS_DESCRIPTORS,
688
+ oAuth2Granters,
689
+ readClientGrants,
690
+ oAuth2AuthorizationRequests,
691
+ log,
692
+ });
693
+
694
+ tokenGranter.authorize.mockResolvedValueOnce({ scopes: ['user'] });
695
+ readClientGrants.mockResolvedValue({
696
+ allowedScopes: ['user'],
697
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
698
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
699
+ authenticationData: {
700
+ clientId: 'the_client_app_id',
701
+ scopes: ['user'],
702
+ userId: 'user_id',
703
+ },
704
+ isPublicClient: false,
705
+ });
706
+
707
+ const response = await getOAuth2Authorize({
708
+ query: {
709
+ response_type: 'token',
710
+ client_id: 'the_client_app_id',
711
+ redirect_uri: 'https://www.example.com/oauth2/cb',
712
+ scope: 'admin',
713
+ state: 'bancal',
714
+ },
715
+ });
716
+
717
+ expect({
718
+ response,
719
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
720
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
721
+ codeGranterAcknowledgerAcknowledgeCalls:
722
+ codeGranter.acknowledge.mock.calls,
723
+ codeGranterAuthenticatorAuthenticateCalls:
724
+ codeGranter.authenticate.mock.calls,
725
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
726
+ tokenGranterAcknowledgerAcknowledgeCalls:
727
+ tokenGranter.acknowledge.mock.calls,
728
+ readClientGrantsCalls: readClientGrants.mock.calls,
729
+ oAuth2AuthorizationRequestsCheckCalls:
730
+ oAuth2AuthorizationRequests.check.mock.calls,
731
+ }).toMatchInlineSnapshot(`
732
+ {
733
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
734
+ "codeGranterAuthenticatorAuthenticateCalls": [],
735
+ "codeGranterAuthorizerAuthorizeCalls": [],
736
+ "logCalls": [
737
+ [
738
+ "debug",
739
+ "👫 - OAuth2 authorize error.",
740
+ ],
741
+ ],
742
+ "oAuth2AuthorizationRequestsCheckCalls": [],
743
+ "readClientGrantsCalls": [
744
+ [
745
+ "the_client_app_id",
746
+ ],
747
+ ],
748
+ "response": {
749
+ "headers": {
750
+ "location": "https://www.example.com/oauth2/cb#error=invalid_scope&error_description=This+scope+is+not+supported+%28admin%29.&state=bancal",
751
+ },
752
+ "status": 302,
753
+ },
754
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
755
+ "tokenGranterAuthorizerAuthorizeCalls": [],
756
+ }
757
+ `);
758
+ });
759
+
760
+ test('should redirect with an error with bad client', async () => {
761
+ [
762
+ codeGranter.authorize,
763
+ codeGranter.acknowledge,
764
+ codeGranter.authenticate,
765
+ tokenGranter.authorize,
766
+ tokenGranter.acknowledge,
767
+ ].forEach((mock: any) =>
768
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
769
+ );
770
+
771
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
772
+ OAUTH2,
773
+ ERRORS_DESCRIPTORS,
774
+ oAuth2Granters,
775
+ readClientGrants,
776
+ oAuth2AuthorizationRequests,
777
+ log,
778
+ });
779
+
780
+ readClientGrants.mockRejectedValueOnce(
781
+ new YError('E_OAUTH2_CLIENT_NOT_FOUND', ['the_client_app_id']),
782
+ );
783
+
784
+ const response = await getOAuth2Authorize({
785
+ query: {
786
+ response_type: 'yolo',
787
+ client_id: 'the_client_app_id',
788
+ redirect_uri: 'https://www.example.com/oauth2/cb',
789
+ scope: 'user',
790
+ state: 'bancal',
791
+ },
792
+ });
793
+
794
+ expect({
795
+ response,
170
796
  logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
171
- codeGranterAuthorizerAuthorizeCalls:
172
- codeGranter.authorizer.authorize.mock.calls,
797
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
173
798
  codeGranterAcknowledgerAcknowledgeCalls:
174
- codeGranter.acknowledger.acknowledge.mock.calls,
799
+ codeGranter.acknowledge.mock.calls,
175
800
  codeGranterAuthenticatorAuthenticateCalls:
176
- codeGranter.authenticator.authenticate.mock.calls,
177
- tokenGranterAuthorizerAuthorizeCalls:
178
- tokenGranter.authorizer.authorize.mock.calls,
801
+ codeGranter.authenticate.mock.calls,
802
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
179
803
  tokenGranterAcknowledgerAcknowledgeCalls:
180
- tokenGranter.acknowledger.acknowledge.mock.calls,
181
- }).toMatchSnapshot();
804
+ tokenGranter.acknowledge.mock.calls,
805
+ readClientGrantsCalls: readClientGrants.mock.calls,
806
+ oAuth2AuthorizationRequestsCheckCalls:
807
+ oAuth2AuthorizationRequests.check.mock.calls,
808
+ }).toMatchInlineSnapshot(`
809
+ {
810
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
811
+ "codeGranterAuthenticatorAuthenticateCalls": [],
812
+ "codeGranterAuthorizerAuthorizeCalls": [],
813
+ "logCalls": [
814
+ [
815
+ "debug",
816
+ "👫 - OAuth2 authorize error.",
817
+ ],
818
+ ],
819
+ "oAuth2AuthorizationRequestsCheckCalls": [],
820
+ "readClientGrantsCalls": [
821
+ [
822
+ "the_client_app_id",
823
+ ],
824
+ ],
825
+ "response": {
826
+ "headers": {
827
+ "location": "https://auth.example.com/sign_in?error=invalid_client&error_description=The+client+provided+does+not+exist+%28the_client_app_id%29.",
828
+ },
829
+ "status": 302,
830
+ },
831
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
832
+ "tokenGranterAuthorizerAuthorizeCalls": [],
833
+ }
834
+ `);
835
+ });
836
+
837
+ test('should redirect with an error with bad request uri', async () => {
838
+ [
839
+ codeGranter.authorize,
840
+ codeGranter.acknowledge,
841
+ codeGranter.authenticate,
842
+ tokenGranter.authorize,
843
+ tokenGranter.acknowledge,
844
+ ].forEach((mock: any) =>
845
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
846
+ );
847
+
848
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
849
+ OAUTH2,
850
+ OAUTH2_PAR: {
851
+ mode: 'enabled',
852
+ },
853
+ ERRORS_DESCRIPTORS,
854
+ oAuth2Granters,
855
+ readClientGrants,
856
+ oAuth2AuthorizationRequests,
857
+ log,
858
+ });
859
+
860
+ readClientGrants.mockResolvedValue({
861
+ allowedScopes: ['user', 'admin'],
862
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
863
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
864
+ authenticationData: {
865
+ clientId: 'the_client_app_id',
866
+ scopes: ['user', 'admin'],
867
+ userId: 'user_id',
868
+ },
869
+ isPublicClient: false,
870
+ });
871
+
872
+ const response = await getOAuth2Authorize({
873
+ query: {
874
+ client_id: 'the_client_app_id',
875
+ request_uri: 'not_a_request_uri',
876
+ },
877
+ });
878
+
879
+ expect({
880
+ response,
881
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
882
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
883
+ codeGranterAcknowledgerAcknowledgeCalls:
884
+ codeGranter.acknowledge.mock.calls,
885
+ codeGranterAuthenticatorAuthenticateCalls:
886
+ codeGranter.authenticate.mock.calls,
887
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
888
+ tokenGranterAcknowledgerAcknowledgeCalls:
889
+ tokenGranter.acknowledge.mock.calls,
890
+ readClientGrantsCalls: readClientGrants.mock.calls,
891
+ oAuth2AuthorizationRequestsCheckCalls:
892
+ oAuth2AuthorizationRequests.check.mock.calls,
893
+ }).toMatchInlineSnapshot(`
894
+ {
895
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
896
+ "codeGranterAuthenticatorAuthenticateCalls": [],
897
+ "codeGranterAuthorizerAuthorizeCalls": [],
898
+ "logCalls": [
899
+ [
900
+ "debug",
901
+ "👫 - OAuth2 authorize error.",
902
+ ],
903
+ ],
904
+ "oAuth2AuthorizationRequestsCheckCalls": [],
905
+ "readClientGrantsCalls": [
906
+ [
907
+ "the_client_app_id",
908
+ ],
909
+ ],
910
+ "response": {
911
+ "headers": {
912
+ "location": "https://www.example.com/oauth2/cb?error=invalid_request_uri&error_description=The+request+URI+is+invalid%2C+expired+or+unknown+%28not_a_request_uri%29.",
913
+ },
914
+ "status": 302,
915
+ },
916
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
917
+ "tokenGranterAuthorizerAuthorizeCalls": [],
918
+ }
919
+ `);
920
+ });
921
+
922
+ test('should redirect with an error with bad request', async () => {
923
+ [
924
+ codeGranter.authorize,
925
+ codeGranter.acknowledge,
926
+ codeGranter.authenticate,
927
+ tokenGranter.authorize,
928
+ tokenGranter.acknowledge,
929
+ ].forEach((mock: any) =>
930
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
931
+ );
932
+
933
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
934
+ OAUTH2,
935
+ OAUTH2_PAR: {
936
+ mode: 'enabled',
937
+ },
938
+ ERRORS_DESCRIPTORS,
939
+ oAuth2Granters,
940
+ readClientGrants,
941
+ oAuth2AuthorizationRequests,
942
+ log,
943
+ });
944
+
945
+ readClientGrants.mockResolvedValue({
946
+ allowedScopes: ['user', 'admin'],
947
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
948
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
949
+ authenticationData: {
950
+ clientId: 'the_client_app_id',
951
+ scopes: ['user', 'admin'],
952
+ userId: 'user_id',
953
+ },
954
+ isPublicClient: false,
955
+ });
956
+
957
+ const response = await getOAuth2Authorize({
958
+ query: {
959
+ client_id: 'the_client_app_id',
960
+ request_uri: 'urn:ietf:params:oauth:request_uri:a_request_ui',
961
+ scope: 'user',
962
+ redirect_uri: 'https://www.example.com/oauth2/cb',
963
+ },
964
+ });
965
+
966
+ expect({
967
+ response,
968
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
969
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
970
+ codeGranterAcknowledgerAcknowledgeCalls:
971
+ codeGranter.acknowledge.mock.calls,
972
+ codeGranterAuthenticatorAuthenticateCalls:
973
+ codeGranter.authenticate.mock.calls,
974
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
975
+ tokenGranterAcknowledgerAcknowledgeCalls:
976
+ tokenGranter.acknowledge.mock.calls,
977
+ readClientGrantsCalls: readClientGrants.mock.calls,
978
+ oAuth2AuthorizationRequestsCheckCalls:
979
+ oAuth2AuthorizationRequests.check.mock.calls,
980
+ }).toMatchInlineSnapshot(`
981
+ {
982
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
983
+ "codeGranterAuthenticatorAuthenticateCalls": [],
984
+ "codeGranterAuthorizerAuthorizeCalls": [],
985
+ "logCalls": [
986
+ [
987
+ "debug",
988
+ "👫 - OAuth2 authorize error.",
989
+ ],
990
+ ],
991
+ "oAuth2AuthorizationRequestsCheckCalls": [],
992
+ "readClientGrantsCalls": [
993
+ [
994
+ "the_client_app_id",
995
+ ],
996
+ ],
997
+ "response": {
998
+ "headers": {
999
+ "location": "https://www.example.com/oauth2/cb?error=invalid_request&error_description=The+request+URI+should+not+have+additional+parameters+%28scope%29.",
1000
+ },
1001
+ "status": 302,
1002
+ },
1003
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
1004
+ "tokenGranterAuthorizerAuthorizeCalls": [],
1005
+ }
1006
+ `);
1007
+ });
1008
+
1009
+ test('should redirect with an error with bad scopes', async () => {
1010
+ [
1011
+ codeGranter.authorize,
1012
+ codeGranter.acknowledge,
1013
+ codeGranter.authenticate,
1014
+ tokenGranter.authorize,
1015
+ tokenGranter.acknowledge,
1016
+ ].forEach((mock: any) =>
1017
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
1018
+ );
1019
+
1020
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
1021
+ OAUTH2: {
1022
+ ...OAUTH2,
1023
+ strictScopesChecks: true,
1024
+ },
1025
+ ERRORS_DESCRIPTORS,
1026
+ oAuth2Granters,
1027
+ readClientGrants,
1028
+ oAuth2AuthorizationRequests,
1029
+ log,
1030
+ });
1031
+
1032
+ readClientGrants.mockResolvedValue({
1033
+ allowedScopes: ['user', 'admin'],
1034
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
1035
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
1036
+ authenticationData: {
1037
+ clientId: 'the_client_app_id',
1038
+ scopes: ['user', 'admin'],
1039
+ userId: 'user_id',
1040
+ },
1041
+ isPublicClient: false,
1042
+ });
1043
+
1044
+ const response = await getOAuth2Authorize({
1045
+ query: {
1046
+ response_type: 'code',
1047
+ client_id: 'the_client_app_id',
1048
+ redirect_uri: 'https://www.example.com/oauth2/cb',
1049
+ scope: 'god',
1050
+ state: 'bancal',
1051
+ },
1052
+ });
1053
+
1054
+ expect({
1055
+ response,
1056
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
1057
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
1058
+ codeGranterAcknowledgerAcknowledgeCalls:
1059
+ codeGranter.acknowledge.mock.calls,
1060
+ codeGranterAuthenticatorAuthenticateCalls:
1061
+ codeGranter.authenticate.mock.calls,
1062
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
1063
+ tokenGranterAcknowledgerAcknowledgeCalls:
1064
+ tokenGranter.acknowledge.mock.calls,
1065
+ readClientGrantsCalls: readClientGrants.mock.calls,
1066
+ oAuth2AuthorizationRequestsCheckCalls:
1067
+ oAuth2AuthorizationRequests.check.mock.calls,
1068
+ }).toMatchInlineSnapshot(`
1069
+ {
1070
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
1071
+ "codeGranterAuthenticatorAuthenticateCalls": [],
1072
+ "codeGranterAuthorizerAuthorizeCalls": [],
1073
+ "logCalls": [
1074
+ [
1075
+ "debug",
1076
+ "👫 - OAuth2 authorize error.",
1077
+ ],
1078
+ ],
1079
+ "oAuth2AuthorizationRequestsCheckCalls": [],
1080
+ "readClientGrantsCalls": [
1081
+ [
1082
+ "the_client_app_id",
1083
+ ],
1084
+ ],
1085
+ "response": {
1086
+ "headers": {
1087
+ "location": "https://www.example.com/oauth2/cb?error=invalid_scope&error_description=This+scope+is+not+supported+%28god%29.&state=bancal",
1088
+ },
1089
+ "status": 302,
1090
+ },
1091
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
1092
+ "tokenGranterAuthorizerAuthorizeCalls": [],
1093
+ }
1094
+ `);
1095
+ });
1096
+ test('should redirect with an error with no PAR when required', async () => {
1097
+ [
1098
+ codeGranter.authorize,
1099
+ codeGranter.acknowledge,
1100
+ codeGranter.authenticate,
1101
+ tokenGranter.authorize,
1102
+ tokenGranter.acknowledge,
1103
+ ].forEach((mock: any) =>
1104
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
1105
+ );
1106
+
1107
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
1108
+ OAUTH2,
1109
+ OAUTH2_PAR: {
1110
+ mode: 'required',
1111
+ },
1112
+ ERRORS_DESCRIPTORS,
1113
+ oAuth2Granters,
1114
+ readClientGrants,
1115
+ oAuth2AuthorizationRequests,
1116
+ log,
1117
+ });
1118
+
1119
+ readClientGrants.mockResolvedValue({
1120
+ allowedScopes: ['user', 'admin'],
1121
+ allowedRedirectURIS: ['https://www.example.com/oauth2/cb'],
1122
+ allowedGrantTypes: ['authorization_code', 'refresh_token'],
1123
+ authenticationData: {
1124
+ clientId: 'the_client_app_id',
1125
+ scopes: ['user', 'admin'],
1126
+ userId: 'user_id',
1127
+ },
1128
+ isPublicClient: false,
1129
+ });
1130
+
1131
+ const response = await getOAuth2Authorize({
1132
+ query: {
1133
+ response_type: 'code',
1134
+ client_id: 'the_client_app_id',
1135
+ redirect_uri: 'https://www.example.com/oauth2/cb',
1136
+ scope: 'god',
1137
+ state: 'bancal',
1138
+ },
1139
+ });
1140
+
1141
+ expect({
1142
+ response,
1143
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
1144
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
1145
+ codeGranterAcknowledgerAcknowledgeCalls:
1146
+ codeGranter.acknowledge.mock.calls,
1147
+ codeGranterAuthenticatorAuthenticateCalls:
1148
+ codeGranter.authenticate.mock.calls,
1149
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
1150
+ tokenGranterAcknowledgerAcknowledgeCalls:
1151
+ tokenGranter.acknowledge.mock.calls,
1152
+ readClientGrantsCalls: readClientGrants.mock.calls,
1153
+ oAuth2AuthorizationRequestsCheckCalls:
1154
+ oAuth2AuthorizationRequests.check.mock.calls,
1155
+ }).toMatchInlineSnapshot(`
1156
+ {
1157
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
1158
+ "codeGranterAuthenticatorAuthenticateCalls": [],
1159
+ "codeGranterAuthorizerAuthorizeCalls": [],
1160
+ "logCalls": [
1161
+ [
1162
+ "debug",
1163
+ "👫 - OAuth2 authorize error.",
1164
+ ],
1165
+ ],
1166
+ "oAuth2AuthorizationRequestsCheckCalls": [],
1167
+ "readClientGrantsCalls": [
1168
+ [
1169
+ "the_client_app_id",
1170
+ ],
1171
+ ],
1172
+ "response": {
1173
+ "headers": {
1174
+ "location": "https://www.example.com/oauth2/cb?error=invalid_request&error_description=Pushed+authorization+requests+are+required+for+this+server.",
1175
+ },
1176
+ "status": 302,
1177
+ },
1178
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
1179
+ "tokenGranterAuthorizerAuthorizeCalls": [],
1180
+ }
1181
+ `);
182
1182
  });
183
1183
  });