@whook/oauth2 25.2.0 → 26.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 +147 -98
- package/dist/index.d.ts +30 -11
- package/dist/index.js +15 -8
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +1258 -399
- package/dist/index.test.js.map +1 -1
- package/dist/libs/clients.d.ts +2 -0
- package/dist/libs/clients.js +13 -0
- package/dist/libs/clients.js.map +1 -0
- package/dist/libs/errors.d.ts +113 -0
- package/dist/libs/errors.js +178 -0
- package/dist/libs/errors.js.map +1 -0
- package/dist/libs/grants.d.ts +2 -0
- package/dist/libs/grants.js +10 -0
- package/dist/libs/grants.js.map +1 -0
- package/dist/libs/redirectURI.d.ts +7 -0
- package/dist/libs/redirectURI.js +78 -0
- package/dist/libs/redirectURI.js.map +1 -0
- package/dist/libs/schemas.d.ts +23 -0
- package/dist/libs/schemas.js +25 -0
- package/dist/libs/schemas.js.map +1 -0
- package/dist/libs/scopes.d.ts +16 -0
- package/dist/libs/scopes.js +37 -0
- package/dist/libs/scopes.js.map +1 -1
- package/dist/libs/utils.d.ts +2 -0
- package/dist/libs/utils.js +17 -0
- package/dist/libs/utils.js.map +1 -0
- package/dist/libs/verifier.d.ts +7 -0
- package/dist/libs/verifier.js +37 -0
- package/dist/libs/verifier.js.map +1 -0
- package/dist/libs/verifier.test.js +21 -0
- package/dist/libs/verifier.test.js.map +1 -0
- package/dist/routes/getOAuth2Authorize.d.ts +13 -50
- package/dist/routes/getOAuth2Authorize.js +60 -86
- package/dist/routes/getOAuth2Authorize.js.map +1 -1
- package/dist/routes/getOAuth2Authorize.test.js +147 -68
- package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.d.ts +8 -8
- package/dist/routes/getOAuth2WellKnownMetadata.js +20 -9
- package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.test.js +38 -37
- package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.d.ts +3 -6
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js +3 -6
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js.map +1 -1
- package/dist/routes/postAuthLogin.d.ts +14 -9
- package/dist/routes/postAuthLogin.js +10 -5
- package/dist/routes/postAuthLogin.js.map +1 -1
- package/dist/routes/postAuthLogin.test.js +79 -55
- package/dist/routes/postAuthLogin.test.js.map +1 -1
- package/dist/routes/postAuthLogout.d.ts +5 -2
- package/dist/routes/postAuthLogout.js +12 -7
- package/dist/routes/postAuthLogout.js.map +1 -1
- package/dist/routes/postAuthLogout.test.js +35 -17
- package/dist/routes/postAuthLogout.test.js.map +1 -1
- package/dist/routes/postAuthRefresh.d.ts +14 -9
- package/dist/routes/postAuthRefresh.js +10 -6
- package/dist/routes/postAuthRefresh.js.map +1 -1
- package/dist/routes/postAuthRefresh.test.js +90 -59
- package/dist/routes/postAuthRefresh.test.js.map +1 -1
- package/dist/routes/postOAuth2Acknowledge.d.ts +20 -10
- package/dist/routes/postOAuth2Acknowledge.js +54 -38
- package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
- package/dist/routes/postOAuth2Acknowledge.test.js +137 -72
- package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -1
- package/dist/routes/postOAuth2Revoke.d.ts +99 -0
- package/dist/routes/postOAuth2Revoke.js +102 -0
- package/dist/routes/postOAuth2Revoke.js.map +1 -0
- package/dist/routes/postOAuth2Revoke.test.d.ts +1 -0
- package/dist/routes/postOAuth2Revoke.test.js +161 -0
- package/dist/routes/postOAuth2Revoke.test.js.map +1 -0
- package/dist/routes/postOAuth2Token.d.ts +35 -108
- package/dist/routes/postOAuth2Token.js +53 -114
- package/dist/routes/postOAuth2Token.js.map +1 -1
- package/dist/routes/postOAuth2Token.test.js +493 -61
- package/dist/routes/postOAuth2Token.test.js.map +1 -1
- package/dist/services/authCookies.d.ts +9 -13
- package/dist/services/authCookies.js +1 -1
- package/dist/services/authCookies.js.map +1 -1
- package/dist/services/authCookies.test.js.map +1 -1
- package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +129 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.js +221 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.d.ts +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js +474 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -0
- package/dist/services/oAuth2ClientCredentialsGranter.d.ts +28 -12
- package/dist/services/oAuth2ClientCredentialsGranter.js +39 -21
- package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
- package/dist/services/oAuth2ClientCredentialsGranter.test.d.ts +1 -0
- package/dist/services/oAuth2ClientCredentialsGranter.test.js +265 -0
- package/dist/services/oAuth2ClientCredentialsGranter.test.js.map +1 -0
- package/dist/services/oAuth2Granters.d.ts +81 -86
- package/dist/services/oAuth2Granters.js +2 -67
- package/dist/services/oAuth2Granters.js.map +1 -1
- package/dist/services/oAuth2ImplicitGranter.d.ts +38 -0
- package/dist/services/oAuth2ImplicitGranter.js +72 -0
- package/dist/services/oAuth2ImplicitGranter.js.map +1 -0
- package/dist/services/oAuth2ImplicitGranter.test.d.ts +1 -0
- package/dist/services/oAuth2ImplicitGranter.test.js +727 -0
- package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -0
- package/dist/services/oAuth2PasswordGranter.d.ts +43 -10
- package/dist/services/oAuth2PasswordGranter.js +53 -18
- package/dist/services/oAuth2PasswordGranter.js.map +1 -1
- package/dist/services/oAuth2PasswordGranter.test.d.ts +1 -0
- package/dist/services/oAuth2PasswordGranter.test.js +455 -0
- package/dist/services/oAuth2PasswordGranter.test.js.map +1 -0
- package/dist/services/oAuth2RefreshTokenGranter.d.ts +42 -9
- package/dist/services/oAuth2RefreshTokenGranter.js +60 -21
- package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.test.d.ts +1 -0
- package/dist/services/oAuth2RefreshTokenGranter.test.js +403 -0
- package/dist/services/oAuth2RefreshTokenGranter.test.js.map +1 -0
- package/package.json +11 -11
- package/src/index.test.ts +1314 -404
- package/src/index.ts +46 -53
- package/src/libs/clients.ts +22 -0
- package/src/libs/errors.ts +325 -0
- package/src/libs/grants.ts +15 -0
- package/src/libs/redirectURI.ts +120 -0
- package/src/libs/schemas.ts +27 -0
- package/src/libs/scopes.ts +49 -0
- package/src/libs/utils.ts +24 -0
- package/src/libs/verifier.test.ts +37 -0
- package/src/libs/verifier.ts +57 -0
- package/src/routes/getOAuth2Authorize.test.ts +179 -96
- package/src/routes/getOAuth2Authorize.ts +97 -124
- package/src/routes/getOAuth2WellKnownMetadata.test.ts +58 -60
- package/src/routes/getOAuth2WellKnownMetadata.ts +26 -11
- package/src/routes/getOAuth2WellKnownProtectedResourceMetadata.ts +4 -6
- package/src/routes/postAuthLogin.test.ts +86 -57
- package/src/routes/postAuthLogin.ts +24 -14
- package/src/routes/postAuthLogout.test.ts +42 -18
- package/src/routes/postAuthLogout.ts +22 -8
- package/src/routes/postAuthRefresh.test.ts +98 -61
- package/src/routes/postAuthRefresh.ts +25 -13
- package/src/routes/postOAuth2Acknowledge.test.ts +169 -110
- package/src/routes/postOAuth2Acknowledge.ts +101 -61
- package/src/routes/postOAuth2Revoke.test.ts +179 -0
- package/src/routes/postOAuth2Revoke.ts +156 -0
- package/src/routes/postOAuth2Token.test.ts +570 -108
- package/src/routes/postOAuth2Token.ts +92 -136
- package/src/services/authCookies.test.ts +5 -5
- package/src/services/authCookies.ts +16 -18
- package/src/services/oAuth2AuthorizationCodeGranter.test.ts +564 -0
- package/src/services/oAuth2AuthorizationCodeGranter.ts +388 -0
- package/src/services/oAuth2ClientCredentialsGranter.test.ts +303 -0
- package/src/services/oAuth2ClientCredentialsGranter.ts +68 -43
- package/src/services/oAuth2Granters.ts +101 -195
- package/src/services/oAuth2ImplicitGranter.test.ts +811 -0
- package/src/services/oAuth2ImplicitGranter.ts +163 -0
- package/src/services/oAuth2PasswordGranter.test.ts +493 -0
- package/src/services/oAuth2PasswordGranter.ts +116 -40
- package/src/services/oAuth2RefreshTokenGranter.test.ts +441 -0
- package/src/services/oAuth2RefreshTokenGranter.ts +118 -51
- package/src/types.d.ts +1 -0
- package/dist/services/oAuth2CodeGranter.d.ts +0 -41
- package/dist/services/oAuth2CodeGranter.js +0 -105
- package/dist/services/oAuth2CodeGranter.js.map +0 -1
- package/dist/services/oAuth2CodeGranter.test.js +0 -123
- package/dist/services/oAuth2CodeGranter.test.js.map +0 -1
- package/dist/services/oAuth2TokenGranter.d.ts +0 -22
- package/dist/services/oAuth2TokenGranter.js +0 -64
- package/dist/services/oAuth2TokenGranter.js.map +0 -1
- package/src/__snapshots__/index.test.ts.snap +0 -401
- package/src/routes/__snapshots__/getOAuth2Authorize.test.ts.snap +0 -37
- package/src/routes/__snapshots__/postOAuth2Acknowledge.test.ts.snap +0 -61
- package/src/routes/__snapshots__/postOAuth2Token.test.ts.snap +0 -52
- package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +0 -52
- package/src/services/oAuth2CodeGranter.test.ts +0 -176
- package/src/services/oAuth2CodeGranter.ts +0 -210
- package/src/services/oAuth2TokenGranter.ts +0 -119
- /package/dist/{services/oAuth2CodeGranter.test.d.ts → libs/verifier.test.d.ts} +0 -0
|
@@ -2,115 +2,123 @@ import { describe, test, beforeEach, jest, expect } from '@jest/globals';
|
|
|
2
2
|
import initPostOAuth2Acknowledge from './postOAuth2Acknowledge.js';
|
|
3
3
|
import { YError } from 'yerror';
|
|
4
4
|
import { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';
|
|
5
|
-
import {
|
|
6
|
-
|
|
5
|
+
import {
|
|
6
|
+
type WhookOAuth2GranterDefinitions,
|
|
7
|
+
type WhookOAuth2Options,
|
|
8
|
+
} from '../services/oAuth2Granters.js';
|
|
9
|
+
import { OAUTH2_ERRORS_DESCRIPTORS } from '../libs/errors.js';
|
|
7
10
|
import { type LogService } from 'common-services';
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
import {
|
|
12
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
13
|
+
type WhookOAuth2GranterService,
|
|
14
|
+
} from '../services/oAuth2Granters.js';
|
|
15
|
+
import { type WhookOAuth2AuthorizationCodeGranterService } from '../services/oAuth2AuthorizationCodeGranter.js';
|
|
16
|
+
import { type WhookOAuth2ImplicitGranterService } from '../services/oAuth2ImplicitGranter.js';
|
|
11
17
|
|
|
12
18
|
describe('postOAuth2Acknowledge', () => {
|
|
19
|
+
const OAUTH2: WhookOAuth2Options = {
|
|
20
|
+
authenticateURL: 'https://auth.example.com/sign_in',
|
|
21
|
+
allowedScopes: ['user'],
|
|
22
|
+
rootClientId: 'an_app_id',
|
|
23
|
+
};
|
|
13
24
|
const ERRORS_DESCRIPTORS = {
|
|
14
25
|
...DEFAULT_ERRORS_DESCRIPTORS,
|
|
15
26
|
...OAUTH2_ERRORS_DESCRIPTORS,
|
|
16
27
|
};
|
|
17
|
-
const
|
|
28
|
+
const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
|
|
18
29
|
const log = jest.fn<LogService>();
|
|
19
30
|
const codeGranter = {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
>
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
authenticate:
|
|
38
|
-
jest.fn<
|
|
39
|
-
NonNullable<OAuth2CodeGranterService['authenticator']>['authenticate']
|
|
40
|
-
>(),
|
|
41
|
-
},
|
|
42
|
-
};
|
|
31
|
+
grantType: 'authorization_code',
|
|
32
|
+
responseType: 'code',
|
|
33
|
+
issuesRefreshToken: true,
|
|
34
|
+
|
|
35
|
+
authorize:
|
|
36
|
+
jest.fn<
|
|
37
|
+
NonNullable<WhookOAuth2AuthorizationCodeGranterService['authorize']>
|
|
38
|
+
>(),
|
|
39
|
+
acknowledge:
|
|
40
|
+
jest.fn<
|
|
41
|
+
NonNullable<WhookOAuth2AuthorizationCodeGranterService['acknowledge']>
|
|
42
|
+
>(),
|
|
43
|
+
authenticate:
|
|
44
|
+
jest.fn<
|
|
45
|
+
NonNullable<WhookOAuth2AuthorizationCodeGranterService['authenticate']>
|
|
46
|
+
>(),
|
|
47
|
+
} satisfies WhookOAuth2AuthorizationCodeGranterService;
|
|
43
48
|
const tokenGranter = {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
>(),
|
|
53
|
-
},
|
|
54
|
-
acknowledger: {
|
|
55
|
-
acknowledgmentType: 'token',
|
|
56
|
-
acknowledge:
|
|
57
|
-
jest.fn<
|
|
58
|
-
NonNullable<OAuth2TokenGranterService['acknowledger']>['acknowledge']
|
|
59
|
-
>(),
|
|
60
|
-
},
|
|
61
|
-
};
|
|
49
|
+
grantType: 'implicit',
|
|
50
|
+
responseType: 'token',
|
|
51
|
+
issuesRefreshToken: false,
|
|
52
|
+
authorize:
|
|
53
|
+
jest.fn<NonNullable<WhookOAuth2ImplicitGranterService['authorize']>>(),
|
|
54
|
+
acknowledge:
|
|
55
|
+
jest.fn<NonNullable<WhookOAuth2ImplicitGranterService['acknowledge']>>(),
|
|
56
|
+
} satisfies WhookOAuth2ImplicitGranterService;
|
|
62
57
|
const oAuth2Granters = [
|
|
63
58
|
codeGranter,
|
|
64
59
|
tokenGranter,
|
|
65
|
-
] as unknown as
|
|
66
|
-
Record<string, unknown>,
|
|
67
|
-
Record<string, unknown>,
|
|
68
|
-
Record<string, unknown>
|
|
69
|
-
>[];
|
|
60
|
+
] as unknown as WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
|
|
70
61
|
|
|
71
62
|
beforeEach(() => {
|
|
72
|
-
|
|
63
|
+
readClientGrants.mockReset();
|
|
73
64
|
log.mockReset();
|
|
74
65
|
[
|
|
75
|
-
codeGranter.
|
|
76
|
-
codeGranter.
|
|
77
|
-
codeGranter.
|
|
78
|
-
tokenGranter.
|
|
79
|
-
tokenGranter.
|
|
66
|
+
codeGranter.authorize,
|
|
67
|
+
codeGranter.acknowledge,
|
|
68
|
+
codeGranter.authenticate,
|
|
69
|
+
tokenGranter.authorize,
|
|
70
|
+
tokenGranter.acknowledge,
|
|
80
71
|
].forEach((mock) => mock.mockReset());
|
|
81
72
|
});
|
|
82
73
|
|
|
83
74
|
test('should redirect', async () => {
|
|
84
75
|
[
|
|
85
|
-
codeGranter.
|
|
86
|
-
codeGranter.
|
|
87
|
-
tokenGranter.
|
|
88
|
-
tokenGranter.
|
|
76
|
+
codeGranter.authorize,
|
|
77
|
+
codeGranter.authenticate,
|
|
78
|
+
tokenGranter.authorize,
|
|
79
|
+
tokenGranter.acknowledge,
|
|
89
80
|
].forEach((mock) =>
|
|
90
81
|
mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
|
|
91
82
|
);
|
|
92
|
-
codeGranter.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
83
|
+
codeGranter.acknowledge.mockResolvedValueOnce({
|
|
84
|
+
acknowledgedAuthenticationData: {
|
|
85
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
86
|
+
scopes: ['user'],
|
|
96
87
|
userId: '1',
|
|
97
88
|
},
|
|
98
|
-
|
|
89
|
+
acknowledgedRedirectURI:
|
|
90
|
+
'https://www.example.com?my_custom_parameter=a_custom_value',
|
|
91
|
+
acknowledgedScopes: ['auth'],
|
|
99
92
|
acknowledgedData: {
|
|
100
|
-
code: '
|
|
93
|
+
code: 'an_authorization_code',
|
|
94
|
+
codeChallenge: 'a_code_challenge',
|
|
95
|
+
codeChallengeMethod: 'plain',
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
readClientGrants.mockResolvedValueOnce({
|
|
99
|
+
allowedGrantTypes: ['password'],
|
|
100
|
+
allowedScopes: [],
|
|
101
|
+
allowedRedirectURIS: [],
|
|
102
|
+
isPublicClient: false,
|
|
103
|
+
canAcknowledge: true,
|
|
104
|
+
authenticationData: {
|
|
105
|
+
clientId: 'root_app_id',
|
|
106
|
+
scopes: [],
|
|
107
|
+
userId: '1',
|
|
101
108
|
},
|
|
102
109
|
});
|
|
103
110
|
|
|
104
111
|
const postOAuth2Acknowledge = await initPostOAuth2Acknowledge({
|
|
105
112
|
ERRORS_DESCRIPTORS,
|
|
113
|
+
OAUTH2,
|
|
106
114
|
oAuth2Granters,
|
|
107
|
-
|
|
115
|
+
readClientGrants,
|
|
108
116
|
log,
|
|
109
117
|
});
|
|
110
118
|
const response = await postOAuth2Acknowledge({
|
|
111
119
|
authenticationData: {
|
|
112
|
-
|
|
113
|
-
|
|
120
|
+
clientId: 'an_app_id',
|
|
121
|
+
scopes: ['auth'],
|
|
114
122
|
userId: '1',
|
|
115
123
|
},
|
|
116
124
|
body: {
|
|
@@ -124,53 +132,91 @@ describe('postOAuth2Acknowledge', () => {
|
|
|
124
132
|
});
|
|
125
133
|
expect({
|
|
126
134
|
response,
|
|
135
|
+
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
|
|
136
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
137
|
+
codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
|
|
138
|
+
codeGranterAcknowledgerAcknowledgeCalls:
|
|
139
|
+
codeGranter.acknowledge.mock.calls,
|
|
140
|
+
codeGranterAuthenticatorAuthenticateCalls:
|
|
141
|
+
codeGranter.authenticate.mock.calls,
|
|
142
|
+
tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
|
|
143
|
+
tokenGranterAcknowledgerAcknowledgeCalls:
|
|
144
|
+
tokenGranter.acknowledge.mock.calls,
|
|
127
145
|
}).toMatchInlineSnapshot(`
|
|
128
146
|
{
|
|
147
|
+
"codeGranterAcknowledgerAcknowledgeCalls": [
|
|
148
|
+
[
|
|
149
|
+
{
|
|
150
|
+
"clientId": "an_app_id",
|
|
151
|
+
"scopes": [
|
|
152
|
+
"auth",
|
|
153
|
+
],
|
|
154
|
+
"userId": "1",
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
"clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
|
|
158
|
+
"demandedRedirectURI": "https://www.example.com",
|
|
159
|
+
"demandedScopes": [
|
|
160
|
+
"user",
|
|
161
|
+
],
|
|
162
|
+
},
|
|
163
|
+
{},
|
|
164
|
+
],
|
|
165
|
+
],
|
|
166
|
+
"codeGranterAuthenticatorAuthenticateCalls": [],
|
|
167
|
+
"codeGranterAuthorizerAuthorizeCalls": [],
|
|
168
|
+
"logCalls": [],
|
|
169
|
+
"readClientGrantsCalls": [
|
|
170
|
+
[
|
|
171
|
+
"an_app_id",
|
|
172
|
+
],
|
|
173
|
+
],
|
|
129
174
|
"response": {
|
|
130
175
|
"headers": {
|
|
131
|
-
"location": "https://www.example.com/?my_custom_parameter=a_custom_value&client_id=abbacaca-abba-caca-abba-cacaabbacaca&scope=user&state=bancal&code=
|
|
176
|
+
"location": "https://www.example.com/?my_custom_parameter=a_custom_value&client_id=abbacaca-abba-caca-abba-cacaabbacaca&scope=user&state=bancal&code=an_authorization_code&code_challenge=a_code_challenge&code_challenge_method=plain",
|
|
132
177
|
},
|
|
133
|
-
"status":
|
|
178
|
+
"status": 201,
|
|
134
179
|
},
|
|
180
|
+
"tokenGranterAcknowledgerAcknowledgeCalls": [],
|
|
181
|
+
"tokenGranterAuthorizerAuthorizeCalls": [],
|
|
135
182
|
}
|
|
136
183
|
`);
|
|
137
|
-
expect({
|
|
138
|
-
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
|
|
139
|
-
checkApplicationCalls: checkApplication.mock.calls,
|
|
140
|
-
codeGranterAuthorizerAuthorizeCalls:
|
|
141
|
-
codeGranter.authorizer.authorize.mock.calls,
|
|
142
|
-
codeGranterAcknowledgerAcknowledgeCalls:
|
|
143
|
-
codeGranter.acknowledger.acknowledge.mock.calls,
|
|
144
|
-
codeGranterAuthenticatorAuthenticateCalls:
|
|
145
|
-
codeGranter.authenticator.authenticate.mock.calls,
|
|
146
|
-
tokenGranterAuthorizerAuthorizeCalls:
|
|
147
|
-
tokenGranter.authorizer.authorize.mock.calls,
|
|
148
|
-
tokenGranterAcknowledgerAcknowledgeCalls:
|
|
149
|
-
tokenGranter.acknowledger.acknowledge.mock.calls,
|
|
150
|
-
}).toMatchSnapshot();
|
|
151
184
|
});
|
|
152
185
|
|
|
153
186
|
test('should redirect errors too', async () => {
|
|
154
187
|
[
|
|
155
|
-
codeGranter.
|
|
156
|
-
codeGranter.
|
|
157
|
-
tokenGranter.
|
|
158
|
-
tokenGranter.
|
|
159
|
-
codeGranter.
|
|
188
|
+
codeGranter.authorize,
|
|
189
|
+
codeGranter.authenticate,
|
|
190
|
+
tokenGranter.authorize,
|
|
191
|
+
tokenGranter.acknowledge,
|
|
192
|
+
codeGranter.acknowledge,
|
|
160
193
|
].forEach((mock) =>
|
|
161
194
|
mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
|
|
162
195
|
);
|
|
196
|
+
readClientGrants.mockResolvedValueOnce({
|
|
197
|
+
allowedGrantTypes: ['password'],
|
|
198
|
+
allowedScopes: [],
|
|
199
|
+
allowedRedirectURIS: [],
|
|
200
|
+
isPublicClient: false,
|
|
201
|
+
canAcknowledge: true,
|
|
202
|
+
authenticationData: {
|
|
203
|
+
clientId: 'root_app_id',
|
|
204
|
+
scopes: [],
|
|
205
|
+
userId: '1',
|
|
206
|
+
},
|
|
207
|
+
});
|
|
163
208
|
|
|
164
209
|
const postOAuth2Acknowledge = await initPostOAuth2Acknowledge({
|
|
165
210
|
ERRORS_DESCRIPTORS,
|
|
211
|
+
OAUTH2,
|
|
166
212
|
oAuth2Granters,
|
|
167
|
-
|
|
213
|
+
readClientGrants,
|
|
168
214
|
log,
|
|
169
215
|
});
|
|
170
216
|
const response = await postOAuth2Acknowledge({
|
|
171
217
|
authenticationData: {
|
|
172
|
-
|
|
173
|
-
|
|
218
|
+
clientId: 'an_app_id',
|
|
219
|
+
scopes: ['auth'],
|
|
174
220
|
userId: '1',
|
|
175
221
|
},
|
|
176
222
|
body: {
|
|
@@ -184,29 +230,42 @@ describe('postOAuth2Acknowledge', () => {
|
|
|
184
230
|
});
|
|
185
231
|
expect({
|
|
186
232
|
response,
|
|
233
|
+
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
|
|
234
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
235
|
+
codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
|
|
236
|
+
codeGranterAcknowledgerAcknowledgeCalls:
|
|
237
|
+
codeGranter.acknowledge.mock.calls,
|
|
238
|
+
codeGranterAuthenticatorAuthenticateCalls:
|
|
239
|
+
codeGranter.authenticate.mock.calls,
|
|
240
|
+
tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
|
|
241
|
+
tokenGranterAcknowledgerAcknowledgeCalls:
|
|
242
|
+
tokenGranter.acknowledge.mock.calls,
|
|
187
243
|
}).toMatchInlineSnapshot(`
|
|
188
244
|
{
|
|
245
|
+
"codeGranterAcknowledgerAcknowledgeCalls": [],
|
|
246
|
+
"codeGranterAuthenticatorAuthenticateCalls": [],
|
|
247
|
+
"codeGranterAuthorizerAuthorizeCalls": [],
|
|
248
|
+
"logCalls": [
|
|
249
|
+
[
|
|
250
|
+
"debug",
|
|
251
|
+
"👫 - OAuth2 acknowledge error",
|
|
252
|
+
"E_OAUTH2_UNKNOWN_ACKNOWLEDGER_TYPE",
|
|
253
|
+
],
|
|
254
|
+
],
|
|
255
|
+
"readClientGrantsCalls": [
|
|
256
|
+
[
|
|
257
|
+
"an_app_id",
|
|
258
|
+
],
|
|
259
|
+
],
|
|
189
260
|
"response": {
|
|
190
261
|
"headers": {
|
|
191
262
|
"location": "https://www.example.com/?error=unsupported_response_type&error_description=Type+%22yolo%22+not+supported.",
|
|
192
263
|
},
|
|
193
|
-
"status":
|
|
264
|
+
"status": 201,
|
|
194
265
|
},
|
|
266
|
+
"tokenGranterAcknowledgerAcknowledgeCalls": [],
|
|
267
|
+
"tokenGranterAuthorizerAuthorizeCalls": [],
|
|
195
268
|
}
|
|
196
269
|
`);
|
|
197
|
-
expect({
|
|
198
|
-
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
|
|
199
|
-
checkApplicationCalls: checkApplication.mock.calls,
|
|
200
|
-
codeGranterAuthorizerAuthorizeCalls:
|
|
201
|
-
codeGranter.authorizer.authorize.mock.calls,
|
|
202
|
-
codeGranterAcknowledgerAcknowledgeCalls:
|
|
203
|
-
codeGranter.acknowledger.acknowledge.mock.calls,
|
|
204
|
-
codeGranterAuthenticatorAuthenticateCalls:
|
|
205
|
-
codeGranter.authenticator.authenticate.mock.calls,
|
|
206
|
-
tokenGranterAuthorizerAuthorizeCalls:
|
|
207
|
-
tokenGranter.authorizer.authorize.mock.calls,
|
|
208
|
-
tokenGranterAcknowledgerAcknowledgeCalls:
|
|
209
|
-
tokenGranter.acknowledger.acknowledge.mock.calls,
|
|
210
|
-
}).toMatchSnapshot();
|
|
211
270
|
});
|
|
212
271
|
});
|
|
@@ -1,18 +1,36 @@
|
|
|
1
1
|
import { autoService, location } from 'knifecycle';
|
|
2
2
|
import { printStackTrace, YError } from 'yerror';
|
|
3
|
-
import { setURLError } from './getOAuth2Authorize.js';
|
|
4
3
|
import {
|
|
5
4
|
type WhookRouteDefinition,
|
|
6
5
|
type WhookErrorsDescriptors,
|
|
6
|
+
refersTo,
|
|
7
7
|
} from '@whook/whook';
|
|
8
8
|
import {
|
|
9
|
-
type
|
|
10
|
-
type
|
|
9
|
+
type WhookOAuth2Options,
|
|
10
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
11
|
+
type WhookOAuth2GranterService,
|
|
12
|
+
type WhookOAuth2GranterDefinitions,
|
|
13
|
+
type WhookOAuth2ClientId,
|
|
11
14
|
} from '../services/oAuth2Granters.js';
|
|
12
15
|
import { type LogService } from 'common-services';
|
|
13
16
|
import { type WhookAuthenticationData } from '@whook/authorization';
|
|
17
|
+
import {
|
|
18
|
+
filterScopes,
|
|
19
|
+
parseOAuth2Scope,
|
|
20
|
+
stringifyScopes,
|
|
21
|
+
} from '../libs/scopes.js';
|
|
22
|
+
import { scopeSchema } from '../libs/schemas.js';
|
|
23
|
+
import {
|
|
24
|
+
codeChallengeMethodSchema,
|
|
25
|
+
codeChallengeSchema,
|
|
26
|
+
} from '../services/oAuth2AuthorizationCodeGranter.js';
|
|
27
|
+
import { addParamsToURL, buildParamsFromError } from '../libs/redirectURI.js';
|
|
28
|
+
import { snakeCase } from '../libs/utils.js';
|
|
29
|
+
|
|
30
|
+
export { scopeSchema, codeChallengeMethodSchema, codeChallengeSchema };
|
|
14
31
|
|
|
15
32
|
/* Architecture Note #2: OAuth2 acknowledge
|
|
33
|
+
|
|
16
34
|
This endpoint is to be used by the authentication SSR frontend
|
|
17
35
|
to acknowledge that the user accepted the client request in it.
|
|
18
36
|
*/
|
|
@@ -54,19 +72,17 @@ export const definition = {
|
|
|
54
72
|
pattern: '^https?://',
|
|
55
73
|
format: 'uri',
|
|
56
74
|
},
|
|
57
|
-
scope:
|
|
58
|
-
type: 'string',
|
|
59
|
-
description:
|
|
60
|
-
'See https://tools.ietf.org/html/rfc6749#section-3.3',
|
|
61
|
-
},
|
|
75
|
+
scope: refersTo(scopeSchema),
|
|
62
76
|
state: {
|
|
63
77
|
type: 'string',
|
|
64
78
|
},
|
|
65
79
|
acknowledged: {
|
|
66
80
|
type: 'boolean',
|
|
67
81
|
description:
|
|
68
|
-
'
|
|
82
|
+
'Whether the user acknowledged the delegation or not.',
|
|
69
83
|
},
|
|
84
|
+
codeChallenge: refersTo(codeChallengeSchema),
|
|
85
|
+
codeChallengeMethod: refersTo(codeChallengeMethodSchema),
|
|
70
86
|
},
|
|
71
87
|
},
|
|
72
88
|
},
|
|
@@ -75,6 +91,13 @@ export const definition = {
|
|
|
75
91
|
responses: {
|
|
76
92
|
'201': {
|
|
77
93
|
description: 'Redirection endpoint URI computed.',
|
|
94
|
+
headers: {
|
|
95
|
+
location: {
|
|
96
|
+
schema: {
|
|
97
|
+
type: 'string',
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
},
|
|
78
101
|
content: {
|
|
79
102
|
'application/json': {
|
|
80
103
|
schema: {
|
|
@@ -97,19 +120,16 @@ export const definition = {
|
|
|
97
120
|
|
|
98
121
|
export interface HandlerDependencies {
|
|
99
122
|
ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
Record<string, unknown>
|
|
104
|
-
>[];
|
|
105
|
-
checkApplication: CheckApplicationService;
|
|
123
|
+
OAUTH2: WhookOAuth2Options;
|
|
124
|
+
oAuth2Granters: WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
|
|
125
|
+
readClientGrants: WhookOAuth2ReadClientGrantsService;
|
|
106
126
|
log: LogService;
|
|
107
127
|
}
|
|
108
128
|
interface HandlerParameters {
|
|
109
129
|
authenticationData: WhookAuthenticationData;
|
|
110
130
|
body: {
|
|
111
131
|
responseType: string;
|
|
112
|
-
clientId:
|
|
132
|
+
clientId: WhookOAuth2ClientId;
|
|
113
133
|
redirectURI: string;
|
|
114
134
|
scope: string;
|
|
115
135
|
state: string;
|
|
@@ -129,95 +149,115 @@ export default location(
|
|
|
129
149
|
|
|
130
150
|
async function initPostOAuth2Acknowledge({
|
|
131
151
|
ERRORS_DESCRIPTORS,
|
|
152
|
+
OAUTH2,
|
|
132
153
|
oAuth2Granters,
|
|
133
|
-
|
|
154
|
+
readClientGrants,
|
|
134
155
|
log,
|
|
135
156
|
}: HandlerDependencies) {
|
|
136
157
|
return async ({
|
|
137
|
-
authenticationData:
|
|
158
|
+
authenticationData: userAuthenticationData,
|
|
138
159
|
body: {
|
|
139
160
|
responseType,
|
|
140
161
|
clientId,
|
|
141
|
-
redirectURI:
|
|
162
|
+
redirectURI: demandedRedirectURI,
|
|
142
163
|
scope: demandedScope,
|
|
143
164
|
state,
|
|
144
165
|
acknowledged = false,
|
|
145
166
|
...additionalProperties
|
|
146
167
|
},
|
|
147
168
|
}: HandlerParameters) => {
|
|
148
|
-
if (!
|
|
169
|
+
if (!userAuthenticationData) {
|
|
149
170
|
throw new YError('E_UNAUTHORIZED');
|
|
150
171
|
}
|
|
151
172
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
173
|
+
const acknowledgeClientGrants = await readClientGrants(
|
|
174
|
+
userAuthenticationData.clientId,
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
if (!acknowledgeClientGrants.canAcknowledge) {
|
|
178
|
+
throw new YError('E_UNAUTHORIZED');
|
|
179
|
+
}
|
|
159
180
|
|
|
160
181
|
let url: URL;
|
|
161
182
|
|
|
162
183
|
try {
|
|
163
184
|
if (!acknowledged) {
|
|
164
|
-
throw new YError('
|
|
185
|
+
throw new YError('E_OAUTH2_ACCESS_DENIED', [clientId]);
|
|
165
186
|
}
|
|
166
187
|
|
|
167
188
|
const granter = oAuth2Granters.find(
|
|
168
|
-
(granter) =>
|
|
169
|
-
granter.acknowledger &&
|
|
170
|
-
granter.acknowledger.acknowledgmentType === responseType,
|
|
189
|
+
(granter) => granter.responseType === responseType,
|
|
171
190
|
);
|
|
172
191
|
|
|
173
|
-
if (!granter
|
|
174
|
-
throw new YError('
|
|
192
|
+
if (!granter?.acknowledge) {
|
|
193
|
+
throw new YError('E_OAUTH2_UNKNOWN_ACKNOWLEDGER_TYPE', [responseType]);
|
|
175
194
|
}
|
|
176
195
|
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
196
|
+
const demandedScopes = filterScopes(
|
|
197
|
+
parseOAuth2Scope(demandedScope),
|
|
198
|
+
OAUTH2.allowedScopes,
|
|
199
|
+
!!OAUTH2.strictScopesChecks,
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
const {
|
|
203
|
+
acknowledgedAuthenticationData,
|
|
204
|
+
acknowledgedData,
|
|
205
|
+
acknowledgedRedirectURI,
|
|
206
|
+
} = await granter.acknowledge(
|
|
207
|
+
userAuthenticationData,
|
|
208
|
+
{
|
|
209
|
+
clientId,
|
|
210
|
+
demandedRedirectURI,
|
|
211
|
+
demandedScopes,
|
|
212
|
+
},
|
|
213
|
+
additionalProperties,
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
url = new URL(acknowledgedRedirectURI);
|
|
217
|
+
|
|
218
|
+
const paramsHash: Record<string, string> = {
|
|
219
|
+
client_id: acknowledgedAuthenticationData.clientId,
|
|
220
|
+
scope: stringifyScopes(acknowledgedAuthenticationData.scopes),
|
|
221
|
+
state: state,
|
|
222
|
+
};
|
|
187
223
|
|
|
188
|
-
|
|
224
|
+
Object.keys(acknowledgedData).forEach((key) => {
|
|
225
|
+
if (typeof acknowledgedData[key] === 'number') {
|
|
226
|
+
paramsHash[snakeCase(key)] = acknowledgedData[key].toString(10);
|
|
227
|
+
} else if (typeof acknowledgedData[key] === 'string') {
|
|
228
|
+
paramsHash[snakeCase(key)] = acknowledgedData[key];
|
|
229
|
+
}
|
|
230
|
+
});
|
|
189
231
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
url.searchParams.set(snakeCase(key), acknowledgedData[key] as string),
|
|
232
|
+
addParamsToURL(
|
|
233
|
+
url,
|
|
234
|
+
paramsHash,
|
|
235
|
+
responseType === 'token' ? 'fragment' : 'query',
|
|
195
236
|
);
|
|
196
237
|
} catch (err) {
|
|
197
238
|
log('debug', '👫 - OAuth2 acknowledge error', (err as YError).code);
|
|
198
239
|
log('debug-stack', printStackTrace(err));
|
|
199
240
|
|
|
200
|
-
url = new URL(
|
|
241
|
+
url = new URL(demandedRedirectURI);
|
|
201
242
|
|
|
202
|
-
|
|
203
|
-
url,
|
|
243
|
+
const paramsHash = buildParamsFromError(
|
|
204
244
|
err as YError,
|
|
205
|
-
ERRORS_DESCRIPTORS[(err as YError).code] ||
|
|
245
|
+
ERRORS_DESCRIPTORS[(err as YError).code] ||
|
|
246
|
+
ERRORS_DESCRIPTORS.E_OAUTH2_UNEXPECTED_ERROR,
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
addParamsToURL(
|
|
250
|
+
url,
|
|
251
|
+
paramsHash,
|
|
252
|
+
responseType === 'token' ? 'fragment' : 'query',
|
|
206
253
|
);
|
|
207
254
|
}
|
|
208
255
|
|
|
209
256
|
return {
|
|
210
|
-
status:
|
|
257
|
+
status: 201,
|
|
211
258
|
headers: {
|
|
212
259
|
location: url.href,
|
|
213
260
|
},
|
|
214
261
|
};
|
|
215
262
|
};
|
|
216
263
|
}
|
|
217
|
-
|
|
218
|
-
function snakeCase(s: string): string {
|
|
219
|
-
return s
|
|
220
|
-
.split(/(?=(?<![A-Z])[A-Z])|[^a-zA-Z]+/)
|
|
221
|
-
.map((s) => s.toLowerCase())
|
|
222
|
-
.join('_');
|
|
223
|
-
}
|