-
Notifications
You must be signed in to change notification settings - Fork 395
Adds support for implicit mTLS (Mutual TLS) transport for client assertion delegates #5670
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
+944
−184
Merged
Changes from all commits
Commits
Show all changes
5 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
162 changes: 162 additions & 0 deletions
162
src/client/Microsoft.Identity.Client/ApiConfig/Parameters/MtlsPopParametersInitializer.cs
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,162 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using System.Security.Cryptography.X509Certificates; | ||
| using Microsoft.Identity.Client.AppConfig; | ||
| using Microsoft.Identity.Client.AuthScheme.PoP; | ||
| using Microsoft.Identity.Client.Internal; | ||
| using Microsoft.Identity.Client.Internal.ClientCredential; | ||
| using Microsoft.Identity.Client.TelemetryCore; | ||
|
|
||
| namespace Microsoft.Identity.Client.ApiConfig.Parameters | ||
| { | ||
| /// <summary> | ||
| /// Encapsulates the mTLS/PoP initialization logic for token requests. | ||
| /// Keeps AcquireTokenCommonParameters lean and makes the init logic testable in isolation. | ||
| /// </summary> | ||
| internal static class MtlsPopParametersInitializer | ||
| { | ||
| internal static async Task TryInitAsync( | ||
| AcquireTokenCommonParameters p, | ||
| IServiceBundle serviceBundle, | ||
| CancellationToken ct) | ||
| { | ||
| if (p.IsMtlsPopRequested) | ||
| { | ||
| await InitExplicitMtlsPopAsync(p, serviceBundle, ct).ConfigureAwait(false); | ||
| return; | ||
| } | ||
|
|
||
| await TryInitImplicitBearerOverMtlsAsync(p, serviceBundle, ct).ConfigureAwait(false); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// NON-PoP request: | ||
| /// We may still need mTLS transport if the credential can return a TokenBindingCertificate. | ||
| /// </summary> | ||
| private static async Task TryInitImplicitBearerOverMtlsAsync( | ||
| AcquireTokenCommonParameters tokenParameters, | ||
| IServiceBundle serviceBundle, | ||
| CancellationToken ct) | ||
| { | ||
| if (tokenParameters.MtlsCertificate != null) | ||
| { | ||
| ThrowIfRegionMissingForImplicitMtls(serviceBundle); | ||
| return; | ||
| } | ||
|
|
||
| // Only cert-capable credentials implement this capability interface. | ||
| if (serviceBundle.Config.ClientCredential is IClientSignedAssertionProvider signedProvider) | ||
| { | ||
| var opts = CreateAssertionRequestOptions(tokenParameters, serviceBundle, ct); | ||
|
|
||
| ClientSignedAssertion ar = | ||
| await signedProvider.GetAssertionAsync(opts, ct).ConfigureAwait(false); | ||
|
|
||
| if (ar?.TokenBindingCertificate != null) | ||
| { | ||
| tokenParameters.MtlsCertificate = ar.TokenBindingCertificate; | ||
| ThrowIfRegionMissingForImplicitMtls(serviceBundle); | ||
trwalke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// EXPLICIT PoP requested: | ||
| /// Validate and initialize PoP parameters (auth scheme + cert + region check). | ||
| /// </summary> | ||
| private static async Task InitExplicitMtlsPopAsync( | ||
| AcquireTokenCommonParameters p, | ||
| IServiceBundle serviceBundle, | ||
| CancellationToken ct) | ||
| { | ||
| // Case 1 – Certificate credential | ||
| if (serviceBundle.Config.ClientCredential is CertificateClientCredential certCred) | ||
| { | ||
| if (certCred.Certificate == null) | ||
| { | ||
| throw new MsalClientException( | ||
| MsalError.MtlsCertificateNotProvided, | ||
| MsalErrorMessage.MtlsCertificateNotProvidedMessage); | ||
| } | ||
|
|
||
| InitMtlsPopParameters(p, certCred.Certificate, serviceBundle); | ||
| return; | ||
| } | ||
|
|
||
| // Case 2 – Signed assertion provider (JWT + optional cert) | ||
| if (serviceBundle.Config.ClientCredential is IClientSignedAssertionProvider signedProvider) | ||
| { | ||
| var opts = CreateAssertionRequestOptions(p, serviceBundle, ct); | ||
|
|
||
| ClientSignedAssertion ar = | ||
| await signedProvider.GetAssertionAsync(opts, ct).ConfigureAwait(false); | ||
|
|
||
| if (ar?.TokenBindingCertificate == null) | ||
| { | ||
| throw new MsalClientException( | ||
| MsalError.MtlsCertificateNotProvided, | ||
| MsalErrorMessage.MtlsCertificateNotProvidedMessage); | ||
| } | ||
|
|
||
| InitMtlsPopParameters(p, ar.TokenBindingCertificate, serviceBundle); | ||
| return; | ||
| } | ||
|
|
||
| // Case 3 – Any other credential (client-secret etc.) | ||
| throw new MsalClientException( | ||
| MsalError.MtlsCertificateNotProvided, | ||
| MsalErrorMessage.MtlsCertificateNotProvidedMessage); | ||
| } | ||
|
|
||
| private static AssertionRequestOptions CreateAssertionRequestOptions( | ||
| AcquireTokenCommonParameters p, | ||
| IServiceBundle serviceBundle, | ||
| CancellationToken ct) | ||
| { | ||
| return new AssertionRequestOptions | ||
| { | ||
| ClientID = serviceBundle.Config.ClientId, | ||
| ClientCapabilities = serviceBundle.Config.ClientCapabilities, | ||
| Claims = p.Claims, | ||
| CancellationToken = ct, | ||
| ClientAssertionFmiPath = p.ClientAssertionFmiPath, | ||
|
|
||
| // Best-effort context. IMPORTANT: use AbsoluteUri, not Uri.Authority (host only). | ||
| TokenEndpoint = serviceBundle.Config.Authority.AuthorityInfo.CanonicalAuthority.AbsoluteUri | ||
| }; | ||
| } | ||
|
|
||
| private static void InitMtlsPopParameters( | ||
| AcquireTokenCommonParameters p, | ||
| X509Certificate2 cert, | ||
| IServiceBundle serviceBundle) | ||
| { | ||
| // region check (AAD only) | ||
| if (serviceBundle.Config.Authority.AuthorityInfo.AuthorityType == AuthorityType.Aad && | ||
| serviceBundle.Config.AzureRegion == null) | ||
| { | ||
| throw new MsalClientException( | ||
| MsalError.MtlsPopWithoutRegion, | ||
| MsalErrorMessage.MtlsPopWithoutRegion); | ||
| } | ||
|
|
||
| p.AuthenticationOperation = new MtlsPopAuthenticationOperation(cert); | ||
| p.MtlsCertificate = cert; | ||
| } | ||
|
|
||
| private static void ThrowIfRegionMissingForImplicitMtls(IServiceBundle serviceBundle) | ||
| { | ||
| // Implicit bearer-over-mTLS requires region only for AAD | ||
| if (serviceBundle.Config.Authority.AuthorityInfo.AuthorityType == AuthorityType.Aad && | ||
| serviceBundle.Config.AzureRegion == null) | ||
| { | ||
| throw new MsalClientException( | ||
| MsalError.MtlsBearerWithoutRegion, | ||
| MsalErrorMessage.MtlsBearerWithoutRegion); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
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.