public void TestSizesCompress()
        {
            var code = new SimpleAuthorizationCode
            {
                Client = new SimpleClient
                {
                    ClientId = "cid"
                },
                RequestedScopes = new List<SimpleScope> { new SimpleScope { Description = "this is description", Enabled = true, Name = "Scope", DisplayName = "Display Name" } },
                Subject = new SimpleClaimsPrincipal
                {
                    Identities = new List<SimpleClaimsIdentity> { new SimpleClaimsIdentity { Claims = new List<SimpleClaim>() } }
                },
                CodeChallenge = "CodeChallenge",
                CodeChallengeMethod = "CodeChallengeMethod",
                CreationTime = new DateTimeOffset(new DateTime(2016, 1, 1)),
                IsOpenId = true,
                Nonce = "Nonce",
                RedirectUri = "RedirectUri",
                SessionId = "SessionId",
                WasConsentShown = true
            };

            var jsonSettingsFactory = new JsonSettingsFactory(new CustomMappersConfiguration()).Create();

            var serializeObject = JsonConvert.SerializeObject(code, jsonSettingsFactory);

            Console.WriteLine($"{Encoding.UTF8.GetByteCount(serializeObject.Compress())} bytes");
        }
        public void GetAsync_WhenCalled_ExpectResponse()
        {
            // Arrange
            var mockCacheConfiguration = new Mock<IConfiguration<RedisCacheConfigurationEntity>>();
            mockCacheConfiguration.Setup(r => r.Get).Returns(
                new RedisCacheConfigurationEntity
                {
                    CacheDuration = 10,
                    RedisCacheDefaultPrefix = "DEFAULT",
                    UseObjectCompression = false
                });

            var jsonSettingsFactory = new JsonSettingsFactory(new CustomMappersConfiguration());

            var cacheManager = new RedisCacheManager<Token>(
                RedisHelpers.ConnectionMultiplexer,
                mockCacheConfiguration.Object,
                jsonSettingsFactory.Create());

            var tokenStore = new TokenHandleStore(
                cacheManager,
                mockCacheConfiguration.Object);

            // Act
            var stopwatch = Stopwatch.StartNew();
            var token = tokenStore.GetAsync("Existing").Result;
            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(token, Is.Not.Null);
        }
        public void GetAsync_WhenCalled_ExpectResponse()
        {
            // Arrange
            var mockCacheConfiguration = new Mock<IConfiguration<RedisCacheConfigurationEntity>>();
            mockCacheConfiguration.Setup(r => r.Get).Returns(
                new RedisCacheConfigurationEntity
                {
                    CacheDuration = 10,
                    RedisCacheDefaultPrefix = "DEFAULT",
                    UseObjectCompression = false
                });

            var jsonSettingsFactory = new JsonSettingsFactory(new CustomMappersConfiguration());

            var cacheManager = new RedisCacheManager<AuthorizationCode>(
                RedisHelpers.ConnectionMultiplexer,
                mockCacheConfiguration.Object,
                jsonSettingsFactory.Create());

            var authorizationCodeStore = new AuthorizationCodeStore(
                cacheManager,
                mockCacheConfiguration.Object);

            // Act
            var stopwatch = Stopwatch.StartNew();

            var authorizationCode = authorizationCodeStore.GetAsync("Existing").Result;

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(authorizationCode, Is.Not.Null);

            Assert.That(authorizationCode.Client, Is.Not.Null);
            Assert.That(authorizationCode.ClientId, Is.EqualTo("cid"));
            Assert.That(authorizationCode.CodeChallenge, Is.EqualTo("CodeChallenge"));
            Assert.That(authorizationCode.CodeChallengeMethod, Is.EqualTo("CodeChallengeMethod"));
            Assert.That(authorizationCode.CreationTime, Is.EqualTo(new DateTimeOffset(new DateTime(2016, 1, 1))));
            Assert.That(authorizationCode.IsOpenId, Is.True);
            Assert.That(authorizationCode.Nonce, Is.EqualTo("Nonce"));
            Assert.That(authorizationCode.RedirectUri, Is.EqualTo("RedirectUri"));

            Assert.That(authorizationCode.RequestedScopes, Is.Not.Null);
            Assert.That(authorizationCode.RequestedScopes.Count(), Is.EqualTo(1));

            Assert.That(authorizationCode.Scopes, Is.Not.Null);
            Assert.That(authorizationCode.Scopes.Count(), Is.EqualTo(1));

            Assert.That(authorizationCode.SessionId, Is.EqualTo("SessionId"));
            Assert.That(authorizationCode.WasConsentShown, Is.True);
            Assert.That(authorizationCode.Subject, Is.Not.Null);
        }
        public void RemoveAsync_WhenCalled_ExpectAction()
        {
            // Arrange
            var mockCacheConfiguration = new Mock<IConfiguration<RedisCacheConfigurationEntity>>();
            mockCacheConfiguration.Setup(r => r.Get).Returns(
                new RedisCacheConfigurationEntity
                {
                    CacheDuration = 10,
                    RedisCacheDefaultPrefix = "DEFAULT",
                    UseObjectCompression = false
                });

            var jsonSettingsFactory = new JsonSettingsFactory(new CustomMappersConfiguration());

            var cacheManager = new RedisCacheManager<RefreshToken>(
                RedisHelpers.ConnectionMultiplexer,
                mockCacheConfiguration.Object,
                jsonSettingsFactory.Create());

            var refreshTokenStore = new RefreshTokenStore(
                cacheManager,
                mockCacheConfiguration.Object);

            // Act
            var stopwatch = Stopwatch.StartNew();

            refreshTokenStore.RemoveAsync("Delete").Wait();

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            var redisValue = RedisHelpers.ConnectionMultiplexer.GetDatabase().StringGet("DEFAULT_RTS_Delete");

            Assert.That(redisValue.HasValue, Is.False);
        }
        public void TestFixtureSetup()
        {
            var database = RedisHelpers.ConnectionMultiplexer.GetDatabase();

            var refreshToken = new RefreshToken
            {
                AccessToken = new Token
                {
                    Client = new Client { ClientId = "cid", },
                    Claims = new List<Claim> { new Claim("SubjectId", "sid") }
                },
                CreationTime = new DateTimeOffset(new DateTime(2016, 1, 1)),
                LifeTime = 1600,
                Version = 1,
                Subject = new ClaimsPrincipal()
            };

            var settings = new JsonSettingsFactory(new CustomMappersConfiguration()).Create();

            var serialized = JsonConvert.SerializeObject(refreshToken, settings);

            database.StringSet("DEFAULT_RTS_Existing", serialized);
            database.StringSet("DEFAULT_RTS_Delete", serialized);
        }
        public void StoreAsync_WhenCalled_ExpectAction()
        {
            // Arrange
            var mockCacheConfiguration = new Mock<IConfiguration<RedisCacheConfigurationEntity>>();
            mockCacheConfiguration.Setup(r => r.Get).Returns(
                new RedisCacheConfigurationEntity
                {
                    CacheDuration = 10,
                    RedisCacheDefaultPrefix = "DEFAULT",
                    UseObjectCompression = false
                });

            var jsonSettingsFactory = new JsonSettingsFactory(new CustomMappersConfiguration());

            var cacheManager = new RedisCacheManager<RefreshToken>(
                RedisHelpers.ConnectionMultiplexer,
                mockCacheConfiguration.Object,
                jsonSettingsFactory.Create());

            var refreshTokenStore = new RefreshTokenStore(
                cacheManager,
                mockCacheConfiguration.Object);

            var refreshToken = new RefreshToken
            {
                AccessToken = new Token
                {
                    Client = new Client { ClientId = "cid", },
                    Claims = new List<Claim> { new Claim("SubjectId", "sid") }
                },
                CreationTime = new DateTimeOffset(new DateTime(2016, 1, 1)),
                LifeTime = 1600,
                Version = 1,
            };

            // Act
            var stopwatch = Stopwatch.StartNew();
            refreshTokenStore.StoreAsync("KeyToStore", refreshToken).Wait();
            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            var redisValue = RedisHelpers.ConnectionMultiplexer.GetDatabase().StringGet("DEFAULT_RTS_KeyToStore");

            Assert.That(redisValue.HasValue, Is.True);
            Console.WriteLine(redisValue);
        }
        public void TestFixtureSetup()
        {
            var database = RedisHelpers.ConnectionMultiplexer.GetDatabase();

            var subject =
                new ClaimsPrincipal(
                    new List<ClaimsIdentity>
                    {
                        new ClaimsIdentity(
                            new List<Claim>
                            {
                                new Claim(Constants.ClaimTypes.Subject, "sid")
                            })
                    });

            var code = new AuthorizationCode
            {
                Client = new Client
                {
                    ClientId = "cid"
                },
                RequestedScopes =
                    new List<Scope>
                    {
                        new Scope
                        {
                            Description = "this is description",
                            Enabled = true,
                            Name = "Scope",
                            DisplayName = "Display Name"
                        }
                    },
                Subject = subject,
                CodeChallenge = "CodeChallenge",
                CodeChallengeMethod = "CodeChallengeMethod",
                CreationTime = new DateTimeOffset(new DateTime(2016, 1, 1)),
                IsOpenId = true,
                Nonce = "Nonce",
                RedirectUri = "RedirectUri",
                SessionId = "SessionId",
                WasConsentShown = true
            };

            var settings = new JsonSettingsFactory(new CustomMappersConfiguration()).Create();

            var serialized = JsonConvert.SerializeObject(code, settings);

            database.StringSet("DEFAULT_ACS_Existing", serialized);
            database.StringSet("DEFAULT_ACS_Delete", serialized);
        }
        public void StoreAsync_WhenCalled_ExpectAction()
        {
            // Arrange
            var mockCacheConfiguration = new Mock<IConfiguration<RedisCacheConfigurationEntity>>();
            mockCacheConfiguration.Setup(r => r.Get).Returns(
                new RedisCacheConfigurationEntity
                {
                    CacheDuration = 10,
                    RedisCacheDefaultPrefix = "DEFAULT",
                    UseObjectCompression = false
                });

            var jsonSettingsFactory = new JsonSettingsFactory(new CustomMappersConfiguration());

            var cacheManager = new RedisCacheManager<AuthorizationCode>(
                RedisHelpers.ConnectionMultiplexer,
                mockCacheConfiguration.Object,
                jsonSettingsFactory.Create());

            var authorizationCodeStore = new AuthorizationCodeStore(
                cacheManager,
                mockCacheConfiguration.Object);

            var subject =
                new ClaimsPrincipal(
                    new List<ClaimsIdentity>
                    {
                        new ClaimsIdentity(
                            new List<Claim>
                            {
                                new Claim(Constants.ClaimTypes.Subject, "sid")
                            })
                    });

            var code = new AuthorizationCode
            {
                Client = new Client
                {
                    ClientId = "cid"
                },
                RequestedScopes = new List<Scope> { new Scope { Description = "this is description", Enabled = true, Name = "Scope", DisplayName = "Display Name" } },
                Subject = subject
            };

            // Act
            var stopwatch = Stopwatch.StartNew();
            authorizationCodeStore.StoreAsync("KeyToStore", code).Wait();
            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            var redisValue = RedisHelpers.ConnectionMultiplexer.GetDatabase().StringGet("DEFAULT_ACS_KeyToStore");

            Assert.That(redisValue.HasValue, Is.True);
            Console.WriteLine(redisValue);
        }
        public void SetAsync_WhenClaimObject_ExpectSuccess()
        {
            // arrange
            const string Key = @"SetAsync_WhenClaimObject_ExpectSuccess";

            var claim = new Claim("mytype1", "myvalue2");

            var claims = new List<Claim> { claim, claim, claim };

            var timeSpan = TimeSpan.FromSeconds(10);

            var mockConfiguration = new Mock<IConfiguration<RedisCacheConfigurationEntity>>();
            mockConfiguration.Setup(r => r.Get)
                .Returns(
                    () =>
                        new RedisCacheConfigurationEntity
                        {
                            CacheDuration = 3600,
                            UseObjectCompression = true,
                            RedisCacheDefaultPrefix = @"RedisCacheManagerTests"
                        });

            var jsonSettingsFactory = new JsonSettingsFactory(new CustomMappersConfiguration());

            // act
            var redisCacheManager = new RedisCacheManager<IEnumerable<Claim>>(
                RedisHelpers.ConnectionMultiplexer,
                mockConfiguration.Object,
                jsonSettingsFactory.Create());
            redisCacheManager.SetAsync(Key, claims, timeSpan).Wait();

            var result = redisCacheManager.GetAsync(Key).Result.ToList();

            // assert
            Console.WriteLine(@"Fetched From Cache:{0}", JsonConvert.SerializeObject(result));
            Assert.That(result.GetType(), Is.EqualTo(typeof(List<Claim>)));
        }
        public void TestFixtureSetup()
        {
            var database = RedisHelpers.ConnectionMultiplexer.GetDatabase();

            var claim1 = new SimpleClaim { Type = "Type1", Value = "Value1" };
            var claim2 = new SimpleClaim { Type = "Type2", Value = "Value2" };

            var client = new SimpleClient
            {
                Claims = new List<SimpleClaim> { claim1, claim2 },
                DataBag = new Dictionary<string, object> { { "AppId", 12 } }
            };

            var token = new SimpleToken
            {
                Claims = new List<SimpleClaim> { claim1, claim2 },
                Client = client,
                Type = "Type",
                CreationTime = new DateTimeOffset(new DateTime(2016, 1, 1)),
                Version = 1,
                Issuer = "Issuer",
                Lifetime = 120,
                Audience = "Audience"
            };

            var settings = new JsonSettingsFactory(new CustomMappersConfiguration { ClientMapper = CustomMapperFactory.CreateClientMapper<CustomClient>() }).Create();

            var serialized = JsonConvert.SerializeObject(token, settings);

            database.StringSet("DEFAULT_THS_Existing", serialized);
            database.StringSet("DEFAULT_THS_Delete", serialized);
        }
        public void StoreAsync_WhenCalled_ExpectAction()
        {
            // Arrange
            var mockCacheConfiguration = new Mock<IConfiguration<RedisCacheConfigurationEntity>>();
            mockCacheConfiguration.Setup(r => r.Get).Returns(
                new RedisCacheConfigurationEntity
                {
                    CacheDuration = 10,
                    RedisCacheDefaultPrefix = "DEFAULT",
                    UseObjectCompression = false
                });

            var customMappersConfiguration = new CustomMappersConfiguration
            {
                ClientMapper = CustomMapperFactory.CreateClientMapper<CustomClient>()
            };

            var jsonSettingsFactory = new JsonSettingsFactory(customMappersConfiguration);

            var cacheManager = new RedisCacheManager<Token>(
                RedisHelpers.ConnectionMultiplexer,
                mockCacheConfiguration.Object,
                jsonSettingsFactory.Create());

            var tokenStore = new TokenHandleStore(
                cacheManager,
                mockCacheConfiguration.Object);

            var claim1 = new Claim("Type1", "Value1");
            var claim2 = new Claim("Type2", "Value2");

            var client = new CustomClient
            {
                Claims = new List<Claim> { claim1, claim2 }
            };

            var token = new Token
            {
                Claims = new List<Claim> { claim1, claim2 },
                Client = client,
                Type = "Type",
                CreationTime = new DateTimeOffset(new DateTime(2016, 1, 1)),
                Version = 1,
                Issuer = "Issuer",
                Lifetime = 120,
                Audience = "Audience"
            };

            // Act
            var stopwatch = Stopwatch.StartNew();
            tokenStore.StoreAsync("KeyToStore", token).Wait();
            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            var redisValue = RedisHelpers.ConnectionMultiplexer.GetDatabase().StringGet("DEFAULT_THS_KeyToStore");

            Assert.That(redisValue.HasValue, Is.True);
            Console.WriteLine(redisValue);
        }
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        /// <returns>The <see cref="Caches"/></returns>
        private static Entities.Caches Initialize()
        {
            var jsonSettingsFactory = new JsonSettingsFactory(incomingMappers);

            var settings = jsonSettingsFactory.Create();

            var authorizationCacheManager = new RedisCacheManager<AuthorizationCode>(
                ConnectionMultiplexer,
                Configuration,
                settings);

            var authorizationCodeStore = new AuthorizationCodeStore(
                authorizationCacheManager,
                Configuration);

            var clientCacheManager = new RedisCacheManager<Client>(
                ConnectionMultiplexer,
                Configuration,
                settings);

            var clientCache = new ClientStoreCache(
                clientCacheManager,
                Configuration);

            var refreshTokenCacheManager = new RedisCacheManager<RefreshToken>(
                ConnectionMultiplexer,
                Configuration,
                settings);

            var refreshTokenStore = new RefreshTokenStore(
                refreshTokenCacheManager,
                Configuration);

            var scopeCacheManager = new RedisCacheManager<IEnumerable<Scope>>(
                ConnectionMultiplexer,
                Configuration,
                settings);

            var scopesCache = new ScopeStoreCache(
                scopeCacheManager,
                Configuration);

            var redisHandleCacheManager = new RedisCacheManager<Token>(
                ConnectionMultiplexer,
                Configuration,
                settings);

            var tokenHandleStore = new TokenHandleStore(
                redisHandleCacheManager,
                Configuration);

            var userServiceCacheManager = new RedisCacheManager<IEnumerable<Claim>>(
                ConnectionMultiplexer,
                Configuration,
                settings);

            var userServiceCache = new UserServiceCache(
                userServiceCacheManager,
                Configuration);

            var caches = new Entities.Caches
            {
                AuthorizationCodeStore = authorizationCodeStore,
                ClientCache = clientCache,
                RefreshTokenStore = refreshTokenStore,
                ScopesCache = scopesCache,
                TokenHandleStore = tokenHandleStore,
                UserServiceCache = userServiceCache
            };

            return caches;
        }