-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathclient.js
More file actions
413 lines (371 loc) · 13.3 KB
/
Copy pathclient.js
File metadata and controls
413 lines (371 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
const crypto = require('crypto');
const client = require('openid-client');
const {
importPKCS8,
importJWK,
exportPKCS8,
SignJWT,
compactDecrypt,
} = require('jose');
const pkg = require('../package.json');
const debug = require('./debug')('client');
const { MtlsError, MtlsErrorCode } = require('./errors');
const telemetryHeader = {
name: 'express-oidc',
version: pkg.version,
env: {
node: process.version,
},
};
function sortSpaceDelimitedString(string) {
return string.split(' ').sort().join(' ');
}
/**
* Import a private key as a CryptoKey for use with openid-client v6.
* @param {CryptoKey|KeyObject|Object|string|Buffer} keyData
* @param {string} [alg] - Required for PEM/Buffer/KeyObject/JWK-without-alg
* @returns {Promise<CryptoKey>}
*/
async function importPrivateKey(keyData, alg) {
// CryptoKey: algorithm already embedded, pass through
if (
typeof keyData?.algorithm?.name === 'string' &&
Array.isArray(keyData?.usages)
) {
return keyData;
}
// Node.js KeyObject: export to PKCS8 PEM then import as CryptoKey
if (keyData?.asymmetricKeyType) {
const pem = await exportPKCS8(keyData);
return importPKCS8(pem, alg);
}
// Plain object that is not a Buffer: treat as JWK
if (typeof keyData === 'object' && !Buffer.isBuffer(keyData)) {
return importJWK(keyData, alg);
}
// PEM string or Buffer
return importPKCS8(keyData.toString(), alg);
}
/**
* Creates a custom fetch function that adds custom headers and respects timeout.
* @param {Object} config - Configuration object
* @returns {Function} Custom fetch function
*/
function createCustomFetch(config) {
const fetchFn = config.customFetch || fetch;
return async (fetchUrl, options) => {
const headers = new Headers(options.headers);
// Add User-Agent header
headers.set(
'User-Agent',
config.httpUserAgent || `${pkg.name}/${pkg.version}`,
);
// Add telemetry header if enabled
if (config.enableTelemetry) {
headers.set(
'Auth0-Client',
Buffer.from(JSON.stringify(telemetryHeader)).toString('base64'),
);
}
return fetchFn(fetchUrl, {
...options,
headers,
});
};
}
/**
* Determines the client authentication method based on configuration.
* @param {Object} config - Configuration object
* @returns {Promise<Function>} Client authentication function
*/
async function getClientAuth(config) {
switch (config.clientAuthMethod) {
case 'client_secret_basic':
return client.ClientSecretBasic(config.clientSecret);
case 'client_secret_post':
return client.ClientSecretPost(config.clientSecret);
case 'client_secret_jwt':
return client.ClientSecretJwt(config.clientSecret);
case 'private_key_jwt': {
const privateKey = await importPrivateKey(
config.clientAssertionSigningKey,
config.clientAssertionSigningAlg,
);
return client.PrivateKeyJwt(privateKey);
}
case 'tls_client_auth':
// mTLS (RFC 8705). Enabled via `useMtls`. Covers both CA-signed and
// self-signed; the distinction is enforced at the authorization server.
return client.TlsClientAuth();
case 'none':
return client.None();
default:
// Default based on whether client_secret is present
if (config.clientSecret) {
return client.ClientSecretPost(config.clientSecret);
}
return client.None();
}
}
/**
* Builds the array of execute functions needed for OIDC discovery.
* Handles response_type configuration and HTTP issuer support.
* @param {Object} config - Configuration object
* @returns {Array} Array of execute functions for discovery
*/
function buildDiscoveryExecute(config) {
const execute = [];
const responseType = config.authorizationParams.response_type;
if (responseType === 'id_token') {
execute.push(client.useIdTokenResponseType);
} else if (responseType === 'code id_token') {
execute.push(client.useCodeIdTokenResponseType);
}
// 'code' is the default in v6, no special execute function needed
// For HTTP issuers (local development), enable allowInsecureRequests
const issuerUrl = new URL(config.issuerBaseURL);
if (issuerUrl.protocol === 'http:') {
execute.push(client.allowInsecureRequests);
}
return execute;
}
async function get(config) {
const clientAuth = await getClientAuth(config);
const execute = buildDiscoveryExecute(config);
// Build client metadata with clock tolerance and ID token signing algorithm
const clientMetadata = {
[client.clockTolerance]: config.clockTolerance,
id_token_signed_response_alg: config.idTokenSigningAlg,
// For mTLS, token/revocation requests must go to the server's
// mtls_endpoint_aliases rather than the standard endpoints. In a
// self-managed-certs custom domain setup only the mTLS alias host is
// configured (at the edge) to extract the client certificate from the TLS
// handshake; sending to the standard endpoint yields invalid_client.
// openid-client routes to the aliases automatically when this is set.
...(config.useMtls && { use_mtls_endpoint_aliases: true }),
};
// Discover and create configuration
const issuerUrl = new URL(config.issuerBaseURL);
const discoveryOptions = {
execute,
timeout: config.httpTimeout / 1000, // Convert ms to seconds
[client.customFetch]: createCustomFetch(config),
};
const configuration = await client.discovery(
issuerUrl,
config.clientID,
clientMetadata,
clientAuth,
discoveryOptions,
);
// Get server metadata for validation
const serverMetadata = configuration.serverMetadata();
const issuerTokenAlgs = Array.isArray(
serverMetadata.id_token_signing_alg_values_supported,
)
? serverMetadata.id_token_signing_alg_values_supported
: [];
if (!issuerTokenAlgs.includes(config.idTokenSigningAlg)) {
debug(
'ID token algorithm %o is not supported by the issuer. Supported ID token algorithms are: %o.',
config.idTokenSigningAlg,
issuerTokenAlgs,
);
}
const configRespType = sortSpaceDelimitedString(
config.authorizationParams.response_type,
);
const issuerRespTypes = Array.isArray(serverMetadata.response_types_supported)
? serverMetadata.response_types_supported
: [];
const sortedRespTypes = issuerRespTypes.map(sortSpaceDelimitedString);
if (!sortedRespTypes.includes(configRespType)) {
debug(
'Response type %o is not supported by the issuer. ' +
'Supported response types are: %o.',
configRespType,
issuerRespTypes,
);
}
const configRespMode = config.authorizationParams.response_mode;
const issuerRespModes = Array.isArray(serverMetadata.response_modes_supported)
? serverMetadata.response_modes_supported
: [];
if (configRespMode && !issuerRespModes.includes(configRespMode)) {
debug(
'Response mode %o is not supported by the issuer. ' +
'Supported response modes are %o.',
configRespMode,
issuerRespModes,
);
}
if (
config.pushedAuthorizationRequests &&
!serverMetadata.pushed_authorization_request_endpoint
) {
throw new TypeError(
'pushed_authorization_request_endpoint must be configured on the issuer to use pushedAuthorizationRequests',
);
}
if (config.useMtls && !serverMetadata.mtls_endpoint_aliases?.token_endpoint) {
throw new MtlsError(
MtlsErrorCode.MTLS_ENDPOINT_ALIASES_MISSING,
'useMtls is enabled but the authorization server discovery document does ' +
'not advertise "mtls_endpoint_aliases.token_endpoint". Ensure mTLS endpoint ' +
'aliases are enabled on the tenant and requests are routed through your ' +
'custom domain.',
);
}
// Handle Auth0-specific logout
let auth0Logout = false;
if (config.idpLogout) {
const issuerHostname = new URL(serverMetadata.issuer).hostname;
if (
config.auth0Logout ||
(issuerHostname.match('\\.auth0\\.com$') && config.auth0Logout !== false)
) {
auth0Logout = true;
} else if (!serverMetadata.end_session_endpoint) {
debug('the issuer does not support RP-Initiated Logout');
}
}
return { configuration, serverMetadata, auth0Logout };
}
/**
* Builds the end session URL, handling Auth0-specific logout.
* @param {Object} config - Configuration object
* @param {Object} options - Options containing configuration, serverMetadata, auth0Logout
* @param {Object} params - End session parameters
* @returns {string} End session URL
*/
function buildEndSessionUrl(
config,
{ configuration, serverMetadata, auth0Logout },
params,
) {
// Filter out null and undefined values from params
const filteredParams = Object.fromEntries(
Object.entries(params).filter(
([, value]) => value !== null && value !== undefined,
),
);
if (auth0Logout) {
const { id_token_hint, post_logout_redirect_uri, ...extraParams } =
filteredParams;
const logoutUrl = new URL('/v2/logout', serverMetadata.issuer);
// Add query parameters in the expected order
if (post_logout_redirect_uri) {
logoutUrl.searchParams.set('returnTo', post_logout_redirect_uri);
}
logoutUrl.searchParams.set('client_id', config.clientID);
// Add any extra params (already filtered for null/undefined by filteredParams)
Object.entries(extraParams).forEach(([key, value]) => {
logoutUrl.searchParams.set(key, value);
});
return logoutUrl.toString();
}
// Use standard RP-Initiated Logout
return client.buildEndSessionUrl(configuration, filteredParams).toString();
}
const cache = new Map();
/**
* Builds a signed JWT request object for JAR (JWT-Secured Authorization Requests).
* All authorization parameters are embedded in the JWT payload along with iss and aud claims.
*/
async function buildRequestObject(authParams, config, audience) {
// `aud` must equal the issuer identifier as advertised in discovery metadata
// (which may carry a trailing slash), not the configured issuerBaseURL. It is
// required: falling back to issuerBaseURL is the exact mismatch JAR rejects with
// invalid_request_object, so callers must pass the discovered issuer.
if (!audience) {
throw new TypeError(
'buildRequestObject requires the discovered issuer as audience',
);
}
const key = await importPrivateKey(
config.requestObjectSigningKey,
config.requestObjectSigningAlg,
);
const now = Math.floor(Date.now() / 1000);
const payload = {
...authParams,
client_id: config.clientID,
iss: config.clientID,
aud: audience,
jti: crypto.randomBytes(16).toString('hex'),
iat: now,
nbf: now,
exp: now + 60,
};
return new SignJWT(payload)
.setProtectedHeader({
alg: config.requestObjectSigningAlg,
...(config.requestObjectSigningKeyId && {
kid: config.requestObjectSigningKeyId,
}),
})
.sign(key);
}
// Key-management ("alg") algorithms accepted for access-token decryption when the
// developer has not pinned one. Deliberately excludes RSA1_5 (PKCS#1 v1.5), which
// is vulnerable to Bleichenbacher padding-oracle attacks. jose enforces this list
// against the JWE header before the key is imported, so a token cannot downgrade
// us to a weak or unexpected algorithm.
const ALLOWED_ACCESS_TOKEN_JWE_ALGS = [
'RSA-OAEP',
'RSA-OAEP-256',
'RSA-OAEP-384',
'RSA-OAEP-512',
'ECDH-ES',
'ECDH-ES+A128KW',
'ECDH-ES+A192KW',
'ECDH-ES+A256KW',
];
/**
* Decrypts a JWE-encrypted access token.
* Co-located here so it can call the internal importPrivateKey helper directly.
*
* A JWE in compact serialization has exactly five dot-separated parts. Anything
* else (a plaintext 3-part JWT, an opaque token, etc.) is returned unchanged, so
* that toggling Token Encryption off, a mid-rollout mismatch, or an audience that
* yields an opaque token does not turn every login and refresh into a hard error.
*
* For genuine JWEs the key-management algorithm is taken from the protected header
* (so the tenant can encrypt with e.g. RSA-OAEP-256 or RSA-OAEP-512 without the
* client pre-declaring it) but ONLY after jose validates it against an allowlist of
* strong algorithms. This prevents algorithm-confusion / downgrade attacks (notably
* RSA1_5). When the developer sets `accessTokenDecryptionAlg`, the allowlist is
* narrowed to that one value, turning it into a hard pin.
*/
async function decryptAccessToken(token, keyData, alg) {
// Not a compact JWE (five parts) — pass through untouched.
if (typeof token !== 'string' || token.split('.').length !== 5) {
return token;
}
const keyManagementAlgorithms = alg ? [alg] : ALLOWED_ACCESS_TOKEN_JWE_ALGS;
const { plaintext } = await compactDecrypt(
token,
(header) => importPrivateKey(keyData, header.alg),
{ keyManagementAlgorithms },
);
return Buffer.from(plaintext).toString('utf8');
}
exports.buildRequestObject = buildRequestObject;
exports.decryptAccessToken = decryptAccessToken;
exports.get = (config) => {
const { discoveryCacheMaxAge: cacheMaxAge } = config;
const now = Date.now();
const entry = cache.get(config);
if (entry && now < entry.expiresAt) {
return entry.promise;
}
const promise = get(config).catch((e) => {
cache.delete(config);
throw e;
});
cache.set(config, { promise, expiresAt: now + cacheMaxAge });
return promise;
};
exports.buildEndSessionUrl = buildEndSessionUrl;
// Re-export client module for access to functions
exports.client = client;