Exemplo n.º 1
0
        public async Task RemoveUserConsentAsync_should_delete_entity()
        {
            using var store = new RavenDbTestDriverWrapper().GetDocumentStore();
            var serializer = new PersistentGrantSerializer();
            var loggerMock = new Mock <ILogger <UserConsentStore> >();

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

            await s1.SaveChangesAsync();

            using var session = store.OpenAsyncSession();
            var sut = new UserConsentStore(new ScopedAsynDocumentcSession(session), serializer, loggerMock.Object);

            await sut.RemoveUserConsentAsync("test", "test");

            using var s2 = store.OpenAsyncSession();

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

            Assert.Null(result);
        }
Exemplo n.º 2
0
        public async Task StoreUserConsentAsync_should_update_entity()
        {
            using var store = new RavenDbTestDriverWrapper().GetDocumentStore();
            var serializer = new PersistentGrantSerializer();
            var loggerMock = new Mock <ILogger <UserConsentStore> >();

            var UserConsent = new Consent
            {
                ClientId  = "test",
                SubjectId = "test"
            };

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

            await s1.SaveChangesAsync();

            using var session = store.OpenAsyncSession();
            var sut = new UserConsentStore(new ScopedAsynDocumentcSession(session), serializer, loggerMock.Object);

            await sut.StoreUserConsentAsync(UserConsent);

            using var s2 = store.OpenAsyncSession();

            var result = await s2.Advanced.LoadStartingWithAsync <Entity.UserConsent>($"{nameof(Entity.UserConsent).ToLowerInvariant()}/");

            Assert.Single(result);
        }
        private static void CreateSut(out Mock <IAdminStore <UserConsent> > storeMock,
                                      out UserConsentStore sut)
        {
            storeMock = new Mock <IAdminStore <UserConsent> >();
            var serializerMock = new Mock <IPersistentGrantSerializer>();

            sut = new UserConsentStore(storeMock.Object, serializerMock.Object);
        }
Exemplo n.º 4
0
        public async Task RemoveUserConsentAsync_should_not_throw_when_entity_not_exist()
        {
            using var store = new RavenDbTestDriverWrapper().GetDocumentStore();
            var serializer = new PersistentGrantSerializer();
            var loggerMock = new Mock <ILogger <UserConsentStore> >();

            using var session = store.OpenAsyncSession();
            var sut = new UserConsentStore(new ScopedAsynDocumentcSession(session), serializer, loggerMock.Object);

            await sut.RemoveUserConsentAsync("test", "test");

            using var s2 = store.OpenAsyncSession();

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

            Assert.Null(result);
        }
Exemplo n.º 5
0
        public override async Task <Boolean> RequiresConsentAsync(ClaimsPrincipal subject, Client client, IEnumerable <String> scopes)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }
            if (subject == null)
            {
                throw new ArgumentNullException(nameof(subject));
            }

            if (!client.RequireConsent)
            {
                Logger.LogDebug("Client is configured to not require consent, no consent is required");
                return(false);
            }

            if (!client.AllowRememberConsent)
            {
                Logger.LogDebug("Client is configured to not allow remembering consent, consent is required");
                return(true);
            }

            if (scopes == null || !scopes.Any())
            {
                Logger.LogDebug("No scopes being requested, no consent is required");
                return(false);
            }

            //if (scopes.Contains(IdentityServerConstants.StandardScopes.OfflineAccess)) {
            //    Logger.LogDebug("Scopes contains offline_access, consent is required");
            //    return true;
            //}

            Consent consent = await UserConsentStore.GetUserConsentAsync(subject.GetSubjectId(), client.ClientId);

            if (consent == null)
            {
                Logger.LogDebug("Found no prior consent from consent store, consent is required");
                return(true);
            }

            if (consent.Expiration.HasExpired(Clock.UtcNow.UtcDateTime))
            {
                Logger.LogDebug("Consent found in consent store is expired, consent is required");
                await UserConsentStore.RemoveUserConsentAsync(consent.SubjectId, consent.ClientId);

                return(true);
            }

            if (consent.Scopes != null)
            {
                IEnumerable <String> intersect = scopes.Intersect(consent.Scopes);
                Boolean different = !(scopes.Count() == intersect.Count());

                if (different)
                {
                    Logger.LogDebug("Consent found in consent store is different than current request, consent is required");
                }
                else
                {
                    Logger.LogDebug("Consent found in consent store is same as current request, consent is not required");
                }

                return(different);
            }

            Logger.LogDebug("Consent found in consent store has no scopes, consent is required");

            return(true);
        }