-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathmtls.js
More file actions
61 lines (54 loc) · 2 KB
/
Copy pathmtls.js
File metadata and controls
61 lines (54 loc) · 2 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
const express = require('express');
const { auth } = require('../');
const { Agent, fetch: undiciFetch } = require('undici');
const fs = require('fs');
const app = express();
// mTLS (Mutual TLS, RFC 8705) client authentication demo.
//
// With `useMtls: true` the SDK authenticates to the token endpoint with a TLS
// client certificate instead of a client secret, and routes token/refresh/
// revocation/userinfo/PAR requests to the server's `mtls_endpoint_aliases`.
// Issued access tokens carry a `cnf.x5t#S256` claim binding them to the
// certificate (certificate-bound tokens).
//
// The certificate is presented at the TLS layer by your customFetch, never by
// the SDK. Node's global fetch ignores the `agent` option, so the cert must ride
// on an undici Agent's `connect` options via the `dispatcher`.
//
// Prerequisites (see the mTLS docs):
// - A custom domain with self-managed certs (does NOT work on *.auth0.com).
// - mTLS endpoint aliases enabled on the tenant.
// - App Credentials > Authentication Method set to mTLS, with the client cert
// uploaded.
// - No clientSecret / clientAssertionSigningKey (mutually exclusive with mTLS).
//
// `AUTH0_MTLS=true` can be used instead of `useMtls: true`.
const tlsAgent = new Agent({
connect: {
cert: fs.readFileSync('./client.crt'),
key: fs.readFileSync('./client.key'),
},
});
app.use(
auth({
// Point issuerBaseURL at your custom domain, not the *.auth0.com host.
issuerBaseURL: 'https://auth.your-domain.com',
authRequired: false,
authorizationParams: {
response_type: 'code',
audience: 'https://your-api/',
scope: 'openid profile email offline_access',
},
useMtls: true,
customFetch: (url, options) =>
undiciFetch(url, { ...options, dispatcher: tlsAgent }),
}),
);
app.get('/', (req, res) => {
if (req.oidc.isAuthenticated()) {
res.send(`hello ${req.oidc.user.sub} <a href="/logout">logout</a>`);
} else {
res.send('<a href="/login">login</a>');
}
});
module.exports = app;