public void SaveAccessToken(MsalAccessTokenCacheItem item) { string itemKey = item.GetKey().ToString(); string partitionKey = CacheKeyFactory.GetKeyFromCachedItem(item); AccessTokenCacheDictionary .GetOrAdd(partitionKey, new ConcurrentDictionary <string, MsalAccessTokenCacheItem>())[itemKey] = item; // if a conflict occurs, pick the latest value }
public void SaveAccount(MsalAccountCacheItem item) { string itemKey = item.GetKey().ToString(); string partitionKey = CacheKeyFactory.GetKeyFromCachedItem(item); AccountCacheDictionary .GetOrAdd(partitionKey, new ConcurrentDictionary <string, MsalAccountCacheItem>())[itemKey] = item; }
public void DeleteAccount(MsalAccountCacheItem item) { string partitionKey = CacheKeyFactory.GetKeyFromCachedItem(item); AccountCacheDictionary.TryGetValue(partitionKey, out var partition); if (partition == null || !partition.TryRemove(item.GetKey().ToString(), out _)) { _logger.InfoPii( $"Cannot delete account because it was not found in the cache. Key {item.GetKey()}.", "Cannot delete account because it was not found in the cache"); } }
public void GetAllAccounts_Test() { var accessor = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null); var acc1 = TokenCacheHelper.CreateAccountItem("tenant1", "homeAccountId"); var acc2 = TokenCacheHelper.CreateAccountItem("tenant1", "homeAccountId2"); var acc3 = TokenCacheHelper.CreateAccountItem("tenant2", "homeAccountId"); string partitionKey1 = CacheKeyFactory.GetKeyFromCachedItem(acc1); string partitionKey2 = CacheKeyFactory.GetKeyFromCachedItem(acc2); // Assert: Returns empty collection Assert.AreEqual(0, accessor.GetAllAccounts().Count); Assert.AreEqual(0, accessor.GetAllAccounts(partitionKey1).Count); accessor.SaveAccount(acc1); accessor.SaveAccount(acc2); accessor.SaveAccount(acc3); // Assert: Get all tokens and get all tokens by partition key Assert.AreEqual(3, accessor.GetAllAccounts().Count); Assert.AreEqual(2, accessor.GetAllAccounts(partitionKey1).Count); Assert.AreEqual(1, accessor.GetAllAccounts(partitionKey2).Count); }
public void GetAllRefreshTokens_Test() { var accessor = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null); var rt1 = TokenCacheHelper.CreateRefreshTokenItem("userAssertion", "homeAccountId"); var rt2 = TokenCacheHelper.CreateRefreshTokenItem("userAssertion", "homeAccountId2"); var rt3 = TokenCacheHelper.CreateRefreshTokenItem("userAssertion2", "homeAccountId"); string partitionKey1 = CacheKeyFactory.GetKeyFromCachedItem(rt1); string partitionKey2 = CacheKeyFactory.GetKeyFromCachedItem(rt3); // Assert: Returns empty collection Assert.AreEqual(0, accessor.GetAllRefreshTokens().Count); Assert.AreEqual(0, accessor.GetAllRefreshTokens(partitionKey1).Count); accessor.SaveRefreshToken(rt1); accessor.SaveRefreshToken(rt2); accessor.SaveRefreshToken(rt3); // Assert: Get all tokens and get all tokens by partition key Assert.AreEqual(3, accessor.GetAllRefreshTokens().Count); Assert.AreEqual(2, accessor.GetAllRefreshTokens(partitionKey1).Count); Assert.AreEqual(1, accessor.GetAllRefreshTokens(partitionKey2).Count); }
public void SaveAccount_Test() { var accessor = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null); var acc1 = TokenCacheHelper.CreateAccountItem("tenant1", "homeAccountId"); // Assert: Saves with new item accessor.SaveAccount(acc1); Assert.AreEqual(1, accessor.GetAllAccounts().Count); Assert.AreEqual(1, accessor.AccountCacheDictionary.Count); string partitionKey1 = CacheKeyFactory.GetKeyFromCachedItem(acc1); Assert.IsNotNull(accessor.AccountCacheDictionary[partitionKey1][acc1.GetKey().ToString()]); var acc2 = TokenCacheHelper.CreateAccountItem("tenant2", "homeAccountId"); // Assert: Save under the existing partition accessor.SaveAccount(acc2); Assert.AreEqual(2, accessor.GetAllAccounts().Count); Assert.AreEqual(1, accessor.AccountCacheDictionary.Count); Assert.IsNotNull(accessor.AccountCacheDictionary[partitionKey1][acc2.GetKey().ToString()]); var acc3 = TokenCacheHelper.CreateAccountItem("tenant1", "homeAccountId2"); // Assert: Save under a new partition accessor.SaveAccount(acc3); // Assert: Save overwrites the existing token accessor.SaveAccount(acc3); Assert.AreEqual(3, accessor.GetAllAccounts().Count); Assert.AreEqual(2, accessor.AccountCacheDictionary.Count); string partitionKey2 = CacheKeyFactory.GetKeyFromCachedItem(acc3); Assert.IsNotNull(accessor.AccountCacheDictionary[partitionKey2][acc3.GetKey().ToString()]); }
public void SaveRefreshToken_Test() { var accessor = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null); var rt1 = TokenCacheHelper.CreateRefreshTokenItem("userAssertion", "homeAccountId"); // Assert: Saves with new item accessor.SaveRefreshToken(rt1); Assert.AreEqual(1, accessor.GetAllRefreshTokens().Count); Assert.AreEqual(1, accessor.RefreshTokenCacheDictionary.Count); string partitionKey1 = CacheKeyFactory.GetKeyFromCachedItem(rt1); Assert.IsNotNull(accessor.RefreshTokenCacheDictionary[partitionKey1][rt1.GetKey().ToString()]); var rt2 = TokenCacheHelper.CreateRefreshTokenItem("userAssertion", "homeAccountId2"); // Assert: Save under the existing partition accessor.SaveRefreshToken(rt2); Assert.AreEqual(2, accessor.GetAllRefreshTokens().Count); Assert.AreEqual(1, accessor.RefreshTokenCacheDictionary.Count); Assert.IsNotNull(accessor.RefreshTokenCacheDictionary[partitionKey1][rt2.GetKey().ToString()]); var rt3 = TokenCacheHelper.CreateRefreshTokenItem("userAssertion2", "homeAccountId"); // Assert: Save under a new partition accessor.SaveRefreshToken(rt3); // Assert: Save overwrites the existing token accessor.SaveRefreshToken(rt3); Assert.AreEqual(3, accessor.GetAllRefreshTokens().Count); Assert.AreEqual(2, accessor.RefreshTokenCacheDictionary.Count); string partitionKey2 = CacheKeyFactory.GetKeyFromCachedItem(rt3); Assert.IsNotNull(accessor.RefreshTokenCacheDictionary[partitionKey2][rt3.GetKey().ToString()]); }
public void PartitionKeyForCache() { var cache = new TokenCache(_serviceBundle, isApplicationTokenCache: false); var accessor = (cache as ITokenCacheInternal).Accessor; TokenCacheHelper.PopulateCache((cache as ITokenCacheInternal).Accessor); var at = accessor.GetAllAccessTokens().First(); var rt = accessor.GetAllRefreshTokens().First(); var idt = accessor.GetAllIdTokens().First(); var acc = accessor.GetAllAccounts().First(); Assert.AreEqual(at.HomeAccountId, CacheKeyFactory.GetKeyFromCachedItem(at)); Assert.AreEqual(rt.HomeAccountId, CacheKeyFactory.GetKeyFromCachedItem(rt)); Assert.AreEqual(idt.HomeAccountId, CacheKeyFactory.GetKeyFromCachedItem(idt)); Assert.AreEqual(acc.HomeAccountId, CacheKeyFactory.GetKeyFromCachedItem(acc)); at = at.WithUserAssertion("at_hash"); rt.OboCacheKey = "rt_hash"; Assert.AreEqual("at_hash", CacheKeyFactory.GetKeyFromCachedItem(at)); Assert.AreEqual("rt_hash", CacheKeyFactory.GetKeyFromCachedItem(rt)); Assert.AreEqual(idt.HomeAccountId, CacheKeyFactory.GetKeyFromCachedItem(idt)); Assert.AreEqual(acc.HomeAccountId, CacheKeyFactory.GetKeyFromCachedItem(acc)); }
private string GetPartitionKey(bool isAppCache, MsalAccessTokenCacheItem atItem) { return(isAppCache ? CacheKeyFactory.GetClientCredentialKey(atItem.ClientId, atItem.TenantId, atItem.KeyId) : CacheKeyFactory.GetKeyFromCachedItem(atItem)); }