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