public async Task TestStoreAsync()
        {
            try
            {
                var subjectSeed = Guid.NewGuid().ToString();
                var th          = MakeTokenHandle(subjectSeed, 0);
                TokenHandleRecord tokenHandleRecord = new TokenHandleRecord(th);
                Guid id = tokenHandleRecord.Id;

                var key    = th.Key;
                var result = await _tokenHandleStore.GetAsync(key);

                Assert.IsNull(result);



                Token token = await th.MakeIdentityServerTokenAsync(_clientStore);

                await _tokenHandleStore.StoreAsync(key, token);

                result = await _tokenHandleStore.GetAsync(key);

                tokenHandleRecord = new TokenHandleRecord(new TokenHandle(key, result));

                Assert.AreEqual(tokenHandleRecord.Id, id);
            }
            catch (Exception e)
            {
            }
        }
        public async Task TestCreateTokenHandleAsync()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            int i      = 0;
            var claims = new List <Claim>()
            {
                new Claim("Type 0:" + i, "Value 0:" + i),
                new Claim("Type 1:" + i, "Value 1:" + i)
            };
            var json = JsonConvert.SerializeObject(claims);

            var insert = await CassandraTestHelper.InsertTestData_Clients(1);

            var flat = new FlattenedTokenHandle
            {
                Key          = Guid.NewGuid().ToString(),
                Audience     = "Audience:" + i,
                Claims       = JsonConvert.SerializeObject(claims),
                ClientId     = insert[0].ClientId,
                CreationTime = DateTimeOffset.UtcNow,
                Expires      = DateTimeOffset.UtcNow,
                Issuer       = "Issuer:" + i,
                Lifetime     = 1,
                SubjectId    = "SubjectId:" + i,
                Type         = "Type:" + i,
                Version      = 1
            };
            IClientStore      cs  = new ClientStore();
            ITokenHandleStore ths = new TokenHandleStore();
            var result            = await dao.CreateTokenHandleAsync(flat);

            var result_of_find = await dao.FindTokenByKey(flat.Key, cs);

            Token tt = result_of_find;

            Assert.AreEqual(flat.ClientId, result_of_find.ClientId);

            var newKey = Guid.NewGuid().ToString();
            await ths.StoreAsync(newKey, tt);

            result_of_find = await ths.GetAsync(newKey);

            Assert.AreEqual(flat.ClientId, result_of_find.ClientId);

            await ths.RemoveAsync(newKey);

            result_of_find = await ths.GetAsync(newKey);

            Assert.AreEqual(result_of_find, null);
        }
Exemplo n.º 3
0
        public async Task StoreAsync()
        {
            //Arrange
            var sut = new TokenHandleStore(NhibernateSession, ScopeStoreMock.Object, ClientStoreMock.Object);

            var testKey  = Guid.NewGuid().ToString();
            var testCode = ObjectCreator.GetTokenHandle();

            //Act
            await sut.StoreAsync(testKey, testCode);

            ExecuteInTransaction(session =>
            {
                //Assert
                var token = session.Query <Token>()
                            .SingleOrDefault(t => t.TokenType == TokenType.TokenHandle &&
                                             t.Key == testKey);

                Assert.NotNull(token);

                //CleanUp
                session.Delete(token);
            });
        }
        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);
        }
        public void StoreAsync_WhenCalled_ExpectAction()
        {
            // Arrange
            var mockCacheManager = new Mock<ICacheManager<Token>>();
            var mockCacheConfiguration = new Mock<IConfiguration<RedisCacheConfigurationEntity>>();

            mockCacheConfiguration.Setup(r => r.Get).Returns(new RedisCacheConfigurationEntity { CacheDuration = 1 });

            var keyCallback = default(string);
            var timespanCallback = default(TimeSpan);
            mockCacheManager.Setup(r => r.SetAsync(It.IsAny<string>(), It.IsAny<Token>(), It.IsAny<TimeSpan>()))
                .Callback((string s, Token a, TimeSpan t) =>
                {
                    keyCallback = s;
                    timespanCallback = t;
                })
                .Returns(Task.FromResult(0));

            var tokenHandleStore = new TokenHandleStore(
                mockCacheManager.Object,
                mockCacheConfiguration.Object);

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

            tokenHandleStore.StoreAsync("string", new Token { Lifetime = 100 }).Wait();

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(keyCallback, Is.EqualTo("THS_string"));
            Assert.That(timespanCallback, Is.EqualTo(TimeSpan.FromSeconds(95)));
            mockCacheManager.Verify(r => r.SetAsync(It.IsAny<string>(), It.IsAny<Token>(), It.IsAny<TimeSpan>()), Times.Once());
        }