Exemplo n.º 1
0
        async Task <T> GetItem <T>(string key, string type)
        {
            key = HashKey(key, type);

            var grant = await _store.GetAsync(key);

            if (grant != null && grant.Type == type)
            {
                return(_serializer.Deserialize <T>(grant.Data));
            }

            return(default(T));
        }
        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 <IEnumerable <Consent> > GetAllGrantsAsync(string subjectId)
        {
            var grants = await _store.GetAllAsync(subjectId);

            var consents = grants.Where(x => x.Type == Constants.PersistedGrantTypes.UserConsent)
                           .Select(x => _serializer.Deserialize <Consent>(x.Data));

            var codes = grants.Where(x => x.Type == Constants.PersistedGrantTypes.AuthorizationCode)
                        .Select(x => _serializer.Deserialize <AuthorizationCode>(x.Data))
                        .Select(x => new Consent
            {
                ClientId     = x.ClientId,
                SubjectId    = subjectId,
                Scopes       = x.RequestedScopes,
                CreationTime = x.CreationTime,
                Expiration   = x.CreationTime.AddSeconds(x.Lifetime)
            });

            var refresh = grants.Where(x => x.Type == Constants.PersistedGrantTypes.RefreshToken)
                          .Select(x => _serializer.Deserialize <RefreshToken>(x.Data))
                          .Select(x => new Consent
            {
                ClientId     = x.ClientId,
                SubjectId    = subjectId,
                Scopes       = x.Scopes,
                CreationTime = x.CreationTime,
                Expiration   = x.CreationTime.AddSeconds(x.Lifetime)
            });

            var access = grants.Where(x => x.Type == Constants.PersistedGrantTypes.ReferenceToken)
                         .Select(x => _serializer.Deserialize <Token>(x.Data))
                         .Select(x => new Consent
            {
                ClientId     = x.ClientId,
                SubjectId    = subjectId,
                Scopes       = x.Scopes,
                CreationTime = x.CreationTime,
                Expiration   = x.CreationTime.AddSeconds(x.Lifetime)
            });

            consents = Join(consents, codes);
            consents = Join(consents, refresh);
            consents = Join(consents, access);

            return(consents.ToArray());
        }
        protected async Task <T> GetItemAsync(string key)
        {
            key = GetHashedKey(key);

            var grant = await _store.GetAsync(key);

            if (grant != null && grant.Type == _grantType)
            {
                return(_serializer.Deserialize <T>(grant.Data));
            }

            return(default(T));
        }
        public async Task UpdateByUserCodeAsync_should_update_token()
        {
            using var store = new RavenDbTestDriverWrapper().GetDocumentStore();
            var serializer = new PersistentGrantSerializer();
            var loggerMock = new Mock <ILogger <RefreshTokenStore> >();

            var token = new RefreshToken
            {
                AccessToken = new Token
                {
                    ClientId = "test"
                }
            };

            using var s1 = store.OpenAsyncSession();
            await s1.StoreAsync(new Entity.RefreshToken
            {
                Id       = "test",
                ClientId = "test",
                Data     = serializer.Serialize(token)
            }, $"{nameof(Entity.RefreshToken)}/test");

            await s1.SaveChangesAsync();

            var sut = new RefreshTokenStore(new ScopedAsynDocumentcSession(store.OpenAsyncSession()), serializer, loggerMock.Object);

            await sut.UpdateRefreshTokenAsync("test", new RefreshToken
            {
                AccessToken = new Token
                {
                    ClientId    = "test",
                    Description = "test"
                }
            });

            using var s2 = store.OpenAsyncSession();

            var updated = await s2.LoadAsync <Entity.RefreshToken>($"{nameof(Entity.RefreshToken)}/test");

            var data = serializer.Deserialize <RefreshToken>(updated.Data);

            Assert.Equal("test", data.AccessToken.Description);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
        }