@whook/oauth2 25.2.1 → 26.0.2
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 +34 -11
- package/dist/index.js +18 -8
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +1671 -393
- package/dist/index.test.js.map +1 -1
- package/dist/libs/authorizationRequests.d.ts +6 -0
- package/dist/libs/authorizationRequests.js +22 -0
- package/dist/libs/authorizationRequests.js.map +1 -0
- package/dist/libs/authorizationRequests.test.js +24 -0
- package/dist/libs/authorizationRequests.test.js.map +1 -0
- 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 +143 -0
- package/dist/libs/errors.js +227 -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 +8 -0
- package/dist/libs/redirectURI.js +94 -0
- package/dist/libs/redirectURI.js.map +1 -0
- package/dist/libs/schemas.d.ts +31 -0
- package/dist/libs/schemas.js +33 -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 +10 -0
- package/dist/libs/verifier.js +51 -0
- package/dist/libs/verifier.js.map +1 -0
- package/dist/libs/verifier.test.d.ts +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 +38 -59
- package/dist/routes/getOAuth2Authorize.js +129 -93
- package/dist/routes/getOAuth2Authorize.js.map +1 -1
- package/dist/routes/getOAuth2Authorize.test.js +959 -73
- package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.d.ts +18 -11
- package/dist/routes/getOAuth2WellKnownMetadata.js +43 -11
- package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.test.js +51 -49
- 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 +11 -10
- package/dist/routes/postOAuth2Acknowledge.js +43 -37
- package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
- package/dist/routes/postOAuth2Acknowledge.test.js +135 -70
- package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -1
- package/dist/routes/postOAuth2PushedAuthorizationRequest.d.ts +137 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.js +187 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.js.map +1 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.test.d.ts +1 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js +554 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js.map +1 -0
- 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 +47 -108
- package/dist/routes/postOAuth2Token.js +65 -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 +9 -5
- package/dist/services/authCookies.js.map +1 -1
- package/dist/services/authCookies.test.js.map +1 -1
- package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +124 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.js +204 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.d.ts +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js +345 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -0
- package/dist/services/oAuth2AuthorizationRequests.d.ts +41 -0
- package/dist/services/oAuth2AuthorizationRequests.js +61 -0
- package/dist/services/oAuth2AuthorizationRequests.js.map +1 -0
- package/dist/services/oAuth2AuthorizationRequests.test.d.ts +1 -0
- package/dist/services/oAuth2AuthorizationRequests.test.js +249 -0
- package/dist/services/oAuth2AuthorizationRequests.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 +79 -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 +61 -0
- package/dist/services/oAuth2ImplicitGranter.js.map +1 -0
- package/dist/services/oAuth2ImplicitGranter.test.d.ts +1 -0
- package/dist/services/oAuth2ImplicitGranter.test.js +589 -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 +14 -13
- package/src/index.test.ts +1773 -407
- package/src/index.ts +63 -53
- package/src/libs/authorizationRequests.test.ts +40 -0
- package/src/libs/authorizationRequests.ts +42 -0
- package/src/libs/clients.ts +22 -0
- package/src/libs/errors.ts +411 -0
- package/src/libs/grants.ts +15 -0
- package/src/libs/redirectURI.ts +142 -0
- package/src/libs/schemas.ts +37 -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 +75 -0
- package/src/routes/getOAuth2Authorize.test.ts +1102 -102
- package/src/routes/getOAuth2Authorize.ts +243 -151
- package/src/routes/getOAuth2WellKnownMetadata.test.ts +73 -74
- package/src/routes/getOAuth2WellKnownMetadata.ts +63 -16
- 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 +167 -108
- package/src/routes/postOAuth2Acknowledge.ts +90 -64
- package/src/routes/postOAuth2PushedAuthorizationRequest.test.ts +681 -0
- package/src/routes/postOAuth2PushedAuthorizationRequest.ts +292 -0
- 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 +104 -136
- package/src/services/authCookies.test.ts +5 -5
- package/src/services/authCookies.ts +24 -22
- package/src/services/oAuth2AuthorizationCodeGranter.test.ts +407 -0
- package/src/services/oAuth2AuthorizationCodeGranter.ts +365 -0
- package/src/services/oAuth2AuthorizationRequests.test.ts +282 -0
- package/src/services/oAuth2AuthorizationRequests.ts +141 -0
- package/src/services/oAuth2ClientCredentialsGranter.test.ts +303 -0
- package/src/services/oAuth2ClientCredentialsGranter.ts +68 -43
- package/src/services/oAuth2Granters.ts +100 -196
- package/src/services/oAuth2ImplicitGranter.test.ts +650 -0
- package/src/services/oAuth2ImplicitGranter.ts +147 -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/authorizationRequests.test.d.ts} +0 -0
|
@@ -2,102 +2,95 @@
|
|
|
2
2
|
import { describe, test, beforeEach, jest, expect } from '@jest/globals';
|
|
3
3
|
import initGetOAuth2WellKnown from './getOAuth2WellKnownMetadata.js';
|
|
4
4
|
import { YError } from 'yerror';
|
|
5
|
-
import { type
|
|
6
|
-
import { type
|
|
5
|
+
import { type WhookOAuth2AuthorizationCodeGranterService } from '../services/oAuth2AuthorizationCodeGranter.js';
|
|
6
|
+
import { type WhookOAuth2ImplicitGranterService } from '../services/oAuth2ImplicitGranter.js';
|
|
7
7
|
import { type LogService } from 'common-services';
|
|
8
|
-
import {
|
|
9
|
-
|
|
8
|
+
import {
|
|
9
|
+
type WhookOAuth2GranterDefinitions,
|
|
10
|
+
type WhookOAuth2GranterService,
|
|
11
|
+
} from '../index.js';
|
|
12
|
+
import { type WhookDefinitions } from '@whook/whook';
|
|
10
13
|
|
|
11
14
|
describe('getOAuth2WellKnown', () => {
|
|
12
15
|
const log = jest.fn<LogService>();
|
|
13
16
|
const codeGranter = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
},
|
|
29
|
-
authenticator: {
|
|
30
|
-
grantType: 'authorization_code',
|
|
31
|
-
authenticate:
|
|
32
|
-
jest.fn<
|
|
33
|
-
NonNullable<OAuth2CodeGranterService['authenticator']>['authenticate']
|
|
34
|
-
>(),
|
|
35
|
-
},
|
|
17
|
+
grantType: 'authorization_code',
|
|
18
|
+
responseType: 'code',
|
|
19
|
+
authorize:
|
|
20
|
+
jest.fn<
|
|
21
|
+
NonNullable<WhookOAuth2AuthorizationCodeGranterService['authorize']>
|
|
22
|
+
>(),
|
|
23
|
+
acknowledge:
|
|
24
|
+
jest.fn<
|
|
25
|
+
NonNullable<WhookOAuth2AuthorizationCodeGranterService['acknowledge']>
|
|
26
|
+
>(),
|
|
27
|
+
authenticate:
|
|
28
|
+
jest.fn<
|
|
29
|
+
NonNullable<WhookOAuth2AuthorizationCodeGranterService['authenticate']>
|
|
30
|
+
>(),
|
|
36
31
|
};
|
|
37
32
|
const tokenGranter = {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
>(),
|
|
47
|
-
},
|
|
48
|
-
acknowledger: {
|
|
49
|
-
acknowledgmentType: 'token',
|
|
50
|
-
acknowledge:
|
|
51
|
-
jest.fn<
|
|
52
|
-
NonNullable<OAuth2TokenGranterService['acknowledger']>['acknowledge']
|
|
53
|
-
>(),
|
|
54
|
-
},
|
|
33
|
+
grantType: 'implicit',
|
|
34
|
+
responseType: 'token',
|
|
35
|
+
authorize:
|
|
36
|
+
jest.fn<
|
|
37
|
+
NonNullable<NonNullable<WhookOAuth2ImplicitGranterService['authorize']>>
|
|
38
|
+
>(),
|
|
39
|
+
acknowledge:
|
|
40
|
+
jest.fn<NonNullable<WhookOAuth2ImplicitGranterService['acknowledge']>>(),
|
|
55
41
|
};
|
|
56
42
|
const oAuth2Granters = [
|
|
57
43
|
codeGranter,
|
|
58
44
|
tokenGranter,
|
|
59
|
-
] as unknown as
|
|
45
|
+
] as unknown as WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
|
|
60
46
|
|
|
61
47
|
beforeEach(() => {
|
|
62
48
|
log.mockReset();
|
|
63
49
|
[
|
|
64
|
-
codeGranter.
|
|
65
|
-
codeGranter.
|
|
66
|
-
codeGranter.
|
|
67
|
-
tokenGranter.
|
|
68
|
-
tokenGranter.
|
|
50
|
+
codeGranter.authorize,
|
|
51
|
+
codeGranter.acknowledge,
|
|
52
|
+
codeGranter.authenticate,
|
|
53
|
+
tokenGranter.authorize,
|
|
54
|
+
tokenGranter.acknowledge,
|
|
69
55
|
].forEach((mock) => mock.mockReset());
|
|
70
56
|
});
|
|
71
57
|
|
|
72
58
|
test('should return OAuth2 metadata', async () => {
|
|
73
59
|
[
|
|
74
|
-
codeGranter.
|
|
75
|
-
codeGranter.
|
|
76
|
-
codeGranter.
|
|
77
|
-
tokenGranter.
|
|
78
|
-
tokenGranter.
|
|
60
|
+
codeGranter.authorize,
|
|
61
|
+
codeGranter.acknowledge,
|
|
62
|
+
codeGranter.authenticate,
|
|
63
|
+
tokenGranter.authorize,
|
|
64
|
+
tokenGranter.acknowledge,
|
|
79
65
|
].forEach((mock: any) =>
|
|
80
66
|
mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
|
|
81
67
|
);
|
|
82
68
|
|
|
83
69
|
const getOAuth2WellKnown = await initGetOAuth2WellKnown({
|
|
84
70
|
BASE_URL: 'https://server.example.com',
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
71
|
+
OAUTH2_PAR: {
|
|
72
|
+
mode: 'required',
|
|
73
|
+
},
|
|
74
|
+
DEFINITIONS: {
|
|
75
|
+
configs: {
|
|
76
|
+
getOAuth2Authorize: {
|
|
77
|
+
type: 'route',
|
|
78
|
+
path: '/v0/oauth2/authorize',
|
|
91
79
|
},
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
80
|
+
postOAuth2Token: {
|
|
81
|
+
type: 'route',
|
|
82
|
+
path: '/v0/oauth2/token',
|
|
83
|
+
},
|
|
84
|
+
postOAuth2Revoke: {
|
|
85
|
+
type: 'route',
|
|
86
|
+
path: '/v0/oauth2/revoke',
|
|
87
|
+
},
|
|
88
|
+
postOAuth2PushedAuthorizationRequest: {
|
|
89
|
+
type: 'route',
|
|
90
|
+
path: '/v0/oauth2/par',
|
|
98
91
|
},
|
|
99
92
|
},
|
|
100
|
-
} as unknown as
|
|
93
|
+
} as unknown as WhookDefinitions,
|
|
101
94
|
API: {
|
|
102
95
|
openapi: '3.2',
|
|
103
96
|
info: {
|
|
@@ -124,16 +117,14 @@ describe('getOAuth2WellKnown', () => {
|
|
|
124
117
|
expect({
|
|
125
118
|
response,
|
|
126
119
|
logCalls: log.mock.calls.filter((args) => args[0].endsWith('stack')),
|
|
127
|
-
codeGranterAuthorizerAuthorizeCalls:
|
|
128
|
-
codeGranter.authorizer.authorize.mock.calls,
|
|
120
|
+
codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
|
|
129
121
|
codeGranterAcknowledgerAcknowledgeCalls:
|
|
130
|
-
codeGranter.
|
|
122
|
+
codeGranter.acknowledge.mock.calls,
|
|
131
123
|
codeGranterAuthenticatorAuthenticateCalls:
|
|
132
|
-
codeGranter.
|
|
133
|
-
tokenGranterAuthorizerAuthorizeCalls:
|
|
134
|
-
tokenGranter.authorizer.authorize.mock.calls,
|
|
124
|
+
codeGranter.authenticate.mock.calls,
|
|
125
|
+
tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
|
|
135
126
|
tokenGranterAcknowledgerAcknowledgeCalls:
|
|
136
|
-
tokenGranter.
|
|
127
|
+
tokenGranter.acknowledge.mock.calls,
|
|
137
128
|
}).toMatchInlineSnapshot(`
|
|
138
129
|
{
|
|
139
130
|
"codeGranterAcknowledgerAcknowledgeCalls": [],
|
|
@@ -145,12 +136,20 @@ describe('getOAuth2WellKnown', () => {
|
|
|
145
136
|
"authorization_endpoint": "https://server.example.com/v0/oauth2/authorize",
|
|
146
137
|
"grant_types_supported": [
|
|
147
138
|
"authorization_code",
|
|
139
|
+
"implicit",
|
|
148
140
|
],
|
|
149
141
|
"issuer": "https://server.example.com",
|
|
142
|
+
"pushed_authorization_request_endpoint": "https://server.example.com/v0/oauth2/par",
|
|
143
|
+
"require_pushed_authorization_requests": true,
|
|
150
144
|
"response_types_supported": [
|
|
151
145
|
"code",
|
|
152
146
|
"token",
|
|
153
147
|
],
|
|
148
|
+
"revocation_endpoint": "https://server.example.com/v0/oauth2/revoke",
|
|
149
|
+
"revocation_endpoint_auth_methods_supported": [
|
|
150
|
+
"client_secret_basic",
|
|
151
|
+
"client_secret_post",
|
|
152
|
+
],
|
|
154
153
|
"scopes_supported": [
|
|
155
154
|
"user",
|
|
156
155
|
"admin",
|
|
@@ -1,16 +1,27 @@
|
|
|
1
1
|
import {
|
|
2
2
|
identity,
|
|
3
3
|
refersTo,
|
|
4
|
-
type
|
|
4
|
+
type WhookDefinitions,
|
|
5
5
|
type WhookAPISchemaDefinition,
|
|
6
6
|
type WhookRouteDefinition,
|
|
7
7
|
} from '@whook/whook';
|
|
8
8
|
import { autoService, location } from 'knifecycle';
|
|
9
|
-
import {
|
|
10
|
-
|
|
9
|
+
import {
|
|
10
|
+
type WhookOAuth2GranterDefinitions,
|
|
11
|
+
type WhookOAuth2GranterService,
|
|
12
|
+
} from '../services/oAuth2Granters.js';
|
|
13
|
+
import { codeChallengeMethodSchema } from '../services/oAuth2AuthorizationCodeGranter.js';
|
|
14
|
+
import { scopeTokenSchema, scopeTokensSchema } from '../libs/schemas.js';
|
|
11
15
|
import { type LogService } from 'common-services';
|
|
12
16
|
import { type OpenAPI } from 'ya-open-api-types';
|
|
13
17
|
import { collectScopesFromAPI } from '../libs/scopes.js';
|
|
18
|
+
import {
|
|
19
|
+
DEFAULT_OAUTH2_PAR,
|
|
20
|
+
type WhookOAuth2AuthorizationRequestsConfig,
|
|
21
|
+
} from '../services/oAuth2AuthorizationRequests.js';
|
|
22
|
+
import { YError } from 'yerror';
|
|
23
|
+
|
|
24
|
+
export { scopeTokenSchema, scopeTokensSchema, codeChallengeMethodSchema };
|
|
14
25
|
|
|
15
26
|
// OAuth Token Endpoint Authentication Methods
|
|
16
27
|
export const endpointAuthenticationMethodsSchema = {
|
|
@@ -87,9 +98,10 @@ export const oAuth2MetadataSchema = {
|
|
|
87
98
|
issuer: refersTo(httpsProtocolURISchema),
|
|
88
99
|
authorization_endpoint: refersTo(httpsProtocolURISchema),
|
|
89
100
|
token_endpoint: refersTo(httpsProtocolURISchema),
|
|
101
|
+
pushed_authorization_request_endpoint: refersTo(httpsProtocolURISchema),
|
|
90
102
|
jwks_uri: refersTo(httpsProtocolURISchema),
|
|
91
103
|
registration_endpoint: refersTo(httpsProtocolURISchema),
|
|
92
|
-
scopes_supported:
|
|
104
|
+
scopes_supported: refersTo(scopeTokensSchema),
|
|
93
105
|
response_types_supported: {
|
|
94
106
|
type: 'array',
|
|
95
107
|
items: {
|
|
@@ -249,6 +261,9 @@ export const oAuth2MetadataSchema = {
|
|
|
249
261
|
request_uri_parameter_supported: {
|
|
250
262
|
type: 'boolean',
|
|
251
263
|
},
|
|
264
|
+
require_pushed_authorization_requests: {
|
|
265
|
+
type: 'boolean',
|
|
266
|
+
},
|
|
252
267
|
require_request_uri_registration: {
|
|
253
268
|
type: 'boolean',
|
|
254
269
|
},
|
|
@@ -288,24 +303,41 @@ export const definition = {
|
|
|
288
303
|
async function initGetOAuth2WellKnownMetadata({
|
|
289
304
|
API,
|
|
290
305
|
BASE_URL,
|
|
291
|
-
|
|
306
|
+
DEFINITIONS,
|
|
307
|
+
OAUTH2_PAR = DEFAULT_OAUTH2_PAR,
|
|
292
308
|
oAuth2Granters,
|
|
293
309
|
log,
|
|
294
|
-
}: {
|
|
310
|
+
}: WhookOAuth2AuthorizationRequestsConfig & {
|
|
295
311
|
API: OpenAPI;
|
|
296
312
|
BASE_URL: string;
|
|
297
|
-
|
|
298
|
-
oAuth2Granters:
|
|
313
|
+
DEFINITIONS: WhookDefinitions;
|
|
314
|
+
oAuth2Granters: WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
|
|
299
315
|
log: LogService;
|
|
300
316
|
}) {
|
|
301
317
|
if (!BASE_URL.startsWith('https')) {
|
|
302
318
|
log('warning', `⚠️ - OAuth2 issuer must start with HTTPS (${BASE_URL}).`);
|
|
303
319
|
}
|
|
304
320
|
|
|
321
|
+
if (
|
|
322
|
+
OAUTH2_PAR.mode !== 'disabled' &&
|
|
323
|
+
!DEFINITIONS.configs['postOAuth2PushedAuthorizationRequest']
|
|
324
|
+
) {
|
|
325
|
+
log('error', `💥 - OAuth2 PAR endpoint required to enable PAR.`);
|
|
326
|
+
throw new YError('E_OAUTH2_MISCONFIGURED');
|
|
327
|
+
}
|
|
328
|
+
|
|
305
329
|
const body = {
|
|
306
330
|
issuer: `${BASE_URL}`,
|
|
307
|
-
authorization_endpoint: `${BASE_URL}${
|
|
308
|
-
|
|
331
|
+
authorization_endpoint: `${BASE_URL}${
|
|
332
|
+
DEFINITIONS.configs['getOAuth2Authorize']?.type === 'route'
|
|
333
|
+
? DEFINITIONS.configs['getOAuth2Authorize'].path
|
|
334
|
+
: '/oauth2/authorize'
|
|
335
|
+
}`,
|
|
336
|
+
token_endpoint: `${BASE_URL}${
|
|
337
|
+
DEFINITIONS.configs['postOAuth2Token']?.type === 'route'
|
|
338
|
+
? DEFINITIONS.configs['postOAuth2Token'].path
|
|
339
|
+
: '/oauth2/token'
|
|
340
|
+
}`,
|
|
309
341
|
// TODO: Put it behind options (allow none)
|
|
310
342
|
token_endpoint_auth_methods_supported: [
|
|
311
343
|
'client_secret_basic',
|
|
@@ -313,19 +345,34 @@ async function initGetOAuth2WellKnownMetadata({
|
|
|
313
345
|
],
|
|
314
346
|
grant_types_supported: [
|
|
315
347
|
...new Set(
|
|
316
|
-
oAuth2Granters
|
|
317
|
-
.map((granter) => granter.authenticator?.grantType)
|
|
318
|
-
.filter(identity),
|
|
348
|
+
oAuth2Granters.map((granter) => granter.grantType).filter(identity),
|
|
319
349
|
),
|
|
320
350
|
],
|
|
321
351
|
response_types_supported: [
|
|
322
352
|
...new Set(
|
|
323
|
-
oAuth2Granters
|
|
324
|
-
.map((granter) => granter.authorizer?.responseType)
|
|
325
|
-
.filter(identity),
|
|
353
|
+
oAuth2Granters.map((granter) => granter.responseType).filter(identity),
|
|
326
354
|
),
|
|
327
355
|
],
|
|
328
356
|
scopes_supported: collectScopesFromAPI(API),
|
|
357
|
+
...(DEFINITIONS.configs['postOAuth2Revoke']?.type === 'route' &&
|
|
358
|
+
DEFINITIONS.configs['postOAuth2Revoke'].path
|
|
359
|
+
? {
|
|
360
|
+
revocation_endpoint: `${BASE_URL}${DEFINITIONS.configs['postOAuth2Revoke'].path}`,
|
|
361
|
+
revocation_endpoint_auth_methods_supported: [
|
|
362
|
+
'client_secret_basic',
|
|
363
|
+
'client_secret_post',
|
|
364
|
+
],
|
|
365
|
+
}
|
|
366
|
+
: {}),
|
|
367
|
+
...(DEFINITIONS.configs['postOAuth2PushedAuthorizationRequest']?.type ===
|
|
368
|
+
'route' &&
|
|
369
|
+
DEFINITIONS.configs['postOAuth2PushedAuthorizationRequest'].path &&
|
|
370
|
+
OAUTH2_PAR.mode !== 'disabled'
|
|
371
|
+
? {
|
|
372
|
+
pushed_authorization_request_endpoint: `${BASE_URL}${DEFINITIONS.configs['postOAuth2PushedAuthorizationRequest'].path}`,
|
|
373
|
+
require_pushed_authorization_requests: OAUTH2_PAR.mode === 'required',
|
|
374
|
+
}
|
|
375
|
+
: {}),
|
|
329
376
|
};
|
|
330
377
|
|
|
331
378
|
return async () => {
|
|
@@ -11,6 +11,9 @@ import {
|
|
|
11
11
|
import { type LogService } from 'common-services';
|
|
12
12
|
import { type OpenAPI } from 'ya-open-api-types';
|
|
13
13
|
import { collectScopesFromAPI } from '../libs/scopes.js';
|
|
14
|
+
import { scopeTokensSchema } from '../libs/schemas.js';
|
|
15
|
+
|
|
16
|
+
export { scopeTokensSchema };
|
|
14
17
|
|
|
15
18
|
export const bearerMethodSchema = {
|
|
16
19
|
name: 'OAuth2BearerMethod',
|
|
@@ -35,12 +38,7 @@ export const oAuth2ProtectedResourceMetadataSchema = {
|
|
|
35
38
|
items: refersTo(httpsProtocolURISchema),
|
|
36
39
|
},
|
|
37
40
|
jwks_uri: refersTo(httpsProtocolURISchema),
|
|
38
|
-
scopes_supported:
|
|
39
|
-
type: 'array',
|
|
40
|
-
items: {
|
|
41
|
-
type: 'string',
|
|
42
|
-
},
|
|
43
|
-
},
|
|
41
|
+
scopes_supported: refersTo(scopeTokensSchema),
|
|
44
42
|
bearer_methods_supported: {
|
|
45
43
|
type: 'array',
|
|
46
44
|
items: refersTo(bearerMethodSchema),
|
|
@@ -1,20 +1,45 @@
|
|
|
1
|
-
import { describe, test, jest, expect } from '@jest/globals';
|
|
1
|
+
import { describe, test, beforeEach, jest, expect } from '@jest/globals';
|
|
2
2
|
import initPostAuthLogin from './postAuthLogin.js';
|
|
3
|
-
import { type
|
|
4
|
-
import {
|
|
3
|
+
import { type WhookAuthCookiesService } from '../services/authCookies.js';
|
|
4
|
+
import {
|
|
5
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
6
|
+
type WhookOAuth2Options,
|
|
7
|
+
} from '../services/oAuth2Granters.js';
|
|
5
8
|
|
|
6
9
|
describe('postAuthLogin', () => {
|
|
10
|
+
const OAUTH2: WhookOAuth2Options = {
|
|
11
|
+
authenticateURL: 'https://auth.example.com/sign_in',
|
|
12
|
+
allowedScopes: ['user', 'admin'],
|
|
13
|
+
rootClientId: 'root_app_id',
|
|
14
|
+
};
|
|
15
|
+
const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
|
|
16
|
+
const authCookies = {
|
|
17
|
+
build: jest.fn<WhookAuthCookiesService['build']>(),
|
|
18
|
+
};
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
+
const postOAuth2Token = jest.fn<any>();
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
readClientGrants.mockReset();
|
|
24
|
+
postOAuth2Token.mockReset();
|
|
25
|
+
authCookies.build.mockReset();
|
|
26
|
+
readClientGrants.mockResolvedValue({
|
|
27
|
+
allowedScopes: ['user', 'admin'],
|
|
28
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
29
|
+
allowedGrantTypes: ['code'],
|
|
30
|
+
authenticationData: {
|
|
31
|
+
clientId: 'root_app_id',
|
|
32
|
+
scopes: ['user', 'admin'],
|
|
33
|
+
userId: 'user_id',
|
|
34
|
+
},
|
|
35
|
+
isPublicClient: true,
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
7
39
|
test('should work', async () => {
|
|
8
|
-
const authCookies = {
|
|
9
|
-
build: jest.fn<AuthCookiesService['build']>(),
|
|
10
|
-
};
|
|
11
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
|
-
const postOAuth2Token = jest.fn<any>();
|
|
13
40
|
const postAuthLogin = await initPostAuthLogin({
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
applicationId: 'root_app_id',
|
|
17
|
-
} as WhookAuthenticationData,
|
|
41
|
+
OAUTH2,
|
|
42
|
+
readClientGrants,
|
|
18
43
|
authCookies,
|
|
19
44
|
postOAuth2Token,
|
|
20
45
|
});
|
|
@@ -44,51 +69,55 @@ describe('postAuthLogin', () => {
|
|
|
44
69
|
authCookiesBuildCalls: authCookies.build.mock.calls,
|
|
45
70
|
postOAuth2TokenCalls: postOAuth2Token.mock.calls,
|
|
46
71
|
}).toMatchInlineSnapshot(`
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
72
|
+
{
|
|
73
|
+
"authCookiesBuildCalls": [
|
|
74
|
+
[
|
|
75
|
+
{
|
|
76
|
+
"access_token": "an_access_token",
|
|
77
|
+
"expiration_date": "2020-02-02T20:22:02Z",
|
|
78
|
+
"expires_in": 3600,
|
|
79
|
+
"token_type": "bearer",
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
"session": true,
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
],
|
|
86
|
+
"postOAuth2TokenCalls": [
|
|
87
|
+
[
|
|
88
|
+
{
|
|
89
|
+
"authenticationData": {
|
|
90
|
+
"clientId": "root_app_id",
|
|
91
|
+
"scopes": [
|
|
92
|
+
"user",
|
|
93
|
+
"admin",
|
|
94
|
+
],
|
|
95
|
+
"userId": "user_id",
|
|
96
|
+
},
|
|
97
|
+
"body": {
|
|
98
|
+
"grant_type": "password",
|
|
99
|
+
"password": "a_password",
|
|
100
|
+
"scope": "user",
|
|
101
|
+
"username": "a_username",
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
],
|
|
106
|
+
"response": {
|
|
107
|
+
"body": {
|
|
108
|
+
"access_token": "an_access_token",
|
|
109
|
+
"expiration_date": "2020-02-02T20:22:02Z",
|
|
110
|
+
"expires_in": 3600,
|
|
111
|
+
"token_type": "bearer",
|
|
112
|
+
},
|
|
113
|
+
"headers": {
|
|
114
|
+
"Set-Cookie": [
|
|
115
|
+
"the_build_cookies",
|
|
116
|
+
],
|
|
117
|
+
},
|
|
118
|
+
"status": 200,
|
|
119
|
+
},
|
|
120
|
+
}
|
|
92
121
|
`);
|
|
93
122
|
});
|
|
94
123
|
});
|
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import { autoService, location } from 'knifecycle';
|
|
2
|
+
import {
|
|
3
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
4
|
+
type WhookOAuth2Options,
|
|
5
|
+
} from '../services/oAuth2Granters.js';
|
|
2
6
|
import { YHTTPError } from 'yhttperror';
|
|
3
|
-
import
|
|
7
|
+
import initPostOAuth2Token from './postOAuth2Token.js';
|
|
4
8
|
import { AUTH_API_PREFIX } from '../services/authCookies.js';
|
|
5
|
-
import { type WhookRouteDefinition } from '@whook/whook';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
import { refersTo, type WhookRouteDefinition } from '@whook/whook';
|
|
10
|
+
import { type WhookAuthCookiesService } from '../services/authCookies.js';
|
|
11
|
+
import { scopeSchema } from '../libs/schemas.js';
|
|
12
|
+
import { PASSWORD_GRANT_TYPE } from '../services/oAuth2PasswordGranter.js';
|
|
13
|
+
|
|
14
|
+
export { scopeSchema };
|
|
10
15
|
|
|
11
16
|
export const definition = {
|
|
12
17
|
method: 'post',
|
|
@@ -29,7 +34,7 @@ export const definition = {
|
|
|
29
34
|
properties: {
|
|
30
35
|
username: { type: 'string' },
|
|
31
36
|
password: { type: 'string' },
|
|
32
|
-
scope:
|
|
37
|
+
scope: refersTo(scopeSchema),
|
|
33
38
|
remember: { type: 'boolean' },
|
|
34
39
|
},
|
|
35
40
|
},
|
|
@@ -46,7 +51,7 @@ export const definition = {
|
|
|
46
51
|
properties: {
|
|
47
52
|
access_token: { type: 'string' },
|
|
48
53
|
expiration_date: { type: 'string' },
|
|
49
|
-
expires_in: { type: '
|
|
54
|
+
expires_in: { type: 'number' },
|
|
50
55
|
token_type: { type: 'string' },
|
|
51
56
|
},
|
|
52
57
|
},
|
|
@@ -58,12 +63,15 @@ export const definition = {
|
|
|
58
63
|
} as const satisfies WhookRouteDefinition;
|
|
59
64
|
|
|
60
65
|
async function initPostAuthLogin({
|
|
61
|
-
|
|
66
|
+
OAUTH2,
|
|
67
|
+
readClientGrants,
|
|
62
68
|
authCookies,
|
|
63
69
|
postOAuth2Token,
|
|
64
|
-
}:
|
|
65
|
-
|
|
66
|
-
|
|
70
|
+
}: {
|
|
71
|
+
OAUTH2: WhookOAuth2Options;
|
|
72
|
+
readClientGrants: WhookOAuth2ReadClientGrantsService;
|
|
73
|
+
authCookies: Pick<WhookAuthCookiesService, 'build'>;
|
|
74
|
+
postOAuth2Token: Awaited<ReturnType<typeof initPostOAuth2Token>>;
|
|
67
75
|
}) {
|
|
68
76
|
return async ({
|
|
69
77
|
body,
|
|
@@ -75,15 +83,17 @@ async function initPostAuthLogin({
|
|
|
75
83
|
remember: boolean;
|
|
76
84
|
};
|
|
77
85
|
}) => {
|
|
86
|
+
const { authenticationData } = await readClientGrants(OAUTH2.rootClientId);
|
|
87
|
+
|
|
78
88
|
try {
|
|
79
89
|
const response = await postOAuth2Token({
|
|
80
90
|
body: {
|
|
81
|
-
grant_type:
|
|
91
|
+
grant_type: PASSWORD_GRANT_TYPE,
|
|
82
92
|
scope: body.scope,
|
|
83
93
|
username: body.username,
|
|
84
94
|
password: body.password,
|
|
85
95
|
},
|
|
86
|
-
authenticationData
|
|
96
|
+
authenticationData,
|
|
87
97
|
});
|
|
88
98
|
|
|
89
99
|
return {
|