-
Notifications
You must be signed in to change notification settings - Fork 159
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
Open
jd3vi1
wants to merge
2
commits into
feature/jwe
Choose a base branch
from
feature/mtls
base: feature/jwe
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,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; | ||
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.