@whook/oauth2 18.1.0 → 19.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 +27 -37
- package/dist/index.d.ts +6 -6
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +19 -18
- package/dist/index.test.js.map +1 -1
- package/dist/routes/getOAuth2Authorize.d.ts +100 -0
- package/dist/{handlers → routes}/getOAuth2Authorize.js +38 -35
- package/dist/routes/getOAuth2Authorize.js.map +1 -0
- package/dist/{handlers → routes}/getOAuth2Authorize.test.js +14 -10
- package/dist/routes/getOAuth2Authorize.test.js.map +1 -0
- package/dist/routes/postAuthLogin.d.ts +89 -0
- package/dist/{handlers → routes}/postAuthLogin.js +40 -39
- package/dist/routes/postAuthLogin.js.map +1 -0
- package/dist/routes/postAuthLogin.test.js.map +1 -0
- package/dist/routes/postAuthLogout.d.ts +28 -0
- package/dist/{handlers → routes}/postAuthLogout.js +9 -9
- package/dist/routes/postAuthLogout.js.map +1 -0
- package/dist/{handlers → routes}/postAuthLogout.test.js +1 -1
- package/dist/routes/postAuthLogout.test.js.map +1 -0
- package/dist/routes/postAuthRefresh.d.ts +94 -0
- package/dist/{handlers → routes}/postAuthRefresh.js +42 -41
- package/dist/routes/postAuthRefresh.js.map +1 -0
- package/dist/routes/postAuthRefresh.test.js.map +1 -0
- package/dist/routes/postOAuth2Acknowledge.d.ts +97 -0
- package/dist/{handlers → routes}/postOAuth2Acknowledge.js +46 -44
- package/dist/routes/postOAuth2Acknowledge.js.map +1 -0
- package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -0
- package/dist/routes/postOAuth2Token.d.ts +198 -0
- package/dist/{handlers → routes}/postOAuth2Token.js +35 -32
- package/dist/routes/postOAuth2Token.js.map +1 -0
- package/dist/routes/postOAuth2Token.test.js.map +1 -0
- package/dist/services/authCookies.d.ts +3 -3
- package/dist/services/authCookies.js.map +1 -1
- package/dist/services/oAuth2ClientCredentialsGranter.d.ts +5 -5
- package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
- package/dist/services/oAuth2CodeGranter.d.ts +2 -3
- package/dist/services/oAuth2CodeGranter.js +2 -0
- package/dist/services/oAuth2CodeGranter.js.map +1 -1
- package/dist/services/oAuth2CodeGranter.test.js +2 -2
- package/dist/services/oAuth2CodeGranter.test.js.map +1 -1
- package/dist/services/oAuth2Granters.d.ts +24 -24
- package/dist/services/oAuth2Granters.js.map +1 -1
- package/dist/services/oAuth2PasswordGranter.d.ts +2 -3
- package/dist/services/oAuth2PasswordGranter.js.map +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.d.ts +2 -3
- package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
- package/dist/services/oAuth2TokenGranter.d.ts +2 -3
- package/dist/services/oAuth2TokenGranter.js +4 -0
- package/dist/services/oAuth2TokenGranter.js.map +1 -1
- package/package.json +9 -8
- package/src/__snapshots__/index.test.ts.snap +1 -0
- package/src/index.test.ts +31 -42
- package/src/index.ts +6 -6
- package/src/{handlers → routes}/getOAuth2Authorize.test.ts +14 -10
- package/src/{handlers → routes}/getOAuth2Authorize.ts +98 -95
- package/src/{handlers → routes}/postAuthLogin.test.ts +2 -1
- package/src/routes/postAuthLogin.ts +118 -0
- package/src/{handlers → routes}/postAuthLogout.test.ts +1 -1
- package/src/{handlers → routes}/postAuthLogout.ts +13 -13
- package/src/{handlers → routes}/postAuthRefresh.test.ts +2 -1
- package/src/routes/postAuthRefresh.ts +138 -0
- package/src/{handlers → routes}/postOAuth2Acknowledge.test.ts +18 -34
- package/src/{handlers → routes}/postOAuth2Acknowledge.ts +82 -90
- package/src/{handlers → routes}/postOAuth2Token.test.ts +3 -2
- package/src/{handlers → routes}/postOAuth2Token.ts +115 -120
- package/src/services/authCookies.ts +3 -5
- package/src/services/oAuth2ClientCredentialsGranter.ts +5 -10
- package/src/services/oAuth2CodeGranter.test.ts +12 -21
- package/src/services/oAuth2CodeGranter.ts +4 -6
- package/src/services/oAuth2Granters.ts +30 -55
- package/src/services/oAuth2PasswordGranter.ts +2 -6
- package/src/services/oAuth2RefreshTokenGranter.ts +2 -6
- package/src/services/oAuth2TokenGranter.ts +6 -6
- package/src/types.d.ts +7 -0
- package/dist/handlers/getOAuth2Authorize.d.ts +0 -27
- package/dist/handlers/getOAuth2Authorize.js.map +0 -1
- package/dist/handlers/getOAuth2Authorize.test.js.map +0 -1
- package/dist/handlers/postAuthLogin.d.ts +0 -28
- package/dist/handlers/postAuthLogin.js.map +0 -1
- package/dist/handlers/postAuthLogin.test.js.map +0 -1
- package/dist/handlers/postAuthLogout.d.ts +0 -12
- package/dist/handlers/postAuthLogout.js.map +0 -1
- package/dist/handlers/postAuthLogout.test.js.map +0 -1
- package/dist/handlers/postAuthRefresh.d.ts +0 -28
- package/dist/handlers/postAuthRefresh.js.map +0 -1
- package/dist/handlers/postAuthRefresh.test.js.map +0 -1
- package/dist/handlers/postOAuth2Acknowledge.d.ts +0 -31
- package/dist/handlers/postOAuth2Acknowledge.js.map +0 -1
- package/dist/handlers/postOAuth2Acknowledge.test.js.map +0 -1
- package/dist/handlers/postOAuth2Token.d.ts +0 -36
- package/dist/handlers/postOAuth2Token.js.map +0 -1
- package/dist/handlers/postOAuth2Token.test.js.map +0 -1
- package/src/handlers/postAuthLogin.ts +0 -123
- package/src/handlers/postAuthRefresh.ts +0 -143
- /package/dist/{handlers → routes}/getOAuth2Authorize.test.d.ts +0 -0
- /package/dist/{handlers → routes}/postAuthLogin.test.d.ts +0 -0
- /package/dist/{handlers → routes}/postAuthLogin.test.js +0 -0
- /package/dist/{handlers → routes}/postAuthLogout.test.d.ts +0 -0
- /package/dist/{handlers → routes}/postAuthRefresh.test.d.ts +0 -0
- /package/dist/{handlers → routes}/postAuthRefresh.test.js +0 -0
- /package/dist/{handlers → routes}/postOAuth2Acknowledge.test.d.ts +0 -0
- /package/dist/{handlers → routes}/postOAuth2Acknowledge.test.js +0 -0
- /package/dist/{handlers → routes}/postOAuth2Token.test.d.ts +0 -0
- /package/dist/{handlers → routes}/postOAuth2Token.test.js +0 -0
- /package/src/{handlers → routes}/__snapshots__/getOAuth2Authorize.test.ts.snap +0 -0
- /package/src/{handlers → routes}/__snapshots__/postOAuth2Acknowledge.test.ts.snap +0 -0
- /package/src/{handlers → routes}/__snapshots__/postOAuth2Token.test.ts.snap +0 -0
|
@@ -1,54 +1,52 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { autoService, location } from 'knifecycle';
|
|
2
2
|
import { camelCaseObjectProperties } from './getOAuth2Authorize.js';
|
|
3
3
|
import { YError, printStackTrace } from 'yerror';
|
|
4
|
-
import { type OpenAPIV3_1 } from 'openapi-types';
|
|
5
4
|
import { type LogService, type TimeService } from 'common-services';
|
|
6
5
|
import {
|
|
7
6
|
noop,
|
|
8
7
|
refersTo,
|
|
9
8
|
type WhookAPISchemaDefinition,
|
|
10
|
-
type
|
|
9
|
+
type WhookRouteDefinition,
|
|
11
10
|
} from '@whook/whook';
|
|
12
11
|
import {
|
|
13
12
|
type OAuth2GranterService,
|
|
14
13
|
type OAuth2RefreshTokenService,
|
|
15
14
|
type OAuth2AccessTokenService,
|
|
16
15
|
} from '../services/oAuth2Granters.js';
|
|
17
|
-
import { type
|
|
16
|
+
import { type WhookAuthenticationData } from '@whook/authorization';
|
|
18
17
|
|
|
19
18
|
/* Architecture Note #2: OAuth2 acknowledge
|
|
20
19
|
This endpoint is to be used by the authentication server page
|
|
21
20
|
to acknowlege that the user accepted the client request.
|
|
22
21
|
*/
|
|
23
|
-
export const authorizationCodeTokenRequestBodySchema
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
format: 'uri',
|
|
46
|
-
},
|
|
22
|
+
export const authorizationCodeTokenRequestBodySchema = {
|
|
23
|
+
name: 'AuthorizationCodeRequestBody',
|
|
24
|
+
schema: {
|
|
25
|
+
type: 'object',
|
|
26
|
+
description:
|
|
27
|
+
'Authorization code grant, see https://tools.ietf.org/html/rfc6749#section-4.1',
|
|
28
|
+
required: ['grant_type'],
|
|
29
|
+
properties: {
|
|
30
|
+
grant_type: {
|
|
31
|
+
type: 'string',
|
|
32
|
+
enum: ['authorization_code'],
|
|
33
|
+
},
|
|
34
|
+
code: {
|
|
35
|
+
type: 'string',
|
|
36
|
+
},
|
|
37
|
+
client_id: {
|
|
38
|
+
type: 'string',
|
|
39
|
+
},
|
|
40
|
+
redirect_uri: {
|
|
41
|
+
type: 'string',
|
|
42
|
+
pattern: '^https?://',
|
|
43
|
+
format: 'uri',
|
|
47
44
|
},
|
|
48
45
|
},
|
|
49
|
-
}
|
|
46
|
+
},
|
|
47
|
+
} as const satisfies WhookAPISchemaDefinition;
|
|
50
48
|
|
|
51
|
-
export const passwordTokenRequestBodySchema
|
|
49
|
+
export const passwordTokenRequestBodySchema = {
|
|
52
50
|
name: 'PasswordRequestBody',
|
|
53
51
|
schema: {
|
|
54
52
|
type: 'object',
|
|
@@ -72,30 +70,29 @@ export const passwordTokenRequestBodySchema: WhookAPISchemaDefinition = {
|
|
|
72
70
|
},
|
|
73
71
|
},
|
|
74
72
|
},
|
|
75
|
-
};
|
|
73
|
+
} as const satisfies WhookAPISchemaDefinition;
|
|
76
74
|
|
|
77
|
-
export const clientCredentialsTokenRequestBodySchema
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',
|
|
93
|
-
},
|
|
75
|
+
export const clientCredentialsTokenRequestBodySchema = {
|
|
76
|
+
name: 'ClientCredentialsRequestBody',
|
|
77
|
+
schema: {
|
|
78
|
+
type: 'object',
|
|
79
|
+
description:
|
|
80
|
+
'Client credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.4',
|
|
81
|
+
required: ['grant_type'],
|
|
82
|
+
properties: {
|
|
83
|
+
grant_type: {
|
|
84
|
+
type: 'string',
|
|
85
|
+
enum: ['client_credentials'],
|
|
86
|
+
},
|
|
87
|
+
scope: {
|
|
88
|
+
type: 'string',
|
|
89
|
+
description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',
|
|
94
90
|
},
|
|
95
91
|
},
|
|
96
|
-
}
|
|
92
|
+
},
|
|
93
|
+
} as const satisfies WhookAPISchemaDefinition;
|
|
97
94
|
|
|
98
|
-
export const refreshTokenRequestBodySchema
|
|
95
|
+
export const refreshTokenRequestBodySchema = {
|
|
99
96
|
name: 'RefreshTokenRequestBody',
|
|
100
97
|
schema: {
|
|
101
98
|
type: 'object',
|
|
@@ -116,9 +113,9 @@ export const refreshTokenRequestBodySchema: WhookAPISchemaDefinition = {
|
|
|
116
113
|
},
|
|
117
114
|
},
|
|
118
115
|
},
|
|
119
|
-
};
|
|
116
|
+
} as const satisfies WhookAPISchemaDefinition;
|
|
120
117
|
|
|
121
|
-
export const tokenBodySchema
|
|
118
|
+
export const tokenBodySchema = {
|
|
122
119
|
name: 'TokenRequestBody',
|
|
123
120
|
schema: {
|
|
124
121
|
oneOf: [
|
|
@@ -127,10 +124,10 @@ export const tokenBodySchema: WhookAPISchemaDefinition = {
|
|
|
127
124
|
refersTo(clientCredentialsTokenRequestBodySchema),
|
|
128
125
|
refersTo(refreshTokenRequestBodySchema),
|
|
129
126
|
],
|
|
130
|
-
}
|
|
131
|
-
};
|
|
127
|
+
},
|
|
128
|
+
} as const satisfies WhookAPISchemaDefinition;
|
|
132
129
|
|
|
133
|
-
export const definition
|
|
130
|
+
export const definition = {
|
|
134
131
|
method: 'post',
|
|
135
132
|
path: '/oauth2/token',
|
|
136
133
|
operation: {
|
|
@@ -210,27 +207,22 @@ export const definition: WhookAPIHandlerDefinition = {
|
|
|
210
207
|
},
|
|
211
208
|
},
|
|
212
209
|
},
|
|
213
|
-
};
|
|
210
|
+
} as const satisfies WhookRouteDefinition;
|
|
214
211
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
oAuth2RefreshToken: OAuth2RefreshTokenService;
|
|
230
|
-
log: LogService;
|
|
231
|
-
time: TimeService;
|
|
232
|
-
},
|
|
233
|
-
{
|
|
212
|
+
async function initPostOAuth2Token({
|
|
213
|
+
oAuth2Granters,
|
|
214
|
+
oAuth2AccessToken,
|
|
215
|
+
oAuth2RefreshToken,
|
|
216
|
+
time = Date.now.bind(Date),
|
|
217
|
+
log = noop,
|
|
218
|
+
}: {
|
|
219
|
+
oAuth2Granters: OAuth2GranterService[];
|
|
220
|
+
oAuth2AccessToken: OAuth2AccessTokenService;
|
|
221
|
+
oAuth2RefreshToken: OAuth2RefreshTokenService;
|
|
222
|
+
log: LogService;
|
|
223
|
+
time: TimeService;
|
|
224
|
+
}) {
|
|
225
|
+
return async ({
|
|
234
226
|
body: { grant_type: grantType, ...grantParameters },
|
|
235
227
|
authenticationData,
|
|
236
228
|
}: {
|
|
@@ -238,54 +230,57 @@ async function postOAuth2Token<
|
|
|
238
230
|
grant_type: string;
|
|
239
231
|
[name: string]: unknown;
|
|
240
232
|
};
|
|
241
|
-
authenticationData:
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
233
|
+
authenticationData: WhookAuthenticationData;
|
|
234
|
+
}) => {
|
|
235
|
+
try {
|
|
236
|
+
const granter = oAuth2Granters.find(
|
|
237
|
+
(granter) =>
|
|
238
|
+
granter.authenticator &&
|
|
239
|
+
granter.authenticator.grantType === grantType,
|
|
240
|
+
);
|
|
249
241
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
242
|
+
if (!granter || !granter.authenticator) {
|
|
243
|
+
throw new YError('E_UNKNOWN_AUTHENTICATOR_TYPE', grantType);
|
|
244
|
+
}
|
|
253
245
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
246
|
+
const newAuthenticationData = await granter.authenticator.authenticate(
|
|
247
|
+
camelCaseObjectProperties(grantParameters),
|
|
248
|
+
authenticationData,
|
|
249
|
+
);
|
|
258
250
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
251
|
+
const [
|
|
252
|
+
{ token: accessToken, expiresAt: accessTokenExpiresAt },
|
|
253
|
+
{ token: refreshToken, expiresAt: refreshTokenExpiresAt },
|
|
254
|
+
] = await Promise.all([
|
|
255
|
+
oAuth2AccessToken.create(authenticationData, newAuthenticationData),
|
|
256
|
+
oAuth2RefreshToken.create(authenticationData, newAuthenticationData),
|
|
257
|
+
]);
|
|
258
|
+
const currentTime = time();
|
|
267
259
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
260
|
+
return {
|
|
261
|
+
status: 200,
|
|
262
|
+
headers: {},
|
|
263
|
+
body: {
|
|
264
|
+
access_token: accessToken,
|
|
265
|
+
token_type: 'bearer',
|
|
266
|
+
expires_in: Math.ceil((accessTokenExpiresAt - currentTime) / 1000),
|
|
267
|
+
expiration_date: new Date(accessTokenExpiresAt).toISOString(),
|
|
268
|
+
refresh_token: refreshToken,
|
|
269
|
+
refresh_token_expires_in: Math.ceil(
|
|
270
|
+
(refreshTokenExpiresAt - currentTime) / 1000,
|
|
271
|
+
),
|
|
272
|
+
refresh_token_expiration_date: new Date(
|
|
273
|
+
refreshTokenExpiresAt,
|
|
274
|
+
).toISOString(),
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
} catch (err) {
|
|
278
|
+
log('debug', '👫 - OAuth2 token issuing error', (err as YError).code);
|
|
279
|
+
log('error-stack', printStackTrace(err as Error));
|
|
288
280
|
|
|
289
|
-
|
|
290
|
-
|
|
281
|
+
throw YError.cast(err as Error, 'E_OAUTH2');
|
|
282
|
+
}
|
|
283
|
+
};
|
|
291
284
|
}
|
|
285
|
+
|
|
286
|
+
export default location(autoService(initPostOAuth2Token), import.meta.url);
|
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import ms from 'ms';
|
|
2
2
|
import cookie, { type SerializeOptions } from 'cookie';
|
|
3
3
|
import { autoService, location } from 'knifecycle';
|
|
4
|
-
import { type
|
|
4
|
+
import { type WhookAuthenticationData } from '@whook/authorization';
|
|
5
5
|
import { type Jsonify } from 'type-fest';
|
|
6
6
|
|
|
7
7
|
export const AUTH_API_PREFIX = '/auth';
|
|
8
8
|
|
|
9
|
-
export type AuthHandlersConfig
|
|
10
|
-
|
|
11
|
-
> = {
|
|
12
|
-
ROOT_AUTHENTICATION_DATA: AUTHENTICATION_DATA;
|
|
9
|
+
export type AuthHandlersConfig = {
|
|
10
|
+
ROOT_AUTHENTICATION_DATA: WhookAuthenticationData;
|
|
13
11
|
};
|
|
14
12
|
|
|
15
13
|
export type AuthCookiesConfig = {
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
type OAuth2GranterService,
|
|
7
7
|
type CheckApplicationService,
|
|
8
8
|
} from './oAuth2Granters.js';
|
|
9
|
-
import { type
|
|
9
|
+
import { type WhookAuthenticationData } from '@whook/authorization';
|
|
10
10
|
import { type OAuth2Options } from '../services/oAuth2Granters.js';
|
|
11
11
|
|
|
12
12
|
export type OAuth2ClientCredentialsGranterDependencies = {
|
|
@@ -14,20 +14,15 @@ export type OAuth2ClientCredentialsGranterDependencies = {
|
|
|
14
14
|
checkApplication: CheckApplicationService;
|
|
15
15
|
log?: LogService;
|
|
16
16
|
};
|
|
17
|
-
export type OAuth2ClientCredentialsGranterParameters
|
|
18
|
-
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
19
|
-
> = {
|
|
17
|
+
export type OAuth2ClientCredentialsGranterParameters = {
|
|
20
18
|
username: string;
|
|
21
19
|
password: string;
|
|
22
|
-
scope?:
|
|
20
|
+
scope?: WhookAuthenticationData['scope'];
|
|
23
21
|
};
|
|
24
|
-
export type OAuth2ClientCredentialsGranterService<
|
|
25
|
-
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
26
|
-
> = OAuth2GranterService<
|
|
22
|
+
export type OAuth2ClientCredentialsGranterService = OAuth2GranterService<
|
|
27
23
|
Record<string, unknown>,
|
|
28
24
|
Record<string, unknown>,
|
|
29
|
-
OAuth2ClientCredentialsGranterParameters
|
|
30
|
-
AUTHENTICATION_DATA
|
|
25
|
+
OAuth2ClientCredentialsGranterParameters
|
|
31
26
|
>;
|
|
32
27
|
|
|
33
28
|
export default location(
|
|
@@ -1,27 +1,15 @@
|
|
|
1
|
-
import { describe,
|
|
2
|
-
import {
|
|
1
|
+
import { describe, test, beforeEach, jest, expect } from '@jest/globals';
|
|
2
|
+
import { type WhookAuthenticationData } from '@whook/authorization';
|
|
3
3
|
import initOAuth2CodeGranter from './oAuth2CodeGranter.js';
|
|
4
4
|
import {
|
|
5
|
-
CheckApplicationService,
|
|
6
|
-
OAuth2CodeService,
|
|
5
|
+
type CheckApplicationService,
|
|
6
|
+
type OAuth2CodeService,
|
|
7
7
|
} from './oAuth2Granters.js';
|
|
8
8
|
|
|
9
9
|
describe('OAuth2CodeGranter', () => {
|
|
10
10
|
const oAuth2Code = {
|
|
11
|
-
create:
|
|
12
|
-
|
|
13
|
-
OAuth2CodeService<
|
|
14
|
-
BaseAuthenticationData<string, string>,
|
|
15
|
-
string
|
|
16
|
-
>['create']
|
|
17
|
-
>(),
|
|
18
|
-
check:
|
|
19
|
-
jest.fn<
|
|
20
|
-
OAuth2CodeService<
|
|
21
|
-
BaseAuthenticationData<string, string>,
|
|
22
|
-
string
|
|
23
|
-
>['check']
|
|
24
|
-
>(),
|
|
11
|
+
create: jest.fn<OAuth2CodeService<string>['create']>(),
|
|
12
|
+
check: jest.fn<OAuth2CodeService<string>['check']>(),
|
|
25
13
|
};
|
|
26
14
|
const checkApplication = jest.fn<CheckApplicationService>();
|
|
27
15
|
const log = jest.fn();
|
|
@@ -33,7 +21,7 @@ describe('OAuth2CodeGranter', () => {
|
|
|
33
21
|
log.mockReset();
|
|
34
22
|
});
|
|
35
23
|
|
|
36
|
-
|
|
24
|
+
test('should work with a complete valid flow', async () => {
|
|
37
25
|
const oAuth2CodeGranter = await initOAuth2CodeGranter({
|
|
38
26
|
checkApplication,
|
|
39
27
|
oAuth2Code,
|
|
@@ -51,6 +39,9 @@ describe('OAuth2CodeGranter', () => {
|
|
|
51
39
|
redirectURI: 'https://www.example2.com',
|
|
52
40
|
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
53
41
|
scope: 'user',
|
|
42
|
+
} as WhookAuthenticationData & {
|
|
43
|
+
redirectURI: string;
|
|
44
|
+
[name: string]: unknown;
|
|
54
45
|
});
|
|
55
46
|
|
|
56
47
|
const authorizerResult = await oAuth2CodeGranter.authorizer?.authorize({
|
|
@@ -63,7 +54,7 @@ describe('OAuth2CodeGranter', () => {
|
|
|
63
54
|
{
|
|
64
55
|
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
65
56
|
scope: 'user, admin',
|
|
66
|
-
},
|
|
57
|
+
} as WhookAuthenticationData,
|
|
67
58
|
{
|
|
68
59
|
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
69
60
|
redirectURI: 'https://www.example.com/oauth2/code',
|
|
@@ -81,7 +72,7 @@ describe('OAuth2CodeGranter', () => {
|
|
|
81
72
|
{
|
|
82
73
|
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
83
74
|
scope: 'user',
|
|
84
|
-
},
|
|
75
|
+
} as WhookAuthenticationData,
|
|
85
76
|
);
|
|
86
77
|
|
|
87
78
|
expect({
|
|
@@ -7,7 +7,6 @@ import {
|
|
|
7
7
|
type OAuth2CodeService,
|
|
8
8
|
type CheckApplicationService,
|
|
9
9
|
} from './oAuth2Granters.js';
|
|
10
|
-
import { type BaseAuthenticationData } from '@whook/authorization';
|
|
11
10
|
|
|
12
11
|
export const CODE_GRANTER_TYPE = 'code';
|
|
13
12
|
|
|
@@ -21,13 +20,10 @@ export type OAuth2CodeGranterParameters = {
|
|
|
21
20
|
redirectURI: string;
|
|
22
21
|
clientId: string;
|
|
23
22
|
};
|
|
24
|
-
export type OAuth2CodeGranterService<
|
|
25
|
-
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
26
|
-
> = OAuth2GranterService<
|
|
23
|
+
export type OAuth2CodeGranterService = OAuth2GranterService<
|
|
27
24
|
Record<string, unknown>,
|
|
28
25
|
Record<string, unknown>,
|
|
29
|
-
OAuth2CodeGranterParameters
|
|
30
|
-
AUTHENTICATION_DATA
|
|
26
|
+
OAuth2CodeGranterParameters
|
|
31
27
|
>;
|
|
32
28
|
|
|
33
29
|
export default location(autoService(initOAuth2CodeGranter), import.meta.url);
|
|
@@ -72,6 +68,8 @@ async function initOAuth2CodeGranter({
|
|
|
72
68
|
);
|
|
73
69
|
|
|
74
70
|
return {
|
|
71
|
+
// TODO: check a way to avoid this by adding params
|
|
72
|
+
...authenticationData,
|
|
75
73
|
applicationId: clientId,
|
|
76
74
|
redirectURI,
|
|
77
75
|
scope,
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
DEFAULT_HELP_URI,
|
|
5
5
|
type WhookErrorsDescriptors,
|
|
6
6
|
} from '@whook/whook';
|
|
7
|
-
import { type
|
|
7
|
+
import { type WhookAuthenticationData } from '@whook/authorization';
|
|
8
8
|
|
|
9
9
|
export const OAUTH2_ERRORS_DESCRIPTORS: WhookErrorsDescriptors = {
|
|
10
10
|
E_UNKNOWN_AUTHORIZER_TYPE: {
|
|
@@ -125,62 +125,50 @@ export const OAUTH2_ERRORS_DESCRIPTORS: WhookErrorsDescriptors = {
|
|
|
125
125
|
},
|
|
126
126
|
};
|
|
127
127
|
|
|
128
|
-
export type OAuth2CodeService<
|
|
129
|
-
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
130
|
-
CODE = string,
|
|
131
|
-
> = {
|
|
128
|
+
export type OAuth2CodeService<CODE = string> = {
|
|
132
129
|
create: (
|
|
133
|
-
authenticationData:
|
|
130
|
+
authenticationData: WhookAuthenticationData,
|
|
134
131
|
redirectURI: string,
|
|
135
132
|
additionalParameters: { [name: string]: unknown },
|
|
136
133
|
) => Promise<CODE>;
|
|
137
134
|
check: (
|
|
138
|
-
authenticationData:
|
|
135
|
+
authenticationData: WhookAuthenticationData,
|
|
139
136
|
code: CODE,
|
|
140
137
|
redirectURI: string,
|
|
141
138
|
) => Promise<
|
|
142
|
-
|
|
139
|
+
WhookAuthenticationData & {
|
|
143
140
|
redirectURI: string;
|
|
144
141
|
} & { [name: string]: unknown }
|
|
145
142
|
>;
|
|
146
143
|
};
|
|
147
144
|
|
|
148
|
-
export type OAuth2PasswordService<
|
|
149
|
-
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
150
|
-
USERNAME = string,
|
|
151
|
-
PASSWORD = string,
|
|
152
|
-
> = {
|
|
145
|
+
export type OAuth2PasswordService<USERNAME = string, PASSWORD = string> = {
|
|
153
146
|
check: (
|
|
154
|
-
authenticationData:
|
|
147
|
+
authenticationData: WhookAuthenticationData,
|
|
155
148
|
username: USERNAME,
|
|
156
149
|
password: PASSWORD,
|
|
157
|
-
demandedScope:
|
|
158
|
-
) => Promise<
|
|
150
|
+
demandedScope: WhookAuthenticationData['scope'],
|
|
151
|
+
) => Promise<WhookAuthenticationData>;
|
|
159
152
|
};
|
|
160
153
|
|
|
161
|
-
export type OAuth2AccessTokenService<
|
|
162
|
-
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
163
|
-
TOKEN = string,
|
|
164
|
-
> = {
|
|
154
|
+
export type OAuth2AccessTokenService<TOKEN = string> = {
|
|
165
155
|
create: (
|
|
166
|
-
authenticationData:
|
|
167
|
-
tokenAuthenticationData:
|
|
156
|
+
authenticationData: WhookAuthenticationData,
|
|
157
|
+
tokenAuthenticationData: WhookAuthenticationData,
|
|
168
158
|
additionalParameters?: { [name: string]: unknown },
|
|
169
159
|
) => Promise<{
|
|
170
160
|
token: TOKEN;
|
|
171
161
|
expiresAt: number;
|
|
172
162
|
}>;
|
|
173
163
|
check: (
|
|
174
|
-
authenticationData:
|
|
164
|
+
authenticationData: WhookAuthenticationData,
|
|
175
165
|
token: TOKEN,
|
|
176
|
-
scope?:
|
|
177
|
-
) => Promise<
|
|
166
|
+
scope?: WhookAuthenticationData['scope'],
|
|
167
|
+
) => Promise<WhookAuthenticationData>;
|
|
178
168
|
};
|
|
179
169
|
|
|
180
|
-
export type OAuth2RefreshTokenService<
|
|
181
|
-
|
|
182
|
-
TOKEN = string,
|
|
183
|
-
> = OAuth2AccessTokenService<AUTHENTICATION_DATA, TOKEN>;
|
|
170
|
+
export type OAuth2RefreshTokenService<TOKEN = string> =
|
|
171
|
+
OAuth2AccessTokenService<TOKEN>;
|
|
184
172
|
|
|
185
173
|
export type CheckApplicationService = {
|
|
186
174
|
(context: {
|
|
@@ -201,44 +189,41 @@ export type OAuth2GranterAuthorize<
|
|
|
201
189
|
string,
|
|
202
190
|
unknown
|
|
203
191
|
>,
|
|
204
|
-
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
205
192
|
> = (
|
|
206
193
|
context: {
|
|
207
|
-
clientId:
|
|
194
|
+
clientId: WhookAuthenticationData['applicationId'];
|
|
208
195
|
redirectURI: string;
|
|
209
|
-
scope:
|
|
196
|
+
scope: WhookAuthenticationData['scope'];
|
|
210
197
|
},
|
|
211
198
|
authorizeParameters?: AUTHORIZE_PARAMETERS,
|
|
212
199
|
) => Promise<{
|
|
213
|
-
applicationId:
|
|
200
|
+
applicationId: WhookAuthenticationData['applicationId'];
|
|
214
201
|
redirectURI: string;
|
|
215
|
-
scope:
|
|
202
|
+
scope: WhookAuthenticationData['scope'];
|
|
216
203
|
}>;
|
|
217
204
|
|
|
218
205
|
export type Oauth2GranterAuthenticate<
|
|
219
206
|
GRANT_PARAMETERS extends Record<string, unknown> = Record<string, unknown>,
|
|
220
|
-
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
221
207
|
> = (
|
|
222
208
|
grantParameters: GRANT_PARAMETERS,
|
|
223
|
-
authenticationData:
|
|
224
|
-
) => Promise<
|
|
209
|
+
authenticationData: WhookAuthenticationData,
|
|
210
|
+
) => Promise<WhookAuthenticationData>;
|
|
225
211
|
|
|
226
212
|
export type OAuth2GranterAcknowledge<
|
|
227
213
|
ACKNOWLEDGE_PARAMETERS extends Record<string, unknown> = Record<
|
|
228
214
|
string,
|
|
229
215
|
unknown
|
|
230
216
|
>,
|
|
231
|
-
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
232
217
|
> = (
|
|
233
|
-
authenticationData:
|
|
218
|
+
authenticationData: WhookAuthenticationData,
|
|
234
219
|
acknowledgeParameters: {
|
|
235
|
-
clientId:
|
|
220
|
+
clientId: WhookAuthenticationData['applicationId'];
|
|
236
221
|
redirectURI: string;
|
|
237
|
-
scope:
|
|
222
|
+
scope: WhookAuthenticationData['scope'];
|
|
238
223
|
},
|
|
239
224
|
additionalParameters: ACKNOWLEDGE_PARAMETERS,
|
|
240
225
|
) => Promise<
|
|
241
|
-
|
|
226
|
+
WhookAuthenticationData & {
|
|
242
227
|
redirectURI: string;
|
|
243
228
|
[name: string]: unknown;
|
|
244
229
|
}
|
|
@@ -254,29 +239,19 @@ export type OAuth2GranterService<
|
|
|
254
239
|
unknown
|
|
255
240
|
>,
|
|
256
241
|
GRANT_PARAMETERS extends Record<string, unknown> = Record<string, unknown>,
|
|
257
|
-
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
258
242
|
> = {
|
|
259
243
|
type: string;
|
|
260
244
|
authorizer?: {
|
|
261
245
|
responseType: string;
|
|
262
|
-
authorize: OAuth2GranterAuthorize<
|
|
263
|
-
AUTHORIZE_PARAMETERS,
|
|
264
|
-
AUTHENTICATION_DATA
|
|
265
|
-
>;
|
|
246
|
+
authorize: OAuth2GranterAuthorize<AUTHORIZE_PARAMETERS>;
|
|
266
247
|
};
|
|
267
248
|
acknowledger?: {
|
|
268
249
|
acknowledgmentType: string;
|
|
269
|
-
acknowledge: OAuth2GranterAcknowledge<
|
|
270
|
-
ACKNOWLEDGE_PARAMETERS,
|
|
271
|
-
AUTHENTICATION_DATA
|
|
272
|
-
>;
|
|
250
|
+
acknowledge: OAuth2GranterAcknowledge<ACKNOWLEDGE_PARAMETERS>;
|
|
273
251
|
};
|
|
274
252
|
authenticator?: {
|
|
275
253
|
grantType: string;
|
|
276
|
-
authenticate: Oauth2GranterAuthenticate<
|
|
277
|
-
GRANT_PARAMETERS,
|
|
278
|
-
AUTHENTICATION_DATA
|
|
279
|
-
>;
|
|
254
|
+
authenticate: Oauth2GranterAuthenticate<GRANT_PARAMETERS>;
|
|
280
255
|
};
|
|
281
256
|
};
|
|
282
257
|
|
|
@@ -7,7 +7,6 @@ import {
|
|
|
7
7
|
type OAuth2PasswordService,
|
|
8
8
|
type CheckApplicationService,
|
|
9
9
|
} from './oAuth2Granters.js';
|
|
10
|
-
import { type BaseAuthenticationData } from '@whook/authorization';
|
|
11
10
|
|
|
12
11
|
export type OAuth2PasswordGranterDependencies = {
|
|
13
12
|
oAuth2Password: OAuth2PasswordService;
|
|
@@ -19,13 +18,10 @@ export type OAuth2PasswordGranterParameters = {
|
|
|
19
18
|
password: string;
|
|
20
19
|
scope?: string;
|
|
21
20
|
};
|
|
22
|
-
export type OAuth2PasswordGranterService<
|
|
23
|
-
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
24
|
-
> = OAuth2GranterService<
|
|
21
|
+
export type OAuth2PasswordGranterService<> = OAuth2GranterService<
|
|
25
22
|
Record<string, unknown>,
|
|
26
23
|
Record<string, unknown>,
|
|
27
|
-
OAuth2PasswordGranterParameters
|
|
28
|
-
AUTHENTICATION_DATA
|
|
24
|
+
OAuth2PasswordGranterParameters
|
|
29
25
|
>;
|
|
30
26
|
|
|
31
27
|
export default location(
|