@whook/oauth2 18.0.3 → 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 +36 -46
- package/dist/index.d.ts +8 -8
- 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 +39 -36
- 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 +43 -42
- 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 +36 -34
- 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 +8 -8
- package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
- package/dist/services/oAuth2CodeGranter.d.ts +4 -5
- 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 +25 -25
- package/dist/services/oAuth2Granters.js +1 -1
- package/dist/services/oAuth2Granters.js.map +1 -1
- package/dist/services/oAuth2PasswordGranter.d.ts +4 -5
- package/dist/services/oAuth2PasswordGranter.js.map +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.d.ts +4 -5
- package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
- package/dist/services/oAuth2TokenGranter.d.ts +4 -5
- package/dist/services/oAuth2TokenGranter.js +4 -0
- package/dist/services/oAuth2TokenGranter.js.map +1 -1
- package/package.json +10 -9
- package/src/__snapshots__/index.test.ts.snap +1 -0
- package/src/index.test.ts +39 -52
- package/src/index.ts +20 -20
- package/src/{handlers → routes}/getOAuth2Authorize.test.ts +18 -14
- package/src/routes/getOAuth2Authorize.ts +217 -0
- package/src/{handlers → routes}/postAuthLogin.test.ts +3 -2
- package/src/routes/postAuthLogin.ts +118 -0
- package/src/{handlers → routes}/postAuthLogout.test.ts +2 -2
- package/src/routes/postAuthLogout.ts +38 -0
- package/src/{handlers → routes}/postAuthRefresh.test.ts +3 -2
- package/src/routes/postAuthRefresh.ts +138 -0
- package/src/{handlers → routes}/postOAuth2Acknowledge.test.ts +23 -39
- package/src/{handlers → routes}/postOAuth2Acknowledge.ts +88 -96
- package/src/{handlers → routes}/postOAuth2Token.test.ts +12 -11
- package/src/{handlers → routes}/postOAuth2Token.ts +124 -129
- package/src/services/authCookies.test.ts +1 -1
- package/src/services/authCookies.ts +3 -5
- package/src/services/oAuth2ClientCredentialsGranter.ts +10 -15
- package/src/services/oAuth2CodeGranter.test.ts +12 -21
- package/src/services/oAuth2CodeGranter.ts +9 -11
- package/src/services/oAuth2Granters.ts +35 -57
- package/src/services/oAuth2PasswordGranter.ts +7 -11
- package/src/services/oAuth2RefreshTokenGranter.ts +7 -11
- package/src/services/oAuth2TokenGranter.ts +12 -12
- 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/getOAuth2Authorize.ts +0 -214
- package/src/handlers/postAuthLogin.ts +0 -123
- package/src/handlers/postAuthLogout.ts +0 -38
- 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
|
-
import { noop } from '@whook/whook';
|
|
4
3
|
import { YError, printStackTrace } from 'yerror';
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
WhookAPISchemaDefinition,
|
|
10
|
-
|
|
4
|
+
import { type LogService, type TimeService } from 'common-services';
|
|
5
|
+
import {
|
|
6
|
+
noop,
|
|
7
|
+
refersTo,
|
|
8
|
+
type WhookAPISchemaDefinition,
|
|
9
|
+
type WhookRouteDefinition,
|
|
11
10
|
} from '@whook/whook';
|
|
12
|
-
import
|
|
13
|
-
OAuth2GranterService,
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
import {
|
|
12
|
+
type OAuth2GranterService,
|
|
13
|
+
type OAuth2RefreshTokenService,
|
|
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,6 +1,6 @@
|
|
|
1
1
|
import { describe, test, expect } from '@jest/globals';
|
|
2
2
|
import initAuthCookies from './authCookies.js';
|
|
3
|
-
import type
|
|
3
|
+
import { type AuthCookiesConfig } from './authCookies.js';
|
|
4
4
|
|
|
5
5
|
describe('authCookies', () => {
|
|
6
6
|
describe('.build()', () => {
|
|
@@ -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 = {
|
|
@@ -1,33 +1,28 @@
|
|
|
1
1
|
import { autoService, location } from 'knifecycle';
|
|
2
2
|
import { noop } from '@whook/whook';
|
|
3
3
|
import { YError } from 'yerror';
|
|
4
|
-
import type
|
|
5
|
-
import
|
|
6
|
-
OAuth2GranterService,
|
|
7
|
-
CheckApplicationService,
|
|
4
|
+
import { type LogService } from 'common-services';
|
|
5
|
+
import {
|
|
6
|
+
type OAuth2GranterService,
|
|
7
|
+
type CheckApplicationService,
|
|
8
8
|
} from './oAuth2Granters.js';
|
|
9
|
-
import type
|
|
10
|
-
import type
|
|
9
|
+
import { type WhookAuthenticationData } from '@whook/authorization';
|
|
10
|
+
import { type OAuth2Options } from '../services/oAuth2Granters.js';
|
|
11
11
|
|
|
12
12
|
export type OAuth2ClientCredentialsGranterDependencies = {
|
|
13
13
|
OAUTH2: OAuth2Options;
|
|
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({
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { autoService, location } from 'knifecycle';
|
|
2
2
|
import { noop } from '@whook/whook';
|
|
3
3
|
import { YError } from 'yerror';
|
|
4
|
-
import type
|
|
5
|
-
import
|
|
6
|
-
OAuth2GranterService,
|
|
7
|
-
OAuth2CodeService,
|
|
8
|
-
CheckApplicationService,
|
|
4
|
+
import { type LogService } from 'common-services';
|
|
5
|
+
import {
|
|
6
|
+
type OAuth2GranterService,
|
|
7
|
+
type OAuth2CodeService,
|
|
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,
|