@whook/oauth2 8.5.1 → 10.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +106 -70
- package/dist/handlers/getOAuth2Authorize.d.ts +8 -6
- package/dist/handlers/getOAuth2Authorize.js +122 -165
- package/dist/handlers/getOAuth2Authorize.js.map +1 -1
- package/dist/handlers/getOAuth2Authorize.test.js +117 -103
- package/dist/handlers/getOAuth2Authorize.test.js.map +1 -1
- package/dist/handlers/postAuthLogin.d.ts +3 -3
- package/dist/handlers/postAuthLogin.js +83 -120
- package/dist/handlers/postAuthLogin.js.map +1 -1
- package/dist/handlers/postAuthLogin.test.js +42 -44
- package/dist/handlers/postAuthLogin.test.js.map +1 -1
- package/dist/handlers/postAuthLogout.d.ts +1 -1
- package/dist/handlers/postAuthLogout.js +26 -42
- package/dist/handlers/postAuthLogout.js.map +1 -1
- package/dist/handlers/postAuthLogout.test.js +19 -21
- package/dist/handlers/postAuthLogout.test.js.map +1 -1
- package/dist/handlers/postAuthRefresh.d.ts +4 -4
- package/dist/handlers/postAuthRefresh.js +93 -131
- package/dist/handlers/postAuthRefresh.js.map +1 -1
- package/dist/handlers/postAuthRefresh.test.js +47 -49
- package/dist/handlers/postAuthRefresh.test.js.map +1 -1
- package/dist/handlers/postOAuth2Acknowledge.d.ts +11 -8
- package/dist/handlers/postOAuth2Acknowledge.js +119 -151
- package/dist/handlers/postOAuth2Acknowledge.js.map +1 -1
- package/dist/handlers/postOAuth2Acknowledge.test.js +139 -125
- package/dist/handlers/postOAuth2Acknowledge.test.js.map +1 -1
- package/dist/handlers/postOAuth2Token.d.ts +4 -4
- package/dist/handlers/postOAuth2Token.js +200 -232
- package/dist/handlers/postOAuth2Token.js.map +1 -1
- package/dist/handlers/postOAuth2Token.test.js +156 -153
- package/dist/handlers/postOAuth2Token.test.js.map +1 -1
- package/dist/index.d.ts +16 -16
- package/dist/index.js +14 -229
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +697 -620
- package/dist/index.test.js.map +1 -1
- package/dist/services/authCookies.d.ts +8 -4
- package/dist/services/authCookies.js +40 -71
- package/dist/services/authCookies.js.map +1 -1
- package/dist/services/authCookies.test.js +57 -62
- package/dist/services/authCookies.test.js.map +1 -1
- package/dist/services/oAuth2ClientCredentialsGranter.d.ts +2 -2
- package/dist/services/oAuth2ClientCredentialsGranter.js +34 -52
- package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
- package/dist/services/oAuth2CodeGranter.d.ts +1 -1
- package/dist/services/oAuth2CodeGranter.js +66 -109
- package/dist/services/oAuth2CodeGranter.js.map +1 -1
- package/dist/services/oAuth2CodeGranter.test.js +65 -64
- package/dist/services/oAuth2CodeGranter.test.js.map +1 -1
- package/dist/services/oAuth2Granters.d.ts +22 -19
- package/dist/services/oAuth2Granters.js +131 -138
- package/dist/services/oAuth2Granters.js.map +1 -1
- package/dist/services/oAuth2PasswordGranter.d.ts +1 -1
- package/dist/services/oAuth2PasswordGranter.js +27 -52
- package/dist/services/oAuth2PasswordGranter.js.map +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.d.ts +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.js +34 -58
- package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
- package/dist/services/oAuth2TokenGranter.d.ts +1 -1
- package/dist/services/oAuth2TokenGranter.js +50 -86
- package/dist/services/oAuth2TokenGranter.js.map +1 -1
- package/package.json +54 -96
- package/src/__snapshots__/index.test.ts.snap +23 -22
- package/src/handlers/__snapshots__/postOAuth2Acknowledge.test.ts.snap +1 -1
- package/src/handlers/getOAuth2Authorize.test.ts +49 -13
- package/src/handlers/getOAuth2Authorize.ts +17 -16
- package/src/handlers/postAuthLogin.test.ts +9 -5
- package/src/handlers/postAuthLogin.ts +8 -7
- package/src/handlers/postAuthLogout.test.ts +8 -4
- package/src/handlers/postAuthLogout.ts +2 -2
- package/src/handlers/postAuthRefresh.test.ts +10 -6
- package/src/handlers/postAuthRefresh.ts +8 -7
- package/src/handlers/postOAuth2Acknowledge.test.ts +83 -35
- package/src/handlers/postOAuth2Acknowledge.ts +48 -30
- package/src/handlers/postOAuth2Token.test.ts +79 -19
- package/src/handlers/postOAuth2Token.ts +7 -7
- package/src/index.test.ts +116 -72
- package/src/index.ts +21 -19
- package/src/services/authCookies.test.ts +2 -2
- package/src/services/authCookies.ts +10 -5
- package/src/services/oAuth2ClientCredentialsGranter.ts +28 -24
- package/src/services/oAuth2CodeGranter.test.ts +42 -18
- package/src/services/oAuth2CodeGranter.ts +68 -62
- package/src/services/oAuth2Granters.ts +59 -29
- package/src/services/oAuth2PasswordGranter.ts +25 -24
- package/src/services/oAuth2RefreshTokenGranter.ts +30 -26
- package/src/services/oAuth2TokenGranter.ts +45 -43
- package/dist/handlers/getOAuth2Authorize.mjs +0 -160
- package/dist/handlers/getOAuth2Authorize.mjs.map +0 -1
- package/dist/handlers/getOAuth2Authorize.test.mjs +0 -114
- package/dist/handlers/getOAuth2Authorize.test.mjs.map +0 -1
- package/dist/handlers/postAuthLogin.mjs +0 -108
- package/dist/handlers/postAuthLogin.mjs.map +0 -1
- package/dist/handlers/postAuthLogin.test.mjs +0 -85
- package/dist/handlers/postAuthLogin.test.mjs.map +0 -1
- package/dist/handlers/postAuthLogout.mjs +0 -33
- package/dist/handlers/postAuthLogout.mjs.map +0 -1
- package/dist/handlers/postAuthLogout.test.mjs +0 -30
- package/dist/handlers/postAuthLogout.test.mjs.map +0 -1
- package/dist/handlers/postAuthRefresh.mjs +0 -121
- package/dist/handlers/postAuthRefresh.mjs.map +0 -1
- package/dist/handlers/postAuthRefresh.test.mjs +0 -94
- package/dist/handlers/postAuthRefresh.test.mjs.map +0 -1
- package/dist/handlers/postOAuth2Acknowledge.mjs +0 -150
- package/dist/handlers/postOAuth2Acknowledge.mjs.map +0 -1
- package/dist/handlers/postOAuth2Acknowledge.test.mjs +0 -140
- package/dist/handlers/postOAuth2Acknowledge.test.mjs.map +0 -1
- package/dist/handlers/postOAuth2Token.mjs +0 -231
- package/dist/handlers/postOAuth2Token.mjs.map +0 -1
- package/dist/handlers/postOAuth2Token.test.mjs +0 -160
- package/dist/handlers/postOAuth2Token.test.mjs.map +0 -1
- package/dist/index.mjs +0 -15
- package/dist/index.mjs.map +0 -1
- package/dist/index.test.mjs +0 -748
- package/dist/index.test.mjs.map +0 -1
- package/dist/services/authCookies.mjs +0 -58
- package/dist/services/authCookies.mjs.map +0 -1
- package/dist/services/authCookies.test.mjs +0 -77
- package/dist/services/authCookies.test.mjs.map +0 -1
- package/dist/services/oAuth2ClientCredentialsGranter.mjs +0 -41
- package/dist/services/oAuth2ClientCredentialsGranter.mjs.map +0 -1
- package/dist/services/oAuth2CodeGranter.mjs +0 -98
- package/dist/services/oAuth2CodeGranter.mjs.map +0 -1
- package/dist/services/oAuth2CodeGranter.test.mjs +0 -81
- package/dist/services/oAuth2CodeGranter.test.mjs.map +0 -1
- package/dist/services/oAuth2Granters.mjs +0 -127
- package/dist/services/oAuth2Granters.mjs.map +0 -1
- package/dist/services/oAuth2PasswordGranter.mjs +0 -40
- package/dist/services/oAuth2PasswordGranter.mjs.map +0 -1
- package/dist/services/oAuth2RefreshTokenGranter.mjs +0 -47
- package/dist/services/oAuth2RefreshTokenGranter.mjs.map +0 -1
- package/dist/services/oAuth2TokenGranter.mjs +0 -78
- package/dist/services/oAuth2TokenGranter.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
> OAuth2 implementation for Whook servers
|
|
9
9
|
|
|
10
10
|
[](https://github.com/nfroidure/whook/blob/master/packages/whook-oauth2/LICENSE)
|
|
11
|
-
[](https://npmjs.org/package/@whook/oauth2)
|
|
12
11
|
|
|
13
12
|
|
|
14
13
|
[//]: # (::contents:start)
|
|
@@ -20,18 +19,21 @@ authenticate and allow client applications to act on behalf of them.
|
|
|
20
19
|
|
|
21
20
|

|
|
22
21
|
|
|
23
|
-
The module provides
|
|
22
|
+
The module provides:
|
|
24
23
|
|
|
25
|
-
-
|
|
26
|
-
`postOAuth2Tokentoken`)
|
|
27
|
-
|
|
28
|
-
-
|
|
29
|
-
|
|
24
|
+
- 2 OAuth2 handlers implementing the 2 OAuth2 standard endpoints
|
|
25
|
+
(`getOAuth2authorize`, `postOAuth2Tokentoken`) to be used by OAuth2 client
|
|
26
|
+
applications,
|
|
27
|
+
- 4 authentication endpoints to be used by the authorization server directly to
|
|
28
|
+
authenticate users (`postAuthLogin`, `postAuthRefresh`, `postAuthLogout`) and
|
|
29
|
+
acknowledge the client requests (`postOAuth2Acknowledge`),
|
|
30
|
+
- 5 OAuth2 services implementing the standard grant types. You can create your
|
|
31
|
+
own granter services to create additional grant types.
|
|
30
32
|
|
|
31
33
|
This module requires you to implement some services it relies on:
|
|
32
34
|
|
|
33
35
|
- `oAuth2AccessToken` that generates and checks the `access_token` and the
|
|
34
|
-
`oAuth2RefreshToken` for the `refresh_token
|
|
36
|
+
`oAuth2RefreshToken` for the `refresh_token`, both have the same interface,
|
|
35
37
|
- `checkApplication` service that is supposed to check whether an application
|
|
36
38
|
can be used or not for a given grant type, a given scope and redirect URI,
|
|
37
39
|
- `oAuth2PasswordService` aimed to check the `password` grant type with your own
|
|
@@ -49,7 +51,7 @@ Install the module in your project:
|
|
|
49
51
|
npm i @whook/oauth2
|
|
50
52
|
```
|
|
51
53
|
|
|
52
|
-
Declare the plugin into your `index.ts` file:
|
|
54
|
+
Declare the plugin into your `src/index.ts` file:
|
|
53
55
|
|
|
54
56
|
```diff
|
|
55
57
|
// (...)
|
|
@@ -61,6 +63,35 @@ Declare the plugin into your `index.ts` file:
|
|
|
61
63
|
// (...)
|
|
62
64
|
```
|
|
63
65
|
|
|
66
|
+
Declare this module types in your `src/whook.d.ts` type definitions:
|
|
67
|
+
|
|
68
|
+
```diff
|
|
69
|
+
+import type {
|
|
70
|
+
+ AuthCookiesEnv,
|
|
71
|
+
+ OAuth2Config,
|
|
72
|
+
+} from '@whook/oauth2';
|
|
73
|
+
|
|
74
|
+
declare module '@whook/whook' {
|
|
75
|
+
|
|
76
|
+
export interface WhookEnv
|
|
77
|
+
extends WhookBaseEnv,
|
|
78
|
+
// (...)
|
|
79
|
+
+ AuthCookiesEnv,
|
|
80
|
+
WhookSwaggerUIEnv {}
|
|
81
|
+
|
|
82
|
+
// ...
|
|
83
|
+
|
|
84
|
+
export interface WhookConfigs
|
|
85
|
+
- extends WhookBaseConfigs {}
|
|
86
|
+
+ extends WhookBaseConfigs,
|
|
87
|
+
+ AuthCookiesConfig,
|
|
88
|
+
+ OAuth2Config {}
|
|
89
|
+
|
|
90
|
+
// ...
|
|
91
|
+
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
64
95
|
Add the OAuth2 configuration to your config files:
|
|
65
96
|
|
|
66
97
|
```diff
|
|
@@ -68,14 +99,11 @@ Add the OAuth2 configuration to your config files:
|
|
|
68
99
|
+ OAUTH2_ERRORS_DESCRIPTORS,
|
|
69
100
|
+ OAuth2Config,
|
|
70
101
|
+ } from '@whook/oauth2';
|
|
102
|
+
import type { WhookConfigs } from '@whook/whook';
|
|
71
103
|
|
|
72
104
|
// ...
|
|
73
105
|
|
|
74
|
-
|
|
75
|
-
+ OAuth2Config &
|
|
76
|
-
APIConfig;
|
|
77
|
-
|
|
78
|
-
const CONFIG: AppConfigs = {
|
|
106
|
+
const CONFIG: WhookConfigs = {
|
|
79
107
|
// ...
|
|
80
108
|
+ OAUTH2: {
|
|
81
109
|
+ // The SSR frontend
|
|
@@ -98,15 +126,15 @@ The `oAuth2Granters` service gather the various granters services you can use in
|
|
|
98
126
|
your application but you can write your own that uses a subset or a superset of
|
|
99
127
|
these granters.
|
|
100
128
|
|
|
101
|
-
Here, for example
|
|
129
|
+
Here, for example a handler that implement a verify token mechanism in order to
|
|
102
130
|
validate a user subscription:
|
|
103
131
|
|
|
104
132
|
```ts
|
|
105
133
|
import { autoService } from 'knifecycle';
|
|
106
134
|
import { noop } from '@whook/whook';
|
|
107
|
-
import YError from 'yerror';
|
|
135
|
+
import { YError } from 'yerror';
|
|
108
136
|
import type { LogService } from 'common-services';
|
|
109
|
-
import type { AuthenticationData } from './authentication';
|
|
137
|
+
import type { AuthenticationData } from './authentication.js';
|
|
110
138
|
import type {
|
|
111
139
|
OAuth2GranterService,
|
|
112
140
|
CheckApplicationService,
|
|
@@ -143,43 +171,39 @@ async function initOAuth2VerifyTokenGranter({
|
|
|
143
171
|
jwtToken,
|
|
144
172
|
pg,
|
|
145
173
|
log = noop,
|
|
146
|
-
}: OAuth2VerifyTokenGranterDependencies): Promise<
|
|
147
|
-
OAuth2VerifyTokenGranterService
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
if (err.code === 'E_BAD_TOKEN') {
|
|
178
|
-
throw YError.wrap(err, 'E_BAD_REFRESH_TOKEN');
|
|
174
|
+
}: OAuth2VerifyTokenGranterDependencies): Promise<OAuth2VerifyTokenGranterService> {
|
|
175
|
+
const authenticateWithVerifyToken: OAuth2VerifyTokenGranterService['authenticator']['authenticate'] =
|
|
176
|
+
async ({ verifyToken }, authenticationData) => {
|
|
177
|
+
try {
|
|
178
|
+
// The client must be authenticated
|
|
179
|
+
if (!authenticationData) {
|
|
180
|
+
throw new YError('E_UNAUTHORIZED');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const newAuthenticationData = await jwtToken.verify(verifyToken);
|
|
184
|
+
|
|
185
|
+
await checkApplication({
|
|
186
|
+
applicationId: authenticationData.applicationId,
|
|
187
|
+
type: 'verify',
|
|
188
|
+
scope: newAuthenticationData.scope,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const result = await pg.query(USER_VERIFY_QUERY, {
|
|
192
|
+
userId: newAuthenticationData.userId,
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
if (result.rowCount === 0) {
|
|
196
|
+
throw new YError('E_ALREADY_VERIFIED', authenticationData.userId);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return newAuthenticationData;
|
|
200
|
+
} catch (err) {
|
|
201
|
+
if (err.code === 'E_BAD_TOKEN') {
|
|
202
|
+
throw YError.wrap(err as Error, 'E_BAD_REFRESH_TOKEN');
|
|
203
|
+
}
|
|
204
|
+
throw err;
|
|
179
205
|
}
|
|
180
|
-
|
|
181
|
-
}
|
|
182
|
-
};
|
|
206
|
+
};
|
|
183
207
|
|
|
184
208
|
log('debug', '👫 - OAuth2VerifyTokenGranter Service Initialized!');
|
|
185
209
|
|
|
@@ -199,6 +223,7 @@ For internal use, you may prefer use cookies based auth handlers like
|
|
|
199
223
|
`postLogin`, `postLogout` and `postRefresh`.
|
|
200
224
|
|
|
201
225
|
To do so, configure the `ROOT_AUTHENTICATION_DATA` and `COOKIES` configurations:
|
|
226
|
+
|
|
202
227
|
```diff
|
|
203
228
|
// src/production/config.ts
|
|
204
229
|
+ COOKIES: {
|
|
@@ -211,8 +236,9 @@ To do so, configure the `ROOT_AUTHENTICATION_DATA` and `COOKIES` configurations:
|
|
|
211
236
|
```
|
|
212
237
|
|
|
213
238
|
Than import the `postLogin`, `postLogout` and `postRefresh` handlers like so:
|
|
239
|
+
|
|
214
240
|
```ts
|
|
215
|
-
// src/handlers/postRefresh.ts
|
|
241
|
+
// src/handlers/postRefresh.ts
|
|
216
242
|
import {
|
|
217
243
|
initPostAuthRefresh,
|
|
218
244
|
postAuthRefreshDefinition,
|
|
@@ -235,15 +261,17 @@ export const definition: WhookAPIHandlerDefinition = {
|
|
|
235
261
|
export default initPostAuthRefresh;
|
|
236
262
|
```
|
|
237
263
|
|
|
238
|
-
Additionnaly, you could create any handler in the `/auth` path in order to
|
|
264
|
+
Additionnaly, you could create any handler in the `/auth` path in order to
|
|
265
|
+
receive the auth cookies. For example, you may want to serve user profiles
|
|
266
|
+
there.
|
|
239
267
|
|
|
240
268
|
## Customizing handlers
|
|
241
269
|
|
|
242
270
|
The endpoints definitions are designed to support the standard OAuth2
|
|
243
271
|
definitions but can be easily overriden.
|
|
244
272
|
|
|
245
|
-
You will have to protect the `postOAuth2Acknowledge` with your own security
|
|
246
|
-
mechanism
|
|
273
|
+
You will also have to protect the `postOAuth2Acknowledge` with your own security
|
|
274
|
+
mechanism:
|
|
247
275
|
|
|
248
276
|
```ts
|
|
249
277
|
import {
|
|
@@ -259,6 +287,7 @@ export const definition: WhookAPIHandlerDefinition = {
|
|
|
259
287
|
...postOAuth2AcknowledgeDefinition,
|
|
260
288
|
operation: {
|
|
261
289
|
...postOAuth2AcknowledgeDefinition.operation,
|
|
290
|
+
// Complete the definition to protect the endpoint
|
|
262
291
|
security: [
|
|
263
292
|
{
|
|
264
293
|
bearerAuth: ['user'],
|
|
@@ -271,22 +300,29 @@ export const definition: WhookAPIHandlerDefinition = {
|
|
|
271
300
|
content: {
|
|
272
301
|
'application/json': {
|
|
273
302
|
schema: {
|
|
274
|
-
...((
|
|
275
|
-
.
|
|
276
|
-
|
|
277
|
-
]
|
|
303
|
+
...((
|
|
304
|
+
postOAuth2AcknowledgeDefinition.operation
|
|
305
|
+
.requestBody as OpenAPIV3.RequestBodyObject
|
|
306
|
+
).content['application/json']
|
|
307
|
+
.schema as OpenAPIV3.NonArraySchemaObject),
|
|
278
308
|
required: [
|
|
279
309
|
'userId',
|
|
280
|
-
...(
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
310
|
+
...(
|
|
311
|
+
(
|
|
312
|
+
postOAuth2AcknowledgeDefinition.operation
|
|
313
|
+
.requestBody as OpenAPIV3.RequestBodyObject
|
|
314
|
+
).content['application/json']
|
|
315
|
+
.schema as OpenAPIV3.NonArraySchemaObject
|
|
316
|
+
).required,
|
|
284
317
|
],
|
|
285
318
|
properties: {
|
|
286
|
-
...(
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
319
|
+
...(
|
|
320
|
+
(
|
|
321
|
+
postOAuth2AcknowledgeDefinition.operation
|
|
322
|
+
.requestBody as OpenAPIV3.RequestBodyObject
|
|
323
|
+
).content['application/json']
|
|
324
|
+
.schema as OpenAPIV3.NonArraySchemaObject
|
|
325
|
+
).properties,
|
|
290
326
|
userId: {
|
|
291
327
|
type: 'string',
|
|
292
328
|
},
|
|
@@ -300,7 +336,7 @@ export const definition: WhookAPIHandlerDefinition = {
|
|
|
300
336
|
```
|
|
301
337
|
|
|
302
338
|
You will probably need to also protect the `postOAuth2Token` endpoint with your
|
|
303
|
-
own security mecanism
|
|
339
|
+
own security mecanism:
|
|
304
340
|
|
|
305
341
|
```ts
|
|
306
342
|
// In a `src/handlers/postOAuth2Token.ts` fileimport {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import YError from 'yerror';
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { YError } from 'yerror';
|
|
3
3
|
import type { WhookAPIHandlerDefinition, WhookAPIParameterDefinition, WhookResponse, WhookErrorsDescriptors, WhookErrorDescriptor } from '@whook/whook';
|
|
4
|
-
import type { OAuth2Options, OAuth2GranterService } from '../services/oAuth2Granters';
|
|
4
|
+
import type { OAuth2Options, OAuth2GranterService } from '../services/oAuth2Granters.js';
|
|
5
5
|
import type { LogService } from 'common-services';
|
|
6
6
|
export declare const responseTypeParameter: WhookAPIParameterDefinition;
|
|
7
7
|
export declare const clientIdParameter: WhookAPIParameterDefinition;
|
|
@@ -17,10 +17,12 @@ declare const _default: import("knifecycle").ServiceInitializer<{
|
|
|
17
17
|
}, (parameters: {
|
|
18
18
|
response_type: string;
|
|
19
19
|
client_id: string;
|
|
20
|
-
redirect_uri?: string;
|
|
21
|
-
scope?: string;
|
|
20
|
+
redirect_uri?: string | undefined;
|
|
21
|
+
scope?: string | undefined;
|
|
22
22
|
state: string;
|
|
23
|
-
} & Record<string, unknown>) => Promise<WhookResponse<
|
|
23
|
+
} & Record<string, unknown>) => Promise<WhookResponse<302, {
|
|
24
|
+
location: string;
|
|
25
|
+
}, void | import("stream").Readable | import("type-fest").JsonValue>>>;
|
|
24
26
|
export default _default;
|
|
25
27
|
export declare function camelCaseObjectProperties<T>(object: Record<string, T>): Record<string, T>;
|
|
26
28
|
export declare function setURLError(url: URL, err: YError | Error, oAuth2Error: WhookErrorDescriptor): void;
|
|
@@ -1,185 +1,142 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
});
|
|
6
|
-
exports.camelCaseObjectProperties = camelCaseObjectProperties;
|
|
7
|
-
exports.scopeParameter = exports.responseTypeParameter = exports.redirectURIParameter = exports.definition = exports.default = exports.clientIdParameter = void 0;
|
|
8
|
-
exports.setURLError = setURLError;
|
|
9
|
-
exports.stateParameter = void 0;
|
|
10
|
-
|
|
11
|
-
var _knifecycle = require("knifecycle");
|
|
12
|
-
|
|
13
|
-
var _camelcase = _interopRequireDefault(require("camelcase"));
|
|
14
|
-
|
|
15
|
-
var _yerror = _interopRequireDefault(require("yerror"));
|
|
16
|
-
|
|
17
|
-
var _whook = require("@whook/whook");
|
|
18
|
-
|
|
19
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
20
|
-
|
|
1
|
+
import { autoHandler } from 'knifecycle';
|
|
2
|
+
import camelCase from 'camelcase';
|
|
3
|
+
import { YError } from 'yerror';
|
|
4
|
+
import { refersTo } from '@whook/whook';
|
|
21
5
|
/* Architecture Note #1: OAuth2 authorize
|
|
22
6
|
This endpoint simply redirect the user to the authentication
|
|
23
7
|
server page by first checking the application details are
|
|
24
8
|
fine.
|
|
25
9
|
*/
|
|
26
|
-
const responseTypeParameter = {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
10
|
+
export const responseTypeParameter = {
|
|
11
|
+
name: 'responseType',
|
|
12
|
+
parameter: {
|
|
13
|
+
in: 'query',
|
|
14
|
+
name: 'response_type',
|
|
15
|
+
required: true,
|
|
16
|
+
schema: {
|
|
17
|
+
type: 'string',
|
|
18
|
+
enum: ['code', 'token'],
|
|
19
|
+
},
|
|
20
|
+
},
|
|
37
21
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
}
|
|
22
|
+
export const clientIdParameter = {
|
|
23
|
+
name: 'clientId',
|
|
24
|
+
parameter: {
|
|
25
|
+
in: 'query',
|
|
26
|
+
name: 'client_id',
|
|
27
|
+
required: true,
|
|
28
|
+
schema: {
|
|
29
|
+
type: 'string',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
49
32
|
};
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
}
|
|
33
|
+
export const redirectURIParameter = {
|
|
34
|
+
name: 'redirectURI',
|
|
35
|
+
parameter: {
|
|
36
|
+
in: 'query',
|
|
37
|
+
name: 'redirect_uri',
|
|
38
|
+
required: false,
|
|
39
|
+
schema: {
|
|
40
|
+
type: 'string',
|
|
41
|
+
pattern: '^https?://',
|
|
42
|
+
format: 'uri',
|
|
43
|
+
},
|
|
44
|
+
},
|
|
63
45
|
};
|
|
64
|
-
|
|
65
|
-
const scopeParameter = {
|
|
66
|
-
name: 'scope',
|
|
67
|
-
parameter: {
|
|
68
|
-
in: 'query',
|
|
46
|
+
export const scopeParameter = {
|
|
69
47
|
name: 'scope',
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
48
|
+
parameter: {
|
|
49
|
+
in: 'query',
|
|
50
|
+
name: 'scope',
|
|
51
|
+
description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',
|
|
52
|
+
required: false,
|
|
53
|
+
schema: {
|
|
54
|
+
type: 'string',
|
|
55
|
+
},
|
|
56
|
+
},
|
|
76
57
|
};
|
|
77
|
-
|
|
78
|
-
const stateParameter = {
|
|
79
|
-
name: 'state',
|
|
80
|
-
parameter: {
|
|
81
|
-
in: 'query',
|
|
58
|
+
export const stateParameter = {
|
|
82
59
|
name: 'state',
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
60
|
+
parameter: {
|
|
61
|
+
in: 'query',
|
|
62
|
+
name: 'state',
|
|
63
|
+
required: true,
|
|
64
|
+
schema: {
|
|
65
|
+
type: 'string',
|
|
66
|
+
},
|
|
67
|
+
},
|
|
88
68
|
};
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
summary: `Implements the [Authorization Endpoint](https://tools.ietf.org/html/rfc6749#section-3.1)
|
|
69
|
+
export const definition = {
|
|
70
|
+
method: 'get',
|
|
71
|
+
path: '/oauth2/authorize',
|
|
72
|
+
operation: {
|
|
73
|
+
operationId: 'getOAuth2Authorize',
|
|
74
|
+
summary: `Implements the [Authorization Endpoint](https://tools.ietf.org/html/rfc6749#section-3.1)
|
|
96
75
|
as defined per the OAuth2 RFC.`,
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
76
|
+
tags: ['oauth2'],
|
|
77
|
+
parameters: [
|
|
78
|
+
refersTo(responseTypeParameter),
|
|
79
|
+
refersTo(clientIdParameter),
|
|
80
|
+
refersTo(redirectURIParameter),
|
|
81
|
+
refersTo(scopeParameter),
|
|
82
|
+
refersTo(stateParameter),
|
|
83
|
+
],
|
|
84
|
+
responses: {
|
|
85
|
+
'302': {
|
|
86
|
+
description: 'Redirects the user to the authorization interface.',
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
},
|
|
105
90
|
};
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
if (!granter) {
|
|
131
|
-
throw new _yerror.default('E_UNKNOWN_AUTHORIZER_TYPE', responseType);
|
|
91
|
+
export default autoHandler(getOAuth2Authorize);
|
|
92
|
+
async function getOAuth2Authorize({ OAUTH2, ERRORS_DESCRIPTORS, oAuth2Granters, log, }, { response_type: responseType, client_id: clientId, redirect_uri: demandedRedirectURI = '', scope: demandedScope = '', state, ...authorizeParameters }) {
|
|
93
|
+
const url = new URL(OAUTH2.authenticateURL);
|
|
94
|
+
try {
|
|
95
|
+
const granter = oAuth2Granters.find((granter) => granter.authorizer && granter.authorizer.responseType === responseType);
|
|
96
|
+
if (!granter) {
|
|
97
|
+
throw new YError('E_UNKNOWN_AUTHORIZER_TYPE', responseType);
|
|
98
|
+
}
|
|
99
|
+
const { applicationId, redirectURI, scope } = await granter.authorizer.authorize({
|
|
100
|
+
clientId,
|
|
101
|
+
redirectURI: demandedRedirectURI,
|
|
102
|
+
scope: demandedScope,
|
|
103
|
+
}, camelCaseObjectProperties(authorizeParameters));
|
|
104
|
+
url.searchParams.set('type', responseType);
|
|
105
|
+
url.searchParams.set('redirect_uri', redirectURI);
|
|
106
|
+
url.searchParams.set('scope', scope);
|
|
107
|
+
url.searchParams.set('client_id', applicationId);
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
log('debug', '👫 - OAuth2 initialization error', err.code);
|
|
111
|
+
log('debug-stack', err.stack || 'no_stack_trace');
|
|
112
|
+
url.searchParams.set('redirect_uri', demandedRedirectURI);
|
|
113
|
+
setURLError(url, err, ERRORS_DESCRIPTORS[err.code] || ERRORS_DESCRIPTORS.E_OAUTH2);
|
|
132
114
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
applicationId,
|
|
136
|
-
redirectURI,
|
|
137
|
-
scope
|
|
138
|
-
} = await granter.authorizer.authorize({
|
|
139
|
-
clientId,
|
|
140
|
-
redirectURI: demandedRedirectURI,
|
|
141
|
-
scope: demandedScope
|
|
142
|
-
}, camelCaseObjectProperties(authorizeParameters));
|
|
143
|
-
url.searchParams.set('type', responseType);
|
|
144
|
-
url.searchParams.set('redirect_uri', redirectURI);
|
|
145
|
-
url.searchParams.set('scope', scope);
|
|
146
|
-
url.searchParams.set('client_id', applicationId);
|
|
147
|
-
} catch (err) {
|
|
148
|
-
log('debug', '👫 - OAuth2 initialization error', err.code);
|
|
149
|
-
log('debug-stack', err.stack);
|
|
150
|
-
url.searchParams.set('redirect_uri', demandedRedirectURI);
|
|
151
|
-
setURLError(url, err, ERRORS_DESCRIPTORS[err.code] || ERRORS_DESCRIPTORS.E_OAUTH2);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (state) {
|
|
155
|
-
url.searchParams.set('state', state);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return {
|
|
159
|
-
status: 302,
|
|
160
|
-
headers: {
|
|
161
|
-
location: url.href
|
|
115
|
+
if (state) {
|
|
116
|
+
url.searchParams.set('state', state);
|
|
162
117
|
}
|
|
163
|
-
|
|
164
|
-
|
|
118
|
+
return {
|
|
119
|
+
status: 302,
|
|
120
|
+
headers: {
|
|
121
|
+
location: url.href,
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
// The OAuth2 standard uses snake case names so we are
|
|
165
126
|
// converting them to the project standards asap
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return camelCasedObject;
|
|
173
|
-
}, {});
|
|
127
|
+
export function camelCaseObjectProperties(object) {
|
|
128
|
+
return Object.keys(object).reduce((camelCasedObject, key) => {
|
|
129
|
+
const newKey = key === 'redirect_uri' ? 'redirectURI' : camelCase(key);
|
|
130
|
+
camelCasedObject[newKey] = object[key];
|
|
131
|
+
return camelCasedObject;
|
|
132
|
+
}, {});
|
|
174
133
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}));
|
|
183
|
-
}
|
|
134
|
+
export function setURLError(url, err, oAuth2Error) {
|
|
135
|
+
url.searchParams.set('error', oAuth2Error.code);
|
|
136
|
+
if (oAuth2Error.description) {
|
|
137
|
+
url.searchParams.set('error_decription', oAuth2Error.description.replace(/\$([0-9]+)/g, (_, paramIndex) => {
|
|
138
|
+
return (err.params || [])[parseInt(paramIndex, 10)];
|
|
139
|
+
}));
|
|
140
|
+
}
|
|
184
141
|
}
|
|
185
142
|
//# sourceMappingURL=getOAuth2Authorize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/handlers/getOAuth2Authorize.ts"],"names":["responseTypeParameter","name","parameter","in","required","schema","type","enum","clientIdParameter","redirectURIParameter","pattern","format","scopeParameter","description","stateParameter","definition","method","path","operation","operationId","summary","tags","parameters","responses","getOAuth2Authorize","OAUTH2","ERRORS_DESCRIPTORS","oAuth2Granters","log","response_type","responseType","client_id","clientId","redirect_uri","demandedRedirectURI","scope","demandedScope","state","authorizeParameters","url","URL","authenticateURL","granter","find","authorizer","YError","applicationId","redirectURI","authorize","camelCaseObjectProperties","searchParams","set","err","code","stack","setURLError","E_OAUTH2","status","headers","location","href","object","Object","keys","reduce","camelCasedObject","key","newKey","oAuth2Error","replace","_","paramIndex","params","parseInt"],"mappings":";;;;;;;;;;AAAA;;AACA;;AACA;;AACA;;;;AAcA;AACA;AACA;AACA;AACA;AAEO,MAAMA,qBAAkD,GAAG;AAChEC,EAAAA,IAAI,EAAE,cAD0D;AAEhEC,EAAAA,SAAS,EAAE;AACTC,IAAAA,EAAE,EAAE,OADK;AAETF,IAAAA,IAAI,EAAE,eAFG;AAGTG,IAAAA,QAAQ,EAAE,IAHD;AAITC,IAAAA,MAAM,EAAE;AACNC,MAAAA,IAAI,EAAE,QADA;AAENC,MAAAA,IAAI,EAAE,CAAC,MAAD,EAAS,OAAT;AAFA;AAJC;AAFqD,CAA3D;;AAYA,MAAMC,iBAA8C,GAAG;AAC5DP,EAAAA,IAAI,EAAE,UADsD;AAE5DC,EAAAA,SAAS,EAAE;AACTC,IAAAA,EAAE,EAAE,OADK;AAETF,IAAAA,IAAI,EAAE,WAFG;AAGTG,IAAAA,QAAQ,EAAE,IAHD;AAITC,IAAAA,MAAM,EAAE;AACNC,MAAAA,IAAI,EAAE;AADA;AAJC;AAFiD,CAAvD;;AAWA,MAAMG,oBAAiD,GAAG;AAC/DR,EAAAA,IAAI,EAAE,aADyD;AAE/DC,EAAAA,SAAS,EAAE;AACTC,IAAAA,EAAE,EAAE,OADK;AAETF,IAAAA,IAAI,EAAE,cAFG;AAGTG,IAAAA,QAAQ,EAAE,KAHD;AAITC,IAAAA,MAAM,EAAE;AACNC,MAAAA,IAAI,EAAE,QADA;AAENI,MAAAA,OAAO,EAAE,YAFH;AAGNC,MAAAA,MAAM,EAAE;AAHF;AAJC;AAFoD,CAA1D;;AAaA,MAAMC,cAA2C,GAAG;AACzDX,EAAAA,IAAI,EAAE,OADmD;AAEzDC,EAAAA,SAAS,EAAE;AACTC,IAAAA,EAAE,E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D,KAFD,CAFF;AAMD;AACF","sourcesContent":["import { autoHandler } from 'knifecycle';\nimport camelCase from 'camelcase';\nimport YError from 'yerror';\nimport { refersTo } from '@whook/whook';\nimport type {\n WhookAPIHandlerDefinition,\n WhookAPIParameterDefinition,\n WhookResponse,\n WhookErrorsDescriptors,\n WhookErrorDescriptor,\n} from '@whook/whook';\nimport type {\n OAuth2Options,\n OAuth2GranterService,\n} from '../services/oAuth2Granters';\nimport type { LogService } from 'common-services';\n\n/* Architecture Note #1: OAuth2 authorize\nThis endpoint simply redirect the user to the authentication\n server page by first checking the application details are\n fine.\n*/\n\nexport const responseTypeParameter: WhookAPIParameterDefinition = {\n name: 'responseType',\n parameter: {\n in: 'query',\n name: 'response_type',\n required: true,\n schema: {\n type: 'string',\n enum: ['code', 'token'],\n },\n },\n};\nexport const clientIdParameter: WhookAPIParameterDefinition = {\n name: 'clientId',\n parameter: {\n in: 'query',\n name: 'client_id',\n required: true,\n schema: {\n type: 'string',\n },\n },\n};\nexport const redirectURIParameter: WhookAPIParameterDefinition = {\n name: 'redirectURI',\n parameter: {\n in: 'query',\n name: 'redirect_uri',\n required: false,\n schema: {\n type: 'string',\n pattern: '^https?://',\n format: 'uri',\n },\n },\n};\nexport const scopeParameter: WhookAPIParameterDefinition = {\n name: 'scope',\n parameter: {\n in: 'query',\n name: 'scope',\n description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',\n required: false,\n schema: {\n type: 'string',\n },\n },\n};\nexport const stateParameter: WhookAPIParameterDefinition = {\n name: 'state',\n parameter: {\n in: 'query',\n name: 'state',\n required: true,\n schema: {\n type: 'string',\n },\n },\n};\n\nexport const definition: WhookAPIHandlerDefinition = {\n method: 'get',\n path: '/oauth2/authorize',\n operation: {\n operationId: 'getOAuth2Authorize',\n summary: `Implements the [Authorization Endpoint](https://tools.ietf.org/html/rfc6749#section-3.1)\n as defined per the OAuth2 RFC.`,\n tags: ['oauth2'],\n parameters: [\n refersTo(responseTypeParameter),\n refersTo(clientIdParameter),\n refersTo(redirectURIParameter),\n refersTo(scopeParameter),\n refersTo(stateParameter),\n ],\n responses: {\n '302': {\n description: 'Redirects the user to the authorization interface.',\n },\n },\n },\n};\n\nexport default autoHandler(getOAuth2Authorize);\n\nasync function getOAuth2Authorize(\n {\n OAUTH2,\n ERRORS_DESCRIPTORS,\n oAuth2Granters,\n log,\n }: {\n OAUTH2: OAuth2Options;\n ERRORS_DESCRIPTORS: WhookErrorsDescriptors;\n oAuth2Granters: OAuth2GranterService[];\n log: LogService;\n },\n {\n response_type: responseType,\n client_id: clientId,\n redirect_uri: demandedRedirectURI = '',\n scope: demandedScope = '',\n state,\n ...authorizeParameters\n }: {\n response_type: string;\n client_id: string;\n redirect_uri?: string;\n scope?: string;\n state: string;\n } & Record<string, unknown>,\n): Promise<WhookResponse> {\n const url = new URL(OAUTH2.authenticateURL);\n\n try {\n const granter = oAuth2Granters.find(\n (granter) =>\n granter.authorizer && granter.authorizer.responseType === responseType,\n );\n\n if (!granter) {\n throw new YError('E_UNKNOWN_AUTHORIZER_TYPE', responseType);\n }\n\n const { applicationId, redirectURI, scope } =\n await granter.authorizer.authorize(\n {\n clientId,\n redirectURI: demandedRedirectURI,\n scope: demandedScope,\n },\n camelCaseObjectProperties(authorizeParameters),\n );\n\n url.searchParams.set('type', responseType);\n url.searchParams.set('redirect_uri', redirectURI);\n url.searchParams.set('scope', scope);\n url.searchParams.set('client_id', applicationId);\n } catch (err) {\n log('debug', '👫 - OAuth2 initialization error', err.code);\n log('debug-stack', err.stack);\n\n url.searchParams.set('redirect_uri', demandedRedirectURI);\n setURLError(\n url,\n err,\n ERRORS_DESCRIPTORS[err.code] || ERRORS_DESCRIPTORS.E_OAUTH2,\n );\n }\n\n if (state) {\n url.searchParams.set('state', state);\n }\n\n return {\n status: 302,\n headers: {\n location: url.href,\n },\n };\n}\n\n// The OAuth2 standard uses snake case names so we are\n// converting them to the project standards asap\nexport function camelCaseObjectProperties<T>(\n object: Record<string, T>,\n): Record<string, T> {\n return Object.keys(object).reduce((camelCasedObject, key) => {\n const newKey = key === 'redirect_uri' ? 'redirectURI' : camelCase(key);\n\n camelCasedObject[newKey] = object[key];\n return camelCasedObject;\n }, {});\n}\n\nexport function setURLError(\n url: URL,\n err: YError | Error,\n oAuth2Error: WhookErrorDescriptor,\n): void {\n url.searchParams.set('error', oAuth2Error.code);\n if (oAuth2Error.description) {\n url.searchParams.set(\n 'error_decription',\n oAuth2Error.description.replace(/\\$([0-9]+)/g, (_, paramIndex) => {\n return ((err as YError).params || [])[parseInt(paramIndex, 10)];\n }),\n );\n }\n}\n"],"file":"getOAuth2Authorize.js"}
|
|
1
|
+
{"version":3,"file":"getOAuth2Authorize.js","sourceRoot":"","sources":["../../src/handlers/getOAuth2Authorize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,SAAS,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAcxC;;;;EAIE;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAgC;IAChE,IAAI,EAAE,cAAc;IACpB,SAAS,EAAE;QACT,EAAE,EAAE,OAAO;QACX,IAAI,EAAE,eAAe;QACrB,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE;YACN,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC;SACxB;KACF;CACF,CAAC;AACF,MAAM,CAAC,MAAM,iBAAiB,GAAgC;IAC5D,IAAI,EAAE,UAAU;IAChB,SAAS,EAAE;QACT,EAAE,EAAE,OAAO;QACX,IAAI,EAAE,WAAW;QACjB,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE;YACN,IAAI,EAAE,QAAQ;SACf;KACF;CACF,CAAC;AACF,MAAM,CAAC,MAAM,oBAAoB,GAAgC;IAC/D,IAAI,EAAE,aAAa;IACnB,S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