@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
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { autoService } from 'knifecycle';
|
|
2
2
|
import { noop } from '@whook/whook';
|
|
3
|
-
import YError from 'yerror';
|
|
3
|
+
import { YError } from 'yerror';
|
|
4
4
|
import type { LogService } from 'common-services';
|
|
5
5
|
import type {
|
|
6
6
|
OAuth2GranterService,
|
|
7
7
|
CheckApplicationService,
|
|
8
|
-
} from './oAuth2Granters';
|
|
8
|
+
} from './oAuth2Granters.js';
|
|
9
9
|
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
10
|
-
import type { OAuth2Options } from '
|
|
10
|
+
import type { OAuth2Options } from '../index.js';
|
|
11
11
|
|
|
12
12
|
export type OAuth2ClientCredentialsGranterDependencies = {
|
|
13
13
|
OAUTH2: OAuth2Options;
|
|
@@ -39,30 +39,34 @@ async function initOAuth2ClientCredentialsGranter({
|
|
|
39
39
|
checkApplication,
|
|
40
40
|
log = noop,
|
|
41
41
|
}: OAuth2ClientCredentialsGranterDependencies): Promise<OAuth2ClientCredentialsGranterService> {
|
|
42
|
-
const authenticateWithClientCredentials:
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
const authenticateWithClientCredentials: NonNullable<
|
|
43
|
+
OAuth2ClientCredentialsGranterService['authenticator']
|
|
44
|
+
>['authenticate'] = async (
|
|
45
|
+
{ scope: demandedScope = '' },
|
|
46
|
+
authenticationData,
|
|
47
|
+
) => {
|
|
48
|
+
// The client must be authenticated
|
|
49
|
+
if (!authenticationData) {
|
|
50
|
+
throw new YError('E_UNAUTHORIZED');
|
|
51
|
+
}
|
|
48
52
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
// Checking the scope and availability of the demanded
|
|
54
|
+
// grant type
|
|
55
|
+
await checkApplication({
|
|
56
|
+
applicationId: authenticationData.applicationId,
|
|
57
|
+
type: 'client_credentials',
|
|
58
|
+
scope: demandedScope,
|
|
59
|
+
});
|
|
56
60
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
};
|
|
61
|
+
return {
|
|
62
|
+
...authenticationData,
|
|
63
|
+
scope: demandedScope
|
|
64
|
+
? demandedScope
|
|
65
|
+
: OAUTH2.defaultToClientScope
|
|
66
|
+
? authenticationData.scope
|
|
67
|
+
: '',
|
|
65
68
|
};
|
|
69
|
+
};
|
|
66
70
|
|
|
67
71
|
log('debug', '👫 - OAuth2ClientCredentialsGranter Service Initialized!');
|
|
68
72
|
|
|
@@ -1,11 +1,29 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { jest } from '@jest/globals';
|
|
2
|
+
import { BaseAuthenticationData } from '@whook/authorization';
|
|
3
|
+
import initOAuth2CodeGranter from './oAuth2CodeGranter.js';
|
|
4
|
+
import {
|
|
5
|
+
CheckApplicationService,
|
|
6
|
+
OAuth2CodeService,
|
|
7
|
+
} from './oAuth2Granters.js';
|
|
2
8
|
|
|
3
9
|
describe('OAuth2CodeGranter', () => {
|
|
4
10
|
const oAuth2Code = {
|
|
5
|
-
create:
|
|
6
|
-
|
|
11
|
+
create:
|
|
12
|
+
jest.fn<
|
|
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
|
+
>(),
|
|
7
25
|
};
|
|
8
|
-
const checkApplication = jest.fn();
|
|
26
|
+
const checkApplication = jest.fn<CheckApplicationService>();
|
|
9
27
|
const log = jest.fn();
|
|
10
28
|
|
|
11
29
|
beforeEach(() => {
|
|
@@ -24,32 +42,37 @@ describe('OAuth2CodeGranter', () => {
|
|
|
24
42
|
|
|
25
43
|
checkApplication.mockResolvedValue({
|
|
26
44
|
redirectURI: 'https://www.example.com',
|
|
45
|
+
type: 'a_type',
|
|
46
|
+
scope: 'a_scope',
|
|
47
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbac0c0',
|
|
27
48
|
});
|
|
28
49
|
oAuth2Code.create.mockResolvedValueOnce('yolo');
|
|
29
50
|
oAuth2Code.check.mockResolvedValueOnce({
|
|
51
|
+
redirectURI: 'https://www.example2.com',
|
|
30
52
|
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
31
53
|
scope: 'user',
|
|
32
54
|
});
|
|
33
55
|
|
|
34
|
-
const authorizerResult = await oAuth2CodeGranter.authorizer
|
|
56
|
+
const authorizerResult = await oAuth2CodeGranter.authorizer?.authorize({
|
|
35
57
|
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
36
58
|
redirectURI: 'https://www.example.com/oauth2/code',
|
|
37
59
|
scope: 'user',
|
|
38
60
|
});
|
|
39
|
-
const acknowledgerResult =
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
61
|
+
const acknowledgerResult =
|
|
62
|
+
await oAuth2CodeGranter.acknowledger?.acknowledge(
|
|
63
|
+
{
|
|
64
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
65
|
+
scope: 'user, admin',
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
69
|
+
redirectURI: 'https://www.example.com/oauth2/code',
|
|
70
|
+
scope: 'user',
|
|
71
|
+
},
|
|
72
|
+
{},
|
|
73
|
+
);
|
|
51
74
|
const authenticatorResult =
|
|
52
|
-
await oAuth2CodeGranter.authenticator
|
|
75
|
+
await oAuth2CodeGranter.authenticator?.authenticate(
|
|
53
76
|
{
|
|
54
77
|
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
55
78
|
redirectURI: 'https://www.example.com/oauth2/code',
|
|
@@ -75,6 +98,7 @@ describe('OAuth2CodeGranter', () => {
|
|
|
75
98
|
},
|
|
76
99
|
"authenticatorResult": Object {
|
|
77
100
|
"applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
|
|
101
|
+
"redirectURI": "https://www.example2.com",
|
|
78
102
|
"scope": "user",
|
|
79
103
|
},
|
|
80
104
|
"authorizerResult": Object {
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { autoService } from 'knifecycle';
|
|
2
2
|
import { noop } from '@whook/whook';
|
|
3
|
-
import YError from 'yerror';
|
|
3
|
+
import { YError } from 'yerror';
|
|
4
4
|
import type { LogService } from 'common-services';
|
|
5
5
|
import type {
|
|
6
6
|
OAuth2GranterService,
|
|
7
7
|
OAuth2CodeService,
|
|
8
8
|
CheckApplicationService,
|
|
9
|
-
} from './oAuth2Granters';
|
|
9
|
+
} from './oAuth2Granters.js';
|
|
10
10
|
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
11
11
|
|
|
12
12
|
export const CODE_GRANTER_TYPE = 'code';
|
|
@@ -39,79 +39,85 @@ async function initOAuth2CodeGranter({
|
|
|
39
39
|
checkApplication,
|
|
40
40
|
log = noop,
|
|
41
41
|
}: OAuth2CodeGranterDependencies): Promise<OAuth2CodeGranterService> {
|
|
42
|
-
const authorizeWithCode:
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
42
|
+
const authorizeWithCode: NonNullable<
|
|
43
|
+
OAuth2CodeGranterService['authorizer']
|
|
44
|
+
>['authorize'] = async ({ clientId, redirectURI, scope = '' }) => {
|
|
45
|
+
const { redirectURI: finalRedirectURI } = await checkApplication({
|
|
46
|
+
applicationId: clientId,
|
|
47
|
+
type: CODE_GRANTER_TYPE,
|
|
48
|
+
redirectURI,
|
|
49
|
+
scope,
|
|
50
|
+
});
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
};
|
|
52
|
+
return {
|
|
53
|
+
applicationId: clientId,
|
|
54
|
+
redirectURI: finalRedirectURI,
|
|
55
|
+
scope,
|
|
56
56
|
};
|
|
57
|
+
};
|
|
57
58
|
|
|
58
59
|
// Authorization Code Response:
|
|
59
60
|
// https://tools.ietf.org/html/rfc6749#section-4.1.2
|
|
60
|
-
const acknowledgeWithCode:
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
const acknowledgeWithCode: NonNullable<
|
|
62
|
+
OAuth2CodeGranterService['acknowledger']
|
|
63
|
+
>['acknowledge'] = async (
|
|
64
|
+
authenticationData,
|
|
65
|
+
{ clientId, redirectURI, scope },
|
|
66
|
+
additionalParameters,
|
|
67
|
+
) => {
|
|
68
|
+
const code = await oAuth2Code.create(
|
|
69
|
+
{ ...authenticationData, applicationId: clientId, scope },
|
|
70
|
+
redirectURI,
|
|
64
71
|
additionalParameters,
|
|
65
|
-
)
|
|
66
|
-
const code = await oAuth2Code.create(
|
|
67
|
-
{ ...authenticationData, applicationId: clientId, scope },
|
|
68
|
-
redirectURI,
|
|
69
|
-
additionalParameters,
|
|
70
|
-
);
|
|
72
|
+
);
|
|
71
73
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
};
|
|
74
|
+
return {
|
|
75
|
+
applicationId: clientId,
|
|
76
|
+
redirectURI,
|
|
77
|
+
scope,
|
|
78
|
+
code,
|
|
78
79
|
};
|
|
80
|
+
};
|
|
79
81
|
|
|
80
|
-
const authenticateWithCode:
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
82
|
+
const authenticateWithCode: NonNullable<
|
|
83
|
+
OAuth2CodeGranterService['authenticator']
|
|
84
|
+
>['authenticate'] = async (
|
|
85
|
+
{ code, clientId, redirectURI },
|
|
86
|
+
authenticationData,
|
|
87
|
+
) => {
|
|
88
|
+
// The client must be authenticated (for now, see below)
|
|
89
|
+
if (!authenticationData) {
|
|
90
|
+
throw new YError('E_UNAUTHORIZED');
|
|
91
|
+
}
|
|
86
92
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
93
|
+
// This check is not really necessary atm but it acts
|
|
94
|
+
// as a reminder that this grant type could be used
|
|
95
|
+
// without authenticating the client. In this
|
|
96
|
+
// scenario, the authenticationData should be deducted
|
|
97
|
+
// from the clientId and the code given in parameters
|
|
98
|
+
// See https://tools.ietf.org/html/rfc6749#section-4.1.3
|
|
99
|
+
if (clientId && clientId !== authenticationData.applicationId) {
|
|
100
|
+
throw new YError(
|
|
101
|
+
'E_UNAUTHORIZED',
|
|
102
|
+
clientId,
|
|
103
|
+
authenticationData.applicationId,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
100
106
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
107
|
+
await checkApplication({
|
|
108
|
+
applicationId: authenticationData.applicationId,
|
|
109
|
+
type: CODE_GRANTER_TYPE,
|
|
110
|
+
scope: '',
|
|
111
|
+
});
|
|
106
112
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
113
|
+
const newAuthenticationData = await oAuth2Code.check(
|
|
114
|
+
authenticationData,
|
|
115
|
+
code,
|
|
116
|
+
redirectURI,
|
|
117
|
+
);
|
|
112
118
|
|
|
113
|
-
|
|
114
|
-
|
|
119
|
+
return newAuthenticationData;
|
|
120
|
+
};
|
|
115
121
|
|
|
116
122
|
log('debug', '👫 - OAuth2CodeGranter Service Initialized!');
|
|
117
123
|
|
|
@@ -193,6 +193,54 @@ export type CheckApplicationService = {
|
|
|
193
193
|
}>;
|
|
194
194
|
};
|
|
195
195
|
|
|
196
|
+
export type OAuth2GranterAuthorize<
|
|
197
|
+
AUTHORIZE_PARAMETERS extends Record<string, unknown> = Record<
|
|
198
|
+
string,
|
|
199
|
+
unknown
|
|
200
|
+
>,
|
|
201
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
202
|
+
> = (
|
|
203
|
+
context: {
|
|
204
|
+
clientId: AUTHENTICATION_DATA['applicationId'];
|
|
205
|
+
redirectURI: string;
|
|
206
|
+
scope: AUTHENTICATION_DATA['scope'];
|
|
207
|
+
},
|
|
208
|
+
authorizeParameters?: AUTHORIZE_PARAMETERS,
|
|
209
|
+
) => Promise<{
|
|
210
|
+
applicationId: AUTHENTICATION_DATA['applicationId'];
|
|
211
|
+
redirectURI: string;
|
|
212
|
+
scope: AUTHENTICATION_DATA['scope'];
|
|
213
|
+
}>;
|
|
214
|
+
|
|
215
|
+
export type Oauth2GranterAuthenticate<
|
|
216
|
+
GRANT_PARAMETERS extends Record<string, unknown> = Record<string, unknown>,
|
|
217
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
218
|
+
> = (
|
|
219
|
+
grantParameters: GRANT_PARAMETERS,
|
|
220
|
+
authenticationData: AUTHENTICATION_DATA,
|
|
221
|
+
) => Promise<AUTHENTICATION_DATA>;
|
|
222
|
+
|
|
223
|
+
export type OAuth2GranterAcknowledge<
|
|
224
|
+
ACKNOWLEDGE_PARAMETERS extends Record<string, unknown> = Record<
|
|
225
|
+
string,
|
|
226
|
+
unknown
|
|
227
|
+
>,
|
|
228
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
229
|
+
> = (
|
|
230
|
+
authenticationData: AUTHENTICATION_DATA,
|
|
231
|
+
acknowledgeParameters: {
|
|
232
|
+
clientId: AUTHENTICATION_DATA['applicationId'];
|
|
233
|
+
redirectURI: string;
|
|
234
|
+
scope: AUTHENTICATION_DATA['applicationId'];
|
|
235
|
+
},
|
|
236
|
+
additionalParameters: ACKNOWLEDGE_PARAMETERS,
|
|
237
|
+
) => Promise<
|
|
238
|
+
AUTHENTICATION_DATA & {
|
|
239
|
+
redirectURI: string;
|
|
240
|
+
[name: string]: unknown;
|
|
241
|
+
}
|
|
242
|
+
>;
|
|
243
|
+
|
|
196
244
|
export type OAuth2GranterService<
|
|
197
245
|
AUTHORIZE_PARAMETERS extends Record<string, unknown> = Record<
|
|
198
246
|
string,
|
|
@@ -208,42 +256,24 @@ export type OAuth2GranterService<
|
|
|
208
256
|
type: string;
|
|
209
257
|
authorizer?: {
|
|
210
258
|
responseType: string;
|
|
211
|
-
authorize:
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
scope: AUTHENTICATION_DATA['scope'];
|
|
216
|
-
},
|
|
217
|
-
authorizeParameters?: AUTHORIZE_PARAMETERS,
|
|
218
|
-
) => Promise<{
|
|
219
|
-
applicationId: AUTHENTICATION_DATA['applicationId'];
|
|
220
|
-
redirectURI: string;
|
|
221
|
-
scope: AUTHENTICATION_DATA['scope'];
|
|
222
|
-
}>;
|
|
259
|
+
authorize: OAuth2GranterAuthorize<
|
|
260
|
+
AUTHORIZE_PARAMETERS,
|
|
261
|
+
AUTHENTICATION_DATA
|
|
262
|
+
>;
|
|
223
263
|
};
|
|
224
264
|
acknowledger?: {
|
|
225
265
|
acknowledgmentType: string;
|
|
226
|
-
acknowledge:
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
clientId: AUTHENTICATION_DATA['applicationId'];
|
|
230
|
-
redirectURI: string;
|
|
231
|
-
scope: AUTHENTICATION_DATA['applicationId'];
|
|
232
|
-
},
|
|
233
|
-
additionalParameters: ACKNOWLEDGE_PARAMETERS,
|
|
234
|
-
) => Promise<
|
|
235
|
-
AUTHENTICATION_DATA & {
|
|
236
|
-
redirectURI: string;
|
|
237
|
-
[name: string]: unknown;
|
|
238
|
-
}
|
|
266
|
+
acknowledge: OAuth2GranterAcknowledge<
|
|
267
|
+
ACKNOWLEDGE_PARAMETERS,
|
|
268
|
+
AUTHENTICATION_DATA
|
|
239
269
|
>;
|
|
240
270
|
};
|
|
241
271
|
authenticator?: {
|
|
242
272
|
grantType: string;
|
|
243
|
-
authenticate:
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
273
|
+
authenticate: Oauth2GranterAuthenticate<
|
|
274
|
+
GRANT_PARAMETERS,
|
|
275
|
+
AUTHENTICATION_DATA
|
|
276
|
+
>;
|
|
247
277
|
};
|
|
248
278
|
};
|
|
249
279
|
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { autoService } from 'knifecycle';
|
|
2
2
|
import { noop } from '@whook/whook';
|
|
3
|
-
import YError from 'yerror';
|
|
3
|
+
import { YError } from 'yerror';
|
|
4
4
|
import type { LogService } from 'common-services';
|
|
5
5
|
import type {
|
|
6
6
|
OAuth2GranterService,
|
|
7
7
|
OAuth2PasswordService,
|
|
8
8
|
CheckApplicationService,
|
|
9
|
-
} from './oAuth2Granters';
|
|
9
|
+
} from './oAuth2Granters.js';
|
|
10
10
|
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
11
11
|
|
|
12
12
|
export type OAuth2PasswordGranterDependencies = {
|
|
@@ -37,31 +37,32 @@ async function initOAuth2PasswordGranter({
|
|
|
37
37
|
checkApplication,
|
|
38
38
|
log = noop,
|
|
39
39
|
}: OAuth2PasswordGranterDependencies): Promise<OAuth2PasswordGranterService> {
|
|
40
|
-
const authenticateWithPassword:
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
40
|
+
const authenticateWithPassword: NonNullable<
|
|
41
|
+
OAuth2PasswordGranterService['authenticator']
|
|
42
|
+
>['authenticate'] = async (
|
|
43
|
+
{ username, password, scope: demandedScope = '' },
|
|
44
|
+
authenticationData,
|
|
45
|
+
) => {
|
|
46
|
+
// The client must be authenticated
|
|
47
|
+
if (!authenticationData) {
|
|
48
|
+
throw new YError('E_UNAUTHORIZED');
|
|
49
|
+
}
|
|
49
50
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
await checkApplication({
|
|
52
|
+
applicationId: authenticationData.applicationId,
|
|
53
|
+
type: 'password',
|
|
54
|
+
scope: demandedScope,
|
|
55
|
+
});
|
|
55
56
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
const finalAuthenticationData = await oAuth2Password.check(
|
|
58
|
+
authenticationData,
|
|
59
|
+
username,
|
|
60
|
+
password,
|
|
61
|
+
demandedScope,
|
|
62
|
+
);
|
|
62
63
|
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
return finalAuthenticationData;
|
|
65
|
+
};
|
|
65
66
|
|
|
66
67
|
log('debug', '👫 - OAuth2PasswordGranter Service Initialized!');
|
|
67
68
|
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { autoService } from 'knifecycle';
|
|
2
2
|
import { noop } from '@whook/whook';
|
|
3
|
-
import YError from 'yerror';
|
|
3
|
+
import { YError } from 'yerror';
|
|
4
4
|
import type { LogService } from 'common-services';
|
|
5
5
|
import type {
|
|
6
6
|
OAuth2GranterService,
|
|
7
7
|
CheckApplicationService,
|
|
8
8
|
OAuth2RefreshTokenService,
|
|
9
|
-
} from './oAuth2Granters';
|
|
9
|
+
} from './oAuth2Granters.js';
|
|
10
10
|
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
11
11
|
|
|
12
12
|
export type OAuth2RefreshTokenGranterDependencies = {
|
|
@@ -36,34 +36,38 @@ async function initOAuth2RefreshTokenGranter({
|
|
|
36
36
|
oAuth2RefreshToken,
|
|
37
37
|
log = noop,
|
|
38
38
|
}: OAuth2RefreshTokenGranterDependencies): Promise<OAuth2RefreshTokenGranterService> {
|
|
39
|
-
const authenticateWithRefreshToken:
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
const authenticateWithRefreshToken: NonNullable<
|
|
40
|
+
OAuth2RefreshTokenGranterService['authenticator']
|
|
41
|
+
>['authenticate'] = async (
|
|
42
|
+
{ refreshToken, scope: demandedScope = '' },
|
|
43
|
+
authenticationData,
|
|
44
|
+
) => {
|
|
45
|
+
try {
|
|
46
|
+
// The client must be authenticated
|
|
47
|
+
if (!authenticationData) {
|
|
48
|
+
throw new YError('E_UNAUTHORIZED');
|
|
49
|
+
}
|
|
46
50
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
await checkApplication({
|
|
52
|
+
applicationId: authenticationData.applicationId,
|
|
53
|
+
type: 'refresh',
|
|
54
|
+
scope: demandedScope,
|
|
55
|
+
});
|
|
52
56
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
const newAuthenticationData = await oAuth2RefreshToken.check(
|
|
58
|
+
authenticationData,
|
|
59
|
+
refreshToken,
|
|
60
|
+
demandedScope,
|
|
61
|
+
);
|
|
58
62
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
throw err;
|
|
63
|
+
return newAuthenticationData;
|
|
64
|
+
} catch (err) {
|
|
65
|
+
if ((err as YError).code === 'E_BAD_TOKEN') {
|
|
66
|
+
throw YError.wrap(err as Error, 'E_BAD_REFRESH_TOKEN');
|
|
65
67
|
}
|
|
66
|
-
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
67
71
|
|
|
68
72
|
log('debug', '👫 - OAuth2RefreshTokenGranter Service Initialized!');
|
|
69
73
|
|