-
Notifications
You must be signed in to change notification settings - Fork 158
feat(mtls): mTLS (RFC 8705) client authentication #865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3a2ec5e
feat(mtls): add mTLS (RFC 8705) client authentication
jd3vi1 fad64cc
fix(mtls): re-check cert binding on refresh, clarify errors
jd3vi1 402eb1c
fix(mtls): validate endpoint aliases per-endpoint and align warnings
jd3vi1 c645a2a
fix(mtls): warn on inert useMtls, harden endpoint resolution, tighten…
jd3vi1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| const express = require('express'); | ||
| const { auth } = require('../'); | ||
| const { Agent, fetch: undiciFetch } = require('undici'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| 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. Requests are sent to the | ||
| // server's `mtls_endpoint_aliases` for each endpoint the server advertises an | ||
| // alias for; an endpoint without an alias is sent over the standard (non-mTLS) | ||
| // channel, so the tenant must advertise aliases for every endpoint you use. | ||
| // 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`. | ||
|
|
||
| // Resolve the certificate and key relative to this file so the example works | ||
| // regardless of the caller's working directory. Fail with a message naming the | ||
| // expected files rather than an opaque ENOENT. | ||
| const readCertFile = (name) => { | ||
| const file = path.join(__dirname, name); | ||
| try { | ||
| return fs.readFileSync(file); | ||
| } catch (e) { | ||
| throw new Error( | ||
| `mTLS example: could not read "${file}". Place your client certificate ` + | ||
| `("client.crt") and private key ("client.key") next to this example. ` + | ||
| `(${e.code || e.message})`, | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| const tlsAgent = new Agent({ | ||
| connect: { | ||
| cert: readCertFile('client.crt'), | ||
| key: readCertFile('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; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,17 @@ | ||
| const auth = require('./middleware/auth'); | ||
| const requiresAuth = require('./middleware/requiresAuth'); | ||
| const attemptSilentLogin = require('./middleware/attemptSilentLogin'); | ||
| const { SessionExpiredError } = require('./lib/errors'); | ||
| const { | ||
| SessionExpiredError, | ||
| MtlsError, | ||
| MtlsErrorCode, | ||
| } = require('./lib/errors'); | ||
|
|
||
| module.exports = { | ||
| auth, | ||
| ...requiresAuth, | ||
| attemptSilentLogin, | ||
| SessionExpiredError, | ||
| MtlsError, | ||
| MtlsErrorCode, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.