-
Notifications
You must be signed in to change notification settings - Fork 378
[POC] Expose secure HTTP client factory and propagate binding certificate in authentication result #5535
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
base: main
Are you sure you want to change the base?
[POC] Expose secure HTTP client factory and propagate binding certificate in authentication result #5535
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,18 +7,18 @@ | |
| namespace Microsoft.Identity.Client | ||
| { | ||
| /// <summary> | ||
| /// Internal factory responsible for creating HttpClient instances configured for mutual TLS (MTLS). | ||
| /// This factory is specifically intended for use within the MSAL library for secure communication with Azure AD using MTLS. | ||
| /// A factory responsible for creating HttpClient instances configured for mutual TLS (MTLS). | ||
| /// This factory is intended for secure communication with Azure AD using MTLS. | ||
| /// For more details on HttpClient instancing, see https://learn.microsoft.com/dotnet/api/system.net.http.httpclient?view=net-7.0#instancing. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Implementations of this interface must be thread-safe. | ||
| /// It is important to reuse HttpClient instances to avoid socket exhaustion. | ||
| /// Do not create a new HttpClient for each call to <see cref="GetHttpClient(X509Certificate2)"/>. | ||
| /// If your application requires Integrated Windows Authentication, set <see cref="HttpClientHandler.UseDefaultCredentials"/> to true. | ||
| /// This interface is intended for internal use by MSAL only and is designed to support MTLS scenarios. | ||
| /// This interface is designed to support MTLS scenarios. | ||
| /// </remarks> | ||
| internal interface IMsalMtlsHttpClientFactory : IMsalHttpClientFactory | ||
| public interface IMsalMtlsHttpClientFactory : IMsalHttpClientFactory | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gladjohn - don't we need this to be made public for MSIv2 as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we don't need to expose this for MSI v2. We will continue to use the existing factory |
||
| { | ||
| /// <summary> | ||
| /// Returns an HttpClient configured with a certificate for mutual TLS authentication. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,13 +12,14 @@ | |
| namespace Microsoft.Identity.Client.PlatformsCommon.Shared | ||
| { | ||
| /// <summary> | ||
| /// A simple implementation of the HttpClient factory that uses a managed HttpClientHandler | ||
| /// An implementation of the HttpClient factory that uses a managed HttpClientHandler. | ||
| /// This factory is intended to be used by MTLS scenarios or where server certificate validation is required. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// .NET should use the IHttpClientFactory, but MSAL cannot take a dependency on it. | ||
| /// .NET should use SocketHandler, but UseDefaultCredentials doesn't work with it | ||
| /// </remarks> | ||
| internal class SimpleHttpClientFactory : IMsalMtlsHttpClientFactory, IMsalSFHttpClientFactory | ||
| public class SecureHttpClientFactory : IMsalMtlsHttpClientFactory, IMsalSFHttpClientFactory | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you make this public? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mostly to reuse the logic to create HTTP client with setting up a certificate by Identity.Web but it can be also beneficial for users of MSAL.NET who wants either secure HTTP client factory (with mTLS or server certificate validation). |
||
| { | ||
| //Please see (https://aka.ms/msal-httpclient-info) for important information regarding the HttpClient. | ||
| private static readonly ConcurrentDictionary<string, HttpClient> s_httpClientPool = new ConcurrentDictionary<string, HttpClient>(); | ||
|
|
@@ -61,11 +62,20 @@ private static HttpClient CreateMtlsHttpClient(X509Certificate2 bindingCertifica | |
| #endif | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| public HttpClient GetHttpClient() | ||
| { | ||
| return s_httpClientPool.GetOrAdd("non_mtls", CreateHttpClient()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| /// <param name="x509Certificate2"></param> | ||
| /// <returns></returns> | ||
| public HttpClient GetHttpClient(X509Certificate2 x509Certificate2) | ||
| { | ||
| if (x509Certificate2 == null) | ||
|
|
@@ -88,6 +98,11 @@ private static void CheckAndManageCache() | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| /// <param name="validateServerCert"></param> | ||
| /// <returns></returns> | ||
| // This method is used for Service Fabric scenarios where a custom server certificate validation callback is required. | ||
| // It allows the caller to provide a custom HttpClientHandler with the callback. | ||
| // The server cert rotates so we need a new HttpClient for each call. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| Microsoft.Identity.Client.IMsalMtlsHttpClientFactory | ||
| Microsoft.Identity.Client.IMsalMtlsHttpClientFactory.GetHttpClient(System.Security.Cryptography.X509Certificates.X509Certificate2 x509Certificate2) -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.GetHttpClient() -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.GetHttpClient(System.Func<System.Net.Http.HttpRequestMessage, System.Security.Cryptography.X509Certificates.X509Certificate2, System.Security.Cryptography.X509Certificates.X509Chain, System.Net.Security.SslPolicyErrors, bool> validateServerCert) -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.GetHttpClient(System.Security.Cryptography.X509Certificates.X509Certificate2 x509Certificate2) -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.SecureHttpClientFactory() -> void |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| Microsoft.Identity.Client.IMsalMtlsHttpClientFactory | ||
| Microsoft.Identity.Client.IMsalMtlsHttpClientFactory.GetHttpClient(System.Security.Cryptography.X509Certificates.X509Certificate2 x509Certificate2) -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.GetHttpClient() -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.GetHttpClient(System.Func<System.Net.Http.HttpRequestMessage, System.Security.Cryptography.X509Certificates.X509Certificate2, System.Security.Cryptography.X509Certificates.X509Chain, System.Net.Security.SslPolicyErrors, bool> validateServerCert) -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.GetHttpClient(System.Security.Cryptography.X509Certificates.X509Certificate2 x509Certificate2) -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.SecureHttpClientFactory() -> void |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| Microsoft.Identity.Client.IMsalMtlsHttpClientFactory | ||
| Microsoft.Identity.Client.IMsalMtlsHttpClientFactory.GetHttpClient(System.Security.Cryptography.X509Certificates.X509Certificate2 x509Certificate2) -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.GetHttpClient() -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.GetHttpClient(System.Func<System.Net.Http.HttpRequestMessage, System.Security.Cryptography.X509Certificates.X509Certificate2, System.Security.Cryptography.X509Certificates.X509Chain, System.Net.Security.SslPolicyErrors, bool> validateServerCert) -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.GetHttpClient(System.Security.Cryptography.X509Certificates.X509Certificate2 x509Certificate2) -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.SecureHttpClientFactory() -> void |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| Microsoft.Identity.Client.IMsalMtlsHttpClientFactory | ||
| Microsoft.Identity.Client.IMsalMtlsHttpClientFactory.GetHttpClient(System.Security.Cryptography.X509Certificates.X509Certificate2 x509Certificate2) -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.GetHttpClient() -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.GetHttpClient(System.Func<System.Net.Http.HttpRequestMessage, System.Security.Cryptography.X509Certificates.X509Certificate2, System.Security.Cryptography.X509Certificates.X509Chain, System.Net.Security.SslPolicyErrors, bool> validateServerCert) -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.GetHttpClient(System.Security.Cryptography.X509Certificates.X509Certificate2 x509Certificate2) -> System.Net.Http.HttpClient | ||
| Microsoft.Identity.Client.PlatformsCommon.Shared.SecureHttpClientFactory.SecureHttpClientFactory() -> void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it an interface not an actual factory?