Skip to content

Commit 597da2f

Browse files
trwalketrwalke
andauthored
Ensure additional cache parameters are persisted in cache serialization (#5262)
* Ensure additional cache parameters are persisted in cache serialization * Updating test attributes --------- Co-authored-by: trwalke <[email protected]>
1 parent 487b1ec commit 597da2f

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

src/client/Microsoft.Identity.Client/Cache/Items/MsalAccessTokenCacheItem.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ internal static MsalAccessTokenCacheItem FromJObject(JObject j)
321321
string tokenType = JsonHelper.ExtractExistingOrDefault<string>(j, StorageJsonKeys.TokenType) ?? StorageJsonValues.TokenTypeBearer;
322322
string scopes = JsonHelper.ExtractExistingOrEmptyString(j, StorageJsonKeys.Target);
323323
var additionalCacheKeyComponents = JsonHelper.ExtractInnerJsonAsDictionary(j, StorageJsonKeys.CacheExtensions);
324+
var persistedCacheParameters = JsonHelper.ExtractInnerJsonAsDictionary(j, StorageJsonKeys.PersistedCacheParameters);
324325

325326
var item = new MsalAccessTokenCacheItem(
326327
scopes: scopes,
@@ -338,6 +339,7 @@ internal static MsalAccessTokenCacheItem FromJObject(JObject j)
338339
item.CredentialType = StorageJsonValues.CredentialTypeAccessTokenExtended;
339340
}
340341

342+
item.PersistedCacheParameters = persistedCacheParameters;
341343
item.OboCacheKey = oboCacheKey;
342344
item.PopulateFieldsFromJObject(j);
343345

@@ -367,22 +369,29 @@ internal override JObject ToJObject()
367369
// previous versions of MSAL used "ext_expires_on" instead of the correct "extended_expires_on".
368370
// this is here for back compatibility
369371
SetItemIfValueNotNull(json, StorageJsonKeys.ExtendedExpiresOn_MsalCompat, extExpiresUnixTimestamp);
370-
if (AdditionalCacheKeyComponents != null)
372+
373+
StoreDictionaryInJson(json, StorageJsonKeys.CacheExtensions, AdditionalCacheKeyComponents);
374+
StoreDictionaryInJson(json, StorageJsonKeys.PersistedCacheParameters, PersistedCacheParameters);
375+
return json;
376+
}
377+
378+
private void StoreDictionaryInJson(JObject json, string key, IDictionary<string, string> values)
379+
{
380+
if (values != null)
371381
{
372382
#if SUPPORTS_SYSTEM_TEXT_JSON
373383
var obj = new JsonObject();
374384

375-
foreach (KeyValuePair<string, string> accId in AdditionalCacheKeyComponents)
385+
foreach (KeyValuePair<string, string> value in values)
376386
{
377-
obj[accId.Key] = accId.Value;
387+
obj[value.Key] = value.Value;
378388
}
379389

380-
json[StorageJsonKeys.CacheExtensions] = obj;
390+
json[key] = obj;
381391
#else
382-
SetItemIfValueNotNull(json, StorageJsonKeys.CacheExtensions, JObject.FromObject(AdditionalCacheKeyComponents));
392+
SetItemIfValueNotNull(json, key, JObject.FromObject(values));
383393
#endif
384394
}
385-
return json;
386395
}
387396

388397
internal string ToJsonString()

src/client/Microsoft.Identity.Client/Cache/StorageJsonKeys.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ internal static class StorageJsonKeys
4343
public const string ExtendedExpiresOn_MsalCompat = "ext_expires_on";
4444

4545
public const string CacheExtensions = "ext";
46+
public const string PersistedCacheParameters = "persisted_cache_parameters";
4647
}
4748
}

tests/Microsoft.Identity.Test.Unit/AuthExtension/AuthenticationOperationTests.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace Microsoft.Identity.Test.Unit
1818
public class AuthenticationOperationTests : TestBase
1919
{
2020
private const string ProtectedUrl = "https://www.contoso.com/path1/path2?queryParam1=a&queryParam2=b";
21+
private byte[] _serializedCache;
2122

2223
[TestMethod]
2324
public async Task Should_UseCustomRequestHeaders_And_StoreAdditionalParameters()
@@ -66,8 +67,10 @@ public async Task Should_UseCustomRequestHeaders_And_StoreAdditionalParameters()
6667
}
6768
}
6869

69-
[TestMethod]
70-
public async Task Should_UseCustomRequestHeaders_And_StoreAdditionalParametersWithCaching()
70+
[DataTestMethod] // Fix for regression https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/5261
71+
[DataRow(false)]
72+
[DataRow(true)]
73+
public async Task Should_UseCustomRequestHeaders_And_StoreAdditionalParametersWithCaching(bool useSerializedCache)
7174
{
7275
using (var httpManager = new MockHttpManager())
7376
{
@@ -100,6 +103,12 @@ public async Task Should_UseCustomRequestHeaders_And_StoreAdditionalParametersWi
100103
AdditionalCacheParameters = new[] { "additional_param1", "additional_param2" }
101104
};
102105

106+
if (useSerializedCache)
107+
{
108+
app.AppTokenCache.SetBeforeAccess(BeforeCacheAccess);
109+
app.AppTokenCache.SetAfterAccess(AfterCacheAccess);
110+
}
111+
103112
// Act
104113
var result = await app.AcquireTokenForClient(TestConstants.s_scope.ToArray())
105114
.WithTenantId(TestConstants.Utid)
@@ -130,6 +139,16 @@ public async Task Should_UseCustomRequestHeaders_And_StoreAdditionalParametersWi
130139
}
131140
}
132141

142+
private void BeforeCacheAccess(TokenCacheNotificationArgs args)
143+
{
144+
args.TokenCache.DeserializeMsalV3(_serializedCache);
145+
}
146+
147+
private void AfterCacheAccess(TokenCacheNotificationArgs args)
148+
{
149+
_serializedCache = args.TokenCache.SerializeMsalV3();
150+
}
151+
133152
[TestMethod]
134153
public async Task Should_UseEmptyExtension_And_Parameters()
135154
{

0 commit comments

Comments
 (0)