@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,141 @@
|
|
|
1
|
+
import { autoService, location } from 'knifecycle';
|
|
2
|
+
import { noop } from '@whook/whook';
|
|
3
|
+
import {
|
|
4
|
+
type RandomBytesService,
|
|
5
|
+
type LogService,
|
|
6
|
+
type TimeService,
|
|
7
|
+
} from 'common-services';
|
|
8
|
+
import { type KVStoreService } from 'memory-kv-store';
|
|
9
|
+
import { type WhookOAuth2ClientId } from './oAuth2Granters.js';
|
|
10
|
+
import { YError } from 'yerror';
|
|
11
|
+
import {
|
|
12
|
+
buildRequestURI,
|
|
13
|
+
readRequestURI,
|
|
14
|
+
type WhookOAuth2AuthorizationRequestURI,
|
|
15
|
+
} from '../libs/authorizationRequests.js';
|
|
16
|
+
import { type WhookOAuth2AuthorizeRequestParameters } from '../routes/getOAuth2Authorize.js';
|
|
17
|
+
|
|
18
|
+
export interface WhookOAuth2AuthorizationRequestData {
|
|
19
|
+
clientId: WhookOAuth2ClientId;
|
|
20
|
+
parameters: WhookOAuth2AuthorizeRequestParameters;
|
|
21
|
+
expiresAt: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface WhookOAuth2AuthorizationRequestsOptions {
|
|
25
|
+
/** Whether PAR is activated and required */
|
|
26
|
+
mode: 'enabled' | 'required' | 'disabled';
|
|
27
|
+
/** Time to live in milliseconds */
|
|
28
|
+
ttl?: number;
|
|
29
|
+
}
|
|
30
|
+
export const DEFAULT_OAUTH2_PAR = {
|
|
31
|
+
mode: 'disabled',
|
|
32
|
+
ttl: 90 * 1000,
|
|
33
|
+
} as const satisfies WhookOAuth2AuthorizationRequestsOptions;
|
|
34
|
+
|
|
35
|
+
export interface WhookOAuth2AuthorizationRequestsConfig {
|
|
36
|
+
OAUTH2_PAR?: WhookOAuth2AuthorizationRequestsOptions;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface WhookOAuth2AuthorizationRequestsDependencies extends WhookOAuth2AuthorizationRequestsConfig {
|
|
40
|
+
oAuth2AuthorizationRequestsStore: Pick<
|
|
41
|
+
KVStoreService<WhookOAuth2AuthorizationRequestData>,
|
|
42
|
+
'set' | 'getDelete'
|
|
43
|
+
>;
|
|
44
|
+
randomBytes: RandomBytesService;
|
|
45
|
+
time?: TimeService;
|
|
46
|
+
log?: LogService;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A service to create and check authorization requests
|
|
51
|
+
*/
|
|
52
|
+
export interface WhookOAuth2AuthorizationRequestsService {
|
|
53
|
+
create: (
|
|
54
|
+
clientId: WhookOAuth2ClientId,
|
|
55
|
+
parameters: WhookOAuth2AuthorizeRequestParameters,
|
|
56
|
+
) => Promise<{
|
|
57
|
+
requestURI: WhookOAuth2AuthorizationRequestURI;
|
|
58
|
+
expiresIn: number;
|
|
59
|
+
}>;
|
|
60
|
+
check: (
|
|
61
|
+
clientId: WhookOAuth2ClientId,
|
|
62
|
+
requestURI: WhookOAuth2AuthorizationRequestURI,
|
|
63
|
+
) => Promise<WhookOAuth2AuthorizationRequestData | undefined>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function initOAuth2AuthorizationRequests({
|
|
67
|
+
OAUTH2_PAR,
|
|
68
|
+
oAuth2AuthorizationRequestsStore,
|
|
69
|
+
randomBytes,
|
|
70
|
+
time = Date.now.bind(Date),
|
|
71
|
+
log = noop,
|
|
72
|
+
}: WhookOAuth2AuthorizationRequestsDependencies): Promise<WhookOAuth2AuthorizationRequestsService> {
|
|
73
|
+
const oAuth2PAROptions: Required<WhookOAuth2AuthorizationRequestsOptions> = {
|
|
74
|
+
...DEFAULT_OAUTH2_PAR,
|
|
75
|
+
...OAUTH2_PAR,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
log('debug', '👫 - OAuth2AuthorizationRequests Service Initialized!');
|
|
79
|
+
|
|
80
|
+
const oAuth2AuthorizationRequests: WhookOAuth2AuthorizationRequestsService = {
|
|
81
|
+
create: async (clientId, parameters) => {
|
|
82
|
+
const currentTime = time();
|
|
83
|
+
const requestId = (await randomBytes(16)).toString('base64url');
|
|
84
|
+
const expiresAt = currentTime + oAuth2PAROptions.ttl;
|
|
85
|
+
const request: WhookOAuth2AuthorizationRequestData = {
|
|
86
|
+
parameters,
|
|
87
|
+
expiresAt,
|
|
88
|
+
clientId,
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
await oAuth2AuthorizationRequestsStore.set(
|
|
92
|
+
requestId,
|
|
93
|
+
request,
|
|
94
|
+
oAuth2PAROptions.ttl,
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
requestURI: buildRequestURI(requestId),
|
|
99
|
+
expiresIn: oAuth2PAROptions.ttl,
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
check: async (clientId, requestURI) => {
|
|
103
|
+
const requestId = readRequestURI(requestURI);
|
|
104
|
+
const currentTime = time();
|
|
105
|
+
const request =
|
|
106
|
+
await oAuth2AuthorizationRequestsStore.getDelete(requestId);
|
|
107
|
+
|
|
108
|
+
if (!request) {
|
|
109
|
+
throw new YError('E_OAUTH2_BAD_REQUEST_URI', [
|
|
110
|
+
requestURI,
|
|
111
|
+
undefined,
|
|
112
|
+
undefined,
|
|
113
|
+
]);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (request.expiresAt <= currentTime) {
|
|
117
|
+
throw new YError('E_OAUTH2_BAD_REQUEST_URI', [
|
|
118
|
+
requestURI,
|
|
119
|
+
request.expiresAt,
|
|
120
|
+
currentTime,
|
|
121
|
+
]);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (clientId !== request.clientId) {
|
|
125
|
+
throw new YError('E_OAUTH2_CLIENT_MISMATCH', [
|
|
126
|
+
clientId,
|
|
127
|
+
request.clientId,
|
|
128
|
+
]);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return request;
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
return oAuth2AuthorizationRequests;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export default location(
|
|
139
|
+
autoService(initOAuth2AuthorizationRequests),
|
|
140
|
+
import.meta.url,
|
|
141
|
+
);
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { describe, test, beforeEach, jest, expect } from '@jest/globals';
|
|
2
|
+
import {
|
|
3
|
+
type WhookOAuth2Options,
|
|
4
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
5
|
+
} from './oAuth2Granters.js';
|
|
6
|
+
import initOAuth2ClientCredentialsGranter from './oAuth2ClientCredentialsGranter.js';
|
|
7
|
+
import { YError } from 'yerror';
|
|
8
|
+
|
|
9
|
+
describe('OAuth2ClientCredentialsGranter', () => {
|
|
10
|
+
const OAUTH2: WhookOAuth2Options = {
|
|
11
|
+
rootClientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
12
|
+
authenticateURL: 'https://auth.example.com/sign_in',
|
|
13
|
+
allowedScopes: ['user', 'admin', 'root'],
|
|
14
|
+
};
|
|
15
|
+
const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
|
|
16
|
+
const log = jest.fn();
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
readClientGrants.mockReset();
|
|
20
|
+
log.mockReset();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('should work with a complete valid flow', async () => {
|
|
24
|
+
const oAuth2ClientCredentialsGranter =
|
|
25
|
+
await initOAuth2ClientCredentialsGranter({
|
|
26
|
+
OAUTH2,
|
|
27
|
+
readClientGrants,
|
|
28
|
+
log,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
readClientGrants.mockResolvedValue({
|
|
32
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
33
|
+
allowedGrantTypes: ['client_credentials', 'authorization_code'],
|
|
34
|
+
allowedScopes: ['user', 'admin'],
|
|
35
|
+
authenticationData: {
|
|
36
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
37
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
38
|
+
scopes: [],
|
|
39
|
+
},
|
|
40
|
+
isPublicClient: false,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const authenticatorResult =
|
|
44
|
+
await oAuth2ClientCredentialsGranter.authenticate?.(
|
|
45
|
+
{
|
|
46
|
+
demandedScopes: ['user', 'root'],
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
50
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
51
|
+
scopes: ['user'],
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
expect({
|
|
56
|
+
authenticatorResult,
|
|
57
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
58
|
+
logCalls: log.mock.calls,
|
|
59
|
+
}).toMatchInlineSnapshot(`
|
|
60
|
+
{
|
|
61
|
+
"authenticatorResult": {
|
|
62
|
+
"clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
|
|
63
|
+
"scopes": [
|
|
64
|
+
"user",
|
|
65
|
+
],
|
|
66
|
+
"userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
|
|
67
|
+
},
|
|
68
|
+
"logCalls": [
|
|
69
|
+
[
|
|
70
|
+
"debug",
|
|
71
|
+
"👫 - OAuth2ClientCredentialsGranter Service Initialized!",
|
|
72
|
+
],
|
|
73
|
+
],
|
|
74
|
+
"readClientGrantsCalls": [
|
|
75
|
+
[
|
|
76
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
77
|
+
],
|
|
78
|
+
],
|
|
79
|
+
}
|
|
80
|
+
`);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('should work with a complete valid flow and filter scopes', async () => {
|
|
84
|
+
const oAuth2ClientCredentialsGranter =
|
|
85
|
+
await initOAuth2ClientCredentialsGranter({
|
|
86
|
+
OAUTH2,
|
|
87
|
+
readClientGrants,
|
|
88
|
+
log,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
readClientGrants.mockResolvedValue({
|
|
92
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
93
|
+
allowedGrantTypes: ['client_credentials', 'authorization_code'],
|
|
94
|
+
allowedScopes: ['user', 'admin'],
|
|
95
|
+
authenticationData: {
|
|
96
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
97
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
98
|
+
scopes: [],
|
|
99
|
+
},
|
|
100
|
+
isPublicClient: false,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const authenticatorResult =
|
|
104
|
+
await oAuth2ClientCredentialsGranter.authenticate?.(
|
|
105
|
+
{
|
|
106
|
+
demandedScopes: ['user', 'admin', 'oauth2', 'a_scope'],
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
110
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
111
|
+
scopes: ['user'],
|
|
112
|
+
},
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
expect({
|
|
116
|
+
authenticatorResult,
|
|
117
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
118
|
+
logCalls: log.mock.calls,
|
|
119
|
+
}).toMatchInlineSnapshot(`
|
|
120
|
+
{
|
|
121
|
+
"authenticatorResult": {
|
|
122
|
+
"clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
|
|
123
|
+
"scopes": [
|
|
124
|
+
"user",
|
|
125
|
+
"admin",
|
|
126
|
+
],
|
|
127
|
+
"userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
|
|
128
|
+
},
|
|
129
|
+
"logCalls": [
|
|
130
|
+
[
|
|
131
|
+
"debug",
|
|
132
|
+
"👫 - OAuth2ClientCredentialsGranter Service Initialized!",
|
|
133
|
+
],
|
|
134
|
+
],
|
|
135
|
+
"readClientGrantsCalls": [
|
|
136
|
+
[
|
|
137
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
138
|
+
],
|
|
139
|
+
],
|
|
140
|
+
}
|
|
141
|
+
`);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test('should fail with no authentication', async () => {
|
|
145
|
+
const oAuth2ClientCredentialsGranter =
|
|
146
|
+
await initOAuth2ClientCredentialsGranter({
|
|
147
|
+
OAUTH2,
|
|
148
|
+
readClientGrants,
|
|
149
|
+
log,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
readClientGrants.mockResolvedValue({
|
|
153
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
154
|
+
allowedGrantTypes: ['client_credentials', 'authorization_code'],
|
|
155
|
+
allowedScopes: ['user', 'admin'],
|
|
156
|
+
authenticationData: {
|
|
157
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
158
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
159
|
+
scopes: [],
|
|
160
|
+
},
|
|
161
|
+
isPublicClient: false,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
await oAuth2ClientCredentialsGranter.authenticate?.({
|
|
166
|
+
demandedScopes: ['user'],
|
|
167
|
+
});
|
|
168
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
169
|
+
} catch (err) {
|
|
170
|
+
expect({
|
|
171
|
+
err,
|
|
172
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
173
|
+
logCalls: log.mock.calls,
|
|
174
|
+
}).toMatchInlineSnapshot(`
|
|
175
|
+
{
|
|
176
|
+
"err": [YError: E_UNAUTHORIZED ([]): E_UNAUTHORIZED],
|
|
177
|
+
"logCalls": [
|
|
178
|
+
[
|
|
179
|
+
"debug",
|
|
180
|
+
"👫 - OAuth2ClientCredentialsGranter Service Initialized!",
|
|
181
|
+
],
|
|
182
|
+
],
|
|
183
|
+
"readClientGrantsCalls": [],
|
|
184
|
+
}
|
|
185
|
+
`);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('should fail with a client with not client_credentials grant', async () => {
|
|
190
|
+
const oAuth2ClientCredentialsGranter =
|
|
191
|
+
await initOAuth2ClientCredentialsGranter({
|
|
192
|
+
OAUTH2,
|
|
193
|
+
readClientGrants,
|
|
194
|
+
log,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
readClientGrants.mockResolvedValue({
|
|
198
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
199
|
+
allowedGrantTypes: ['authorization_code'],
|
|
200
|
+
allowedScopes: ['user', 'admin'],
|
|
201
|
+
authenticationData: {
|
|
202
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
203
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
204
|
+
scopes: [],
|
|
205
|
+
},
|
|
206
|
+
isPublicClient: false,
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
await oAuth2ClientCredentialsGranter.authenticate?.(
|
|
211
|
+
{
|
|
212
|
+
demandedScopes: ['user'],
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
216
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
217
|
+
scopes: ['user'],
|
|
218
|
+
},
|
|
219
|
+
);
|
|
220
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
221
|
+
} catch (err) {
|
|
222
|
+
expect({
|
|
223
|
+
err,
|
|
224
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
225
|
+
logCalls: log.mock.calls,
|
|
226
|
+
}).toMatchInlineSnapshot(`
|
|
227
|
+
{
|
|
228
|
+
"err": [YError: E_OAUTH2_GRANT_TYPE_NOT_ALLOWED (["client_credentials",["authorization_code"]]): E_OAUTH2_GRANT_TYPE_NOT_ALLOWED],
|
|
229
|
+
"logCalls": [
|
|
230
|
+
[
|
|
231
|
+
"debug",
|
|
232
|
+
"👫 - OAuth2ClientCredentialsGranter Service Initialized!",
|
|
233
|
+
],
|
|
234
|
+
],
|
|
235
|
+
"readClientGrantsCalls": [
|
|
236
|
+
[
|
|
237
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
238
|
+
],
|
|
239
|
+
],
|
|
240
|
+
}
|
|
241
|
+
`);
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('should fail with a client with a bad scope for the client', async () => {
|
|
246
|
+
const oAuth2ClientCredentialsGranter =
|
|
247
|
+
await initOAuth2ClientCredentialsGranter({
|
|
248
|
+
OAUTH2: {
|
|
249
|
+
...OAUTH2,
|
|
250
|
+
strictScopesChecks: true,
|
|
251
|
+
},
|
|
252
|
+
readClientGrants,
|
|
253
|
+
log,
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
readClientGrants.mockResolvedValue({
|
|
257
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
258
|
+
allowedGrantTypes: ['client_credentials'],
|
|
259
|
+
allowedScopes: ['user'],
|
|
260
|
+
authenticationData: {
|
|
261
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
262
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
263
|
+
scopes: [],
|
|
264
|
+
},
|
|
265
|
+
isPublicClient: false,
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
try {
|
|
269
|
+
await oAuth2ClientCredentialsGranter.authenticate?.(
|
|
270
|
+
{
|
|
271
|
+
demandedScopes: ['user', 'admin'],
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
275
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
276
|
+
scopes: ['user'],
|
|
277
|
+
},
|
|
278
|
+
);
|
|
279
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
280
|
+
} catch (err) {
|
|
281
|
+
expect({
|
|
282
|
+
err,
|
|
283
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
284
|
+
logCalls: log.mock.calls,
|
|
285
|
+
}).toMatchInlineSnapshot(`
|
|
286
|
+
{
|
|
287
|
+
"err": [YError: E_OAUTH2_BAD_SCOPE (["admin"]): E_OAUTH2_BAD_SCOPE],
|
|
288
|
+
"logCalls": [
|
|
289
|
+
[
|
|
290
|
+
"debug",
|
|
291
|
+
"👫 - OAuth2ClientCredentialsGranter Service Initialized!",
|
|
292
|
+
],
|
|
293
|
+
],
|
|
294
|
+
"readClientGrantsCalls": [
|
|
295
|
+
[
|
|
296
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
297
|
+
],
|
|
298
|
+
],
|
|
299
|
+
}
|
|
300
|
+
`);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
});
|
|
@@ -1,30 +1,52 @@
|
|
|
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
|
|
6
|
+
type WhookOAuth2GranterService,
|
|
7
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
8
|
+
type WhookOAuth2Options,
|
|
9
|
+
type WhookOAuth2GranterDefinitions,
|
|
8
10
|
} from './oAuth2Granters.js';
|
|
9
|
-
import { type
|
|
10
|
-
import {
|
|
11
|
+
import { type WhookAuthenticationScope } from '@whook/authorization';
|
|
12
|
+
import { checkGrantType } from '../libs/grants.js';
|
|
13
|
+
import { filterScopes } from '../libs/scopes.js';
|
|
14
|
+
import { scopeSchema } from '../libs/schemas.js';
|
|
11
15
|
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
export const CLIENT_CREDENTIALS_GRANT_TYPE = 'client_credentials';
|
|
17
|
+
|
|
18
|
+
export const clientCredentialsTokenRequestBodySchema = {
|
|
19
|
+
name: 'ClientCredentialsRequestBody',
|
|
20
|
+
schema: {
|
|
21
|
+
type: 'object',
|
|
22
|
+
description:
|
|
23
|
+
'Client credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.4',
|
|
24
|
+
required: ['grant_type'],
|
|
25
|
+
properties: {
|
|
26
|
+
grant_type: {
|
|
27
|
+
type: 'string',
|
|
28
|
+
const: CLIENT_CREDENTIALS_GRANT_TYPE,
|
|
29
|
+
},
|
|
30
|
+
scope: refersTo(scopeSchema),
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
} as const satisfies WhookAPISchemaDefinition;
|
|
34
|
+
|
|
35
|
+
export interface WhookOAuth2ClientCredentialsGranterDependencies {
|
|
36
|
+
OAUTH2: WhookOAuth2Options;
|
|
37
|
+
readClientGrants: WhookOAuth2ReadClientGrantsService;
|
|
15
38
|
log?: LogService;
|
|
16
39
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
40
|
+
|
|
41
|
+
export interface WhookOAuth2ClientCredentialsGranterDefinitions extends WhookOAuth2GranterDefinitions {
|
|
42
|
+
grantType: typeof CLIENT_CREDENTIALS_GRANT_TYPE;
|
|
43
|
+
authenticateParameters: {
|
|
44
|
+
demandedScopes: WhookAuthenticationScope[];
|
|
45
|
+
};
|
|
21
46
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
Record<string, unknown>,
|
|
26
|
-
OAuth2ClientCredentialsGranterParameters
|
|
27
|
-
>;
|
|
47
|
+
|
|
48
|
+
export type WhookOAuth2ClientCredentialsGranterService =
|
|
49
|
+
WhookOAuth2GranterService<WhookOAuth2ClientCredentialsGranterDefinitions>;
|
|
28
50
|
|
|
29
51
|
export default location(
|
|
30
52
|
autoService(initOAuth2ClientCredentialsGranter),
|
|
@@ -35,45 +57,48 @@ export default location(
|
|
|
35
57
|
// https://tools.ietf.org/html/rfc6749#section-4.4
|
|
36
58
|
async function initOAuth2ClientCredentialsGranter({
|
|
37
59
|
OAUTH2,
|
|
38
|
-
|
|
60
|
+
readClientGrants,
|
|
39
61
|
log = noop,
|
|
40
|
-
}:
|
|
62
|
+
}: WhookOAuth2ClientCredentialsGranterDependencies): Promise<WhookOAuth2ClientCredentialsGranterService> {
|
|
41
63
|
const authenticateWithClientCredentials: NonNullable<
|
|
42
|
-
|
|
43
|
-
>
|
|
44
|
-
{ scope: demandedScope = '' },
|
|
45
|
-
authenticationData,
|
|
46
|
-
) => {
|
|
47
|
-
// The client must be authenticated
|
|
64
|
+
WhookOAuth2ClientCredentialsGranterService['authenticate']
|
|
65
|
+
> = async ({ demandedScopes }, authenticationData) => {
|
|
48
66
|
if (!authenticationData) {
|
|
49
67
|
throw new YError('E_UNAUTHORIZED');
|
|
50
68
|
}
|
|
51
69
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
70
|
+
const grants = await readClientGrants(authenticationData.clientId);
|
|
71
|
+
|
|
72
|
+
if (authenticationData.clientId !== grants.authenticationData.clientId) {
|
|
73
|
+
throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
|
|
74
|
+
authenticationData.clientId,
|
|
75
|
+
grants.authenticationData.clientId,
|
|
76
|
+
]);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
checkGrantType(grants.allowedGrantTypes, CLIENT_CREDENTIALS_GRANT_TYPE);
|
|
80
|
+
|
|
81
|
+
const filteredScopes = filterScopes(
|
|
82
|
+
demandedScopes.length
|
|
83
|
+
? demandedScopes
|
|
84
|
+
: OAUTH2.defaultToClientScope
|
|
85
|
+
? authenticationData.scopes
|
|
86
|
+
: [],
|
|
87
|
+
grants.allowedScopes,
|
|
88
|
+
!!OAUTH2.strictScopesChecks,
|
|
89
|
+
);
|
|
59
90
|
|
|
60
91
|
return {
|
|
61
92
|
...authenticationData,
|
|
62
|
-
|
|
63
|
-
? demandedScope
|
|
64
|
-
: OAUTH2.defaultToClientScope
|
|
65
|
-
? authenticationData.scope
|
|
66
|
-
: '',
|
|
93
|
+
scopes: filteredScopes,
|
|
67
94
|
};
|
|
68
95
|
};
|
|
69
96
|
|
|
70
97
|
log('debug', '👫 - OAuth2ClientCredentialsGranter Service Initialized!');
|
|
71
98
|
|
|
72
99
|
return {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
authenticate: authenticateWithClientCredentials,
|
|
77
|
-
},
|
|
100
|
+
grantType: CLIENT_CREDENTIALS_GRANT_TYPE,
|
|
101
|
+
issuesRefreshToken: false,
|
|
102
|
+
authenticate: authenticateWithClientCredentials,
|
|
78
103
|
};
|
|
79
104
|
}
|