@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
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import { describe, test, beforeEach, jest, expect } from '@jest/globals';
|
|
2
|
+
import {
|
|
3
|
+
type WhookOAuth2Options,
|
|
4
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
5
|
+
} from './oAuth2Granters.js';
|
|
6
|
+
import initOAuth2AuthorizationCodeGranter, {
|
|
7
|
+
type WhookOAuth2AuthorizationCodeService,
|
|
8
|
+
} from './oAuth2AuthorizationCodeGranter.js';
|
|
9
|
+
import { YError } from 'yerror';
|
|
10
|
+
|
|
11
|
+
describe('OAuth2AuthorizationCodeGranter', () => {
|
|
12
|
+
const OAUTH2: WhookOAuth2Options = {
|
|
13
|
+
rootClientId: 'the_root_app_id',
|
|
14
|
+
authenticateURL: 'https://auth.example.com/sign_in',
|
|
15
|
+
allowedScopes: ['user', 'admin', 'root'],
|
|
16
|
+
};
|
|
17
|
+
const oAuth2AuthorizationCode = {
|
|
18
|
+
create: jest.fn<WhookOAuth2AuthorizationCodeService['create']>(),
|
|
19
|
+
check: jest.fn<WhookOAuth2AuthorizationCodeService['check']>(),
|
|
20
|
+
};
|
|
21
|
+
const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
|
|
22
|
+
const log = jest.fn();
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
oAuth2AuthorizationCode.create.mockReset();
|
|
26
|
+
oAuth2AuthorizationCode.check.mockReset();
|
|
27
|
+
readClientGrants.mockReset();
|
|
28
|
+
log.mockReset();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('.authorize()', () => {
|
|
32
|
+
test('should work with a valid request', async () => {
|
|
33
|
+
const oAuth2AuthorizationCodeGranter =
|
|
34
|
+
await initOAuth2AuthorizationCodeGranter({
|
|
35
|
+
OAUTH2,
|
|
36
|
+
readClientGrants,
|
|
37
|
+
oAuth2AuthorizationCode,
|
|
38
|
+
log,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
oAuth2AuthorizationCode.create.mockRejectedValueOnce(
|
|
42
|
+
new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
|
|
43
|
+
);
|
|
44
|
+
oAuth2AuthorizationCode.check.mockRejectedValueOnce(
|
|
45
|
+
new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const authorizerResult = await oAuth2AuthorizationCodeGranter.authorize?.(
|
|
49
|
+
{
|
|
50
|
+
clientId: 'the_client_app_id',
|
|
51
|
+
clientGrants: {
|
|
52
|
+
allowedScopes: ['user', 'admin'],
|
|
53
|
+
allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
|
|
54
|
+
allowedGrantTypes: ['authorization_code', 'refresh_token'],
|
|
55
|
+
authenticationData: {
|
|
56
|
+
clientId: 'the_client_app_id',
|
|
57
|
+
scopes: ['user', 'admin'],
|
|
58
|
+
userId: 'user_id',
|
|
59
|
+
},
|
|
60
|
+
isPublicClient: false,
|
|
61
|
+
},
|
|
62
|
+
demandedScopes: ['user'],
|
|
63
|
+
},
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect({
|
|
67
|
+
authorizerResult,
|
|
68
|
+
oAuth2AuthorizationCodeCreateCalls:
|
|
69
|
+
oAuth2AuthorizationCode.create.mock.calls,
|
|
70
|
+
oAuth2AuthorizationCodeCheckCalls:
|
|
71
|
+
oAuth2AuthorizationCode.check.mock.calls,
|
|
72
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
73
|
+
logCalls: log.mock.calls,
|
|
74
|
+
}).toMatchInlineSnapshot(`
|
|
75
|
+
{
|
|
76
|
+
"authorizerResult": {
|
|
77
|
+
"scopes": [
|
|
78
|
+
"user",
|
|
79
|
+
],
|
|
80
|
+
},
|
|
81
|
+
"logCalls": [
|
|
82
|
+
[
|
|
83
|
+
"debug",
|
|
84
|
+
"👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
|
|
85
|
+
],
|
|
86
|
+
],
|
|
87
|
+
"oAuth2AuthorizationCodeCheckCalls": [],
|
|
88
|
+
"oAuth2AuthorizationCodeCreateCalls": [],
|
|
89
|
+
"readClientGrantsCalls": [],
|
|
90
|
+
}
|
|
91
|
+
`);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('.acknowledge()', () => {
|
|
96
|
+
test('should work', async () => {
|
|
97
|
+
const oAuth2AuthorizationCodeGranter =
|
|
98
|
+
await initOAuth2AuthorizationCodeGranter({
|
|
99
|
+
OAUTH2,
|
|
100
|
+
readClientGrants,
|
|
101
|
+
oAuth2AuthorizationCode,
|
|
102
|
+
log,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
readClientGrants.mockResolvedValue({
|
|
106
|
+
allowedScopes: ['user', 'admin'],
|
|
107
|
+
allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
|
|
108
|
+
allowedGrantTypes: ['authorization_code', 'refresh_token'],
|
|
109
|
+
authenticationData: {
|
|
110
|
+
clientId: 'an_app_id',
|
|
111
|
+
scopes: ['user', 'admin'],
|
|
112
|
+
userId: 'user_id',
|
|
113
|
+
},
|
|
114
|
+
isPublicClient: false,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
oAuth2AuthorizationCode.create.mockResolvedValueOnce('yolo');
|
|
118
|
+
oAuth2AuthorizationCode.check.mockRejectedValueOnce(
|
|
119
|
+
new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const acknowledgerResult =
|
|
123
|
+
await oAuth2AuthorizationCodeGranter.acknowledge?.(
|
|
124
|
+
{
|
|
125
|
+
clientId: 'the_root_app_id',
|
|
126
|
+
scopes: ['user', 'admin'],
|
|
127
|
+
userId: 'the_user_id',
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
clientId: 'the_client_app_id',
|
|
131
|
+
demandedRedirectURI: 'https://www.example.com/oauth2/code',
|
|
132
|
+
demandedScopes: ['user'],
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
codeChallenge: 'my_code_challenge',
|
|
136
|
+
codeChallengeMethod: 'plain',
|
|
137
|
+
},
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
expect({
|
|
141
|
+
acknowledgerResult,
|
|
142
|
+
oAuth2AuthorizationCodeCreateCalls:
|
|
143
|
+
oAuth2AuthorizationCode.create.mock.calls,
|
|
144
|
+
oAuth2AuthorizationCodeCheckCalls:
|
|
145
|
+
oAuth2AuthorizationCode.check.mock.calls,
|
|
146
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
147
|
+
logCalls: log.mock.calls,
|
|
148
|
+
}).toMatchInlineSnapshot(`
|
|
149
|
+
{
|
|
150
|
+
"acknowledgerResult": {
|
|
151
|
+
"acknowledgedAuthenticationData": {
|
|
152
|
+
"clientId": "the_client_app_id",
|
|
153
|
+
"scopes": [
|
|
154
|
+
"user",
|
|
155
|
+
],
|
|
156
|
+
"userId": "the_user_id",
|
|
157
|
+
},
|
|
158
|
+
"acknowledgedData": {
|
|
159
|
+
"code": "yolo",
|
|
160
|
+
"codeChallenge": "my_code_challenge",
|
|
161
|
+
"codeChallengeMethod": "plain",
|
|
162
|
+
},
|
|
163
|
+
"acknowledgedRedirectURI": "https://www.example.com/oauth2/code",
|
|
164
|
+
"acknowledgedScopes": [
|
|
165
|
+
"user",
|
|
166
|
+
],
|
|
167
|
+
},
|
|
168
|
+
"logCalls": [
|
|
169
|
+
[
|
|
170
|
+
"debug",
|
|
171
|
+
"👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
|
|
172
|
+
],
|
|
173
|
+
],
|
|
174
|
+
"oAuth2AuthorizationCodeCheckCalls": [],
|
|
175
|
+
"oAuth2AuthorizationCodeCreateCalls": [
|
|
176
|
+
[
|
|
177
|
+
{
|
|
178
|
+
"clientId": "the_client_app_id",
|
|
179
|
+
"scopes": [
|
|
180
|
+
"user",
|
|
181
|
+
],
|
|
182
|
+
"userId": "the_user_id",
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
"codeChallenge": "my_code_challenge",
|
|
186
|
+
"codeChallengeMethod": "plain",
|
|
187
|
+
"demandedRedirectURI": "https://www.example.com/oauth2/code",
|
|
188
|
+
"demandedScopes": [
|
|
189
|
+
"user",
|
|
190
|
+
],
|
|
191
|
+
"filteredScopes": [
|
|
192
|
+
"user",
|
|
193
|
+
],
|
|
194
|
+
},
|
|
195
|
+
],
|
|
196
|
+
],
|
|
197
|
+
"readClientGrantsCalls": [
|
|
198
|
+
[
|
|
199
|
+
"the_client_app_id",
|
|
200
|
+
],
|
|
201
|
+
],
|
|
202
|
+
}
|
|
203
|
+
`);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
describe('.authenticate()', () => {
|
|
208
|
+
test('should work with plain code verifier', async () => {
|
|
209
|
+
const oAuth2AuthorizationCodeGranter =
|
|
210
|
+
await initOAuth2AuthorizationCodeGranter({
|
|
211
|
+
OAUTH2,
|
|
212
|
+
readClientGrants,
|
|
213
|
+
oAuth2AuthorizationCode,
|
|
214
|
+
log,
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
readClientGrants.mockResolvedValue({
|
|
218
|
+
allowedScopes: ['user', 'admin'],
|
|
219
|
+
allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
|
|
220
|
+
allowedGrantTypes: ['authorization_code', 'refresh_token'],
|
|
221
|
+
authenticationData: {
|
|
222
|
+
clientId: 'the_client_app_id',
|
|
223
|
+
scopes: ['user', 'admin'],
|
|
224
|
+
userId: 'user_id',
|
|
225
|
+
},
|
|
226
|
+
isPublicClient: false,
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
oAuth2AuthorizationCode.create.mockRejectedValueOnce(
|
|
230
|
+
new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
|
|
231
|
+
);
|
|
232
|
+
oAuth2AuthorizationCode.check.mockResolvedValueOnce({
|
|
233
|
+
codeAuthenticationData: {
|
|
234
|
+
clientId: 'the_client_app_id',
|
|
235
|
+
scopes: ['user', 'admin'],
|
|
236
|
+
userId: 'the_user_id',
|
|
237
|
+
},
|
|
238
|
+
context: {
|
|
239
|
+
demandedRedirectURI: 'https://www.example.com/oauth2/code',
|
|
240
|
+
demandedScopes: ['user'],
|
|
241
|
+
filteredScopes: ['user'],
|
|
242
|
+
codeChallenge: 'a_code_verifier',
|
|
243
|
+
codeChallengeMethod: 'plain',
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const authenticatorResult =
|
|
248
|
+
await oAuth2AuthorizationCodeGranter.authenticate?.(
|
|
249
|
+
{
|
|
250
|
+
code: 'a_code',
|
|
251
|
+
redirectURI: 'https://www.example.com/oauth2/code',
|
|
252
|
+
clientId: 'the_client_app_id',
|
|
253
|
+
codeVerifier: 'a_code_verifier',
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
clientId: 'the_client_app_id',
|
|
257
|
+
scopes: ['user', 'admin'],
|
|
258
|
+
userId: 'the_user_id',
|
|
259
|
+
},
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
expect({
|
|
263
|
+
authenticatorResult,
|
|
264
|
+
oAuth2AuthorizationCodeCreateCalls:
|
|
265
|
+
oAuth2AuthorizationCode.create.mock.calls,
|
|
266
|
+
oAuth2AuthorizationCodeCheckCalls:
|
|
267
|
+
oAuth2AuthorizationCode.check.mock.calls,
|
|
268
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
269
|
+
logCalls: log.mock.calls,
|
|
270
|
+
}).toMatchInlineSnapshot(`
|
|
271
|
+
{
|
|
272
|
+
"authenticatorResult": {
|
|
273
|
+
"clientId": "the_client_app_id",
|
|
274
|
+
"scopes": [
|
|
275
|
+
"user",
|
|
276
|
+
],
|
|
277
|
+
"userId": "the_user_id",
|
|
278
|
+
},
|
|
279
|
+
"logCalls": [
|
|
280
|
+
[
|
|
281
|
+
"debug",
|
|
282
|
+
"👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
|
|
283
|
+
],
|
|
284
|
+
],
|
|
285
|
+
"oAuth2AuthorizationCodeCheckCalls": [
|
|
286
|
+
[
|
|
287
|
+
{
|
|
288
|
+
"clientId": "the_client_app_id",
|
|
289
|
+
"scopes": [
|
|
290
|
+
"user",
|
|
291
|
+
"admin",
|
|
292
|
+
],
|
|
293
|
+
"userId": "the_user_id",
|
|
294
|
+
},
|
|
295
|
+
"a_code",
|
|
296
|
+
],
|
|
297
|
+
],
|
|
298
|
+
"oAuth2AuthorizationCodeCreateCalls": [],
|
|
299
|
+
"readClientGrantsCalls": [
|
|
300
|
+
[
|
|
301
|
+
"the_client_app_id",
|
|
302
|
+
],
|
|
303
|
+
],
|
|
304
|
+
}
|
|
305
|
+
`);
|
|
306
|
+
});
|
|
307
|
+
test('should work with plain S256 verifier', async () => {
|
|
308
|
+
const oAuth2AuthorizationCodeGranter =
|
|
309
|
+
await initOAuth2AuthorizationCodeGranter({
|
|
310
|
+
OAUTH2,
|
|
311
|
+
readClientGrants,
|
|
312
|
+
oAuth2AuthorizationCode,
|
|
313
|
+
log,
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
readClientGrants.mockResolvedValue({
|
|
317
|
+
allowedScopes: ['user', 'admin'],
|
|
318
|
+
allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
|
|
319
|
+
allowedGrantTypes: ['authorization_code', 'refresh_token'],
|
|
320
|
+
authenticationData: {
|
|
321
|
+
clientId: 'the_client_app_id',
|
|
322
|
+
scopes: ['user', 'admin'],
|
|
323
|
+
userId: 'user_id',
|
|
324
|
+
},
|
|
325
|
+
isPublicClient: false,
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
oAuth2AuthorizationCode.create.mockRejectedValueOnce(
|
|
329
|
+
new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
|
|
330
|
+
);
|
|
331
|
+
oAuth2AuthorizationCode.check.mockResolvedValueOnce({
|
|
332
|
+
codeAuthenticationData: {
|
|
333
|
+
clientId: 'the_client_app_id',
|
|
334
|
+
scopes: ['user', 'admin'],
|
|
335
|
+
userId: 'the_user_id',
|
|
336
|
+
},
|
|
337
|
+
context: {
|
|
338
|
+
demandedRedirectURI: 'https://www.example.com/oauth2/code',
|
|
339
|
+
demandedScopes: ['user'],
|
|
340
|
+
filteredScopes: ['user'],
|
|
341
|
+
codeChallenge: 'pOX5Ly27TpE8bQ0ZRggD_gN3gS_Q6LZDdR7DDP9wwKU',
|
|
342
|
+
codeChallengeMethod: 'S256',
|
|
343
|
+
},
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
const authenticatorResult =
|
|
347
|
+
await oAuth2AuthorizationCodeGranter.authenticate?.(
|
|
348
|
+
{
|
|
349
|
+
code: 'a_code',
|
|
350
|
+
redirectURI: 'https://www.example.com/oauth2/code',
|
|
351
|
+
clientId: 'the_client_app_id',
|
|
352
|
+
codeVerifier: 'a_code_verifier',
|
|
353
|
+
},
|
|
354
|
+
{
|
|
355
|
+
clientId: 'the_client_app_id',
|
|
356
|
+
scopes: ['user', 'admin'],
|
|
357
|
+
userId: 'the_user_id',
|
|
358
|
+
},
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
expect({
|
|
362
|
+
authenticatorResult,
|
|
363
|
+
oAuth2AuthorizationCodeCreateCalls:
|
|
364
|
+
oAuth2AuthorizationCode.create.mock.calls,
|
|
365
|
+
oAuth2AuthorizationCodeCheckCalls:
|
|
366
|
+
oAuth2AuthorizationCode.check.mock.calls,
|
|
367
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
368
|
+
logCalls: log.mock.calls,
|
|
369
|
+
}).toMatchInlineSnapshot(`
|
|
370
|
+
{
|
|
371
|
+
"authenticatorResult": {
|
|
372
|
+
"clientId": "the_client_app_id",
|
|
373
|
+
"scopes": [
|
|
374
|
+
"user",
|
|
375
|
+
],
|
|
376
|
+
"userId": "the_user_id",
|
|
377
|
+
},
|
|
378
|
+
"logCalls": [
|
|
379
|
+
[
|
|
380
|
+
"debug",
|
|
381
|
+
"👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
|
|
382
|
+
],
|
|
383
|
+
],
|
|
384
|
+
"oAuth2AuthorizationCodeCheckCalls": [
|
|
385
|
+
[
|
|
386
|
+
{
|
|
387
|
+
"clientId": "the_client_app_id",
|
|
388
|
+
"scopes": [
|
|
389
|
+
"user",
|
|
390
|
+
"admin",
|
|
391
|
+
],
|
|
392
|
+
"userId": "the_user_id",
|
|
393
|
+
},
|
|
394
|
+
"a_code",
|
|
395
|
+
],
|
|
396
|
+
],
|
|
397
|
+
"oAuth2AuthorizationCodeCreateCalls": [],
|
|
398
|
+
"readClientGrantsCalls": [
|
|
399
|
+
[
|
|
400
|
+
"the_client_app_id",
|
|
401
|
+
],
|
|
402
|
+
],
|
|
403
|
+
}
|
|
404
|
+
`);
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
});
|