Skip to content

Commit 1c399c3

Browse files
ImdsV2: Throw on MTLS when OS is not Windows (#5520)
1 parent 1ae351b commit 1c399c3

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/client/Microsoft.Identity.Client/ApiConfig/AcquireTokenForClientParameterBuilder.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.ComponentModel;
7-
using System.Security.Cryptography;
8-
using System.Security.Cryptography.X509Certificates;
9-
using System.Text;
107
using System.Threading;
118
using System.Threading.Tasks;
129
using Microsoft.Identity.Client.ApiConfig.Executors;
@@ -15,10 +12,9 @@
1512
using Microsoft.Identity.Client.Extensibility;
1613
using Microsoft.Identity.Client.Internal;
1714
using Microsoft.Identity.Client.Internal.ClientCredential;
18-
using Microsoft.Identity.Client.ManagedIdentity.V2;
1915
using Microsoft.Identity.Client.OAuth2;
16+
using Microsoft.Identity.Client.PlatformsCommon.Shared;
2017
using Microsoft.Identity.Client.TelemetryCore.Internal.Events;
21-
using Microsoft.Identity.Client.Utils;
2218

2319
namespace Microsoft.Identity.Client
2420
{
@@ -100,14 +96,25 @@ public AcquireTokenForClientParameterBuilder WithSendX5C(bool withSendX5C)
10096
/// <returns>The current instance of <see cref="AcquireTokenForClientParameterBuilder"/> to enable method chaining.</returns>
10197
public AcquireTokenForClientParameterBuilder WithMtlsProofOfPossession()
10298
{
103-
#if NET462
10499
if (ServiceBundle.Config.IsManagedIdentity)
105100
{
106-
throw new MsalClientException(
107-
MsalError.MtlsNotSupportedForManagedIdentity,
108-
MsalErrorMessage.MtlsNotSupportedForManagedIdentityMessage);
109-
}
101+
void MtlsNotSupportedForManagedIdentity(string message)
102+
{
103+
throw new MsalClientException(
104+
MsalError.MtlsNotSupportedForManagedIdentity,
105+
message);
106+
}
107+
108+
if (!DesktopOsHelper.IsWindows())
109+
{
110+
MtlsNotSupportedForManagedIdentity(MsalErrorMessage.MtlsNotSupportedForNonWindowsMessage);
111+
}
112+
113+
#if NET462
114+
MtlsNotSupportedForManagedIdentity(MsalErrorMessage.MtlsNotSupportedForManagedIdentityMessage);
110115
#endif
116+
}
117+
111118
if (ServiceBundle.Config.ClientCredential is CertificateClientCredential certificateCredential)
112119
{
113120
if (certificateCredential.Certificate == null)

src/client/Microsoft.Identity.Client/MsalErrorMessage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ public static string InvalidTokenProviderResponseValue(string invalidValueName)
442442
public const string MtlsInvalidAuthorityTypeMessage = "mTLS PoP is only supported for AAD authority type. See https://aka.ms/msal-net-pop for details.";
443443
public const string MtlsNonTenantedAuthorityNotAllowedMessage = "mTLS authentication requires a tenanted authority. Using 'common', 'organizations', or similar non-tenanted authorities is not allowed. Please provide an authority with a specific tenant ID (e.g., 'https://login.microsoftonline.com/{tenantId}'). See https://aka.ms/msal-net-pop for details.";
444444
public const string MtlsNotSupportedForManagedIdentityMessage = "IMDSv2 flow is not supported on .NET Framework 4.6.2. Cryptographic operations required for managed identity authentication are unavailable on this platform.";
445+
public const string MtlsNotSupportedForNonWindowsMessage = "mTLS PoP with Managed Identity is not supported on this OS. See https://aka.ms/msal-net-pop.";
445446
public const string RegionRequiredForMtlsPopMessage = "Regional auto-detect failed. mTLS Proof-of-Possession requires a region to be specified, as there is no global endpoint for mTLS. See https://aka.ms/msal-net-pop for details.";
446447
public const string ForceRefreshAndTokenHasNotCompatible = "Cannot specify ForceRefresh and AccessTokenSha256ToRefresh in the same request.";
447448
public const string RequestTimeOut = "Request to the endpoint timed out.";

0 commit comments

Comments
 (0)