Esempio n. 1
0
        /// <summary>
        /// Configures a token cache using the provide <see cref="IAuthContext"/>.
        /// </summary>
        /// <param name="tokenCache">MSAL's token cache to configure.</param>
        /// <param name="authContext">The <see cref="IAuthContext"/> to get configure an token cache for.</param>
        private static void ConfigureTokenCache(ITokenCache tokenCache, IAuthContext authContext)
        {
            tokenCache.SetBeforeAccess((TokenCacheNotificationArgs args) =>
            {
                try
                {
                    _cacheLock.EnterReadLock();
                    args.TokenCache.DeserializeMsalV3(TokenCacheStorage.GetToken(authContext), shouldClearExistingCache: true);
                }
                finally
                {
                    _cacheLock.ExitReadLock();
                }
            });

            tokenCache.SetAfterAccess((TokenCacheNotificationArgs args) =>
            {
                if (args.HasStateChanged)
                {
                    try
                    {
                        _cacheLock.EnterWriteLock();
                        TokenCacheStorage.SetToken(authContext, args.TokenCache.SerializeMsalV3());
                    }
                    finally
                    {
                        _cacheLock.ExitWriteLock();
                    }
                }
            });
        }
        public void ShouldStoreMultipleAppTokensInPlatformCache()
        {
            // Arrange
            string app1StrContent = "random data for app 1.";

            byte[] app1BufferToStore = Encoding.UTF8.GetBytes(app1StrContent);

            IAuthContext testAppContext2 = new AuthContext {
                ClientId = "test_app_id_2", ContextScope = _userContextScope
            };
            string app2StrContent = "random data for app 2 plus more data.";

            byte[] app2BufferToStore = Encoding.UTF8.GetBytes(app2StrContent);

            // Act
            TokenCacheStorage.SetToken(_testAppContext1, app1BufferToStore);
            TokenCacheStorage.SetToken(testAppContext2, app2BufferToStore);

            // Assert
            byte[] app1StoredBuffer = TokenCacheStorage.GetToken(_testAppContext1);
            Assert.Equal(app1BufferToStore.Length, app1StoredBuffer.Length);
            Assert.Equal(app1StrContent, Encoding.UTF8.GetString(app1StoredBuffer));

            byte[] app2StoredBuffer = TokenCacheStorage.GetToken(testAppContext2);
            Assert.Equal(app2BufferToStore.Length, app2StoredBuffer.Length);
            Assert.Equal(app2StrContent, Encoding.UTF8.GetString(app2StoredBuffer));

            // Cleanup
            CleanTokenCache(_testAppContext1);
            CleanTokenCache(testAppContext2);
        }
        public void ShouldMakeParallelCallsToTokenCache()
        {
            // Arrange
            int  executions = 50;
            int  count      = 0;
            bool failed     = false;

            // Act
            Parallel.For(0, executions, (index) => {
                byte[] contentBuffer = Encoding.UTF8.GetBytes(index.ToString());
                var testAuthContext  = new AuthContext {
                    ClientId = index.ToString(), ContextScope = _userContextScope
                };
                TokenCacheStorage.SetToken(testAuthContext, contentBuffer);

                byte[] storedBuffer = TokenCacheStorage.GetToken(testAuthContext);
                if (index.ToString() != Encoding.UTF8.GetString(storedBuffer))
                {
                    failed = true;
                }

                CleanTokenCache(testAuthContext);
                Interlocked.Increment(ref count);
            });

            // Assert
            Assert.Equal(executions, count);
            Assert.False(failed, "Unexpected content found.");
        }
        public void ShouldStoreMultipleAppTokensInPlatformCache()
        {
            // Arrange
            string app1StrContent = "random data for app 1.";

            byte[] app1BufferToStore = Encoding.UTF8.GetBytes(app1StrContent);

            string TestAppId2     = "test_app_id_2";
            string app2StrContent = "random data for app 2 plus more data.";

            byte[] app2BufferToStore = Encoding.UTF8.GetBytes(app2StrContent);

            // Act
            TokenCacheStorage.SetToken(TestAppId1, app1BufferToStore);
            TokenCacheStorage.SetToken(TestAppId2, app2BufferToStore);

            // Assert
            byte[] app1StoredBuffer = TokenCacheStorage.GetToken(TestAppId1);
            Assert.Equal(app1BufferToStore.Length, app1StoredBuffer.Length);
            Assert.Equal(app1StrContent, Encoding.UTF8.GetString(app1StoredBuffer));

            byte[] app2StoredBuffer = TokenCacheStorage.GetToken(TestAppId2);
            Assert.Equal(app2BufferToStore.Length, app2StoredBuffer.Length);
            Assert.Equal(app2StrContent, Encoding.UTF8.GetString(app2StoredBuffer));

            // Cleanup
            CleanTokenCache(TestAppId1);
            CleanTokenCache(TestAppId2);
        }
