public async Task RevokeAsync() { //Arrange var sut = new TokenHandleStore(NhibernateSession, ScopeStoreMock.Object, ClientStoreMock.Object); var subjectIdToRevoke = Guid.NewGuid().ToString(); var clientIdToRevoke = Guid.NewGuid().ToString(); var testKey = Guid.NewGuid().ToString(); var testCode = ObjectCreator.GetTokenHandle(); var tokenHandle = new Token { Key = testKey, SubjectId = testCode.SubjectId, ClientId = testCode.ClientId, JsonCode = ConvertToJson(testCode), Expiry = DateTime.UtcNow.AddSeconds(testCode.Client.AuthorizationCodeLifetime), TokenType = TokenType.TokenHandle }; var testKeyToRevoke = Guid.NewGuid().ToString(); var testCodeToRevoke = ObjectCreator.GetTokenHandle(subjectIdToRevoke, clientIdToRevoke); var tokenHandleToRevoke = new Token { Key = testKeyToRevoke, SubjectId = testCodeToRevoke.SubjectId, ClientId = testCodeToRevoke.ClientId, JsonCode = ConvertToJson(testCodeToRevoke), Expiry = DateTime.UtcNow.AddSeconds(testCodeToRevoke.Client.AuthorizationCodeLifetime), TokenType = TokenType.TokenHandle }; ExecuteInTransaction(session => { session.Save(tokenHandle); session.Save(tokenHandleToRevoke); }); //Act await sut.RevokeAsync(subjectIdToRevoke, clientIdToRevoke); ExecuteInTransaction(session => { //Assert var tokenRevoked = session.Query <Token>() .SingleOrDefault(t => t.TokenType == TokenType.TokenHandle && t.Key == testKeyToRevoke); var tokenNotRevoked = session.Query <Token>() .SingleOrDefault(t => t.TokenType == TokenType.TokenHandle && t.Key == testKey); Assert.Null(tokenRevoked); Assert.NotNull(tokenNotRevoked); //CleanUp session.Delete(tokenNotRevoked); }); }
public void GetAsync_WhenCalled_ExpectResponse() { // 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); mockCacheManager.Setup(r => r.GetAsync(It.IsAny<string>())) .Callback((string s) => keyCallback = s) .ReturnsAsync(new Token()); var tokenHandleStore = new TokenHandleStore( mockCacheManager.Object, mockCacheConfiguration.Object); // Act var stopwatch = Stopwatch.StartNew(); var authorizationCode = tokenHandleStore.GetAsync("string").Result; stopwatch.Stop(); // Assert this.WriteTimeElapsed(stopwatch); Assert.That(authorizationCode, Is.Not.Null); Assert.That(keyCallback, Is.EqualTo("THS_string")); }
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 Setup() { base.Setup(); _clientStore = new ClientStore(StoreSettings.UsingFolder(TargetFolder)); _tokenHandleStore = new TokenHandleStore(StoreSettings.UsingFolder(TargetFolder)); _scopeStore = new ScopeStore(StoreSettings.UsingFolder(TargetFolder)); }
public async Task TestGetAllAsync() { var insert = await CassandraTestHelper.InsertTestData_Tokens(10); ITokenHandleStore ths = new TokenHandleStore(); var find_metadata = await ths.GetAllAsync(insert[0].SubjectId); Assert.AreEqual(find_metadata.Count(), insert.Count); }
static List <AuthorizationCodeHandleRecord> InsertTestData(ClientStore clientStore, ScopeStore scopeStore, AuthorizationCodeStore authorizationCodeStore, TokenHandleStore ths, int count = 1) { var tokenInsert = TokenHandleStoreTest.InsertTestData(clientStore, scopeStore, ths, 10); var clientId = tokenInsert[0].Record.ClientId; string subjectSeed = Guid.NewGuid().ToString(); List <AuthorizationCodeHandleRecord> result = new List <AuthorizationCodeHandleRecord>(); int i = 0; foreach (var tokenRecord in tokenInsert) { var client = clientStore.FindClientByIdAsync(tokenRecord.Record.ClientId); AuthorizationCodeHandle handle = new AuthorizationCodeHandle { ClientId = tokenRecord.Record.ClientId, SubjectId = tokenRecord.Record.SubjectId, Expires = DateTimeOffset.UtcNow.AddMinutes(5), CreationTime = DateTimeOffset.UtcNow, IsOpenId = true, RedirectUri = "REDIRECTURI/" + i, WasConsentShown = true, Nonce = "NONCE:" + i, ClaimIdentityRecords = new List <ClaimIdentityRecord>() { new ClaimIdentityRecord() { AuthenticationType = Constants.PrimaryAuthenticationType, ClaimTypeRecords = new List <ClaimTypeRecord>() { new ClaimTypeRecord() { Type = Constants.ClaimTypes.Subject, Value = tokenRecord.Record.SubjectId, ValueType = "VALUETYPE:" + i } } } }, RequestedScopes = client.Result.AllowedScopes, Key = Guid.NewGuid().ToString(), }; var handleRecord = new AuthorizationCodeHandleRecord(handle); authorizationCodeStore.CreateAsync(handleRecord.Record); result.Add(handleRecord); ++i; } return(result); }
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); }
public async Task TestRemoveAsync() { var insert = await CassandraTestHelper.InsertTestData_Tokens(1); ITokenHandleStore ths = new TokenHandleStore(); var subjectId = insert[0].SubjectId; var clientId = insert[0].ClientId; var key = insert[0].Key; var find_metadata = await ths.GetAllAsync(subjectId); Assert.AreEqual(find_metadata.Count(), insert.Count); await ths.RemoveAsync(key); find_metadata = await ths.GetAllAsync(subjectId); Assert.AreEqual(find_metadata.Count(), 0); }
public void GetAllAsync_WhenCalled_ExpectThrows() { // Arrange var mockCacheManager = new Mock<ICacheManager<Token>>(); var mockCacheConfiguration = new Mock<IConfiguration<RedisCacheConfigurationEntity>>(); mockCacheConfiguration.Setup(r => r.Get).Returns(new RedisCacheConfigurationEntity { CacheDuration = 1 }); var tokenHandleStore = new TokenHandleStore( mockCacheManager.Object, mockCacheConfiguration.Object); // Act and Assert var stopwatch = Stopwatch.StartNew(); Assert.Throws<NotImplementedException>( () => tokenHandleStore.GetAllAsync("string").GetAwaiter().GetResult()); stopwatch.Stop(); this.WriteTimeElapsed(stopwatch); }
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 async Task GetAsync() { //Arrange var sut = new TokenHandleStore(NhibernateSession, ScopeStoreMock.Object, ClientStoreMock.Object); var testKey = Guid.NewGuid().ToString(); var testCode = ObjectCreator.GetTokenHandle(); var tokenHandle = new Token { Key = testKey, SubjectId = testCode.SubjectId, ClientId = testCode.ClientId, JsonCode = ConvertToJson(testCode), Expiry = DateTime.UtcNow.AddSeconds(testCode.Client.AuthorizationCodeLifetime), TokenType = TokenType.TokenHandle }; SetupScopeStoreMock(); ExecuteInTransaction(session => { session.Save(tokenHandle); }); //Act var token = await sut.GetAsync(testKey); //Assert Assert.NotNull(token); //CleanUp ExecuteInTransaction(session => { session.Delete(tokenHandle); }); }
public async Task GetAllAsync() { //Arrange var sut = new TokenHandleStore(NhibernateSession, ScopeStoreMock.Object, ClientStoreMock.Object); var subjectId1 = Guid.NewGuid().ToString(); var subjectId2 = Guid.NewGuid().ToString(); var testKey1 = Guid.NewGuid().ToString(); var testCode1 = ObjectCreator.GetTokenHandle(subjectId1); var tokenHandle1 = new Token { Key = testKey1, SubjectId = testCode1.SubjectId, ClientId = testCode1.ClientId, JsonCode = ConvertToJson(testCode1), Expiry = DateTime.UtcNow.AddSeconds(testCode1.Client.AuthorizationCodeLifetime), TokenType = TokenType.TokenHandle }; var testKey2 = Guid.NewGuid().ToString(); var testCode2 = ObjectCreator.GetTokenHandle(subjectId1); var tokenHandle2 = new Token { Key = testKey2, SubjectId = testCode2.SubjectId, ClientId = testCode2.ClientId, JsonCode = ConvertToJson(testCode2), Expiry = DateTime.UtcNow.AddSeconds(testCode2.Client.AuthorizationCodeLifetime), TokenType = TokenType.TokenHandle }; var testKey3 = Guid.NewGuid().ToString(); var testCode3 = ObjectCreator.GetTokenHandle(subjectId2); var tokenHandle3 = new Token { Key = testKey3, SubjectId = testCode3.SubjectId, ClientId = testCode3.ClientId, JsonCode = ConvertToJson(testCode3), Expiry = DateTime.UtcNow.AddSeconds(testCode3.Client.AuthorizationCodeLifetime), TokenType = TokenType.TokenHandle }; var testKey4 = Guid.NewGuid().ToString(); var testCode4 = ObjectCreator.GetTokenHandle(subjectId2); var tokenHandle4 = new Token { Key = testKey4, SubjectId = testCode4.SubjectId, ClientId = testCode4.ClientId, JsonCode = ConvertToJson(testCode4), Expiry = DateTime.UtcNow.AddSeconds(testCode4.Client.AuthorizationCodeLifetime), TokenType = TokenType.TokenHandle }; SetupScopeStoreMock(); ExecuteInTransaction(session => { session.SaveOrUpdate(tokenHandle1); session.SaveOrUpdate(tokenHandle2); session.SaveOrUpdate(tokenHandle3); session.SaveOrUpdate(tokenHandle4); }); //Act var tokens = (await sut.GetAllAsync(subjectId1)).ToList(); //Assert Assert.True(tokens.Count == 2); Assert.True(tokens.All(t => t.SubjectId == subjectId1)); //CleanUp ExecuteInTransaction(session => { session.Delete(tokenHandle1); session.Delete(tokenHandle2); session.Delete(tokenHandle3); session.Delete(tokenHandle4); }); }
public static List <TokenHandleRecord> InsertTestData(ClientStore clientStore, ScopeStore scopeStore, TokenHandleStore store, int count = 1) { var subjectSeed = Guid.NewGuid().ToString(); var clientInsert = ClientStoreTest.InsertTestData(clientStore, scopeStore, 1); List <TokenHandleRecord> result = new List <TokenHandleRecord>(); for (int i = 0; i < count; ++i) { var tokenHandle = MakeTokenHandle(subjectSeed, i); tokenHandle.ClientId = clientInsert[0].Record.ClientId; var tokenHandleRecord = new TokenHandleRecord(tokenHandle); store.CreateAsync(tokenHandleRecord.Record); result.Add(tokenHandleRecord); } return(result); }
public static List <RefreshTokenHandleRecord> InsertTestData(ClientStore clientStore, ScopeStore scopeStore, TokenHandleStore ths, RefreshTokenStore store, int count = 1) { List <RefreshTokenHandleRecord> result = new List <RefreshTokenHandleRecord>(); var insert = TokenHandleStoreTest.InsertTestData(clientStore, scopeStore, ths, count); var subjectSeed = Guid.NewGuid().ToString(); var clientId = insert[0].Record.ClientId; foreach (var item in insert) { RefreshTokenHandle tokenHandle = new RefreshTokenHandle { ClientId = clientId, AccessToken = item.Record, CreationTime = DateTimeOffset.UtcNow, Key = Guid.NewGuid().ToString(), Expires = DateTimeOffset.UtcNow.AddMinutes(5), LifeTime = 5, SubjectId = item.Record.SubjectId, Version = 1 }; var tokenHandleRecord = new RefreshTokenHandleRecord(tokenHandle); store.CreateAsync(tokenHandleRecord.Record); result.Add(tokenHandleRecord); } return(result); }
public void RemoveAsync_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); mockCacheManager.Setup(r => r.DeleteAsync(It.IsAny<string>())) .Callback((string s) => keyCallback = s) .Returns(Task.FromResult(0)); var tokenHandleStore = new TokenHandleStore( mockCacheManager.Object, mockCacheConfiguration.Object); // Act var stopwatch = Stopwatch.StartNew(); tokenHandleStore.RemoveAsync("string").Wait(); stopwatch.Stop(); // Assert this.WriteTimeElapsed(stopwatch); Assert.That(keyCallback, Is.EqualTo("THS_string")); mockCacheManager.Verify(r => r.DeleteAsync(It.IsAny<string>()), Times.Once()); }
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()); }
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<Token>( RedisHelpers.ConnectionMultiplexer, mockCacheConfiguration.Object, jsonSettingsFactory.Create()); var tokenStore = new TokenHandleStore( cacheManager, mockCacheConfiguration.Object); // Act var stopwatch = Stopwatch.StartNew(); tokenStore.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 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; }