internal TokenCache(IServiceBundle serviceBundle, bool isApplicationTokenCache, ICacheSerializationProvider optionalDefaultSerializer = null)
        {
            if (serviceBundle == null)
            {
                throw new ArgumentNullException(nameof(serviceBundle));
            }

            // useRealSemaphore= false for MyApps and potentially for all apps when using non-singleton MSAL
            _semaphoreSlim = new OptionalSemaphoreSlim(useRealSemaphore: serviceBundle.Config.CacheSynchronizationEnabled);

            var proxy = serviceBundle?.PlatformProxy ?? PlatformProxyFactory.CreatePlatformProxy(null);

            _accessor     = proxy.CreateTokenCacheAccessor();
            _featureFlags = proxy.GetFeatureFlags();

            _usesDefaultSerialization = optionalDefaultSerializer != null;
            optionalDefaultSerializer?.Initialize(this);

            LegacyCachePersistence = proxy.CreateLegacyCachePersistence();

#if iOS
            SetIosKeychainSecurityGroup(serviceBundle.Config.IosKeychainSecurityGroup);
#endif // iOS

            IsAppTokenCache = isApplicationTokenCache;

            // Must happen last, this code can access things like _accessor and such above.
            ServiceBundle = serviceBundle;
        }
Exemplo n.º 2
0
        public async Task AcquireTokenForClient_DoesNotFireNotifications_WhenTokenCacheIsNotSerialized_Async()
        {
            // Arrange
            var appTokenCache = Substitute.For <ITokenCacheInternal>();
            var semaphore     = new OptionalSemaphoreSlim(true);

            appTokenCache.Semaphore.Returns(semaphore);

            appTokenCache.FindAccessTokenAsync(default).ReturnsForAnyArgs(TokenCacheHelper.CreateAccessTokenItem());
Exemplo n.º 3
0
        public async Task GetAccounts_DoesNotFireNotifications_WhenTokenCacheIsNotSerialized_Async()
        {
            // Arrange
            var userTokenCacheInternal = Substitute.For <ITokenCacheInternal>();
            var semaphore = new OptionalSemaphoreSlim(true);

            userTokenCacheInternal.Semaphore.Returns(semaphore);

            var cca = ConfidentialClientApplicationBuilder
                      .Create(TestConstants.ClientId)
                      .WithAuthority(new Uri(TestConstants.AuthorityTestTenant))
                      .WithRedirectUri(TestConstants.RedirectUri)
                      .WithClientSecret(TestConstants.ClientSecret)
                      .WithUserTokenCacheInternalForTest(userTokenCacheInternal)
                      .BuildConcrete();

            userTokenCacheInternal.IsTokenCacheSerialized().Returns(false);

            // Act
            await cca.GetAccountsAsync().ConfigureAwait(false);

            // Assert
            await cca.UserTokenCacheInternal.DidNotReceiveWithAnyArgs().OnBeforeAccessAsync(Arg.Any <TokenCacheNotificationArgs>()).ConfigureAwait(true);

            await cca.UserTokenCacheInternal.DidNotReceiveWithAnyArgs().OnBeforeWriteAsync(Arg.Any <TokenCacheNotificationArgs>()).ConfigureAwait(true);

            await cca.UserTokenCacheInternal.DidNotReceiveWithAnyArgs().OnAfterAccessAsync(Arg.Any <TokenCacheNotificationArgs>()).ConfigureAwait(true);


            // Arrange
            userTokenCacheInternal.IsTokenCacheSerialized().Returns(true);

            // Act
            await cca.GetAccountsAsync().ConfigureAwait(true);

            // Assert
            await cca.UserTokenCacheInternal.Received().OnBeforeAccessAsync(Arg.Any <TokenCacheNotificationArgs>()).ConfigureAwait(true);

            await cca.UserTokenCacheInternal.DidNotReceiveWithAnyArgs().OnBeforeWriteAsync(Arg.Any <TokenCacheNotificationArgs>()).ConfigureAwait(true);

            await cca.UserTokenCacheInternal.Received().OnAfterAccessAsync(Arg.Any <TokenCacheNotificationArgs>()).ConfigureAwait(true);
        }