Skip to content

Commit 4d4728e

Browse files
address pr comments
1 parent 706851c commit 4d4728e

File tree

7 files changed

+70
-38
lines changed

7 files changed

+70
-38
lines changed

src/client/Microsoft.Identity.Client/Internal/Requests/ManagedIdentityAuthRequest.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ protected override async Task<AuthenticationResult> ExecuteAsync(CancellationTok
3939
// 1. FIRST, handle ForceRefresh
4040
if (_managedIdentityParameters.ForceRefresh)
4141
{
42+
//log a warning if Claims are also set
43+
if (!string.IsNullOrEmpty(AuthenticationRequestParameters.Claims))
44+
{
45+
logger.Warning("[ManagedIdentityRequest] Both ForceRefresh and Claims are set. Using ForceRefresh to skip cache.");
46+
}
47+
4248
AuthenticationRequestParameters.RequestContext.ApiEvent.CacheInfo = CacheRefreshReason.ForceRefreshOrClaims;
4349
logger.Info("[ManagedIdentityRequest] Skipped using the cache because ForceRefresh was set.");
4450

45-
// We still respect claims if present
46-
_managedIdentityParameters.Claims = AuthenticationRequestParameters.Claims;
47-
4851
// Straight to the MI endpoint
4952
authResult = await GetAccessTokenAsync(cancellationToken, logger).ConfigureAwait(false);
5053
return authResult;
@@ -60,19 +63,19 @@ protected override async Task<AuthenticationResult> ExecuteAsync(CancellationTok
6063
_managedIdentityParameters.Claims = AuthenticationRequestParameters.Claims;
6164
AuthenticationRequestParameters.RequestContext.ApiEvent.CacheInfo = CacheRefreshReason.ForceRefreshOrClaims;
6265

63-
// If there is a cached token, compute its hash for the “bad token” scenario
66+
// If there is a cached token, compute its hash for the “revoked token” scenario
6467
if (cachedAccessTokenItem != null)
6568
{
6669
string cachedTokenHash = _cryptoManager.CreateSha256HashHex(cachedAccessTokenItem.Secret);
6770
_managedIdentityParameters.RevokedTokenHash = cachedTokenHash;
6871

69-
logger.Info("[ManagedIdentityRequest] Claims are present. Computed hash of the cached (bad) token. " +
72+
logger.Info("[ManagedIdentityRequest] Claims are present. Computed hash of the cached (revoked) token. " +
7073
"Will now request a fresh token from the MI endpoint.");
7174
}
7275
else
7376
{
7477
logger.Info("[ManagedIdentityRequest] Claims are present, but no cached token was found. " +
75-
"Requesting a fresh token from the MI endpoint without a bad-token hash.");
78+
"Requesting a fresh token from the MI endpoint without a revoked-token hash.");
7679
}
7780

7881
// In both cases, we skip using the cached token and get a new one

src/client/Microsoft.Identity.Client/ManagedIdentity/AbstractManagedIdentity.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ public virtual async Task<ManagedIdentityResponse> AuthenticateAsync(
5757

5858
ManagedIdentityRequest request = CreateRequest(resource, parameters);
5959

60+
// Automatically add claims / capabilities if this MI source supports them
61+
if (_sourceType.SupportsClaimsAndCapabilities())
62+
{
63+
request.AddClaimsAndCapabilities(
64+
_requestContext.ServiceBundle.Config.ClientCapabilities,
65+
parameters,
66+
_requestContext.Logger);
67+
}
68+
6069
_requestContext.Logger.Info("[Managed Identity] Sending request to managed identity endpoints.");
6170

6271
IRetryPolicy retryPolicy = _requestContext.ServiceBundle.Config.RetryPolicyFactory.GetRetryPolicy(request.RequestType);
@@ -308,23 +317,5 @@ private static void CreateAndThrowException(string errorCode,
308317

309318
throw exception;
310319
}
311-
312-
/// <summary>
313-
/// Sets the request parameter in either the query or body based on the request method.
314-
/// </summary>
315-
/// <param name="request"></param>
316-
/// <param name="key"></param>
317-
/// <param name="value"></param>
318-
protected void SetRequestParameter(ManagedIdentityRequest request, string key, string value)
319-
{
320-
if (request.Method == HttpMethod.Post)
321-
{
322-
request.BodyParameters[key] = value;
323-
}
324-
else
325-
{
326-
request.QueryParameters[key] = value;
327-
}
328-
}
329320
}
330321
}

src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal void AddClaimsAndCapabilities(
6161
!string.IsNullOrEmpty(parameters.RevokedTokenHash))
6262
{
6363
QueryParameters["token_sha256_to_refresh"] = parameters.RevokedTokenHash;
64-
logger.Info("[Managed Identity] Passing SHA-256 of the 'bad' token to Managed Identity endpoint.");
64+
logger.Info("[Managed Identity] Passing SHA-256 of the 'revoked' token to Managed Identity endpoint.");
6565
}
6666
}
6767
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Collections.Generic;
5+
6+
namespace Microsoft.Identity.Client.ManagedIdentity
7+
{
8+
internal static class ManagedIdentitySourceExtensions
9+
{
10+
private static readonly HashSet<ManagedIdentitySource> s_supportsClaimsAndCaps =
11+
[
12+
// add other sources here as they light up
13+
ManagedIdentitySource.ServiceFabric,
14+
];
15+
16+
internal static bool SupportsClaimsAndCapabilities(
17+
this ManagedIdentitySource source) => s_supportsClaimsAndCaps.Contains(source);
18+
}
19+
}

src/client/Microsoft.Identity.Client/ManagedIdentity/ServiceFabricManagedIdentitySource.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ protected override ManagedIdentityRequest CreateRequest(string resource,
8585
request.QueryParameters["api-version"] = ServiceFabricMsiApiVersion;
8686
request.QueryParameters["resource"] = resource;
8787

88-
request.AddClaimsAndCapabilities(
89-
_requestContext.ServiceBundle.Config.ClientCapabilities,
90-
parameters, _requestContext.Logger);
91-
9288
switch (_requestContext.ServiceBundle.Config.ManagedIdentityId.IdType)
9389
{
9490
case AppConfig.ManagedIdentityIdType.ClientId:

src/client/Microsoft.Identity.Client/Utils/CoreHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public static Dictionary<string, string> ParseKeyValueList(
103103
// The value is everything after the first '=' (including any trailing '=')
104104
string value = queryPair.Substring(idx + 1);
105105

106-
// If urlDecode == true, decode both key and value
106+
// Url decoding is needed for parsing OAuth response, but not for parsing WWW-Authenticate header in 401 challenge
107107
if (urlDecode)
108108
{
109109
key = UrlDecode(key);

tests/Microsoft.Identity.Test.Common/Core/Mocks/MockHttpManagerExtensions.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -486,30 +486,53 @@ private static MockHttpMessageHandler BuildMockHandlerForManagedIdentitySource(
486486

487487
var manager = new CommonCryptographyManager();
488488

489-
// If capabilityEnabled, add "xms_cc": "cp1"
490-
if (capabilityEnabled)
489+
// ---------------------------------------------------------------------------
490+
// Client-capabilities (xms_cc) and revoked-token hash
491+
// ---------------------------------------------------------------------------
492+
493+
bool sourceSupportsExtras = managedIdentitySourceType.SupportsClaimsAndCapabilities();
494+
495+
// ----- xms_cc --------------------------------------------------------------
496+
if (sourceSupportsExtras)
491497
{
492-
if (managedIdentitySourceType == ManagedIdentitySource.ServiceFabric)
498+
if (capabilityEnabled)
493499
{
500+
// This source should send xms_cc
494501
expectedQueryParams.Add("xms_cc", "cp1,cp2");
495502
}
503+
else
504+
{
505+
// Capability flag disabled → xms_cc must NOT be present
506+
notExpectedQueryParams.Add("xms_cc", "cp1,cp2");
507+
}
496508
}
497509
else
498510
{
511+
// Source does not support capabilities → never expect xms_cc
499512
notExpectedQueryParams.Add("xms_cc", "cp1,cp2");
500513
}
501514

502-
if (claimsEnabled)
515+
// ----- token_sha256_to_refresh --------------------------------------------
516+
if (sourceSupportsExtras)
503517
{
504-
if (managedIdentitySourceType == ManagedIdentitySource.ServiceFabric)
518+
string hash = manager.CreateSha256HashHex(TestConstants.ATSecret);
519+
520+
if (claimsEnabled)
521+
{
522+
// Claims path active → expect the hash
523+
expectedQueryParams.Add("token_sha256_to_refresh", hash);
524+
}
525+
else
505526
{
506-
expectedQueryParams.Add("token_sha256_to_refresh",
507-
manager.CreateSha256HashHex(TestConstants.ATSecret));
527+
// No claims → hash must be absent
528+
notExpectedQueryParams.Add("token_sha256_to_refresh", hash);
508529
}
509530
}
510531
else
511532
{
512-
notExpectedQueryParams.Add("token_sha256_to_refresh",
533+
// Source does not support hash param → ensure it's absent
534+
notExpectedQueryParams.Add(
535+
"token_sha256_to_refresh",
513536
manager.CreateSha256HashHex(TestConstants.ATSecret));
514537
}
515538

0 commit comments

Comments
 (0)