Skip to content

Commit f112da9

Browse files
trwalketrwalkegladjohn
authored
Adding a flag to TokenCacheNotificationArgs to signal that the clientId is an RMA node. (#5175)
* Adding a flag to TokenCacheNotificationArgs to signal that the client id is an RMA node. * Update src/client/Microsoft.Identity.Client/TokenCacheNotificationArgs.cs Co-authored-by: Gladwin Johnson <[email protected]> * Refactoring to remove bool and add string --------- Co-authored-by: trwalke <[email protected]> Co-authored-by: Gladwin Johnson <[email protected]>
1 parent e090343 commit f112da9

File tree

10 files changed

+84
-15
lines changed

10 files changed

+84
-15
lines changed

src/client/Microsoft.Identity.Client/Internal/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ internal static class Constants
4848
public const string ManagedIdentityDefaultTenant = "managed_identity";
4949
public const string CiamAuthorityHostSuffix = ".ciamlogin.com";
5050
public const string CertSerialNumber = "cert_sn";
51+
public const string FmiNodeClientId = "urn:microsoft:identity:fmi";
5152

5253
public const int CallerSdkIdMaxLength = 10;
5354
public const int CallerSdkVersionMaxLength = 20;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Microsoft.Identity.Client.TokenCacheNotificationArgs.NoDistributedCacheUseReason.get -> string
12
const Microsoft.Identity.Client.MsalError.ForceRefreshNotCompatibleWithTokenHash = "force_refresh_and_token_hash_not_compatible" -> string
23
Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders
3-
static Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders.WithAccessTokenSha256ToRefresh(this Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder builder, string hash) -> Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder
4+
static Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders.WithAccessTokenSha256ToRefresh(this Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder builder, string hash) -> Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Microsoft.Identity.Client.TokenCacheNotificationArgs.NoDistributedCacheUseReason.get -> string
12
const Microsoft.Identity.Client.MsalError.ForceRefreshNotCompatibleWithTokenHash = "force_refresh_and_token_hash_not_compatible" -> string
23
Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders
3-
static Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders.WithAccessTokenSha256ToRefresh(this Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder builder, string hash) -> Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder
4+
static Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders.WithAccessTokenSha256ToRefresh(this Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder builder, string hash) -> Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Microsoft.Identity.Client.TokenCacheNotificationArgs.NoDistributedCacheUseReason.get -> string
12
const Microsoft.Identity.Client.MsalError.ForceRefreshNotCompatibleWithTokenHash = "force_refresh_and_token_hash_not_compatible" -> string
23
Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders
3-
static Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders.WithAccessTokenSha256ToRefresh(this Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder builder, string hash) -> Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder
4+
static Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders.WithAccessTokenSha256ToRefresh(this Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder builder, string hash) -> Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Microsoft.Identity.Client.TokenCacheNotificationArgs.NoDistributedCacheUseReason.get -> string
12
const Microsoft.Identity.Client.MsalError.ForceRefreshNotCompatibleWithTokenHash = "force_refresh_and_token_hash_not_compatible" -> string
23
Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders
3-
static Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders.WithAccessTokenSha256ToRefresh(this Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder builder, string hash) -> Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder
4+
static Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders.WithAccessTokenSha256ToRefresh(this Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder builder, string hash) -> Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Microsoft.Identity.Client.TokenCacheNotificationArgs.NoDistributedCacheUseReason.get -> string
12
const Microsoft.Identity.Client.MsalError.ForceRefreshNotCompatibleWithTokenHash = "force_refresh_and_token_hash_not_compatible" -> string
23
Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders
3-
static Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders.WithAccessTokenSha256ToRefresh(this Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder builder, string hash) -> Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder
4+
static Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders.WithAccessTokenSha256ToRefresh(this Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder builder, string hash) -> Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Microsoft.Identity.Client.TokenCacheNotificationArgs.NoDistributedCacheUseReason.get -> string
12
const Microsoft.Identity.Client.MsalError.ForceRefreshNotCompatibleWithTokenHash = "force_refresh_and_token_hash_not_compatible" -> string
23
Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders
3-
static Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders.WithAccessTokenSha256ToRefresh(this Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder builder, string hash) -> Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder
4+
static Microsoft.Identity.Client.RP.AcquireTokenForClientParameterBuilderForResourceProviders.WithAccessTokenSha256ToRefresh(this Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder builder, string hash) -> Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections;
66
using System.Collections.Generic;
77
using System.Threading;
8+
using Microsoft.Identity.Client.Internal;
89
using Microsoft.Identity.Client.TelemetryCore.TelemetryClient;
910
using Microsoft.IdentityModel.Abstractions;
1011

@@ -40,13 +41,13 @@ public TokenCacheNotificationArgs(
4041
hasTokens,
4142
suggestedCacheExpiry,
4243
cancellationToken,
43-
default,
44-
default,
44+
default,
45+
default,
4546
default,
4647
null,
4748
default)
48-
{
49-
}
49+
{
50+
}
5051

5152
/// <summary>
5253
/// This constructor is for test purposes only. It allows apps to unit test their MSAL token cache implementation code.
@@ -61,7 +62,7 @@ public TokenCacheNotificationArgs(
6162
bool hasTokens,
6263
DateTimeOffset? suggestedCacheExpiry,
6364
CancellationToken cancellationToken,
64-
Guid correlationId)
65+
Guid correlationId)
6566
: this(tokenCache,
6667
clientId,
6768
account,
@@ -76,7 +77,7 @@ public TokenCacheNotificationArgs(
7677
default,
7778
null,
7879
default)
79-
{
80+
{
8081
}
8182

8283
/// <summary>
@@ -92,10 +93,10 @@ public TokenCacheNotificationArgs( // only use this constructor in product co
9293
bool hasTokens,
9394
DateTimeOffset? suggestedCacheExpiry,
9495
CancellationToken cancellationToken,
95-
Guid correlationId,
96+
Guid correlationId,
9697
IEnumerable<string> requestScopes,
9798
string requestTenantId)
98-
99+
99100
{
100101
TokenCache = tokenCache;
101102
ClientId = clientId;
@@ -145,7 +146,7 @@ public TokenCacheNotificationArgs( // only use this constructor in product co
145146
SuggestedCacheExpiry = suggestedCacheExpiry;
146147
IdentityLogger = identityLogger;
147148
PiiLoggingEnabled = piiLoggingEnabled;
148-
TelemetryData = telemetryData?? new TelemetryData();
149+
TelemetryData = telemetryData ?? new TelemetryData();
149150
}
150151

151152
/// <summary>
@@ -255,5 +256,21 @@ public TokenCacheNotificationArgs( // only use this constructor in product co
255256
/// Cache Details contains the details of L1/ L2 cache for telemetry logging.
256257
/// </summary>
257258
public TelemetryData TelemetryData { get; }
259+
260+
/// <summary>
261+
/// Determines whether the client application authentication instance is classified as an FMI (Federated Managed Identity) node under a specified RMA (Resource Managed Authority).
262+
/// </summary>
263+
public string NoDistributedCacheUseReason
264+
{
265+
get
266+
{
267+
if (ClientId.Equals(Constants.FmiNodeClientId))
268+
{
269+
return "The currently provided client id indicates that this is a RMA (Resource Managed Authority) node client. RMA node clients should not use a distributed cache, please use an in memory cache instead.";
270+
}
271+
272+
return string.Empty;
273+
}
274+
}
258275
}
259276
}

tests/Microsoft.Identity.Test.Common/TestConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public static HashSet<string> s_scope
219219

220220
public const string Bearer = "Bearer";
221221
public const string Pop = "PoP";
222+
public const string FmiNodeClientId = "urn:microsoft:identity:fmi";
222223

223224
public static IDictionary<string, string> ExtraQueryParameters
224225
{

tests/Microsoft.Identity.Test.Unit/CacheTests/TokenCacheNotificationTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,5 +533,49 @@ public async Task TokenCacheSerializationArgs_UserCache_TenantIdScopes_Async()
533533

534534
}
535535
}
536+
537+
[TestMethod]
538+
[DataRow(TestConstants.ClientId)]
539+
[DataRow(TestConstants.FmiNodeClientId)]
540+
public async Task TokenCacheSerializationArgs_AppCache_IsFmiClientNode_Async(string clientId)
541+
{
542+
using (var harness = CreateTestHarness())
543+
{
544+
// Confirm that NoDistributedCacheUseReason is correct
545+
// Arrange
546+
var cca = ConfidentialClientApplicationBuilder
547+
.Create(clientId)
548+
.WithClientSecret(TestConstants.ClientSecret)
549+
.WithHttpManager(harness.HttpManager)
550+
.BuildConcrete();
551+
552+
var appTokenCacheRecoder = cca.AppTokenCache.RecordAccess((args) =>
553+
{
554+
Assert.AreEqual(clientId, args.ClientId);
555+
if (clientId.Equals(TestConstants.FmiNodeClientId))
556+
{
557+
// string should not be null or empty
558+
Assert.IsTrue(!string.IsNullOrEmpty(args.NoDistributedCacheUseReason));
559+
}
560+
else
561+
{
562+
// string should be null or empty
563+
Assert.IsTrue(string.IsNullOrEmpty(args.NoDistributedCacheUseReason));
564+
}
565+
566+
CollectionAssert.AreEquivalent(TestConstants.s_scope.ToArray(), args.RequestScopes.ToArray());
567+
});
568+
569+
harness.HttpManager.AddAllMocks(TokenResponseType.Valid_ClientCredentials);
570+
571+
// Act - Client Credentials with authority override
572+
await cca.AcquireTokenForClient(TestConstants.s_scope)
573+
.WithTenantId(TestConstants.TenantId2)
574+
.ExecuteAsync()
575+
.ConfigureAwait(false);
576+
577+
appTokenCacheRecoder.AssertAccessCounts(1, 1);
578+
}
579+
}
536580
}
537581
}

0 commit comments

Comments
 (0)