Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,29 @@ public TokenCacheJsonSerializer(ITokenCacheAccessor accessor)
}

public byte[] Serialize(IDictionary<string, JToken> unknownNodes)
{
return Serialize(null, unknownNodes);
}

public byte[] Serialize(string cacheKey, IDictionary<string, JToken> unknownNodes)
{
var cache = new CacheSerializationContract(unknownNodes);
foreach (var token in _accessor.GetAllAccessTokens())
foreach (var token in _accessor.GetAllAccessTokens(cacheKey))
{
cache.AccessTokens[token.CacheKey] = token;
}

foreach (var token in _accessor.GetAllRefreshTokens())
foreach (var token in _accessor.GetAllRefreshTokens(cacheKey))
{
cache.RefreshTokens[token.CacheKey] = token;
}

foreach (var token in _accessor.GetAllIdTokens())
foreach (var token in _accessor.GetAllIdTokens(cacheKey))
{
cache.IdTokens[token.CacheKey] = token;
}

foreach (var accountItem in _accessor.GetAllAccounts())
foreach (var accountItem in _accessor.GetAllAccounts(cacheKey))
{
cache.Accounts[accountItem.CacheKey] = accountItem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Microsoft.Identity.Client
{
internal interface ITokenCacheInternal : ITokenCache, ITokenCacheSerializer
internal interface ITokenCacheInternal : ITokenCache, ITokenCacheSerializer, IPartitionedTokenCacheSerializer
{
OptionalSemaphoreSlim Semaphore { get; }
ILegacyCachePersistence LegacyPersistence { get; }
Expand Down
19 changes: 19 additions & 0 deletions src/client/Microsoft.Identity.Client/ITokenCacheSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,23 @@ public interface ITokenCacheSerializer
[EditorBrowsable(EditorBrowsableState.Never)]
void DeserializeMsalV2(byte[] msalV2State);
}

/// <summary>
/// Similar to <see cref="ITokenCacheSerializer"/> except accepts a cache partition to serialize into.
/// </summary>
public interface IPartitionedTokenCacheSerializer
{
/// <summary>
/// Similar to <see cref="ITokenCacheSerializer.SerializeMsalV3"/> except serializes only the specified partition.
/// </summary>
/// <param name="cacheKey"></param>
byte[] SerializeMsal(string cacheKey);

/// <summary>
/// Similar to <see cref="ITokenCacheSerializer.DeserializeMsalV3(byte[], bool)"/> except doesn't clear the existing cache data.
/// </summary>
/// <param name="msalState"></param>

void DeserializeMsal(byte[] msalState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,19 @@ void ITokenCacheSerializer.DeserializeMsalV3(byte[] msalV3State, bool shouldClea
}
_unknownNodes = new TokenCacheJsonSerializer(Accessor).Deserialize(msalV3State, shouldClearExistingCache);
}

byte[] IPartitionedTokenCacheSerializer.SerializeMsal(string cacheKey)
{
return new TokenCacheJsonSerializer(Accessor).Serialize(cacheKey, _unknownNodes);
}

void IPartitionedTokenCacheSerializer.DeserializeMsal(byte[] msalState)
{
if (msalState == null || msalState.Length == 0)
{
return;
}
_unknownNodes = new TokenCacheJsonSerializer(Accessor).Deserialize(msalState, false);
}
}
}