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.");
        }
 internal static void Logout(IAuthContext authConfig)
 {
     lock (FileLock)
     {
         TokenCacheStorage.DeleteToken(authConfig.ClientId);
     }
 }
        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 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);
        }
Example #5
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();
                    }
                }
            });
        }
Example #6
0
        public void ShouldReturnNoContentWhenProccessCacheIsEmpty()
        {
            // Act
            byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);

            // Assert
            Assert.Empty(storedBuffer);
        }
        public void ShouldReturnNoContentWhenPlatformCacheIsEmpty()
        {
            // Arrange
            CleanTokenCache(_testAppContext1);

            // Act
            byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);

            // Assert
            Assert.Empty(storedBuffer);
        }
Example #8
0
 internal static void Logout(IAuthContext authConfig)
 {
     try
     {
         _cacheLock.EnterWriteLock();
         TokenCacheStorage.DeleteToken(authConfig);
     }
     finally
     {
         _cacheLock.ExitWriteLock();
     }
 }
Example #9
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);
        }
Example #10
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);
        }
Example #12
0
 /// <summary>
 /// Signs out of the current session using the provided <see cref="IAuthContext"/>.
 /// </summary>
 /// <param name="authContext">The <see cref="IAuthContext"/> to sign-out from.</param>
 internal static void Logout(IAuthContext authContext)
 {
     try
     {
         _cacheLock.EnterWriteLock();
         if (authContext.AuthType == AuthenticationType.UserProvidedAccessToken)
         {
             GraphSession.Instance.UserProvidedToken = null;
         }
         else
         {
             TokenCacheStorage.DeleteToken(authContext);
         }
     }
     finally
     {
         _cacheLock.ExitWriteLock();
     }
 }
        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());
                    }
                }
            });
        }
Example #14
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.");
        }
Example #15
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));
        }
 private void CleanTokenCache(string appId)
 {
     TokenCacheStorage.DeleteToken(appId);
 }
 private void CleanTokenCache(IAuthContext authContext)
 {
     TokenCacheStorage.DeleteToken(authContext);
 }
Example #18
0
 private void CleanTokenCache(IAuthContext authContext)
 {
     TokenCacheStorage.DeleteToken(authContext);
     GraphSession.Reset();
 }