@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
|
@@ -1,29 +1,78 @@
|
|
|
1
1
|
import { autoService, location } from 'knifecycle';
|
|
2
|
-
import { noop } from '@whook/whook';
|
|
2
|
+
import { noop, refersTo, type WhookAPISchemaDefinition } from '@whook/whook';
|
|
3
3
|
import { YError } from 'yerror';
|
|
4
4
|
import { type LogService } from 'common-services';
|
|
5
5
|
import {
|
|
6
|
-
type
|
|
7
|
-
type
|
|
8
|
-
type
|
|
6
|
+
type WhookOAuth2GranterService,
|
|
7
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
8
|
+
type WhookOAuth2Options,
|
|
9
|
+
type WhookOAuth2GranterDefinitions,
|
|
9
10
|
} from './oAuth2Granters.js';
|
|
11
|
+
import {
|
|
12
|
+
type WhookAuthenticationData,
|
|
13
|
+
type WhookAuthenticationScope,
|
|
14
|
+
} from '@whook/authorization';
|
|
15
|
+
import { filterScopes } from '../libs/scopes.js';
|
|
16
|
+
import { checkGrantType } from '../libs/grants.js';
|
|
17
|
+
import { scopeSchema } from '../libs/schemas.js';
|
|
18
|
+
|
|
19
|
+
export const PASSWORD_GRANT_TYPE = 'password';
|
|
10
20
|
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
21
|
+
export const passwordTokenRequestBodySchema = {
|
|
22
|
+
name: 'PasswordRequestBody',
|
|
23
|
+
schema: {
|
|
24
|
+
type: 'object',
|
|
25
|
+
description:
|
|
26
|
+
'Resource owner password credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.3',
|
|
27
|
+
required: ['grant_type', 'username', 'password'],
|
|
28
|
+
properties: {
|
|
29
|
+
grant_type: {
|
|
30
|
+
type: 'string',
|
|
31
|
+
enum: [PASSWORD_GRANT_TYPE],
|
|
32
|
+
},
|
|
33
|
+
username: {
|
|
34
|
+
type: 'string',
|
|
35
|
+
},
|
|
36
|
+
password: {
|
|
37
|
+
type: 'string',
|
|
38
|
+
},
|
|
39
|
+
scope: refersTo(scopeSchema),
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
} as const satisfies WhookAPISchemaDefinition;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A service to check a user password
|
|
46
|
+
*/
|
|
47
|
+
export interface WhookOAuth2PasswordService<
|
|
48
|
+
USERNAME extends string = string,
|
|
49
|
+
PASSWORD extends string = string,
|
|
50
|
+
> {
|
|
51
|
+
check: (
|
|
52
|
+
authenticationData: WhookAuthenticationData,
|
|
53
|
+
username: USERNAME,
|
|
54
|
+
password: PASSWORD,
|
|
55
|
+
) => Promise<WhookAuthenticationData>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface WhookOAuth2PasswordGranterDependencies {
|
|
59
|
+
OAUTH2: WhookOAuth2Options;
|
|
60
|
+
oAuth2Password: WhookOAuth2PasswordService;
|
|
61
|
+
readClientGrants: WhookOAuth2ReadClientGrantsService;
|
|
14
62
|
log?: LogService;
|
|
15
63
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
64
|
+
|
|
65
|
+
export interface WhookOAuth2PasswordGranterDefinitions extends WhookOAuth2GranterDefinitions {
|
|
66
|
+
grantType: typeof PASSWORD_GRANT_TYPE;
|
|
67
|
+
authenticateParameters: {
|
|
68
|
+
username: string;
|
|
69
|
+
password: string;
|
|
70
|
+
demandedScopes: WhookAuthenticationScope[];
|
|
71
|
+
};
|
|
20
72
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
Record<string, unknown>,
|
|
25
|
-
OAuth2PasswordGranterParameters
|
|
26
|
-
>;
|
|
73
|
+
|
|
74
|
+
export type WhookOAuth2PasswordGranterService =
|
|
75
|
+
WhookOAuth2GranterService<WhookOAuth2PasswordGranterDefinitions>;
|
|
27
76
|
|
|
28
77
|
export default location(
|
|
29
78
|
autoService(initOAuth2PasswordGranter),
|
|
@@ -33,44 +82,71 @@ export default location(
|
|
|
33
82
|
// Resource Owner Password Credentials Grant
|
|
34
83
|
// https://tools.ietf.org/html/rfc6749#section-4.3
|
|
35
84
|
async function initOAuth2PasswordGranter({
|
|
85
|
+
OAUTH2,
|
|
36
86
|
oAuth2Password,
|
|
37
|
-
|
|
87
|
+
readClientGrants,
|
|
38
88
|
log = noop,
|
|
39
|
-
}:
|
|
89
|
+
}: WhookOAuth2PasswordGranterDependencies): Promise<WhookOAuth2PasswordGranterService> {
|
|
90
|
+
log(
|
|
91
|
+
'warning',
|
|
92
|
+
`⚠️ - Using the password flow is deprecated and not recommended.`,
|
|
93
|
+
);
|
|
94
|
+
|
|
40
95
|
const authenticateWithPassword: NonNullable<
|
|
41
|
-
|
|
42
|
-
>
|
|
43
|
-
{ username, password,
|
|
44
|
-
|
|
96
|
+
WhookOAuth2PasswordGranterService['authenticate']
|
|
97
|
+
> = async (
|
|
98
|
+
{ username, password, demandedScopes },
|
|
99
|
+
optionalAuthenticationData,
|
|
45
100
|
) => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
101
|
+
const usableClientId =
|
|
102
|
+
optionalAuthenticationData?.clientId || OAUTH2.rootClientId;
|
|
103
|
+
|
|
104
|
+
const grants = await readClientGrants(usableClientId);
|
|
105
|
+
|
|
106
|
+
if (usableClientId !== grants.authenticationData.clientId) {
|
|
107
|
+
throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
|
|
108
|
+
usableClientId,
|
|
109
|
+
grants.authenticationData.clientId,
|
|
110
|
+
]);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (!grants.isPublicClient) {
|
|
114
|
+
if (!optionalAuthenticationData) {
|
|
115
|
+
throw new YError('E_OAUTH2_AUTHENTICATION_REQUIRED', [
|
|
116
|
+
grants.authenticationData.clientId,
|
|
117
|
+
]);
|
|
118
|
+
}
|
|
49
119
|
}
|
|
50
120
|
|
|
51
|
-
|
|
52
|
-
applicationId: authenticationData.applicationId,
|
|
53
|
-
type: 'password',
|
|
54
|
-
scope: demandedScope,
|
|
55
|
-
});
|
|
121
|
+
checkGrantType(grants.allowedGrantTypes, PASSWORD_GRANT_TYPE);
|
|
56
122
|
|
|
57
|
-
const
|
|
58
|
-
authenticationData,
|
|
123
|
+
const userAuthenticationData = await oAuth2Password.check(
|
|
124
|
+
optionalAuthenticationData || grants.authenticationData,
|
|
59
125
|
username,
|
|
60
126
|
password,
|
|
61
|
-
demandedScope,
|
|
62
127
|
);
|
|
63
128
|
|
|
64
|
-
|
|
129
|
+
const filteredScopes = filterScopes(
|
|
130
|
+
demandedScopes.length ? demandedScopes : userAuthenticationData.scopes,
|
|
131
|
+
grants.allowedScopes,
|
|
132
|
+
!!OAUTH2.strictScopesChecks,
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
...userAuthenticationData,
|
|
137
|
+
scopes: filterScopes(
|
|
138
|
+
filteredScopes,
|
|
139
|
+
userAuthenticationData.scopes,
|
|
140
|
+
!!OAUTH2.strictScopesChecks,
|
|
141
|
+
),
|
|
142
|
+
};
|
|
65
143
|
};
|
|
66
144
|
|
|
67
145
|
log('debug', '👫 - OAuth2PasswordGranter Service Initialized!');
|
|
68
146
|
|
|
69
147
|
return {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
authenticate: authenticateWithPassword,
|
|
74
|
-
},
|
|
148
|
+
grantType: PASSWORD_GRANT_TYPE,
|
|
149
|
+
issuesRefreshToken: true,
|
|
150
|
+
authenticate: authenticateWithPassword,
|
|
75
151
|
};
|
|
76
152
|
}
|
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
import { describe, test, beforeEach, jest, expect } from '@jest/globals';
|
|
2
|
+
import {
|
|
3
|
+
type WhookOAuth2Options,
|
|
4
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
5
|
+
} from './oAuth2Granters.js';
|
|
6
|
+
import initOAuth2RefreshTokenGranter, {
|
|
7
|
+
type WhookOAuth2RefreshTokenService,
|
|
8
|
+
} from './oAuth2RefreshTokenGranter.js';
|
|
9
|
+
import { YError } from 'yerror';
|
|
10
|
+
|
|
11
|
+
describe('OAuth2RefreshTokenGranter', () => {
|
|
12
|
+
const OAUTH2: WhookOAuth2Options = {
|
|
13
|
+
rootClientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
14
|
+
authenticateURL: 'https://auth.example.com/sign_in',
|
|
15
|
+
allowedScopes: ['user', 'admin', 'root'],
|
|
16
|
+
};
|
|
17
|
+
const oAuth2RefreshToken = {
|
|
18
|
+
check: jest.fn<WhookOAuth2RefreshTokenService['check']>(),
|
|
19
|
+
};
|
|
20
|
+
const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
|
|
21
|
+
const log = jest.fn();
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
oAuth2RefreshToken.check.mockReset();
|
|
25
|
+
readClientGrants.mockReset();
|
|
26
|
+
log.mockReset();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('should work with a valid request', async () => {
|
|
30
|
+
const oAuth2RefreshTokenGranter = await initOAuth2RefreshTokenGranter({
|
|
31
|
+
OAUTH2,
|
|
32
|
+
readClientGrants,
|
|
33
|
+
oAuth2RefreshToken,
|
|
34
|
+
log,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
readClientGrants.mockResolvedValue({
|
|
38
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
39
|
+
allowedGrantTypes: ['refresh_token', 'authorization_code'],
|
|
40
|
+
allowedScopes: ['user', 'admin'],
|
|
41
|
+
authenticationData: {
|
|
42
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
43
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
44
|
+
scopes: ['user', 'admin'],
|
|
45
|
+
},
|
|
46
|
+
isPublicClient: false,
|
|
47
|
+
});
|
|
48
|
+
oAuth2RefreshToken.check.mockResolvedValueOnce({
|
|
49
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
50
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
51
|
+
scopes: ['user'],
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const authenticatorResult = await oAuth2RefreshTokenGranter.authenticate?.(
|
|
55
|
+
{
|
|
56
|
+
refreshToken: 'a_refresh_token',
|
|
57
|
+
demandedScopes: ['user', 'root'],
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
61
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
62
|
+
scopes: ['user'],
|
|
63
|
+
},
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect({
|
|
67
|
+
authenticatorResult,
|
|
68
|
+
oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
|
|
69
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
70
|
+
logCalls: log.mock.calls,
|
|
71
|
+
}).toMatchInlineSnapshot(`
|
|
72
|
+
{
|
|
73
|
+
"authenticatorResult": {
|
|
74
|
+
"clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
|
|
75
|
+
"scopes": [
|
|
76
|
+
"user",
|
|
77
|
+
],
|
|
78
|
+
"userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
|
|
79
|
+
},
|
|
80
|
+
"logCalls": [
|
|
81
|
+
[
|
|
82
|
+
"debug",
|
|
83
|
+
"👫 - OAuth2RefreshTokenGranter Service Initialized!",
|
|
84
|
+
],
|
|
85
|
+
],
|
|
86
|
+
"oAuth2RefreshTokenCheckCalls": [
|
|
87
|
+
[
|
|
88
|
+
"a_refresh_token",
|
|
89
|
+
],
|
|
90
|
+
],
|
|
91
|
+
"readClientGrantsCalls": [
|
|
92
|
+
[
|
|
93
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
94
|
+
],
|
|
95
|
+
],
|
|
96
|
+
}
|
|
97
|
+
`);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('should work with a valid request and filter scopes', async () => {
|
|
101
|
+
const oAuth2RefreshTokenGranter = await initOAuth2RefreshTokenGranter({
|
|
102
|
+
OAUTH2,
|
|
103
|
+
readClientGrants,
|
|
104
|
+
oAuth2RefreshToken,
|
|
105
|
+
log,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
readClientGrants.mockResolvedValue({
|
|
109
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
110
|
+
allowedGrantTypes: ['refresh_token', 'authorization_code'],
|
|
111
|
+
allowedScopes: ['user', 'admin'],
|
|
112
|
+
authenticationData: {
|
|
113
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
114
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
115
|
+
scopes: ['user', 'admin'],
|
|
116
|
+
},
|
|
117
|
+
isPublicClient: false,
|
|
118
|
+
});
|
|
119
|
+
oAuth2RefreshToken.check.mockResolvedValueOnce({
|
|
120
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
121
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
122
|
+
scopes: ['user', 'oauth2'],
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const authenticatorResult = await oAuth2RefreshTokenGranter.authenticate?.(
|
|
126
|
+
{
|
|
127
|
+
refreshToken: 'a_refresh_token',
|
|
128
|
+
demandedScopes: ['user', 'admin', 'oauth2', 'a_scope'],
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
132
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
133
|
+
scopes: ['user'],
|
|
134
|
+
},
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
expect({
|
|
138
|
+
authenticatorResult,
|
|
139
|
+
oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
|
|
140
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
141
|
+
logCalls: log.mock.calls,
|
|
142
|
+
}).toMatchInlineSnapshot(`
|
|
143
|
+
{
|
|
144
|
+
"authenticatorResult": {
|
|
145
|
+
"clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
|
|
146
|
+
"scopes": [
|
|
147
|
+
"user",
|
|
148
|
+
],
|
|
149
|
+
"userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
|
|
150
|
+
},
|
|
151
|
+
"logCalls": [
|
|
152
|
+
[
|
|
153
|
+
"debug",
|
|
154
|
+
"👫 - OAuth2RefreshTokenGranter Service Initialized!",
|
|
155
|
+
],
|
|
156
|
+
],
|
|
157
|
+
"oAuth2RefreshTokenCheckCalls": [
|
|
158
|
+
[
|
|
159
|
+
"a_refresh_token",
|
|
160
|
+
],
|
|
161
|
+
],
|
|
162
|
+
"readClientGrantsCalls": [
|
|
163
|
+
[
|
|
164
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
165
|
+
],
|
|
166
|
+
],
|
|
167
|
+
}
|
|
168
|
+
`);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test('should fail with a secret client and no authentication', async () => {
|
|
172
|
+
const oAuth2RefreshTokenGranter = await initOAuth2RefreshTokenGranter({
|
|
173
|
+
OAUTH2,
|
|
174
|
+
readClientGrants,
|
|
175
|
+
oAuth2RefreshToken,
|
|
176
|
+
log,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
readClientGrants.mockResolvedValue({
|
|
180
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
181
|
+
allowedGrantTypes: ['refresh_token', 'authorization_code'],
|
|
182
|
+
allowedScopes: ['user', 'admin'],
|
|
183
|
+
authenticationData: {
|
|
184
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
185
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
186
|
+
scopes: [],
|
|
187
|
+
},
|
|
188
|
+
isPublicClient: false,
|
|
189
|
+
});
|
|
190
|
+
oAuth2RefreshToken.check.mockResolvedValueOnce({
|
|
191
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
192
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
193
|
+
scopes: [],
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
try {
|
|
197
|
+
await oAuth2RefreshTokenGranter.authenticate?.({
|
|
198
|
+
refreshToken: 'a_refresh_token',
|
|
199
|
+
demandedScopes: ['user'],
|
|
200
|
+
});
|
|
201
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
202
|
+
} catch (err) {
|
|
203
|
+
expect({
|
|
204
|
+
err,
|
|
205
|
+
oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
|
|
206
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
207
|
+
logCalls: log.mock.calls,
|
|
208
|
+
}).toMatchInlineSnapshot(`
|
|
209
|
+
{
|
|
210
|
+
"err": [YError: E_OAUTH2_AUTHENTICATION_REQUIRED (["abbacaca-abba-caca-abba-cacaabbacaca"]): E_OAUTH2_AUTHENTICATION_REQUIRED],
|
|
211
|
+
"logCalls": [
|
|
212
|
+
[
|
|
213
|
+
"debug",
|
|
214
|
+
"👫 - OAuth2RefreshTokenGranter Service Initialized!",
|
|
215
|
+
],
|
|
216
|
+
],
|
|
217
|
+
"oAuth2RefreshTokenCheckCalls": [
|
|
218
|
+
[
|
|
219
|
+
"a_refresh_token",
|
|
220
|
+
],
|
|
221
|
+
],
|
|
222
|
+
"readClientGrantsCalls": [
|
|
223
|
+
[
|
|
224
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
225
|
+
],
|
|
226
|
+
],
|
|
227
|
+
}
|
|
228
|
+
`);
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test('should fail with a client without refresh_token grant', async () => {
|
|
233
|
+
const oAuth2RefreshTokenGranter = await initOAuth2RefreshTokenGranter({
|
|
234
|
+
OAUTH2,
|
|
235
|
+
readClientGrants,
|
|
236
|
+
oAuth2RefreshToken,
|
|
237
|
+
log,
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
readClientGrants.mockResolvedValue({
|
|
241
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
242
|
+
allowedGrantTypes: ['authorization_code'],
|
|
243
|
+
allowedScopes: ['user', 'admin'],
|
|
244
|
+
authenticationData: {
|
|
245
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
246
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
247
|
+
scopes: [],
|
|
248
|
+
},
|
|
249
|
+
isPublicClient: false,
|
|
250
|
+
});
|
|
251
|
+
oAuth2RefreshToken.check.mockResolvedValueOnce({
|
|
252
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
253
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
254
|
+
scopes: ['user'],
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
await oAuth2RefreshTokenGranter.authenticate?.(
|
|
259
|
+
{
|
|
260
|
+
refreshToken: 'a_refresh_token',
|
|
261
|
+
demandedScopes: ['user'],
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
265
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
266
|
+
scopes: ['user'],
|
|
267
|
+
},
|
|
268
|
+
);
|
|
269
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
270
|
+
} catch (err) {
|
|
271
|
+
expect({
|
|
272
|
+
err,
|
|
273
|
+
oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
|
|
274
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
275
|
+
logCalls: log.mock.calls,
|
|
276
|
+
}).toMatchInlineSnapshot(`
|
|
277
|
+
{
|
|
278
|
+
"err": [YError: E_OAUTH2_GRANT_TYPE_NOT_ALLOWED (["refresh_token",["authorization_code"]]): E_OAUTH2_GRANT_TYPE_NOT_ALLOWED],
|
|
279
|
+
"logCalls": [
|
|
280
|
+
[
|
|
281
|
+
"debug",
|
|
282
|
+
"👫 - OAuth2RefreshTokenGranter Service Initialized!",
|
|
283
|
+
],
|
|
284
|
+
],
|
|
285
|
+
"oAuth2RefreshTokenCheckCalls": [
|
|
286
|
+
[
|
|
287
|
+
"a_refresh_token",
|
|
288
|
+
],
|
|
289
|
+
],
|
|
290
|
+
"readClientGrantsCalls": [
|
|
291
|
+
[
|
|
292
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
293
|
+
],
|
|
294
|
+
],
|
|
295
|
+
}
|
|
296
|
+
`);
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
test('should fail with a client with a bad scope for the client', async () => {
|
|
301
|
+
const oAuth2RefreshTokenGranter = await initOAuth2RefreshTokenGranter({
|
|
302
|
+
OAUTH2: {
|
|
303
|
+
...OAUTH2,
|
|
304
|
+
strictScopesChecks: true,
|
|
305
|
+
},
|
|
306
|
+
readClientGrants,
|
|
307
|
+
oAuth2RefreshToken,
|
|
308
|
+
log,
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
readClientGrants.mockResolvedValue({
|
|
312
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
313
|
+
allowedGrantTypes: ['refresh_token'],
|
|
314
|
+
allowedScopes: ['user'],
|
|
315
|
+
authenticationData: {
|
|
316
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
317
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
318
|
+
scopes: [],
|
|
319
|
+
},
|
|
320
|
+
isPublicClient: false,
|
|
321
|
+
});
|
|
322
|
+
oAuth2RefreshToken.check.mockResolvedValueOnce({
|
|
323
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
324
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
325
|
+
scopes: ['user', 'admin'],
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
try {
|
|
329
|
+
await oAuth2RefreshTokenGranter.authenticate?.(
|
|
330
|
+
{
|
|
331
|
+
refreshToken: 'a_refresh_token',
|
|
332
|
+
demandedScopes: ['user', 'admin'],
|
|
333
|
+
},
|
|
334
|
+
{
|
|
335
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
336
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
337
|
+
scopes: ['user'],
|
|
338
|
+
},
|
|
339
|
+
);
|
|
340
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
341
|
+
} catch (err) {
|
|
342
|
+
expect({
|
|
343
|
+
err,
|
|
344
|
+
oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
|
|
345
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
346
|
+
logCalls: log.mock.calls,
|
|
347
|
+
}).toMatchInlineSnapshot(`
|
|
348
|
+
{
|
|
349
|
+
"err": [YError: E_OAUTH2_BAD_SCOPE (["admin"]): E_OAUTH2_BAD_SCOPE],
|
|
350
|
+
"logCalls": [
|
|
351
|
+
[
|
|
352
|
+
"debug",
|
|
353
|
+
"👫 - OAuth2RefreshTokenGranter Service Initialized!",
|
|
354
|
+
],
|
|
355
|
+
],
|
|
356
|
+
"oAuth2RefreshTokenCheckCalls": [
|
|
357
|
+
[
|
|
358
|
+
"a_refresh_token",
|
|
359
|
+
],
|
|
360
|
+
],
|
|
361
|
+
"readClientGrantsCalls": [
|
|
362
|
+
[
|
|
363
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
364
|
+
],
|
|
365
|
+
],
|
|
366
|
+
}
|
|
367
|
+
`);
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
test('should fail with a client with a bad scope for the user', async () => {
|
|
372
|
+
const oAuth2RefreshTokenGranter = await initOAuth2RefreshTokenGranter({
|
|
373
|
+
OAUTH2: {
|
|
374
|
+
...OAUTH2,
|
|
375
|
+
strictScopesChecks: true,
|
|
376
|
+
},
|
|
377
|
+
readClientGrants,
|
|
378
|
+
oAuth2RefreshToken,
|
|
379
|
+
log,
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
readClientGrants.mockResolvedValue({
|
|
383
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
384
|
+
allowedGrantTypes: ['refresh_token'],
|
|
385
|
+
allowedScopes: ['user', 'admin'],
|
|
386
|
+
authenticationData: {
|
|
387
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
388
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
389
|
+
scopes: [],
|
|
390
|
+
},
|
|
391
|
+
isPublicClient: false,
|
|
392
|
+
});
|
|
393
|
+
oAuth2RefreshToken.check.mockResolvedValueOnce({
|
|
394
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
395
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
396
|
+
scopes: ['user'],
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
try {
|
|
400
|
+
await oAuth2RefreshTokenGranter.authenticate?.(
|
|
401
|
+
{
|
|
402
|
+
refreshToken: 'a_refresh_token',
|
|
403
|
+
demandedScopes: ['user', 'admin'],
|
|
404
|
+
},
|
|
405
|
+
{
|
|
406
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
407
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
408
|
+
scopes: ['user'],
|
|
409
|
+
},
|
|
410
|
+
);
|
|
411
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
412
|
+
} catch (err) {
|
|
413
|
+
expect({
|
|
414
|
+
err,
|
|
415
|
+
oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
|
|
416
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
417
|
+
logCalls: log.mock.calls,
|
|
418
|
+
}).toMatchInlineSnapshot(`
|
|
419
|
+
{
|
|
420
|
+
"err": [YError: E_OAUTH2_BAD_SCOPE (["admin"]): E_OAUTH2_BAD_SCOPE],
|
|
421
|
+
"logCalls": [
|
|
422
|
+
[
|
|
423
|
+
"debug",
|
|
424
|
+
"👫 - OAuth2RefreshTokenGranter Service Initialized!",
|
|
425
|
+
],
|
|
426
|
+
],
|
|
427
|
+
"oAuth2RefreshTokenCheckCalls": [
|
|
428
|
+
[
|
|
429
|
+
"a_refresh_token",
|
|
430
|
+
],
|
|
431
|
+
],
|
|
432
|
+
"readClientGrantsCalls": [
|
|
433
|
+
[
|
|
434
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
435
|
+
],
|
|
436
|
+
],
|
|
437
|
+
}
|
|
438
|
+
`);
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
});
|