public async Task StoreDeviceAuthorizationAsync_should_check_parameters() { using var store = new RavenDbTestDriverWrapper().GetDocumentStore(); var serializer = new PersistentGrantSerializer(); var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); var sut = new DeviceFlowStore(new ScopedAsynDocumentcSession(store.OpenAsyncSession()), serializer, loggerMock.Object); await Assert.ThrowsAsync <ArgumentNullException>(() => sut.StoreDeviceAuthorizationAsync(null, null, null)); await Assert.ThrowsAsync <ArgumentNullException>(() => sut.StoreDeviceAuthorizationAsync("code", null, null)); await Assert.ThrowsAsync <ArgumentNullException>(() => sut.StoreDeviceAuthorizationAsync("code", "userCode", null)); }
public async Task StoreDeviceAuthorizationAsync_WhenSuccessful_ExpectDataStored() { var storeHolder = GetOperationalDocumentStoreHolder(); var deviceCode = Guid.NewGuid().ToString(); var userCode = Guid.NewGuid().ToString(); var data = new DeviceCode { ClientId = Guid.NewGuid().ToString(), CreationTime = DateTime.UtcNow, Lifetime = 300 }; var store = new DeviceFlowStore(storeHolder, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, data); using (var session = storeHolder.OpenAsyncSession()) { var foundDeviceFlowCodes = await session.Query <DeviceFlowCode>().FirstOrDefaultAsync(x => x.DeviceCode == deviceCode); foundDeviceFlowCodes.Should().NotBeNull(); var deserializedData = new PersistentGrantSerializer().Deserialize <DeviceCode>(foundDeviceFlowCodes?.Data); deserializedData.CreationTime.Should().BeCloseTo(data.CreationTime); deserializedData.ClientId.Should().Be(data.ClientId); deserializedData.Lifetime.Should().Be(data.Lifetime); } }
public async Task StoreDeviceAuthorizationAsync_WhenSuccessful_ExpectDataStored(DbContextOptions <PersistedGrantDbContext> options) { var deviceCode = Guid.NewGuid().ToString(); var userCode = Guid.NewGuid().ToString(); var data = new DeviceCode { ClientId = Guid.NewGuid().ToString(), CreationTime = DateTime.UtcNow, Lifetime = 300 }; using (var context = new PersistedGrantDbContext(options)) { var store = new DeviceFlowStore(context, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create(), new NoneCancellationTokenProvider()); await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, data); } using (var context = new PersistedGrantDbContext(options)) { var foundDeviceFlowCodes = context.DeviceFlowCodes.FirstOrDefault(x => x.DeviceCode == deviceCode); foundDeviceFlowCodes.Should().NotBeNull(); var deserializedData = new PersistentGrantSerializer().Deserialize <DeviceCode>(foundDeviceFlowCodes?.Data); deserializedData.CreationTime.Should().BeCloseTo(data.CreationTime); deserializedData.ClientId.Should().Be(data.ClientId); deserializedData.Lifetime.Should().Be(data.Lifetime); } }
public async Task Should_Store_DeviceCode_And_UserCode_When_Authorization_Successful(TestDatabase testDb) { var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); var deviceCode = Guid.NewGuid().ToString(); var userCode = Guid.NewGuid().ToString(); var data = new DeviceCode { ClientId = Guid.NewGuid().ToString(), CreationTime = DateTime.UtcNow, Lifetime = 42 }; using (var session = testDb.SessionFactory.OpenSession()) { var store = new DeviceFlowStore(session, new PersistentGrantSerializer(), loggerMock.Object); await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, data); } using (var session = testDb.SessionFactory.OpenSession()) { var foundDeviceFlowCodes = await session.QueryOver <DeviceFlowCodes>() .Where(c => c.DeviceCode == deviceCode) .SingleOrDefaultAsync(); foundDeviceFlowCodes.Should().NotBeNull(); foundDeviceFlowCodes?.DeviceCode.Should().Be(deviceCode); foundDeviceFlowCodes?.ID.Should().Be(userCode); } await CleanupTestDataAsync(testDb); }
public async Task DeviceFlowStore_SaveUpdateGetByCodesTest() { var storageContext = Services.BuildServiceProvider().GetService <DeviceFlowStorageContext>(); Assert.IsNotNull(storageContext); var store = new DeviceFlowStore(storageContext, _persistentGrantSerializer, _logger); Assert.IsNotNull(store); string userCode = Guid.NewGuid().ToString("n"); string deviceCode = Guid.NewGuid().ToString("n"); Console.WriteLine($"userCode: {userCode}"); Console.WriteLine($"deviceCode: {deviceCode}"); DeviceCode deviceCodeData = CreateTestObject(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, deviceCodeData); stopwatch.Stop(); Console.WriteLine($"DeviceFlowStore.StoreDeviceAuthorizationAsync({deviceCode},{userCode}): {stopwatch.ElapsedMilliseconds} ms"); stopwatch.Reset(); stopwatch.Start(); string oldClientId = deviceCodeData.ClientId; string newClientId = Guid.NewGuid().ToString("n"); deviceCodeData.ClientId = newClientId; await store.UpdateByUserCodeAsync(userCode, deviceCodeData); stopwatch.Stop(); Console.WriteLine($"DeviceFlowStore.UpdateByUserCodeAsync({userCode}): {stopwatch.ElapsedMilliseconds} ms"); stopwatch.Reset(); stopwatch.Start(); var findByDeviceCode = await store.FindByDeviceCodeAsync(deviceCode); stopwatch.Stop(); Console.WriteLine($"DeviceFlowStore.FindByDeviceCodeAsync({deviceCode}): {stopwatch.ElapsedMilliseconds} ms"); AssertDeviceCodesEqual(deviceCodeData, findByDeviceCode); Assert.AreEqual <string>(newClientId, findByDeviceCode.ClientId); Assert.AreNotEqual <string>(oldClientId, findByDeviceCode.ClientId); stopwatch.Reset(); stopwatch.Start(); var findByUserCode = await store.FindByUserCodeAsync(userCode); stopwatch.Stop(); Console.WriteLine($"DeviceFlowStore.FindByUserCodeAsync({userCode}): {stopwatch.ElapsedMilliseconds} ms"); AssertDeviceCodesEqual(deviceCodeData, findByUserCode); Assert.AreEqual <string>(newClientId, findByUserCode.ClientId); Assert.AreNotEqual <string>(oldClientId, findByUserCode.ClientId); AssertDeviceCodesEqual(findByDeviceCode, findByUserCode); }
public async Task StoreDeviceAuthorizationAsync_WhenSuccessful_ExpectDataStored() { var deviceCode = Guid.NewGuid().ToString(); var userCode = Guid.NewGuid().ToString(); var data = new DeviceCode { ClientId = Guid.NewGuid().ToString(), CreationTime = DateTime.UtcNow, Lifetime = 300 }; var store = new DeviceFlowStore(g.operationalDb, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, data); var foundDeviceFlowCodes = g.operationalDb.Select <Storage.Entities.DeviceFlowCodes>().Where(x => x.DeviceCode == deviceCode).First(); foundDeviceFlowCodes.Should().NotBeNull(); var deserializedData = new PersistentGrantSerializer().Deserialize <DeviceCode>(foundDeviceFlowCodes?.Data); deserializedData.CreationTime.Should().BeCloseTo(data.CreationTime); deserializedData.ClientId.Should().Be(data.ClientId); deserializedData.Lifetime.Should().Be(data.Lifetime); }
public async Task StoreDeviceAuthorizationAsync_WhenSuccessful_ExpectDeviceCodeAndUserCodeStored() { using var ravenStore = GetDocumentStore(); await new DeviceFlowCodeIndex().ExecuteAsync(ravenStore); var deviceCode = Guid.NewGuid().ToString(); var userCode = Guid.NewGuid().ToString(); var data = new DeviceCode { ClientId = Guid.NewGuid().ToString(), CreationTime = DateTime.UtcNow, Lifetime = 300 }; using (var session = ravenStore.OpenAsyncSession()) { var store = new DeviceFlowStore(session, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, data); } using (var session = ravenStore.OpenSession()) { var foundDeviceFlowCodes = session.Query <DeviceFlowCode>().FirstOrDefault(x => x.DeviceCode == deviceCode); foundDeviceFlowCodes.Should().NotBeNull(); foundDeviceFlowCodes?.DeviceCode.Should().Be(deviceCode); foundDeviceFlowCodes?.UserCode.Should().Be(userCode); } }
private static async Task SaveDevices(PersistedGrantDbContext context) { var store = new DeviceFlowStore(context, new PersistentGrantSerializer(), new FakeLogger <DeviceFlowStore>()); await store.StoreDeviceAuthorizationAsync( "DC1", "U1", new DeviceCode { CreationTime = DateTime.Now, Lifetime = 100, ClientId = "C1", Description = "D1", IsOpenId = false, IsAuthorized = true, RequestedScopes = new List <string>(), AuthorizedScopes = new List <string>(), SessionId = "S1" }); await store.StoreDeviceAuthorizationAsync( "DC2", "U2", new DeviceCode { CreationTime = DateTime.Now, Lifetime = 100, ClientId = "C2", Description = "D2", IsOpenId = false, IsAuthorized = true, RequestedScopes = new List <string>(), AuthorizedScopes = new List <string>(), SessionId = "S2" }); await store.StoreDeviceAuthorizationAsync( "DC3", "U3", new DeviceCode { CreationTime = DateTime.Now, Lifetime = 100, ClientId = "C3", Description = "D3", IsOpenId = false, IsAuthorized = true, RequestedScopes = new List <string>(), AuthorizedScopes = new List <string>(), SessionId = "S3" }); }
public async Task Should_Throw_Exception_If_DeviceCode_Already_Exists(TestDatabase testDb) { var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); var serializer = new PersistentGrantSerializer(); var existingUserCode = $"user_{Guid.NewGuid()}"; var deviceCodeData = new DeviceCode() { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 12, 07, 8, 00, 00), Lifetime = 300, IsOpenId = true, Subject = new ClaimsPrincipal( new ClaimsIdentity( new List <Claim> { new Claim(JwtClaimTypes.Subject, $"sub_{Guid.NewGuid()}") } ) ) }; using (var session = testDb.SessionFactory.OpenSession()) { using (var tx = session.BeginTransaction()) { await session.SaveAsync(new DeviceFlowCodes() { DeviceCode = $"device_{Guid.NewGuid()}", ID = existingUserCode, ClientId = deviceCodeData.ClientId, SubjectId = deviceCodeData.Subject.FindFirst(JwtClaimTypes.Subject).Value, CreationTime = deviceCodeData.CreationTime, Expiration = deviceCodeData.CreationTime.AddSeconds(deviceCodeData.Lifetime), Data = serializer.Serialize(deviceCodeData) }); await tx.CommitAsync(); } } using (var session = testDb.SessionFactory.OpenSession()) { var store = new DeviceFlowStore(session, new PersistentGrantSerializer(), loggerMock.Object); Func <Task> act = async() => await store.StoreDeviceAuthorizationAsync($"device_{Guid.NewGuid()}", existingUserCode, deviceCodeData); await act.Should().ThrowAsync <HibernateException>(); } await CleanupTestDataAsync(testDb); }
public async Task StoreDeviceAuthorizationAsync_WhenUserCodeAlreadyExists_ExpectException() { using var ravenStore = GetDocumentStore(); await new DeviceFlowCodeIndex().ExecuteAsync(ravenStore); var existingUserCode = $"user_{Guid.NewGuid().ToString()}"; var deviceCodeData = new DeviceCode { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300, IsOpenId = true, Subject = new ClaimsPrincipal(new ClaimsIdentity( new List <Claim> { new Claim(JwtClaimTypes.Subject, $"sub_{Guid.NewGuid().ToString()}") })) }; using (var session = ravenStore.OpenSession()) { session.Store(new DeviceFlowCode { DeviceCode = $"device_{Guid.NewGuid().ToString()}", UserCode = existingUserCode, ClientId = deviceCodeData.ClientId, SubjectId = deviceCodeData.Subject.FindFirst(JwtClaimTypes.Subject).Value, CreationTime = deviceCodeData.CreationTime, Expiration = deviceCodeData.CreationTime.AddSeconds(deviceCodeData.Lifetime), Data = serializer.Serialize(deviceCodeData) }); session.SaveChanges(); } WaitForIndexing(ravenStore); using (var session = ravenStore.OpenAsyncSession()) { var store = new DeviceFlowStore(session, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await Assert.ThrowsAsync <Exception>(() => store.StoreDeviceAuthorizationAsync($"device_{Guid.NewGuid().ToString()}", existingUserCode, deviceCodeData)); } }
public async Task StoreDeviceAuthorizationAsync_should_create_device_code() { using var store = new RavenDbTestDriverWrapper().GetDocumentStore(); var serializer = new PersistentGrantSerializer(); var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); var sut = new DeviceFlowStore(new ScopedAsynDocumentcSession(store.OpenAsyncSession()), serializer, loggerMock.Object); var code = Guid.NewGuid().ToString(); await sut.StoreDeviceAuthorizationAsync(code, "usercode", new DeviceCode { AuthorizedScopes = new [] { "authorization_code" } }); using var s2 = store.OpenAsyncSession(); Assert.NotEmpty(await s2.Query <Entity.DeviceCode>().Where(d => d.Code == code).ToListAsync()); }
public async Task StoreDeviceAuthorizationAsync_WhenUserCodeAlreadyExists_ExpectException(DbContextOptions <PersistedGrantDbContext> options) { var existingUserCode = $"user_{Guid.NewGuid().ToString()}"; var deviceCodeData = new DeviceCode { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300, IsOpenId = true, Subject = new ClaimsPrincipal(new ClaimsIdentity( new List <Claim> { new Claim(JwtClaimTypes.Subject, $"sub_{Guid.NewGuid().ToString()}") })) }; using (var context = new PersistedGrantDbContext(options)) { context.DeviceFlowCodes.Add(new DeviceFlowCodes { DeviceCode = $"device_{Guid.NewGuid().ToString()}", UserCode = existingUserCode, ClientId = deviceCodeData.ClientId, SubjectId = deviceCodeData.Subject.FindFirst(JwtClaimTypes.Subject).Value, CreationTime = deviceCodeData.CreationTime, Expiration = deviceCodeData.CreationTime.AddSeconds(deviceCodeData.Lifetime), Data = serializer.Serialize(deviceCodeData) }); context.SaveChanges(); } using (var context = new PersistedGrantDbContext(options)) { var store = new DeviceFlowStore(context, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create(), new NoneCancellationTokenProvider()); // skip odd behaviour of in-memory provider if (options.Extensions.All(x => x.GetType() != typeof(InMemoryOptionsExtension))) { await Assert.ThrowsAsync <DbUpdateException>(() => store.StoreDeviceAuthorizationAsync($"device_{Guid.NewGuid().ToString()}", existingUserCode, deviceCodeData)); } } }
public async Task StoreDeviceAuthorizationAsync_WhenSuccessful_ExpectDeviceCodeAndUserCodeStored() { var deviceCode = Guid.NewGuid().ToString(); var userCode = Guid.NewGuid().ToString(); var data = new DeviceCode { ClientId = Guid.NewGuid().ToString(), CreationTime = DateTime.UtcNow, Lifetime = 300 }; var store = new DeviceFlowStore(_context, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, data); var foundDeviceFlowCodes = (await _context.DeviceFlowCodes.WhereEqualTo("DeviceCode", deviceCode).GetSnapshotAsync())[0].ConvertTo <DeviceFlowCodes>(); foundDeviceFlowCodes.Should().NotBeNull(); foundDeviceFlowCodes?.DeviceCode.Should().Be(deviceCode); foundDeviceFlowCodes?.UserCode.Should().Be(userCode); }
public async Task StoreDeviceAuthorizationAsync_WhenUserCodeAlreadyExists_ExpectException() { var existingUserCode = $"user_{Guid.NewGuid().ToString()}"; var deviceCodeData = new DeviceCode { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300, IsOpenId = true, Subject = new ClaimsPrincipal(new ClaimsIdentity( new List <Claim> { new Claim(JwtClaimTypes.Subject, $"sub_{Guid.NewGuid().ToString()}") })) }; var entity = new Storage.Entities.DeviceFlowCodes { DeviceCode = $"device_{Guid.NewGuid().ToString()}", UserCode = existingUserCode, ClientId = deviceCodeData.ClientId, SubjectId = deviceCodeData.Subject.FindFirst(JwtClaimTypes.Subject).Value, CreationTime = deviceCodeData.CreationTime, Expiration = deviceCodeData.CreationTime.AddSeconds(deviceCodeData.Lifetime), Data = serializer.Serialize(deviceCodeData) }; g.operationalDb.Insert(entity).ExecuteAffrows(); var store = new DeviceFlowStore(g.operationalDb, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await Assert.ThrowsAsync <Exception>(() => store.StoreDeviceAuthorizationAsync($"device_{Guid.NewGuid().ToString()}", existingUserCode, deviceCodeData)); }
public async Task Should_Find_Stored_Data(TestDatabase testDb) { var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); var serializer = new PersistentGrantSerializer(); var deviceCode = Guid.NewGuid().ToString(); var userCode = Guid.NewGuid().ToString(); var data = new DeviceCode() { ClientId = Guid.NewGuid().ToString(), CreationTime = DateTime.UtcNow, Lifetime = 300 }; using (var session = testDb.SessionFactory.OpenSession()) { var store = new DeviceFlowStore(session, new PersistentGrantSerializer(), loggerMock.Object); await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, data); } using (var session = testDb.SessionFactory.OpenSession()) { var foundDeviceFlowCodes = await session.QueryOver <DeviceFlowCodes>() .Where(c => c.DeviceCode == deviceCode) .SingleOrDefaultAsync(); foundDeviceFlowCodes.Should().NotBeNull(); var deserializedData = serializer.Deserialize <DeviceCode>(foundDeviceFlowCodes?.Data); deserializedData.CreationTime.Should().BeCloseTo(data.CreationTime); deserializedData.ClientId.Should().Be(data.ClientId); deserializedData.Lifetime.Should().Be(data.Lifetime); } await CleanupTestDataAsync(testDb); }