@whook/oauth2 26.0.0 → 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/dist/index.d.ts +6 -2
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +421 -2
- 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.d.ts +1 -0
- package/dist/libs/authorizationRequests.test.js +24 -0
- package/dist/libs/authorizationRequests.test.js.map +1 -0
- package/dist/libs/errors.d.ts +31 -1
- package/dist/libs/errors.js +49 -0
- package/dist/libs/errors.js.map +1 -1
- package/dist/libs/redirectURI.d.ts +1 -0
- package/dist/libs/redirectURI.js +16 -0
- package/dist/libs/redirectURI.js.map +1 -1
- package/dist/libs/schemas.d.ts +8 -0
- package/dist/libs/schemas.js +8 -0
- package/dist/libs/schemas.js.map +1 -1
- package/dist/libs/verifier.d.ts +3 -0
- package/dist/libs/verifier.js +14 -0
- package/dist/libs/verifier.js.map +1 -1
- package/dist/routes/getOAuth2Authorize.d.ts +30 -14
- package/dist/routes/getOAuth2Authorize.js +92 -30
- package/dist/routes/getOAuth2Authorize.js.map +1 -1
- package/dist/routes/getOAuth2Authorize.test.js +826 -19
- package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.d.ts +10 -3
- package/dist/routes/getOAuth2WellKnownMetadata.js +30 -5
- package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.test.js +20 -18
- package/dist/routes/getOAuth2WellKnownMetadata.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/postOAuth2Token.d.ts +12 -0
- package/dist/routes/postOAuth2Token.js +12 -0
- package/dist/routes/postOAuth2Token.js.map +1 -1
- package/dist/services/authCookies.js +8 -4
- package/dist/services/authCookies.js.map +1 -1
- package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +2 -7
- package/dist/services/oAuth2AuthorizationCodeGranter.js +5 -22
- package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -1
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js +12 -141
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -1
- 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/oAuth2Granters.d.ts +10 -12
- package/dist/services/oAuth2Granters.js.map +1 -1
- package/dist/services/oAuth2ImplicitGranter.js +3 -14
- package/dist/services/oAuth2ImplicitGranter.js.map +1 -1
- package/dist/services/oAuth2ImplicitGranter.test.js +25 -163
- package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -1
- package/package.json +7 -6
- package/src/index.test.ts +459 -3
- package/src/index.ts +17 -0
- package/src/libs/authorizationRequests.test.ts +40 -0
- package/src/libs/authorizationRequests.ts +42 -0
- package/src/libs/errors.ts +87 -1
- package/src/libs/redirectURI.ts +22 -0
- package/src/libs/schemas.ts +10 -0
- package/src/libs/verifier.ts +18 -0
- package/src/routes/getOAuth2Authorize.test.ts +940 -23
- package/src/routes/getOAuth2Authorize.ts +168 -49
- package/src/routes/getOAuth2WellKnownMetadata.test.ts +22 -20
- package/src/routes/getOAuth2WellKnownMetadata.ts +44 -8
- package/src/routes/postOAuth2PushedAuthorizationRequest.test.ts +681 -0
- package/src/routes/postOAuth2PushedAuthorizationRequest.ts +292 -0
- package/src/routes/postOAuth2Token.ts +12 -0
- package/src/services/authCookies.ts +8 -4
- package/src/services/oAuth2AuthorizationCodeGranter.test.ts +12 -169
- package/src/services/oAuth2AuthorizationCodeGranter.ts +8 -31
- package/src/services/oAuth2AuthorizationRequests.test.ts +282 -0
- package/src/services/oAuth2AuthorizationRequests.ts +141 -0
- package/src/services/oAuth2Granters.ts +12 -14
- package/src/services/oAuth2ImplicitGranter.test.ts +29 -190
- package/src/services/oAuth2ImplicitGranter.ts +3 -19
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { autoService, location } from 'knifecycle';
|
|
2
|
+
import { YError } from 'yerror';
|
|
3
|
+
import { type LogService } from 'common-services';
|
|
4
|
+
import {
|
|
5
|
+
noop,
|
|
6
|
+
refersTo,
|
|
7
|
+
type WhookAPISchemaDefinition,
|
|
8
|
+
type WhookRouteDefinition,
|
|
9
|
+
} from '@whook/whook';
|
|
10
|
+
import {
|
|
11
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
12
|
+
type WhookOAuth2GranterDefinitions,
|
|
13
|
+
type WhookOAuth2GranterService,
|
|
14
|
+
type WhookOAuth2Options,
|
|
15
|
+
} from '../services/oAuth2Granters.js';
|
|
16
|
+
import {
|
|
17
|
+
filterScopes,
|
|
18
|
+
parseOAuth2Scope,
|
|
19
|
+
stringifyScopes,
|
|
20
|
+
} from '../libs/scopes.js';
|
|
21
|
+
import {
|
|
22
|
+
AUTHORIZATION_CODE_RESPONSE_TYPE,
|
|
23
|
+
codeChallengeMethodSchema,
|
|
24
|
+
codeChallengeSchema,
|
|
25
|
+
} from '../services/oAuth2AuthorizationCodeGranter.js';
|
|
26
|
+
import { IMPLICIT_RESPONSE_TYPE } from '../services/oAuth2ImplicitGranter.js';
|
|
27
|
+
import { scopeSchema, requestURISchema } from '../libs/schemas.js';
|
|
28
|
+
import {
|
|
29
|
+
checkCodeChallengeParameters,
|
|
30
|
+
PLAIN_CODE_CHALLENGE_METHOD,
|
|
31
|
+
} from '../libs/verifier.js';
|
|
32
|
+
import {
|
|
33
|
+
DEFAULT_OAUTH2_PAR,
|
|
34
|
+
type WhookOAuth2AuthorizationRequestsConfig,
|
|
35
|
+
type WhookOAuth2AuthorizationRequestsService,
|
|
36
|
+
} from '../services/oAuth2AuthorizationRequests.js';
|
|
37
|
+
import { type WhookAuthenticationData } from '@whook/authorization';
|
|
38
|
+
import { toUsableClientId } from '../libs/clients.js';
|
|
39
|
+
import { getUsableRedirectURI } from '../libs/redirectURI.js';
|
|
40
|
+
import { type WhookOAuth2AuthorizeRequestParameters } from './getOAuth2Authorize.js';
|
|
41
|
+
|
|
42
|
+
export {
|
|
43
|
+
scopeSchema,
|
|
44
|
+
requestURISchema,
|
|
45
|
+
codeChallengeSchema,
|
|
46
|
+
codeChallengeMethodSchema,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const pushedAuthorizationRequestBodySchema = {
|
|
50
|
+
name: 'PushedAuthorizationRequestBody',
|
|
51
|
+
schema: {
|
|
52
|
+
description:
|
|
53
|
+
'OAuth2 Pushed Authorization Request, see https://datatracker.ietf.org/doc/html/rfc9126#section-2.1.',
|
|
54
|
+
type: 'object',
|
|
55
|
+
required: ['response_type', 'client_id'],
|
|
56
|
+
properties: {
|
|
57
|
+
response_type: {
|
|
58
|
+
type: 'string',
|
|
59
|
+
enum: [AUTHORIZATION_CODE_RESPONSE_TYPE, IMPLICIT_RESPONSE_TYPE],
|
|
60
|
+
},
|
|
61
|
+
client_id: {
|
|
62
|
+
type: 'string',
|
|
63
|
+
},
|
|
64
|
+
redirect_uri: {
|
|
65
|
+
type: 'string',
|
|
66
|
+
pattern: '^https?://',
|
|
67
|
+
format: 'uri',
|
|
68
|
+
},
|
|
69
|
+
scope: refersTo(scopeSchema),
|
|
70
|
+
state: {
|
|
71
|
+
type: 'string',
|
|
72
|
+
},
|
|
73
|
+
code_challenge: refersTo(codeChallengeSchema),
|
|
74
|
+
code_challenge_method: refersTo(codeChallengeMethodSchema),
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
} as const satisfies WhookAPISchemaDefinition;
|
|
78
|
+
|
|
79
|
+
export const definition = {
|
|
80
|
+
method: 'post',
|
|
81
|
+
path: '/oauth2/par',
|
|
82
|
+
config: {
|
|
83
|
+
environments: [],
|
|
84
|
+
},
|
|
85
|
+
operation: {
|
|
86
|
+
operationId: 'postOAuth2PushedAuthorizationRequest',
|
|
87
|
+
summary: `Implements the [Pushed Authorization Request Endpoint](https://datatracker.ietf.org/doc/html/rfc9126#section-2).`,
|
|
88
|
+
tags: ['oauth2'],
|
|
89
|
+
parameters: [],
|
|
90
|
+
requestBody: {
|
|
91
|
+
required: true,
|
|
92
|
+
content: {
|
|
93
|
+
'application/x-www-form-urlencoded': {
|
|
94
|
+
schema: refersTo(pushedAuthorizationRequestBodySchema),
|
|
95
|
+
},
|
|
96
|
+
'application/json': {
|
|
97
|
+
schema: refersTo(pushedAuthorizationRequestBodySchema),
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
responses: {
|
|
102
|
+
'201': {
|
|
103
|
+
description: 'Request URI created.',
|
|
104
|
+
headers: {
|
|
105
|
+
'cache-control': {
|
|
106
|
+
schema: {
|
|
107
|
+
type: 'string',
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
pragma: {
|
|
111
|
+
schema: {
|
|
112
|
+
type: 'string',
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
content: {
|
|
117
|
+
'application/json': {
|
|
118
|
+
schema: {
|
|
119
|
+
type: 'object',
|
|
120
|
+
required: ['request_uri', 'expires_in'],
|
|
121
|
+
properties: {
|
|
122
|
+
request_uri: refersTo(requestURISchema),
|
|
123
|
+
expires_in: {
|
|
124
|
+
type: 'number',
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
'400': {
|
|
132
|
+
description:
|
|
133
|
+
'Request URI error response, see https://datatracker.ietf.org/doc/html/rfc9126#section-2.3.',
|
|
134
|
+
content: {
|
|
135
|
+
'application/json': {
|
|
136
|
+
schema: {
|
|
137
|
+
type: 'object',
|
|
138
|
+
required: ['error'],
|
|
139
|
+
properties: {
|
|
140
|
+
error: {
|
|
141
|
+
type: 'string',
|
|
142
|
+
enum: [
|
|
143
|
+
'invalid_request',
|
|
144
|
+
'invalid_scope',
|
|
145
|
+
'invalid_client',
|
|
146
|
+
'unauthorized_client',
|
|
147
|
+
'unsupported_response_type',
|
|
148
|
+
],
|
|
149
|
+
},
|
|
150
|
+
error_description: { type: 'string' },
|
|
151
|
+
error_uri: { type: 'string' },
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
} as const satisfies WhookRouteDefinition;
|
|
160
|
+
|
|
161
|
+
async function initPostOAuth2PushedAuthorizationRequest({
|
|
162
|
+
OAUTH2,
|
|
163
|
+
OAUTH2_PAR = DEFAULT_OAUTH2_PAR,
|
|
164
|
+
oAuth2Granters,
|
|
165
|
+
oAuth2AuthorizationRequests,
|
|
166
|
+
readClientGrants,
|
|
167
|
+
log = noop,
|
|
168
|
+
}: WhookOAuth2AuthorizationRequestsConfig & {
|
|
169
|
+
OAUTH2: WhookOAuth2Options;
|
|
170
|
+
oAuth2Granters: WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
|
|
171
|
+
oAuth2AuthorizationRequests: Pick<
|
|
172
|
+
WhookOAuth2AuthorizationRequestsService,
|
|
173
|
+
'create'
|
|
174
|
+
>;
|
|
175
|
+
readClientGrants: WhookOAuth2ReadClientGrantsService;
|
|
176
|
+
log?: LogService;
|
|
177
|
+
}) {
|
|
178
|
+
if (OAUTH2_PAR.mode === 'disabled') {
|
|
179
|
+
log('error', `💥 - OAuth2 PAR endpoint should be disabled when PAR is.`);
|
|
180
|
+
throw new YError('E_OAUTH2_MISCONFIGURED');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return async ({
|
|
184
|
+
body: {
|
|
185
|
+
response_type: responseType,
|
|
186
|
+
client_id: givenClientId,
|
|
187
|
+
redirect_uri: demandedRedirectURI = '',
|
|
188
|
+
scope: demandedScope = '',
|
|
189
|
+
state,
|
|
190
|
+
code_challenge: codeChallenge,
|
|
191
|
+
code_challenge_method: codeChallengeMethod,
|
|
192
|
+
request_uri: requestURI,
|
|
193
|
+
},
|
|
194
|
+
authenticationData: optionalAuthenticationData,
|
|
195
|
+
}: {
|
|
196
|
+
body: WhookOAuth2AuthorizeRequestParameters & {
|
|
197
|
+
request_uri?: string;
|
|
198
|
+
} & Record<string, string>;
|
|
199
|
+
authenticationData?: WhookAuthenticationData;
|
|
200
|
+
}) => {
|
|
201
|
+
const usableClientId = toUsableClientId([
|
|
202
|
+
optionalAuthenticationData?.clientId,
|
|
203
|
+
givenClientId,
|
|
204
|
+
]);
|
|
205
|
+
|
|
206
|
+
const clientGrants = await readClientGrants(usableClientId);
|
|
207
|
+
|
|
208
|
+
if (usableClientId !== clientGrants.authenticationData.clientId) {
|
|
209
|
+
throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
|
|
210
|
+
usableClientId,
|
|
211
|
+
clientGrants.authenticationData.clientId,
|
|
212
|
+
]);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (!clientGrants.isPublicClient) {
|
|
216
|
+
if (!optionalAuthenticationData) {
|
|
217
|
+
throw new YError('E_OAUTH2_AUTHENTICATION_REQUIRED', [usableClientId]);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const usableRedirectURI = getUsableRedirectURI(
|
|
222
|
+
clientGrants.allowedRedirectURIS,
|
|
223
|
+
demandedRedirectURI,
|
|
224
|
+
);
|
|
225
|
+
const demandedScopes = filterScopes(
|
|
226
|
+
filterScopes(
|
|
227
|
+
parseOAuth2Scope(demandedScope),
|
|
228
|
+
OAUTH2.allowedScopes,
|
|
229
|
+
!!OAUTH2.strictScopesChecks,
|
|
230
|
+
),
|
|
231
|
+
clientGrants.allowedScopes,
|
|
232
|
+
!!OAUTH2.strictScopesChecks,
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
if (requestURI) {
|
|
236
|
+
throw new YError('E_OAUTH2_REQUEST_URI_NOT_ALLOWED');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
checkCodeChallengeParameters(responseType, codeChallenge, OAUTH2.forcePKCE);
|
|
240
|
+
|
|
241
|
+
const granter = oAuth2Granters.find(
|
|
242
|
+
(granter) => granter.responseType === responseType,
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
if (!granter?.authorize) {
|
|
246
|
+
throw new YError('E_OAUTH2_UNKNOWN_RESPONSE_TYPE', [responseType]);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const { scopes } = await granter.authorize({
|
|
250
|
+
clientId: usableClientId,
|
|
251
|
+
clientGrants,
|
|
252
|
+
demandedScopes,
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
const paramsHash: WhookOAuth2AuthorizeRequestParameters = {
|
|
256
|
+
response_type: responseType,
|
|
257
|
+
redirect_uri: usableRedirectURI,
|
|
258
|
+
scope: stringifyScopes(scopes),
|
|
259
|
+
client_id: usableClientId,
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
if (responseType === AUTHORIZATION_CODE_RESPONSE_TYPE && codeChallenge) {
|
|
263
|
+
paramsHash.code_challenge = codeChallenge;
|
|
264
|
+
paramsHash.code_challenge_method =
|
|
265
|
+
codeChallengeMethod || PLAIN_CODE_CHALLENGE_METHOD;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (state) {
|
|
269
|
+
paramsHash.state = state;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const { requestURI: pushedRequestURI, expiresIn } =
|
|
273
|
+
await oAuth2AuthorizationRequests.create(usableClientId, paramsHash);
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
status: 201,
|
|
277
|
+
headers: {
|
|
278
|
+
'cache-control': 'no-store',
|
|
279
|
+
pragma: 'no-cache',
|
|
280
|
+
},
|
|
281
|
+
body: {
|
|
282
|
+
request_uri: pushedRequestURI,
|
|
283
|
+
expires_in: Math.floor(expiresIn / 1000),
|
|
284
|
+
},
|
|
285
|
+
};
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export default location(
|
|
290
|
+
autoService(initPostOAuth2PushedAuthorizationRequest),
|
|
291
|
+
import.meta.url,
|
|
292
|
+
);
|
|
@@ -82,6 +82,18 @@ export const definition = {
|
|
|
82
82
|
'200': {
|
|
83
83
|
description:
|
|
84
84
|
'Token successfully issued, see https://tools.ietf.org/html/rfc6749#section-5 .',
|
|
85
|
+
headers: {
|
|
86
|
+
'cache-control': {
|
|
87
|
+
schema: {
|
|
88
|
+
type: 'string',
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
pragma: {
|
|
92
|
+
schema: {
|
|
93
|
+
type: 'string',
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
},
|
|
85
97
|
content: {
|
|
86
98
|
'application/json': {
|
|
87
99
|
schema: {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import ms from 'ms';
|
|
2
|
-
import
|
|
2
|
+
import { stringifySetCookie, parseCookie, type SerializeOptions } from 'cookie';
|
|
3
3
|
import { autoService, location } from 'knifecycle';
|
|
4
4
|
import { type Jsonify } from 'type-fest';
|
|
5
5
|
import { type WhookRouteDefinitionBasePath } from '@whook/whook';
|
|
@@ -39,7 +39,9 @@ async function initAuthCookies({
|
|
|
39
39
|
{ session = true } = {},
|
|
40
40
|
) {
|
|
41
41
|
return [
|
|
42
|
-
|
|
42
|
+
stringifySetCookie({
|
|
43
|
+
name: 'access_token',
|
|
44
|
+
value: data.access_token || '',
|
|
43
45
|
path: BASE_PATH + AUTH_API_PREFIX,
|
|
44
46
|
httpOnly: true,
|
|
45
47
|
sameSite: true,
|
|
@@ -47,7 +49,9 @@ async function initAuthCookies({
|
|
|
47
49
|
...COOKIES,
|
|
48
50
|
...(data.access_token ? {} : { maxAge: 0 }),
|
|
49
51
|
}),
|
|
50
|
-
|
|
52
|
+
stringifySetCookie({
|
|
53
|
+
name: 'refresh_token',
|
|
54
|
+
value: data.refresh_token || '',
|
|
51
55
|
path: BASE_PATH + AUTH_API_PREFIX,
|
|
52
56
|
httpOnly: true,
|
|
53
57
|
sameSite: true,
|
|
@@ -59,7 +63,7 @@ async function initAuthCookies({
|
|
|
59
63
|
}
|
|
60
64
|
|
|
61
65
|
function parse(cookieHeader: string): Partial<WhookAuthCookiesData> {
|
|
62
|
-
const data =
|
|
66
|
+
const data = parseCookie(cookieHeader);
|
|
63
67
|
|
|
64
68
|
if (data.access_token && data.refresh_token) {
|
|
65
69
|
return {
|
|
@@ -38,18 +38,6 @@ describe('OAuth2AuthorizationCodeGranter', () => {
|
|
|
38
38
|
log,
|
|
39
39
|
});
|
|
40
40
|
|
|
41
|
-
readClientGrants.mockResolvedValue({
|
|
42
|
-
allowedScopes: ['user', 'admin'],
|
|
43
|
-
allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
|
|
44
|
-
allowedGrantTypes: ['authorization_code', 'refresh_token'],
|
|
45
|
-
authenticationData: {
|
|
46
|
-
clientId: 'the_client_app_id',
|
|
47
|
-
scopes: ['user', 'admin'],
|
|
48
|
-
userId: 'user_id',
|
|
49
|
-
},
|
|
50
|
-
isPublicClient: false,
|
|
51
|
-
});
|
|
52
|
-
|
|
53
41
|
oAuth2AuthorizationCode.create.mockRejectedValueOnce(
|
|
54
42
|
new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
|
|
55
43
|
);
|
|
@@ -60,13 +48,19 @@ describe('OAuth2AuthorizationCodeGranter', () => {
|
|
|
60
48
|
const authorizerResult = await oAuth2AuthorizationCodeGranter.authorize?.(
|
|
61
49
|
{
|
|
62
50
|
clientId: 'the_client_app_id',
|
|
63
|
-
|
|
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
|
+
},
|
|
64
62
|
demandedScopes: ['user'],
|
|
65
63
|
},
|
|
66
|
-
{
|
|
67
|
-
codeChallenge: 'my_code_challenge',
|
|
68
|
-
codeChallengeMethod: 'plain',
|
|
69
|
-
},
|
|
70
64
|
);
|
|
71
65
|
|
|
72
66
|
expect({
|
|
@@ -80,8 +74,6 @@ describe('OAuth2AuthorizationCodeGranter', () => {
|
|
|
80
74
|
}).toMatchInlineSnapshot(`
|
|
81
75
|
{
|
|
82
76
|
"authorizerResult": {
|
|
83
|
-
"clientId": "the_client_app_id",
|
|
84
|
-
"redirectURI": "https://www.example.com/oauth2/code",
|
|
85
77
|
"scopes": [
|
|
86
78
|
"user",
|
|
87
79
|
],
|
|
@@ -94,159 +86,10 @@ describe('OAuth2AuthorizationCodeGranter', () => {
|
|
|
94
86
|
],
|
|
95
87
|
"oAuth2AuthorizationCodeCheckCalls": [],
|
|
96
88
|
"oAuth2AuthorizationCodeCreateCalls": [],
|
|
97
|
-
"readClientGrantsCalls": [
|
|
98
|
-
[
|
|
99
|
-
"the_client_app_id",
|
|
100
|
-
],
|
|
101
|
-
],
|
|
89
|
+
"readClientGrantsCalls": [],
|
|
102
90
|
}
|
|
103
91
|
`);
|
|
104
92
|
});
|
|
105
|
-
|
|
106
|
-
test('should fail with a bad uri', async () => {
|
|
107
|
-
const oAuth2AuthorizationCodeGranter =
|
|
108
|
-
await initOAuth2AuthorizationCodeGranter({
|
|
109
|
-
OAUTH2,
|
|
110
|
-
readClientGrants,
|
|
111
|
-
oAuth2AuthorizationCode,
|
|
112
|
-
log,
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
readClientGrants.mockResolvedValue({
|
|
116
|
-
allowedScopes: ['user', 'admin'],
|
|
117
|
-
allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
|
|
118
|
-
allowedGrantTypes: ['authorization_code', 'refresh_token'],
|
|
119
|
-
authenticationData: {
|
|
120
|
-
clientId: 'the_client_app_id',
|
|
121
|
-
scopes: ['user', 'admin'],
|
|
122
|
-
userId: 'user_id',
|
|
123
|
-
},
|
|
124
|
-
isPublicClient: false,
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
oAuth2AuthorizationCode.create.mockRejectedValueOnce(
|
|
128
|
-
new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
|
|
129
|
-
);
|
|
130
|
-
oAuth2AuthorizationCode.check.mockRejectedValueOnce(
|
|
131
|
-
new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
|
|
132
|
-
);
|
|
133
|
-
|
|
134
|
-
try {
|
|
135
|
-
await oAuth2AuthorizationCodeGranter.authorize?.(
|
|
136
|
-
{
|
|
137
|
-
clientId: 'the_client_app_id',
|
|
138
|
-
demandedRedirectURI: 'https://www.example.com/admin',
|
|
139
|
-
demandedScopes: ['user'],
|
|
140
|
-
},
|
|
141
|
-
{
|
|
142
|
-
codeChallenge: 'my_code_challenge',
|
|
143
|
-
codeChallengeMethod: 'plain',
|
|
144
|
-
},
|
|
145
|
-
);
|
|
146
|
-
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
147
|
-
} catch (err) {
|
|
148
|
-
expect({
|
|
149
|
-
err,
|
|
150
|
-
oAuth2AuthorizationCodeCreateCalls:
|
|
151
|
-
oAuth2AuthorizationCode.create.mock.calls,
|
|
152
|
-
oAuth2AuthorizationCodeCheckCalls:
|
|
153
|
-
oAuth2AuthorizationCode.check.mock.calls,
|
|
154
|
-
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
155
|
-
logCalls: log.mock.calls,
|
|
156
|
-
}).toMatchInlineSnapshot(`
|
|
157
|
-
{
|
|
158
|
-
"err": [YError: E_OAUTH2_BAD_REDIRECT_URI (["https://www.example.com/admin",["https://www.example.com/oauth2/code"]]): E_OAUTH2_BAD_REDIRECT_URI],
|
|
159
|
-
"logCalls": [
|
|
160
|
-
[
|
|
161
|
-
"debug",
|
|
162
|
-
"👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
|
|
163
|
-
],
|
|
164
|
-
],
|
|
165
|
-
"oAuth2AuthorizationCodeCheckCalls": [],
|
|
166
|
-
"oAuth2AuthorizationCodeCreateCalls": [],
|
|
167
|
-
"readClientGrantsCalls": [
|
|
168
|
-
[
|
|
169
|
-
"the_client_app_id",
|
|
170
|
-
],
|
|
171
|
-
],
|
|
172
|
-
}
|
|
173
|
-
`);
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
test('should fail with a bad scope', async () => {
|
|
178
|
-
const oAuth2AuthorizationCodeGranter =
|
|
179
|
-
await initOAuth2AuthorizationCodeGranter({
|
|
180
|
-
OAUTH2: {
|
|
181
|
-
...OAUTH2,
|
|
182
|
-
strictScopesChecks: true,
|
|
183
|
-
},
|
|
184
|
-
readClientGrants,
|
|
185
|
-
oAuth2AuthorizationCode,
|
|
186
|
-
log,
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
readClientGrants.mockResolvedValue({
|
|
190
|
-
allowedScopes: ['user', 'admin'],
|
|
191
|
-
allowedRedirectURIS: ['https://www.example.com/oauth2/code'],
|
|
192
|
-
allowedGrantTypes: ['authorization_code', 'refresh_token'],
|
|
193
|
-
authenticationData: {
|
|
194
|
-
clientId: 'the_client_app_id',
|
|
195
|
-
scopes: ['user', 'admin'],
|
|
196
|
-
userId: 'user_id',
|
|
197
|
-
},
|
|
198
|
-
isPublicClient: false,
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
oAuth2AuthorizationCode.create.mockRejectedValueOnce(
|
|
202
|
-
new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
|
|
203
|
-
);
|
|
204
|
-
oAuth2AuthorizationCode.check.mockRejectedValueOnce(
|
|
205
|
-
new YError('E_NOT_SUPPOSED_TO_BE_HERE'),
|
|
206
|
-
);
|
|
207
|
-
|
|
208
|
-
try {
|
|
209
|
-
await oAuth2AuthorizationCodeGranter.authorize?.(
|
|
210
|
-
{
|
|
211
|
-
clientId: 'the_client_app_id',
|
|
212
|
-
demandedRedirectURI: 'https://www.example.com/admin',
|
|
213
|
-
demandedScopes: ['god'],
|
|
214
|
-
},
|
|
215
|
-
{
|
|
216
|
-
codeChallenge: 'my_code_challenge',
|
|
217
|
-
codeChallengeMethod: 'plain',
|
|
218
|
-
},
|
|
219
|
-
);
|
|
220
|
-
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
221
|
-
} catch (err) {
|
|
222
|
-
expect({
|
|
223
|
-
err,
|
|
224
|
-
oAuth2AuthorizationCodeCreateCalls:
|
|
225
|
-
oAuth2AuthorizationCode.create.mock.calls,
|
|
226
|
-
oAuth2AuthorizationCodeCheckCalls:
|
|
227
|
-
oAuth2AuthorizationCode.check.mock.calls,
|
|
228
|
-
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
229
|
-
logCalls: log.mock.calls,
|
|
230
|
-
}).toMatchInlineSnapshot(`
|
|
231
|
-
{
|
|
232
|
-
"err": [YError: E_OAUTH2_BAD_SCOPE (["god"]): E_OAUTH2_BAD_SCOPE],
|
|
233
|
-
"logCalls": [
|
|
234
|
-
[
|
|
235
|
-
"debug",
|
|
236
|
-
"👫 - OAuth2AuthorizationCodeGranter Service Initialized!",
|
|
237
|
-
],
|
|
238
|
-
],
|
|
239
|
-
"oAuth2AuthorizationCodeCheckCalls": [],
|
|
240
|
-
"oAuth2AuthorizationCodeCreateCalls": [],
|
|
241
|
-
"readClientGrantsCalls": [
|
|
242
|
-
[
|
|
243
|
-
"the_client_app_id",
|
|
244
|
-
],
|
|
245
|
-
],
|
|
246
|
-
}
|
|
247
|
-
`);
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
93
|
});
|
|
251
94
|
|
|
252
95
|
describe('.acknowledge()', () => {
|
|
@@ -23,14 +23,15 @@ import { filterScopes } from '../libs/scopes.js';
|
|
|
23
23
|
import { checkRedirectURI } from '../libs/redirectURI.js';
|
|
24
24
|
import {
|
|
25
25
|
checkCodeChallenge,
|
|
26
|
+
AUTHORIZATION_CODE_GRANT_TYPE,
|
|
27
|
+
AUTHORIZATION_CODE_RESPONSE_TYPE,
|
|
26
28
|
CODE_CHALLENGE_METHODS,
|
|
27
29
|
PLAIN_CODE_CHALLENGE_METHOD,
|
|
28
30
|
type CodeChallengeMethod,
|
|
29
31
|
} from '../libs/verifier.js';
|
|
30
32
|
import { toUsableClientId } from '../libs/clients.js';
|
|
31
33
|
|
|
32
|
-
export
|
|
33
|
-
export const AUTHORIZATION_CODE_RESPONSE_TYPE = 'code';
|
|
34
|
+
export { AUTHORIZATION_CODE_GRANT_TYPE, AUTHORIZATION_CODE_RESPONSE_TYPE };
|
|
34
35
|
|
|
35
36
|
export const codeVerifierSchema = {
|
|
36
37
|
name: 'CodeVerifier',
|
|
@@ -145,10 +146,6 @@ export interface WhookOAuth2AuthorizationCodeGranterDependencies {
|
|
|
145
146
|
export interface WhookOAuth2AuthorizationCodeGranterDefinitions extends WhookOAuth2GranterDefinitions {
|
|
146
147
|
grantType: typeof AUTHORIZATION_CODE_GRANT_TYPE;
|
|
147
148
|
responseType: typeof AUTHORIZATION_CODE_RESPONSE_TYPE;
|
|
148
|
-
authorizeParameters: {
|
|
149
|
-
codeChallenge: string;
|
|
150
|
-
codeChallengeMethod: CodeChallengeMethod;
|
|
151
|
-
};
|
|
152
149
|
acknowledgeParameters: {
|
|
153
150
|
codeChallenge: string;
|
|
154
151
|
codeChallengeMethod: CodeChallengeMethod;
|
|
@@ -179,34 +176,14 @@ async function initOAuth2AuthorizationCodeGranter({
|
|
|
179
176
|
}: WhookOAuth2AuthorizationCodeGranterDependencies): Promise<WhookOAuth2AuthorizationCodeGranterService> {
|
|
180
177
|
const authorizeWithCode: NonNullable<
|
|
181
178
|
WhookOAuth2AuthorizationCodeGranterService['authorize']
|
|
182
|
-
> = async ({
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
|
|
187
|
-
clientId,
|
|
188
|
-
grants.authenticationData.clientId,
|
|
189
|
-
]);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
checkGrantType(grants.allowedGrantTypes, AUTHORIZATION_CODE_GRANT_TYPE);
|
|
193
|
-
|
|
194
|
-
const filteredScopes = filterScopes(
|
|
195
|
-
demandedScopes,
|
|
196
|
-
grants.allowedScopes,
|
|
197
|
-
!!OAUTH2.strictScopesChecks,
|
|
179
|
+
> = async ({ clientGrants, demandedScopes }) => {
|
|
180
|
+
checkGrantType(
|
|
181
|
+
clientGrants.allowedGrantTypes,
|
|
182
|
+
AUTHORIZATION_CODE_GRANT_TYPE,
|
|
198
183
|
);
|
|
199
184
|
|
|
200
|
-
if (demandedRedirectURI) {
|
|
201
|
-
checkRedirectURI(grants.allowedRedirectURIS, demandedRedirectURI);
|
|
202
|
-
} else {
|
|
203
|
-
demandedRedirectURI = grants.allowedRedirectURIS[0];
|
|
204
|
-
}
|
|
205
|
-
|
|
206
185
|
return {
|
|
207
|
-
|
|
208
|
-
redirectURI: demandedRedirectURI,
|
|
209
|
-
scopes: filteredScopes,
|
|
186
|
+
scopes: demandedScopes,
|
|
210
187
|
};
|
|
211
188
|
};
|
|
212
189
|
|