Esempio n. 5
0
        public void ShouldDeleteProccessCache()
        {
            // Arrange
            string originalStrContent = "random data for app.";

            byte[] originalBuffer = Encoding.UTF8.GetBytes(originalStrContent);
            TokenCacheStorage.SetToken(_testAppContext1, originalBuffer);

            // Act
            TokenCacheStorage.DeleteToken(_testAppContext1);

            // Assert
            byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
            Assert.Empty(storedBuffer);
        }
Esempio n. 6
0
        public void ShouldStoreNewTokenInProccessCache()
        {
            // Arrange
            string strContent = "random data for app.";

            byte[] bufferToStore = Encoding.UTF8.GetBytes(strContent);

            // Act
            TokenCacheStorage.SetToken(_testAppContext1, bufferToStore);

            // Assert
            byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
            Assert.Equal(bufferToStore.Length, storedBuffer.Length);
            Assert.Equal(strContent, Encoding.UTF8.GetString(storedBuffer));
        }
        public void ShouldStoreNewTokenToPlatformCache()
        {
            // Arrange
            string strContent = "random data for app.";

            byte[] bufferToStore = Encoding.UTF8.GetBytes(strContent);

            // Act
            TokenCacheStorage.SetToken(TestAppId1, bufferToStore);

            // Assert
            byte[] storedBuffer = TokenCacheStorage.GetToken(TestAppId1);
            Assert.Equal(bufferToStore.Length, storedBuffer.Length);
            Assert.Equal(strContent, Encoding.UTF8.GetString(storedBuffer));

            // Cleanup
            CleanTokenCache(TestAppId1);
        }
        private static void ConfigureTokenCache(ITokenCache tokenCache, string appId)
        {
            tokenCache.SetBeforeAccess((TokenCacheNotificationArgs args) => {
                lock (FileLock)
                {
                    args.TokenCache.DeserializeMsalV3(TokenCacheStorage.GetToken(appId), shouldClearExistingCache: true);
                }
            });

            tokenCache.SetAfterAccess((TokenCacheNotificationArgs args) => {
                lock (FileLock)
                {
                    if (args.HasStateChanged)
                    {
                        TokenCacheStorage.SetToken(appId, args.TokenCache.SerializeMsalV3());
                    }
                }
            });
        }
Esempio n. 9
0
        public void ProccessTokenCacheShouldBeThreadSafe()
        {
            // Arrange
            int  executions = 50;
            int  count      = 0;
            bool failed     = false;

            Thread[] threads = new Thread[executions];

            // Act
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(() =>
                {
                    byte[] contentBuffer = Encoding.UTF8.GetBytes(i.ToString());
                    TokenCacheStorage.SetToken(_testAppContext1, contentBuffer);
                    Thread.Sleep(2000);

                    byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
                    if (i.ToString() != Encoding.UTF8.GetString(storedBuffer))
                    {
                        failed = true;
                    }

                    Interlocked.Increment(ref count);
                });
            }

            foreach (Thread thread in threads)
            {
                thread.Start();
            }

            foreach (Thread thread in threads)
            {
                thread.Join();
            }

            // Assert
            Assert.Equal(executions, count);
            Assert.False(failed, "Unexpected content found.");
        }
Esempio n. 10
0
        public void ShouldUpdateTokenInProccessCache()
        {
            // Arrange
            string originalStrContent = "random data for app.";

            byte[] originalBuffer = Encoding.UTF8.GetBytes(originalStrContent);
            TokenCacheStorage.SetToken(_testAppContext1, originalBuffer);

            // Act
            string strContentToUpdate = "updated random data for app.";

            byte[] updateBuffer = Encoding.UTF8.GetBytes(strContentToUpdate);
            TokenCacheStorage.SetToken(_testAppContext1, updateBuffer);

            // Assert
            byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
            Assert.NotEqual(originalBuffer.Length, storedBuffer.Length);
            Assert.Equal(updateBuffer.Length, storedBuffer.Length);
            Assert.Equal(strContentToUpdate, Encoding.UTF8.GetString(storedBuffer));
        }