Ejemplo n.º 1
0
        private ITokenCacheAccessor CreateTokenCacheAccessorWithKeyPrefix(
            string keyPrefix,
            int numAccessTokens,
            int numRefreshTokens,
            int numIdTokens,
            int numAccounts)
        {
            var accessor = new InMemoryPartitionedUserTokenCacheAccessor(Substitute.For <ICoreLogger>(), null);

            for (int i = 1; i <= numAccessTokens; i++)
            {
                var item = CreateAccessTokenItem(
                    cachedAt: DateTimeHelpers.UnixTimestampToDateTime(34567),
                    expiresOn: DateTimeHelpers.UnixTimestampToDateTime(12345),
                    extendedExpiresOn: DateTimeHelpers.UnixTimestampToDateTime(23456));

                item.Environment = item.Environment + $"_{keyPrefix}{i}"; // ensure we get unique cache keys
                accessor.SaveAccessToken(item);
            }

            for (int i = 1; i <= numRefreshTokens; i++)
            {
                var item = CreateRefreshTokenItem();
                item.Environment = item.Environment + $"_{keyPrefix}{i}"; // ensure we get unique cache keys
                accessor.SaveRefreshToken(item);
            }

            // Create an FRT
            var frt = CreateRefreshTokenItem(true);

            frt.FamilyId = "1";
            accessor.SaveRefreshToken(frt);

            for (int i = 1; i <= numIdTokens; i++)
            {
                var item = CreateIdTokenItem();
                item.Environment = item.Environment + $"_{keyPrefix}{i}"; // ensure we get unique cache keys
                accessor.SaveIdToken(item);
            }

            for (int i = 1; i <= numAccounts; i++)
            {
                var item = CreateAccountItem();
                item.Environment = item.Environment + $"_{keyPrefix}{i}"; // ensure we get unique cache keys
                accessor.SaveAccount(item);
            }

            accessor.SaveAppMetadata(new MsalAppMetadataCacheItem(TestConstants.ClientId, "env_1", "1"));
            accessor.SaveAppMetadata(new MsalAppMetadataCacheItem(TestConstants.ClientId, "env_2", ""));
            accessor.SaveAppMetadata(new MsalAppMetadataCacheItem(TestConstants.ClientId2, "env_1", "another_family"));

            return(accessor);
        }
