public async Task UpdateByUserCodeAsync_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.UpdateByUserCodeAsync(null, null)); await Assert.ThrowsAsync <ArgumentNullException>(() => sut.UpdateByUserCodeAsync("code", null)); }
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 UpdateByUserCodeAsync_should_update_device_code() { using var store = new RavenDbTestDriverWrapper().GetDocumentStore(); var serializer = new PersistentGrantSerializer(); var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); using var s1 = store.OpenAsyncSession(); await s1.StoreAsync(new Entity.DeviceCode { Id = "test", UserCode = "code", Data = serializer.Serialize(new DeviceCode { AuthorizedScopes = new[] { "client_credential" } }) }, $"{nameof(Entity.DeviceCode)}/test"); await s1.SaveChangesAsync(); var sut = new DeviceFlowStore(new ScopedAsynDocumentcSession(store.OpenAsyncSession()), serializer, loggerMock.Object); await sut.UpdateByUserCodeAsync("code", new DeviceCode { ClientId = "test" }); using var s2 = store.OpenAsyncSession(); var updated = await s2.LoadAsync <Entity.DeviceCode>($"{nameof(Entity.DeviceCode)}/test"); var data = serializer.Deserialize <DeviceCode>(updated.Data); Assert.Equal("test", data.ClientId); }
public async Task UpdateByUserCodeAsync_WhenDeviceCodeAuthorized_ExpectSubjectAndDataUpdated() { var testDeviceCode = $"device_{Guid.NewGuid().ToString()}"; var testUserCode = $"user_{Guid.NewGuid().ToString()}"; var expectedSubject = $"sub_{Guid.NewGuid().ToString()}"; var unauthorizedDeviceCode = new DeviceCode { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300, IsOpenId = true }; var repo = g.operationalDb.GetRepository <Storage.Entities.DeviceFlowCodes>(); var entity = new Storage.Entities.DeviceFlowCodes { DeviceCode = testDeviceCode, UserCode = testUserCode, ClientId = unauthorizedDeviceCode.ClientId, CreationTime = unauthorizedDeviceCode.CreationTime, Expiration = unauthorizedDeviceCode.CreationTime.AddSeconds(unauthorizedDeviceCode.Lifetime), Data = serializer.Serialize(unauthorizedDeviceCode) }; repo.Insert(entity); var authorizedDeviceCode = new DeviceCode { ClientId = unauthorizedDeviceCode.ClientId, RequestedScopes = unauthorizedDeviceCode.RequestedScopes, AuthorizedScopes = unauthorizedDeviceCode.RequestedScopes, Subject = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> { new Claim(JwtClaimTypes.Subject, expectedSubject) })), IsAuthorized = true, IsOpenId = true, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300 }; var store = new DeviceFlowStore(g.operationalDb, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.UpdateByUserCodeAsync(testUserCode, authorizedDeviceCode); Storage.Entities.DeviceFlowCodes updatedCodes; updatedCodes = repo.Where(x => x.UserCode == testUserCode).First(); // should be unchanged updatedCodes.DeviceCode.Should().Be(testDeviceCode); updatedCodes.ClientId.Should().Be(unauthorizedDeviceCode.ClientId); updatedCodes.CreationTime.Should().Be(unauthorizedDeviceCode.CreationTime); updatedCodes.Expiration.Should().Be(unauthorizedDeviceCode.CreationTime.AddSeconds(authorizedDeviceCode.Lifetime)); // should be changed var parsedCode = serializer.Deserialize <DeviceCode>(updatedCodes.Data); parsedCode.Should().BeEquivalentTo(authorizedDeviceCode, assertionOptions => assertionOptions.Excluding(x => x.Subject)); parsedCode.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject && x.Value == expectedSubject).Should().NotBeNull(); }
public async Task UpdateByUserCodeAsync_should_throw_when_code_not_found() { 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 <InvalidOperationException>(() => sut.UpdateByUserCodeAsync("code", new DeviceCode { ClientId = "test" })); }
public async Task UpdateByUserCodeAsync_WhenDeviceCodeAuthorized_ExpectSubjectAndDataUpdated() { var storeHolder = GetOperationalDocumentStoreHolder(); var testDeviceCode = $"device_{Guid.NewGuid().ToString()}"; var testUserCode = $"user_{Guid.NewGuid().ToString()}"; var expectedSubject = $"sub_{Guid.NewGuid().ToString()}"; var unauthorizedDeviceCode = new DeviceCode { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300, IsOpenId = true }; using (var session = storeHolder.OpenAsyncSession()) { await session.StoreAsync(new DeviceFlowCode { DeviceCode = testDeviceCode, UserCode = testUserCode, ClientId = unauthorizedDeviceCode.ClientId, CreationTime = unauthorizedDeviceCode.CreationTime, Expiration = unauthorizedDeviceCode.CreationTime.AddSeconds(unauthorizedDeviceCode.Lifetime), Data = _serializer.Serialize(unauthorizedDeviceCode) }); await session.SaveChangesAsync(); } WaitForIndexing(storeHolder.IntegrationTest_GetDocumentStore()); var authorizedDeviceCode = new DeviceCode { ClientId = unauthorizedDeviceCode.ClientId, RequestedScopes = unauthorizedDeviceCode.RequestedScopes, AuthorizedScopes = unauthorizedDeviceCode.RequestedScopes, Subject = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> { new Claim(JwtClaimTypes.Subject, expectedSubject) })), IsAuthorized = true, IsOpenId = true, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300 }; var store = new DeviceFlowStore(storeHolder, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create()); await store.UpdateByUserCodeAsync(testUserCode, authorizedDeviceCode); WaitForIndexing(storeHolder.IntegrationTest_GetDocumentStore()); using (var session = storeHolder.OpenAsyncSession()) { var updatedCodes = await session.Query <DeviceFlowCode>().SingleAsync(x => x.UserCode == testUserCode); // should be unchanged updatedCodes.DeviceCode.Should().Be(testDeviceCode); updatedCodes.ClientId.Should().Be(unauthorizedDeviceCode.ClientId); updatedCodes.CreationTime.Should().Be(unauthorizedDeviceCode.CreationTime); updatedCodes.Expiration.Should() .Be(unauthorizedDeviceCode.CreationTime.AddSeconds(authorizedDeviceCode.Lifetime)); // should be changed var parsedCode = _serializer.Deserialize <DeviceCode>(updatedCodes.Data); parsedCode.Should().BeEquivalentTo(authorizedDeviceCode, assertionOptions => assertionOptions.Excluding(x => x.Subject)); parsedCode.Subject.Claims .FirstOrDefault(x => x.Type == JwtClaimTypes.Subject && x.Value == expectedSubject).Should() .NotBeNull(); } }
public async Task UpdateByUserCodeAsync_WhenDeviceCodeAuthorized_ExpectSubjectAndDataUpdated(DbContextOptions <PersistedGrantDbContext> options) { var testDeviceCode = $"device_{Guid.NewGuid().ToString()}"; var testUserCode = $"user_{Guid.NewGuid().ToString()}"; var expectedSubject = $"sub_{Guid.NewGuid().ToString()}"; var unauthorizedDeviceCode = new DeviceCode { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300, IsOpenId = true }; using (var context = new PersistedGrantDbContext(options)) { context.DeviceFlowCodes.Add(new DeviceFlowCodes { DeviceCode = testDeviceCode, UserCode = testUserCode, ClientId = unauthorizedDeviceCode.ClientId, CreationTime = unauthorizedDeviceCode.CreationTime, Expiration = unauthorizedDeviceCode.CreationTime.AddSeconds(unauthorizedDeviceCode.Lifetime), Data = serializer.Serialize(unauthorizedDeviceCode) }); context.SaveChanges(); } var authorizedDeviceCode = new DeviceCode { ClientId = unauthorizedDeviceCode.ClientId, RequestedScopes = unauthorizedDeviceCode.RequestedScopes, AuthorizedScopes = unauthorizedDeviceCode.RequestedScopes, Subject = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> { new Claim(JwtClaimTypes.Subject, expectedSubject) })), IsAuthorized = true, IsOpenId = true, CreationTime = new DateTime(2018, 10, 19, 16, 14, 29), Lifetime = 300 }; using (var context = new PersistedGrantDbContext(options)) { var store = new DeviceFlowStore(context, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create(), new NoneCancellationTokenProvider()); await store.UpdateByUserCodeAsync(testUserCode, authorizedDeviceCode); } DeviceFlowCodes updatedCodes; using (var context = new PersistedGrantDbContext(options)) { updatedCodes = context.DeviceFlowCodes.Single(x => x.UserCode == testUserCode); } // should be unchanged updatedCodes.DeviceCode.Should().Be(testDeviceCode); updatedCodes.ClientId.Should().Be(unauthorizedDeviceCode.ClientId); updatedCodes.CreationTime.Should().Be(unauthorizedDeviceCode.CreationTime); updatedCodes.Expiration.Should().Be(unauthorizedDeviceCode.CreationTime.AddSeconds(authorizedDeviceCode.Lifetime)); // should be changed var parsedCode = serializer.Deserialize <DeviceCode>(updatedCodes.Data); parsedCode.Should().BeEquivalentTo(authorizedDeviceCode, assertionOptions => assertionOptions.Excluding(x => x.Subject)); parsedCode.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject && x.Value == expectedSubject).Should().NotBeNull(); }
public async Task Should_Update_Authorized_Device_Code(TestDatabase testDb) { var loggerMock = new Mock <ILogger <DeviceFlowStore> >(); var serializer = new PersistentGrantSerializer(); var testDeviceCode = $"device_{Guid.NewGuid()}"; var testUserCode = $"user_{Guid.NewGuid()}"; var expectedSubject = $"sub_{Guid.NewGuid()}"; var unauthorizedDeviceCode = new DeviceCode() { ClientId = "device_flow", RequestedScopes = new[] { "openid", "api1" }, CreationTime = new DateTime(2018, 12, 7, 14, 15, 16), Lifetime = 42, IsOpenId = true }; using (var session = testDb.SessionFactory.OpenSession()) { using (var tx = session.BeginTransaction()) { await session.SaveAsync(new DeviceFlowCodes { DeviceCode = testDeviceCode, ID = testUserCode, ClientId = unauthorizedDeviceCode.ClientId, CreationTime = unauthorizedDeviceCode.CreationTime, Expiration = unauthorizedDeviceCode.CreationTime.AddSeconds(unauthorizedDeviceCode.Lifetime), Data = serializer.Serialize(unauthorizedDeviceCode) }); await tx.CommitAsync(); } } var authorizedDeviceCode = new DeviceCode { ClientId = unauthorizedDeviceCode.ClientId, RequestedScopes = unauthorizedDeviceCode.RequestedScopes, AuthorizedScopes = unauthorizedDeviceCode.RequestedScopes, Subject = new ClaimsPrincipal( new ClaimsIdentity( new List <Claim> { new Claim(JwtClaimTypes.Subject, expectedSubject) } ) ), IsAuthorized = true, IsOpenId = true, CreationTime = new DateTime(2018, 12, 7, 14, 15, 16), Lifetime = 42 }; using (var session = testDb.SessionFactory.OpenSession()) { var store = new DeviceFlowStore(session, serializer, loggerMock.Object); await store.UpdateByUserCodeAsync(testUserCode, authorizedDeviceCode); } DeviceFlowCodes updatedCodes; using (var session = testDb.SessionFactory.OpenSession()) { updatedCodes = await session.GetAsync <DeviceFlowCodes>(testUserCode); } // Unchanged updatedCodes.DeviceCode.Should().Be(testDeviceCode); updatedCodes.ClientId.Should().Be(unauthorizedDeviceCode.ClientId); updatedCodes.CreationTime.Should().Be(unauthorizedDeviceCode.CreationTime); updatedCodes.Expiration.Should().Be(unauthorizedDeviceCode.CreationTime.AddSeconds(unauthorizedDeviceCode.Lifetime)); // Updated var parsedCode = serializer.Deserialize <DeviceCode>(updatedCodes.Data); parsedCode.Should().BeEquivalentTo(authorizedDeviceCode, assertionOptions => assertionOptions.Excluding(x => x.Subject)); parsedCode.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject && x.Value == expectedSubject).Should().NotBeNull(); await CleanupTestDataAsync(testDb); }