Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse lists in token cache filtering logic. #3233

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal interface ITokenCacheAccessor
/// It should only support external token caching, in the hope that the external token cache is partitioned.
/// Not all classes that implement this method are required to filter by partition (e.g. mobile)
/// </remarks>
IReadOnlyList<MsalAccessTokenCacheItem> GetAllAccessTokens(string optionalPartitionKey = null);
List<MsalAccessTokenCacheItem> GetAllAccessTokens(string optionalPartitionKey = null);

/// <summary>
/// Returns all refresh tokens from the underlying cache collection.
Expand All @@ -53,7 +53,7 @@ internal interface ITokenCacheAccessor
/// It should only support external token caching, in the hope that the external token cache is partitioned.
/// Not all classes that implement this method are required to filter by partition (e.g. mobile)
/// </remarks>
IReadOnlyList<MsalRefreshTokenCacheItem> GetAllRefreshTokens(string optionalPartitionKey = null);
List<MsalRefreshTokenCacheItem> GetAllRefreshTokens(string optionalPartitionKey = null);

/// <summary>
/// Returns all ID tokens from the underlying cache collection.
Expand All @@ -64,7 +64,7 @@ internal interface ITokenCacheAccessor
/// It should only support external token caching, in the hope that the external token cache is partitioned.
/// Not all classes that implement this method are required to filter by partition (e.g. mobile)
/// </remarks>
IReadOnlyList<MsalIdTokenCacheItem> GetAllIdTokens(string optionalPartitionKey = null);
List<MsalIdTokenCacheItem> GetAllIdTokens(string optionalPartitionKey = null);

/// <summary>
/// Returns all accounts from the underlying cache collection.
Expand All @@ -75,9 +75,9 @@ internal interface ITokenCacheAccessor
/// It should only support external token caching, in the hope that the external token cache is partitioned.
/// Not all classes that implement this method are required to filter by partition (e.g. mobile)
/// </remarks>
IReadOnlyList<MsalAccountCacheItem> GetAllAccounts(string optionalPartitionKey = null);
List<MsalAccountCacheItem> GetAllAccounts(string optionalPartitionKey = null);

IReadOnlyList<MsalAppMetadataCacheItem> GetAllAppMetadata();
List<MsalAppMetadataCacheItem> GetAllAppMetadata();