Ejemplo n.º 2
0
        public void TestDictionarySerialization()
        {
            var accessor = CreateTokenCacheAccessor();

            var s1 = new TokenCacheDictionarySerializer(accessor);

            byte[] bytes = s1.Serialize(null);
            string json  = new UTF8Encoding().GetString(bytes);

            var otherAccessor = new InMemoryPartitionedUserTokenCacheAccessor(Substitute.For <ICoreLogger>(), null);
            var s2            = new TokenCacheDictionarySerializer(otherAccessor);

            s2.Deserialize(bytes, false);

            AssertAccessorsAreEqual(accessor, otherAccessor);
        }
        public void GetAccount_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");

            // Assert: Null non-existing item
            Assert.IsNull(accessor.GetAccount(acc1.GetKey()));

            accessor.SaveAccount(acc1);
            accessor.SaveAccount(acc2);
            accessor.SaveAccount(acc3);

            // Assert: Get token by key
            Assert.AreEqual(acc2.GetKey(), accessor.GetAccount(acc2.GetKey()).GetKey());
            Assert.AreEqual(acc3.GetKey(), accessor.GetAccount(acc3.GetKey()).GetKey());
        }
        public void GetIdToken_Test()
        {
            var accessor = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null);
            var idt1     = TokenCacheHelper.CreateIdTokenCacheItem("tenant1", "homeAccountId");
            var idt2     = TokenCacheHelper.CreateIdTokenCacheItem("tenant1", "homeAccountId2");
            var idt3     = TokenCacheHelper.CreateIdTokenCacheItem("tenant2", "homeAccountId");
            var at2      = TokenCacheHelper.CreateAccessTokenItem("scope1", "tenant1", "homeAccountId2");
            var at3      = TokenCacheHelper.CreateAccessTokenItem("scope1", "tenant2", "homeAccountId");

            // Assert: Null non-existing item
            Assert.IsNull(accessor.GetIdToken(at2));

            accessor.SaveIdToken(idt1);
            accessor.SaveIdToken(idt2);
            accessor.SaveIdToken(idt3);

            // Assert: Get token by key
            Assert.AreEqual(idt2.GetKey(), accessor.GetIdToken(at2).GetKey());
            Assert.AreEqual(idt3.GetKey(), accessor.GetIdToken(at3).GetKey());
        }
        public void ClearCache_UserCache_Test()
        {
            var accessor = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null);

            accessor.SaveAccessToken(TokenCacheHelper.CreateAccessTokenItem());
            accessor.SaveRefreshToken(TokenCacheHelper.CreateRefreshTokenItem());
            accessor.SaveIdToken(TokenCacheHelper.CreateIdTokenCacheItem());
            accessor.SaveAccount(TokenCacheHelper.CreateAccountItem());

            Assert.AreEqual(1, accessor.AccessTokenCacheDictionary.Count);
            Assert.AreEqual(1, accessor.RefreshTokenCacheDictionary.Count);
            Assert.AreEqual(1, accessor.IdTokenCacheDictionary.Count);
            Assert.AreEqual(1, accessor.AccountCacheDictionary.Count);

            accessor.Clear();

            Assert.AreEqual(0, accessor.AccessTokenCacheDictionary.Count);
            Assert.AreEqual(0, accessor.RefreshTokenCacheDictionary.Count);
            Assert.AreEqual(0, accessor.IdTokenCacheDictionary.Count);
            Assert.AreEqual(0, accessor.AccountCacheDictionary.Count);
        }
        public void DeleteAccount_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");

            // Assert: Delete on empty collection doesn't throw
            accessor.DeleteAccount(acc1);

            accessor.SaveAccount(acc1);
            accessor.SaveAccount(acc2);
            accessor.SaveAccount(acc3);

            Assert.AreEqual(3, accessor.GetAllAccounts().Count);

            // Assert: Delete on existing item
            accessor.DeleteAccount(acc1);

            Assert.AreEqual(2, accessor.GetAllAccounts().Count);
        }
        public void DeleteRefreshToken_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");

            // Assert: Delete on empty collection doesn't throw
            accessor.DeleteRefreshToken(rt1);

            accessor.SaveRefreshToken(rt1);
            accessor.SaveRefreshToken(rt2);
            accessor.SaveRefreshToken(rt3);

            Assert.AreEqual(3, accessor.GetAllRefreshTokens().Count);

            // Assert: Delete on existing item
            accessor.DeleteRefreshToken(rt1);

            Assert.AreEqual(2, accessor.GetAllRefreshTokens().Count);
        }
        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 HasAccessOrRefreshTokens_UserCache_Test()
        {
            var accessor = new InMemoryPartitionedUserTokenCacheAccessor(new NullLogger(), null);

            // Assert: false with empty collection
            Assert.IsFalse(accessor.HasAccessOrRefreshTokens());

            accessor.SaveAccessToken(TokenCacheHelper.CreateAccessTokenItem(isExpired: true));

            // Assert: false with expired access token
            Assert.IsFalse(accessor.HasAccessOrRefreshTokens());

            accessor.SaveAccessToken(TokenCacheHelper.CreateAccessTokenItem());

            // Assert: true with access token
            Assert.IsTrue(accessor.HasAccessOrRefreshTokens());

            accessor.Clear();
            accessor.SaveRefreshToken(TokenCacheHelper.CreateRefreshTokenItem());

            // Assert: true with valid refresh token
            Assert.IsTrue(accessor.HasAccessOrRefreshTokens());
        }
        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()]);
        }