private void AssertAccessorsAreEqual(ITokenCacheAccessor expected, ITokenCacheAccessor actual)
 {
     Assert.AreEqual(expected.GetAllAccessTokens().Count(), actual.GetAllAccessTokens().Count());
     Assert.AreEqual(expected.GetAllRefreshTokens().Count(), actual.GetAllRefreshTokens().Count());
     Assert.AreEqual(expected.GetAllIdTokens().Count(), actual.GetAllIdTokens().Count());
     Assert.AreEqual(expected.GetAllAccounts().Count(), actual.GetAllAccounts().Count());
 }
 public static void ClearRefreshTokens(this ITokenCacheAccessor accessor)
 {
     foreach (var item in accessor.GetAllRefreshTokens())
     {
         accessor.DeleteRefreshToken(item.GetKey());
     }
 }
Example #3
0
        private IEnumerable <MsalRefreshTokenCacheItem> GetAllRefreshTokensWithNoLocks(bool filterByClientId)
        {
            var refreshTokens = _accessor.GetAllRefreshTokens();

            return(filterByClientId
                ? refreshTokens.Where(x => x.ClientId.Equals(ClientId, StringComparison.OrdinalIgnoreCase))
                : refreshTokens);
        }
 internal static void AssertItemCount(
     this ITokenCacheAccessor accessor,
     int expectedAtCount,
     int expectedRtCount,
     int expectedIdtCount,
     int expectedAccountCount,
     int expectedAppMetadataCount = 0)
 {
     Assert.AreEqual(expectedAtCount, accessor.GetAllAccessTokens().Count());
     Assert.AreEqual(expectedRtCount, accessor.GetAllRefreshTokens().Count());
     Assert.AreEqual(expectedIdtCount, accessor.GetAllIdTokens().Count());
     Assert.AreEqual(expectedAccountCount, accessor.GetAllAccounts().Count());
     Assert.AreEqual(expectedAppMetadataCount, accessor.GetAllAppMetadata().Count());
 }
        public byte[] Serialize(IDictionary <string, JToken> unkownNodes)
        {
            var accessTokensAsString  = new List <string>();
            var refreshTokensAsString = new List <string>();
            var idTokensAsString      = new List <string>();
            var accountsAsString      = new List <string>();

            foreach (var accessToken in _accessor.GetAllAccessTokens())
            {
                accessTokensAsString.Add(accessToken.ToJsonString());
            }

            foreach (var refreshToken in _accessor.GetAllRefreshTokens())
            {
                refreshTokensAsString.Add(refreshToken.ToJsonString());
            }

            foreach (var idToken in _accessor.GetAllIdTokens())
            {
                idTokensAsString.Add(idToken.ToJsonString());
            }

            foreach (var account in _accessor.GetAllAccounts())
            {
                accountsAsString.Add(account.ToJsonString());
            }

            // reads the underlying in-memory dictionary and dumps out the content as a JSON
            var cacheDict = new Dictionary <string, IEnumerable <string> >
            {
                [AccessTokenKey]  = accessTokensAsString,
                [RefreshTokenKey] = refreshTokensAsString,
                [IdTokenKey]      = idTokensAsString,
                [AccountKey]      = accountsAsString
            };

            // Serializes as an array of Key Value pairs
            return(JsonHelper.SerializeToJson(cacheDict.ToList())
                   .ToByteArray());
        }
        public byte[] Serialize(IDictionary <string, JToken> unkownNodes)
        {
            var cache = new CacheSerializationContract(unkownNodes);

            foreach (var token in _accessor.GetAllAccessTokens())
            {
                cache.AccessTokens[token.GetKey()
                                   .ToString()] = token;
            }

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

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

            foreach (var accountItem in _accessor.GetAllAccounts())
            {
                cache.Accounts[accountItem.GetKey()
                               .ToString()] = accountItem;
            }

            foreach (var appMetadata in _accessor.GetAllAppMetadata())
            {
                cache.AppMetadata[appMetadata.GetKey()
                                  .ToString()] = appMetadata;
            }

            return(cache.ToJsonString()
                   .ToByteArray());
        }