#if iOS
void SetiOSKeychainSecurityGroup(string keychainSecurityGroup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,22 +139,22 @@ public MsalIdTokenCacheItem GetIdToken(MsalAccessTokenCacheItem accessTokenCache
#endregion

#region GetAll
public IReadOnlyList<MsalAccessTokenCacheItem> GetAllAccessTokens(string optionalPartitionKey = null)
public List<MsalAccessTokenCacheItem> GetAllAccessTokens(string optionalPartitionKey = null)
{
return _accessTokenSharedPreference.All.Values.Cast<string>().Select(x => MsalAccessTokenCacheItem.FromJsonString(x)).ToList();
}

public IReadOnlyList<MsalRefreshTokenCacheItem> GetAllRefreshTokens(string optionalPartitionKey = null)
public List<MsalRefreshTokenCacheItem> GetAllRefreshTokens(string optionalPartitionKey = null)
{
return _refreshTokenSharedPreference.All.Values.Cast<string>().Select(x => MsalRefreshTokenCacheItem.FromJsonString(x)).ToList();
}

public IReadOnlyList<MsalIdTokenCacheItem> GetAllIdTokens(string optionalPartitionKey = null)
public List<MsalIdTokenCacheItem> GetAllIdTokens(string optionalPartitionKey = null)
{
return _idTokenSharedPreference.All.Values.Cast<string>().Select(x => MsalIdTokenCacheItem.FromJsonString(x)).ToList();
}

public IReadOnlyList<MsalAccountCacheItem> GetAllAccounts(string optionalPartitionKey = null)
public List<MsalAccountCacheItem> GetAllAccounts(string optionalPartitionKey = null)
{
return _accountSharedPreference.All.Values.Cast<string>().Select(x => MsalAccountCacheItem.FromJsonString(x)).ToList();
}
Expand Down Expand Up @@ -189,7 +189,7 @@ public void SaveAppMetadata(MsalAppMetadataCacheItem item)
throw new NotImplementedException();
}

public IReadOnlyList<MsalAppMetadataCacheItem> GetAllAppMetadata()
public List<MsalAppMetadataCacheItem> GetAllAppMetadata()
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,28 @@ public void DeleteAccount(MsalAccountCacheItem item)
#endregion

#region GetAllItems
public IReadOnlyList<MsalAccessTokenCacheItem> GetAllAccessTokens(string optionalPartitionKey = null)
public List<MsalAccessTokenCacheItem> GetAllAccessTokens(string optionalPartitionKey = null)
{
return GetPayloadAsString((int)MsalCacheKeys.iOSCredentialAttrType.AccessToken)
.Select(x => MsalAccessTokenCacheItem.FromJsonString(x))
.ToList();
}

public IReadOnlyList<MsalRefreshTokenCacheItem> GetAllRefreshTokens(string optionalPartitionKey = null)
public List<MsalRefreshTokenCacheItem> GetAllRefreshTokens(string optionalPartitionKey = null)
{
return GetPayloadAsString((int)MsalCacheKeys.iOSCredentialAttrType.RefreshToken)
.Select(x => MsalRefreshTokenCacheItem.FromJsonString(x))
.ToList();
}

public IReadOnlyList<MsalIdTokenCacheItem> GetAllIdTokens(string optionalPartitionKey = null)
public List<MsalIdTokenCacheItem> GetAllIdTokens(string optionalPartitionKey = null)
{
return GetPayloadAsString((int)MsalCacheKeys.iOSCredentialAttrType.IdToken)
.Select(x => MsalIdTokenCacheItem.FromJsonString(x))
.ToList();
}

public IReadOnlyList<MsalAccountCacheItem> GetAllAccounts(string optionalPartitionKey = null)
public List<MsalAccountCacheItem> GetAllAccounts(string optionalPartitionKey = null)
{
return GetPayloadAsString(MsalCacheKeys.iOSAuthorityTypeToAttrType[CacheAuthorityType.MSSTS.ToString()])
.Select(x => MsalAccountCacheItem.FromJsonString(x))
Expand Down Expand Up @@ -366,7 +366,7 @@ public void SaveAppMetadata(MsalAppMetadataCacheItem item)
throw new NotImplementedException();
}

public IReadOnlyList<MsalAppMetadataCacheItem> GetAllAppMetadata()
public List<MsalAppMetadataCacheItem> GetAllAppMetadata()
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void DeleteAccount(MsalAccountCacheItem item)
/// WARNING: if partitonKey = null, this API is slow as it loads all tokens, not just from 1 partition.
/// It should only support external token caching, in the hope that the external token cache is partitioned.
/// </summary>
public virtual IReadOnlyList<MsalAccessTokenCacheItem> GetAllAccessTokens(string partitionKey = null)
public virtual List<MsalAccessTokenCacheItem> GetAllAccessTokens(string partitionKey = null)
{
_logger.Always($"[GetAllAccessTokens] Total number of cache partitions found while getting access tokens: {AccessTokenCacheDictionary.Count}");
if (string.IsNullOrEmpty(partitionKey))
Expand All @@ -183,26 +183,26 @@ public virtual IReadOnlyList<MsalAccessTokenCacheItem> GetAllAccessTokens(string
else
{
AccessTokenCacheDictionary.TryGetValue(partitionKey, out ConcurrentDictionary<string, MsalAccessTokenCacheItem> partition);
return partition?.Select(kv => kv.Value)?.ToList() ?? CollectionHelpers.GetEmptyReadOnlyList<MsalAccessTokenCacheItem>();
return partition?.Select(kv => kv.Value)?.ToList() ?? CollectionHelpers.GetEmptyList<MsalAccessTokenCacheItem>();
}
}

public virtual IReadOnlyList<MsalRefreshTokenCacheItem> GetAllRefreshTokens(string partitionKey = null)
public virtual List<MsalRefreshTokenCacheItem> GetAllRefreshTokens(string partitionKey = null)
{
return CollectionHelpers.GetEmptyReadOnlyList<MsalRefreshTokenCacheItem>();
return CollectionHelpers.GetEmptyList<MsalRefreshTokenCacheItem>();
}

public virtual IReadOnlyList<MsalIdTokenCacheItem> GetAllIdTokens(string partitionKey = null)
public virtual List<MsalIdTokenCacheItem> GetAllIdTokens(string partitionKey = null)
{
return CollectionHelpers.GetEmptyReadOnlyList<MsalIdTokenCacheItem>();
return CollectionHelpers.GetEmptyList<MsalIdTokenCacheItem>();
}

public virtual IReadOnlyList<MsalAccountCacheItem> GetAllAccounts(string partitionKey = null)
public virtual List<MsalAccountCacheItem> GetAllAccounts(string partitionKey = null)
{
return CollectionHelpers.GetEmptyReadOnlyList<MsalAccountCacheItem>();
return CollectionHelpers.GetEmptyList<MsalAccountCacheItem>();
}

public IReadOnlyList<MsalAppMetadataCacheItem> GetAllAppMetadata()
public List<MsalAppMetadataCacheItem> GetAllAppMetadata()
{
return AppMetadataDictionary.Select(kv => kv.Value).ToList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void DeleteAccount(MsalAccountCacheItem item)

/// WARNING: if partitionKey is null, this API is slow as it loads all tokens, not just from 1 partition.
/// It should only support external token caching, in the hope that the external token cache is partitioned.
public virtual IReadOnlyList<MsalAccessTokenCacheItem> GetAllAccessTokens(string partitionKey = null)
public virtual List<MsalAccessTokenCacheItem> GetAllAccessTokens(string partitionKey = null)
{
_logger.Always($"[GetAllAccessTokens] Total number of cache partitions found while getting access tokens: {AccessTokenCacheDictionary.Count}");
if (string.IsNullOrEmpty(partitionKey))
Expand All @@ -214,13 +214,13 @@ public virtual IReadOnlyList<MsalAccessTokenCacheItem> GetAllAccessTokens(string
else
{
AccessTokenCacheDictionary.TryGetValue(partitionKey, out ConcurrentDictionary<string, MsalAccessTokenCacheItem> partition);
return partition?.Select(kv => kv.Value)?.ToList() ?? CollectionHelpers.GetEmptyReadOnlyList<MsalAccessTokenCacheItem>();
return partition?.Select(kv => kv.Value)?.ToList() ?? CollectionHelpers.GetEmptyList<MsalAccessTokenCacheItem>();
}
}

/// WARNING: if partitionKey is null, this API is slow as it loads all tokens, not just from 1 partition.
/// It should only support external token caching, in the hope that the external token cache is partitioned.
public virtual IReadOnlyList<MsalRefreshTokenCacheItem> GetAllRefreshTokens(string partitionKey = null)
public virtual List<MsalRefreshTokenCacheItem> GetAllRefreshTokens(string partitionKey = null)
{
_logger.Always($"[GetAllAccessTokens] Total number of cache partitions found while getting refresh tokens: {RefreshTokenCacheDictionary.Count}");
if (string.IsNullOrEmpty(partitionKey))
Expand All @@ -230,13 +230,13 @@ public virtual IReadOnlyList<MsalRefreshTokenCacheItem> GetAllRefreshTokens(stri
else
{
RefreshTokenCacheDictionary.TryGetValue(partitionKey, out ConcurrentDictionary<string, MsalRefreshTokenCacheItem> partition);
return partition?.Select(kv => kv.Value)?.ToList() ?? CollectionHelpers.GetEmptyReadOnlyList<MsalRefreshTokenCacheItem>();
return partition?.Select(kv => kv.Value)?.ToList() ?? CollectionHelpers.GetEmptyList<MsalRefreshTokenCacheItem>();
}
}

/// WARNING: if partitionKey is null, this API is slow as it loads all tokens, not just from 1 partition.
/// It should only support external token caching, in the hope that the external token cache is partitioned.
public virtual IReadOnlyList<MsalIdTokenCacheItem> GetAllIdTokens(string partitionKey = null)
public virtual List<MsalIdTokenCacheItem> GetAllIdTokens(string partitionKey = null)
{
if (string.IsNullOrEmpty(partitionKey))
{
Expand All @@ -245,13 +245,13 @@ public virtual IReadOnlyList<MsalIdTokenCacheItem> GetAllIdTokens(string partiti
else
{
IdTokenCacheDictionary.TryGetValue(partitionKey, out ConcurrentDictionary<string, MsalIdTokenCacheItem> partition);
return partition?.Select(kv => kv.Value)?.ToList() ?? CollectionHelpers.GetEmptyReadOnlyList<MsalIdTokenCacheItem>();
return partition?.Select(kv => kv.Value)?.ToList() ?? CollectionHelpers.GetEmptyList<MsalIdTokenCacheItem>();
}
}

/// WARNING: if partitionKey is null, this API is slow as it loads all tokens, not just from 1 partition.
/// It should only support external token caching, in the hope that the external token cache is partitioned.
public virtual IReadOnlyList<MsalAccountCacheItem> GetAllAccounts(string partitionKey = null)
public virtual List<MsalAccountCacheItem> GetAllAccounts(string partitionKey = null)
{
if (string.IsNullOrEmpty(partitionKey))
{
Expand All @@ -260,11 +260,11 @@ public virtual IReadOnlyList<MsalAccountCacheItem> GetAllAccounts(string partiti
else
{
AccountCacheDictionary.TryGetValue(partitionKey, out ConcurrentDictionary<string, MsalAccountCacheItem> partition);
return partition?.Select(kv => kv.Value)?.ToList() ?? CollectionHelpers.GetEmptyReadOnlyList<MsalAccountCacheItem>();
return partition?.Select(kv => kv.Value)?.ToList() ?? CollectionHelpers.GetEmptyList<MsalAccountCacheItem>();
}
}

public virtual IReadOnlyList<MsalAppMetadataCacheItem> GetAllAppMetadata()
public virtual List<MsalAppMetadataCacheItem> GetAllAppMetadata()
{
return AppMetadataDictionary.Select(kv => kv.Value).ToList();
}
Expand Down