-
Notifications
You must be signed in to change notification settings - Fork 379
Enable mTLS Proof‑of‑Possession for Client‑Assertion Delegates #5409
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 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
898aa83
initial
GladwinJohnson 0dbf269
pr comments
GladwinJohnson 43bcc9e
build break for public api change
GladwinJohnson cac7a77
IsMtlsPopEnabled
GladwinJohnson 4469c4b
pr comments
GladwinJohnson 2ab5e7f
Merge branch 'main' into gladjohn/new-assertion-api
gladjohn 4bfc589
name change
GladwinJohnson 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
22 changes: 22 additions & 0 deletions
22
src/client/Microsoft.Identity.Client/AppConfig/AssertionResponse.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,22 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Security.Cryptography.X509Certificates; | ||
|
|
||
| namespace Microsoft.Identity.Client | ||
| { | ||
| /// <summary> | ||
| /// Container returned from <c>WithClientAssertion</c>. | ||
| /// </summary> | ||
| public class AssertionResponse | ||
| { | ||
| /// <summary>Base-64 JWT that MSAL sends as <c>client_assertion</c>.</summary> | ||
| public string Assertion { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Certificate for mutual-TLS PoP. | ||
| /// Leave <c>null</c> for a bearer assertion. | ||
| /// </summary> | ||
| public X509Certificate2 TokenBindingCertificate { get; init; } | ||
| } | ||
| } |
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
109 changes: 109 additions & 0 deletions
109
.../Microsoft.Identity.Client/Internal/ClientCredential/ClientAssertionDelegateCredential.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,109 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Security.Cryptography.X509Certificates; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Identity.Client.Internal.Requests; | ||
| using Microsoft.Identity.Client.OAuth2; | ||
| using Microsoft.Identity.Client.PlatformsCommon.Interfaces; | ||
| using Microsoft.Identity.Client.TelemetryCore; | ||
|
|
||
| namespace Microsoft.Identity.Client.Internal.ClientCredential | ||
| { | ||
| /// <summary> | ||
| /// Handles client assertions supplied via a delegate that returns | ||
| /// <see cref="AssertionResponse"/> (JWT + optional certificate). | ||
| /// </summary> | ||
| internal sealed class ClientAssertionDelegateCredential : IClientCredential | ||
| { | ||
| private readonly Func<AssertionRequestOptions, CancellationToken, Task<AssertionResponse>> _assertionDelegate; | ||
|
|
||
| public ClientAssertionDelegateCredential( | ||
| Func<AssertionRequestOptions, CancellationToken, Task<AssertionResponse>> assertionDelegate) | ||
| { | ||
| _assertionDelegate = assertionDelegate ?? throw new ArgumentNullException(nameof(assertionDelegate)); | ||
| } | ||
|
|
||
| public AssertionType AssertionType => AssertionType.ClientAssertion; | ||
|
|
||
| private X509Certificate2 _cachedCertificate; | ||
|
|
||
| internal X509Certificate2 PeekCertificate(string clientId) | ||
| { | ||
| // Return the cached value if we already probed once | ||
| if (_cachedCertificate != null) | ||
| { | ||
| return _cachedCertificate; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var probeOpts = new AssertionRequestOptions | ||
| { | ||
| ClientID = clientId, | ||
| }; | ||
|
|
||
| var resp = _assertionDelegate(probeOpts, CancellationToken.None) | ||
| .ConfigureAwait(false) | ||
| .GetAwaiter() | ||
gladjohn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .GetResult(); | ||
|
|
||
| _cachedCertificate = resp?.TokenBindingCertificate; | ||
| } | ||
| catch | ||
gladjohn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| } | ||
|
|
||
| return _cachedCertificate; | ||
| } | ||
|
|
||
| public async Task AddConfidentialClientParametersAsync( | ||
| OAuth2Client oAuth2Client, | ||
| AuthenticationRequestParameters requestParameters, | ||
| ICryptographyManager cryptographyManager, | ||
| string tokenEndpoint, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| // Build the same AssertionRequestOptions old code produced | ||
| var opts = new AssertionRequestOptions | ||
| { | ||
| CancellationToken = cancellationToken, | ||
| ClientID = requestParameters.AppConfig.ClientId, | ||
| TokenEndpoint = tokenEndpoint, | ||
| ClientCapabilities = requestParameters.RequestContext.ServiceBundle.Config.ClientCapabilities, | ||
| Claims = requestParameters.Claims, | ||
| ClientAssertionFmiPath = requestParameters.ClientAssertionFmiPath | ||
| }; | ||
|
|
||
| // Execute delegate | ||
| AssertionResponse resp = await _assertionDelegate(opts, cancellationToken) | ||
| .ConfigureAwait(false); | ||
|
|
||
| // Empty JWT is not allowed | ||
| if (string.IsNullOrWhiteSpace(resp.Assertion)) | ||
| { | ||
| throw new ArgumentException( | ||
gladjohn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "The assertion delegate returned an empty JWT.", | ||
| nameof(_assertionDelegate)); | ||
| } | ||
|
|
||
| // Set assertion type | ||
| if (resp.TokenBindingCertificate != null) | ||
| { | ||
| oAuth2Client.AddBodyParameter( | ||
| OAuth2Parameter.ClientAssertionType, | ||
| OAuth2AssertionType.JwtPop); | ||
| } | ||
| else | ||
| { | ||
| oAuth2Client.AddBodyParameter( | ||
| OAuth2Parameter.ClientAssertionType, | ||
| OAuth2AssertionType.JwtBearer); | ||
| } | ||
|
|
||
| oAuth2Client.AddBodyParameter(OAuth2Parameter.ClientAssertion, resp.Assertion); | ||
| } | ||
| } | ||
| } | ||
81 changes: 0 additions & 81 deletions
81
...soft.Identity.Client/Internal/ClientCredential/SignedAssertionDelegateClientCredential.cs
This file was deleted.
Oops, something went wrong.
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,11 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #if !NET8_0_OR_GREATER | ||
gladjohn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| namespace System.Runtime.CompilerServices | ||
| { | ||
| internal static class IsExternalInit | ||
| { | ||
| } | ||
| } | ||
| #endif | ||
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
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,7 @@ | ||
| Microsoft.Identity.Client.AssertionResponse | ||
| Microsoft.Identity.Client.AssertionResponse.Assertion.get -> string | ||
| Microsoft.Identity.Client.AssertionResponse.Assertion.init -> void | ||
| Microsoft.Identity.Client.AssertionResponse.AssertionResponse() -> void | ||
| Microsoft.Identity.Client.AssertionResponse.TokenBindingCertificate.get -> System.Security.Cryptography.X509Certificates.X509Certificate2 | ||
| Microsoft.Identity.Client.AssertionResponse.TokenBindingCertificate.init -> void | ||
| Microsoft.Identity.Client.ConfidentialClientApplicationBuilder.WithClientAssertion(System.Func<Microsoft.Identity.Client.AssertionRequestOptions, System.Threading.CancellationToken, System.Threading.Tasks.Task<Microsoft.Identity.Client.AssertionResponse>> boundAssertionAsync) -> Microsoft.Identity.Client.ConfidentialClientApplicationBuilder |
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,7 @@ | ||
| Microsoft.Identity.Client.AssertionResponse | ||
| Microsoft.Identity.Client.AssertionResponse.Assertion.get -> string | ||
| Microsoft.Identity.Client.AssertionResponse.Assertion.init -> void | ||
| Microsoft.Identity.Client.AssertionResponse.AssertionResponse() -> void | ||
| Microsoft.Identity.Client.AssertionResponse.TokenBindingCertificate.get -> System.Security.Cryptography.X509Certificates.X509Certificate2 | ||
| Microsoft.Identity.Client.AssertionResponse.TokenBindingCertificate.init -> void | ||
| Microsoft.Identity.Client.ConfidentialClientApplicationBuilder.WithClientAssertion(System.Func<Microsoft.Identity.Client.AssertionRequestOptions, System.Threading.CancellationToken, System.Threading.Tasks.Task<Microsoft.Identity.Client.AssertionResponse>> boundAssertionAsync) -> Microsoft.Identity.Client.ConfidentialClientApplicationBuilder |
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.