public void RemoveAsync_WhenSubIdAndClientIdOfExistingReceived_ExpectGrantDeleted()
        {
            var store = new PersistedGrantStore(new TestOptions(), new FakeLogger <PersistedGrantStore>());

            var persistedGrant = CreateTestObject();

            store.StoreAsync(persistedGrant).Wait();

            store.RemoveAllAsync(persistedGrant.SubjectId, persistedGrant.ClientId).Wait();

            var foundGrant = store.GetAsync(persistedGrant.Key).Result;

            Assert.Null(foundGrant);
        }
        public void RemoveAsync_WhenKeyOfExistingReceived_ExpectGrantDeleted()
        {
            var store = new PersistedGrantStore(StoreOptions, new FakeLogger <PersistedGrantStore>());

            var persistedGrant = CreateTestObject();

            store.StoreAsync(persistedGrant).Wait();

            store.RemoveAsync(persistedGrant.Key).Wait();

            var foundGrant = store.GetAsync(persistedGrant.Key).Result;

            Assert.Null(foundGrant);
        }
        public void GetAsync_WithSubAndTypeAndPersistedGrantExists_ExpectPersistedGrantReturned()
        {
            var store = new PersistedGrantStore(new TestOptions(), new FakeLogger <PersistedGrantStore>());

            var persistedGrant = CreateTestObject();

            store.StoreAsync(persistedGrant).Wait();

            var foundPersistedGrants = store.GetAllAsync(persistedGrant.SubjectId).Result.ToList();

            Assert.NotNull(foundPersistedGrants);
            Assert.NotEmpty(foundPersistedGrants);
            Assert.Equal(1, foundPersistedGrants.Count);
            Assert.Equal(persistedGrant.SubjectId, foundPersistedGrants[0].SubjectId);
        }
コード例 #4
0
    public async Task Can_call_PersistedGrantStore_RemoveAsync()
    => await ExecuteWithStrategyInTransactionAsync(
        async context =>
    {
        await SaveGrants(context);
    },
        async context =>
    {
        var store = new PersistedGrantStore(context, new FakeLogger <PersistedGrantStore>());

        await store.RemoveAsync("K2");
        Assert.Null(await store.GetAsync("K2"));
        await store.RemoveAsync("???");
    }
        );
コード例 #5
0
    private static async Task SaveGrants(PersistedGrantDbContext context)
    {
        var store = new PersistedGrantStore(context, new FakeLogger <PersistedGrantStore>());
        await store.StoreAsync(
            new PersistedGrant
        {
            Key          = "K1",
            Type         = "T1",
            SubjectId    = "Su1",
            SessionId    = "Se1",
            ClientId     = "C1",
            Description  = "D1",
            CreationTime = DateTime.Now,
            Expiration   = null,
            ConsumedTime = null,
            Data         = "Data1"
        });

        await store.StoreAsync(
            new PersistedGrant
        {
            Key          = "K2",
            Type         = "T1",
            SubjectId    = "Su1",
            SessionId    = "Se1",
            ClientId     = "C2",
            Description  = "D2",
            CreationTime = DateTime.Now,
            Expiration   = DateTime.Now + new TimeSpan(1, 0, 0, 0),
            ConsumedTime = null,
            Data         = "Data2"
        });

        await store.StoreAsync(
            new PersistedGrant
        {
            Key          = "K3",
            Type         = "T2",
            SubjectId    = "Su2",
            SessionId    = "Se2",
            ClientId     = "C1",
            Description  = "D3",
            CreationTime = DateTime.Now,
            Expiration   = null,
            ConsumedTime = null,
            Data         = "Data3"
        });
    }
コード例 #6
0
        public async Task GetAllAsync_WithSubAndTypeAndPersistedGrantExists_ExpectPersistedGrantReturned()
        {
            var persistedGrant = CreateTestObject();

            await _context.PersistedGrants.Document(persistedGrant.Key).SetAsync(persistedGrant.ToEntity());

            IList <PersistedGrant> foundPersistedGrants;
            var store = new PersistedGrantStore(_context, FakeLogger <PersistedGrantStore> .Create());

            foundPersistedGrants = (await store.GetAllAsync(new PersistedGrantFilter {
                SubjectId = persistedGrant.SubjectId
            })).ToList();

            Assert.NotNull(foundPersistedGrants);
            Assert.NotEmpty(foundPersistedGrants);
        }
        public void Store_should_create_new_record_if_key_does_not_exist()
        {
            var store          = new PersistedGrantStore(new TestOptions(), new FakeLogger <PersistedGrantStore>());
            var persistedGrant = CreateTestObject();

            store.RemoveAsync(persistedGrant.Key).Wait();
            var missingGrant = store.GetAsync(persistedGrant.Key).Result;

            Assert.Null(missingGrant);

            store.StoreAsync(persistedGrant).Wait();

            var foundGrant = store.GetAsync(persistedGrant.Key).Result;

            Assert.NotNull(foundGrant);
        }
コード例 #8
0
        public void StoreAsync_WhenPersistedGrantStored_ExpectSuccess()
        {
            var persistedGrant = GenFu.GenFu.New <PersistedGrant>();

            using (var session = martenFixture.Store.OpenSession())
            {
                var store = new PersistedGrantStore(session);
                store.StoreAsync(persistedGrant.ToModel()).Wait();
            }

            using (var session = martenFixture.Store.OpenSession())
            {
                var foundGrant = session.Query <PersistedGrant>().First();
                Assert.NotNull(foundGrant);
            }
        }
コード例 #9
0
        public void GetAsync_WithKeyAndPersistedGrantExists_ExpectPersistedGrantReturned()
        {
            var persistedGrant = GenFu.GenFu.New <PersistedGrant>();

            using (var session = martenFixture.Store.OpenSession())
            {
                var store = new PersistedGrantStore(session);
                store.StoreAsync(persistedGrant.ToModel()).Wait();
            }
            using (var session = martenFixture.Store.OpenSession())
            {
                var store      = new PersistedGrantStore(session);
                var foundGrant = store.GetAsync(persistedGrant.Key).Result;
                Assert.NotNull(foundGrant);
            }
        }
コード例 #10
0
        public async Task StoreAsync_WhenPersistedGrantStored_ExpectSuccess(DbContextOptions <PersistedGrantDbContext> options)
        {
            var persistedGrant = CreateTestObject();

            using (var context = new PersistedGrantDbContext(options, StoreOptions))
            {
                var store = new PersistedGrantStore(context, FakeLogger <PersistedGrantStore> .Create());
                await store.StoreAsync(persistedGrant);
            }

            using (var context = new PersistedGrantDbContext(options, StoreOptions))
            {
                var foundGrant = context.PersistedGrants.FirstOrDefault(x => x.Key == persistedGrant.Key);
                Assert.NotNull(foundGrant);
            }
        }
コード例 #11
0
        public async Task RemoveAllAsync_WhenSubIdAndClientIdOfExistingReceived_ExpectGrantDeleted()
        {
            var persistedGrant = CreateTestObject();

            await _context.PersistedGrants.Document(persistedGrant.Key).SetAsync(persistedGrant.ToEntity());

            var store = new PersistedGrantStore(_context, FakeLogger <PersistedGrantStore> .Create());
            await store.RemoveAllAsync(new PersistedGrantFilter
            {
                SubjectId = persistedGrant.SubjectId,
                ClientId  = persistedGrant.ClientId
            });

            var foundGrant = (await _context.PersistedGrants.Document(persistedGrant.Key).GetSnapshotAsync()).ConvertTo <Entities.PersistedGrant>();

            Assert.Null(foundGrant);
        }
コード例 #12
0
        public void RemoveAsync_ShouldRemoveByKey()
        {
            var persistedGrant = GenFu.GenFu.New <PersistedGrant>();

            using (var session = martenFixture.Store.LightweightSession())
            {
                session.Store(persistedGrant);
                session.SaveChanges();
            }
            using (var session = martenFixture.Store.LightweightSession())
            {
                var _store = new PersistedGrantStore(session);
                _store.RemoveAsync(persistedGrant.Key).Wait();
                var grant = session.Query <PersistedGrant>().FirstOrDefault(x => x.Key == persistedGrant.Key);
                Assert.Null(grant);
            }
        }
コード例 #13
0
        public void RemoveAllAsync_ShouldRemoveAllGrantsBySubjectAndClientIdAndType()
        {
            var persistedGrant = GenFu.GenFu.New <PersistedGrant>();

            using (var session = martenFixture.Store.LightweightSession())
            {
                session.Store(persistedGrant);
                session.SaveChanges();
            }
            using (var session = martenFixture.Store.LightweightSession())
            {
                var _store = new PersistedGrantStore(session);
                _store.RemoveAllAsync(persistedGrant.SubjectId, persistedGrant.ClientId, persistedGrant.Type).Wait();
                var grants = session.Query <PersistedGrant>().Where(x => x.SubjectId == persistedGrant.SubjectId && x.ClientId == persistedGrant.ClientId && x.Type == persistedGrant.Type).ToList();
                Assert.True(grants.Count == 0);
            }
        }
コード例 #14
0
        /// <summary>
        /// Constructor for TokenCleanupService.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="persistedGrantStore"></param>
        /// <param name="operationalStoreNotification"></param>
        /// <param name="logger"></param>
        public TokenCleanupService(
            OperationalStoreOptions options,
            IPersistedGrantStore persistedGrantStore,
            ILogger <TokenCleanupService> logger,
            IPersistedGrantStoreNotification operationalStoreNotification = null)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));
            if (_options.TokenCleanupBatchSize < 1)
            {
                throw new ArgumentException("Token cleanup batch size interval must be at least 1");
            }

            _persistedGrantStore = persistedGrantStore as PersistedGrantStore ?? throw new ArgumentNullException(nameof(persistedGrantStore));
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));

            _operationalStoreNotification = operationalStoreNotification;
        }
コード例 #15
0
        public async Task Store_should_update_record_if_key_already_exists()
        {
            var persistedGrant = CreateTestObject();

            await _context.PersistedGrants.AddAsync(persistedGrant.ToEntity());

            var newDate = persistedGrant.Expiration.Value.AddHours(1);
            var store   = new PersistedGrantStore(_context, FakeLogger <PersistedGrantStore> .Create());

            persistedGrant.Expiration = newDate;
            await store.StoreAsync(persistedGrant);

            var foundGrant = (await _context.PersistedGrants.WhereEqualTo("Key", persistedGrant.Key).GetSnapshotAsync())[0].ConvertTo <Entities.PersistedGrant>();

            Assert.NotNull(foundGrant);
            Assert.Equal(newDate, persistedGrant.Expiration);
        }
コード例 #16
0
        public async Task StoreAsync_WhenPersistedGrantStored_ExpectSuccess(ISessionFactory sessionFactory)
        {
            var persistedGrant = CreateTestObject();

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var store = new PersistedGrantStore(provider);
                await store.StoreAsync(persistedGrant);
            }

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var foundGrant = await provider.Session.GetAsync <Entities.PersistedGrant>(persistedGrant.Key);

                Assert.NotNull(foundGrant);
            }
        }
コード例 #17
0
        public async Task RemoveAsync_WhenKeyOfExistingReceived_ExpectGrantDeleted()
        {
            var persistedGrant = CreateTestObject();

            var repo = g.operationalDb.GetRepository <Storage.Entities.PersistedGrant>();

            var entity = persistedGrant.ToEntity();

            repo.Insert(entity);

            var store = new PersistedGrantStore(g.operationalDb, FakeLogger <PersistedGrantStore> .Create());
            await store.RemoveAsync(persistedGrant.Key);

            var foundGrant = repo.Select.Where(x => x.Key == persistedGrant.Key).First();

            Assert.Null(foundGrant);
        }
コード例 #18
0
        public async Task Store_should_create_new_record_if_key_does_not_exist()
        {
            var persistedGrant = CreateTestObject();

            var repo = g.operationalDb.GetRepository <Storage.Entities.PersistedGrant>();

            var foundGrant = repo.Select.Where(x => x.Key == persistedGrant.Key).First();

            Assert.Null(foundGrant);

            var store = new PersistedGrantStore(g.operationalDb, FakeLogger <PersistedGrantStore> .Create());
            await store.StoreAsync(persistedGrant);

            var foundGrant2 = repo.Select.Where(x => x.Key == persistedGrant.Key).First();

            Assert.NotNull(foundGrant2);
        }
コード例 #19
0
        public async Task Store_should_create_new_record_if_key_does_not_exist()
        {
            var persistedGrant = CreateTestObject();

            var foundGrant = (await _context.PersistedGrants
                              .WhereEqualTo("Key", persistedGrant.Key)
                              .GetSnapshotAsync()).FirstOrDefault()?.ConvertTo <Entities.PersistedGrant>();

            Assert.Null(foundGrant);

            var store = new PersistedGrantStore(_context, FakeLogger <PersistedGrantStore> .Create());
            await store.StoreAsync(persistedGrant);

            foundGrant = (await _context.PersistedGrants
                          .WhereEqualTo("Key", persistedGrant.Key)
                          .GetSnapshotAsync())[0].ConvertTo <Entities.PersistedGrant>();
            Assert.NotNull(foundGrant);
        }
コード例 #20
0
        public async Task Should_Store_New_Grant(TestDatabase testDb)
        {
            var testGrant  = CreateTestGrant();
            var loggerMock = new Mock <ILogger <PersistedGrantStore> >();

            using (var session = testDb.OpenSession())
            {
                var store = new PersistedGrantStore(session, loggerMock.Object);
                await store.StoreAsync(testGrant);
            }

            using (var session = testDb.OpenSession())
            {
                (await session.GetAsync <Entities.PersistedGrant>(testGrant.Key)).Should().NotBeNull();
            }

            await CleanupTestDataAsync(testDb);
        }
        public void Store_should_update_record_if_key_already_exists()
        {
            var store = new PersistedGrantStore(new TestOptions(), new FakeLogger <PersistedGrantStore>());

            var persistedGrant = CreateTestObject();

            store.StoreAsync(persistedGrant).Wait();

            var newDate = persistedGrant.Expiration.Value.AddHours(1);

            persistedGrant.Expiration = newDate;
            store.StoreAsync(persistedGrant).Wait();

            var foundGrant = store.GetAsync(persistedGrant.Key).Result;

            Assert.NotNull(foundGrant);
            Assert.Equal(newDate, persistedGrant.Expiration);
        }
コード例 #22
0
        public async Task GetAsync_WithKeyAndPersistedGrantExists_ExpectPersistedGrantReturned()
        {
            var persistedGrant = CreateTestObject();

            var repo = g.operationalDb.GetRepository <Storage.Entities.PersistedGrant>();

            var entity = persistedGrant.ToEntity();

            repo.Insert(entity);

            PersistedGrant foundPersistedGrant;

            var store = new PersistedGrantStore(g.operationalDb, FakeLogger <PersistedGrantStore> .Create());

            foundPersistedGrant = await store.GetAsync(persistedGrant.Key);

            Assert.NotNull(foundPersistedGrant);
        }
コード例 #23
0
        public async Task PersistedGrantStore_RemoveSubjectClientTypeTest()
        {
            var storageContext = Services.BuildServiceProvider().GetService <PersistedGrantStorageContext>();

            Assert.IsNotNull(storageContext);

            var store = new PersistedGrantStore(storageContext, _logger);

            Assert.IsNotNull(store);

            string subject = Guid.NewGuid().ToString();
            string client  = Guid.NewGuid().ToString();
            string type    = Guid.NewGuid().ToString();

            List <PersistedGrant> grants = new List <PersistedGrant>();

            for (int iCounter = 0; iCounter < 10; iCounter++)
            {
                var grant = CreateTestObject(subjectId: subject, clientId: client, type: type);
                Console.WriteLine(JsonConvert.SerializeObject(grant));

                await store.StoreAsync(grant);

                grants.Add(grant);
            }
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            var returnGrants = (await store.GetAllAsync(subject)).Where(w => w.ClientId == client && w.Type == type).ToList();

            stopwatch.Stop();
            Console.WriteLine($"PersistedGrantStore.GetAllAsync({subject}): {stopwatch.ElapsedMilliseconds} ms");
            Assert.AreEqual <int>(grants.Count, returnGrants.Count);
            grants.ForEach(g => AssertGrantsEqual(g, returnGrants.FirstOrDefault(f => f.Key == g.Key)));

            stopwatch.Reset();
            stopwatch.Start();
            await store.RemoveAllAsync(subject, client, type);

            stopwatch.Stop();
            Console.WriteLine($"PersistedGrantStore.RemoveAllAsync({subject}, {client}, {type}): {stopwatch.ElapsedMilliseconds} ms");
            returnGrants = (await store.GetAllAsync(subject)).Where(w => w.ClientId == client && w.Type == type).ToList();
            Assert.AreEqual <int>(0, returnGrants.Count);
        }
コード例 #24
0
        public async Task GetAsync_WithKeyAndPersistedGrantExists_ExpectPersistedGrantReturned(ISessionFactory sessionFactory)
        {
            var persistedGrant = CreateTestObject();

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                await provider.Session.SaveAsync(_mapper.Map <Entities.PersistedGrant>(persistedGrant));
            }

            PersistedGrant foundPersistedGrant;

            using (var provider = new OperationalSessionProvider(sessionFactory.OpenSession))
            {
                var store = new PersistedGrantStore(provider);
                foundPersistedGrant = await store.GetAsync(persistedGrant.Key);
            }

            Assert.NotNull(foundPersistedGrant);
        }
        public async void ShouldStoreToken()
        {
            //Arrange
            for (int i = 0; i < 10; i++)
            {
                var token = new PersistedGrant
                {
                    Key          = "key" + i,
                    ClientId     = "clientId" + i,
                    CreationTime = DateTime.Now,
                    Expiration   = DateTime.Now.AddDays(1),
                    SubjectId    = "subjectId" + i,
                    Data         = "data" + i,
                    Type         = "type" + i,
                };

                await PersistedGrantStore.StoreAsync(token);
            }
        }
コード例 #26
0
        public async Task PersistedGrantStore_RemoveByKeyNegativeTest()
        {
            var storageContext = Services.BuildServiceProvider().GetService <PersistedGrantStorageContext>();

            Assert.IsNotNull(storageContext);

            var store = new PersistedGrantStore(storageContext, _logger);

            Assert.IsNotNull(store);

            string    key       = Guid.NewGuid().ToString();
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            await store.RemoveAsync(key);

            stopwatch.Stop();
            Console.WriteLine($"PersistedGrantStore.RemoveAsync({key}): {stopwatch.ElapsedMilliseconds} ms");
        }
        public async Task StoreAsync_WhenPersistedGrantStored_ExpectSuccess()
        {
            var storeHolder = GetOperationalDocumentStoreHolder();

            var persistedGrant = CreateTestObject();

            var store = new PersistedGrantStore(storeHolder, FakeLogger <PersistedGrantStore> .Create());
            await store.StoreAsync(persistedGrant);

            WaitForIndexing(storeHolder.IntegrationTest_GetDocumentStore());

            var hashedTokenKey = CryptographyHelper.CreateHash(persistedGrant.Key);

            using (var session = storeHolder.OpenAsyncSession())
            {
                var foundGrant = await session.Query <PersistedGrant>().FirstOrDefaultAsync(x => x.Key == hashedTokenKey);

                Assert.NotNull(foundGrant);
            }
        }
コード例 #28
0
        public async Task RemoveAllAsync_WhenSubIdClientIdAndTypeOfExistingReceived_ExpectGrantDeleted()
        {
            var persistedGrant = CreateTestObject();

            var repo = g.operationalDb.GetRepository <Storage.Entities.PersistedGrant>();

            var entity = persistedGrant.ToEntity();

            var store = new PersistedGrantStore(g.operationalDb, FakeLogger <PersistedGrantStore> .Create());
            await store.RemoveAllAsync(new PersistedGrantFilter
            {
                SubjectId = persistedGrant.SubjectId,
                ClientId  = persistedGrant.ClientId,
                Type      = persistedGrant.Type
            });

            var foundGrant = repo.Select.Where(x => x.Key == persistedGrant.Key).First();

            Assert.Null(foundGrant);
        }
コード例 #29
0
        public async Task GetAsync_WithKeyAndPersistedGrantExists_ExpectPersistedGrantReturned(DbContextOptions <PersistedGrantDbContext> options)
        {
            var persistedGrant = CreateTestObject();

            using (var context = new PersistedGrantDbContext(options, StoreOptions))
            {
                context.PersistedGrants.Add(persistedGrant.ToEntity());
                context.SaveChanges();
            }

            PersistedGrant foundPersistedGrant;

            using (var context = new PersistedGrantDbContext(options, StoreOptions))
            {
                var store = new PersistedGrantStore(context, FakeLogger <PersistedGrantStore> .Create());
                foundPersistedGrant = await store.GetAsync(persistedGrant.Key);
            }

            Assert.NotNull(foundPersistedGrant);
        }
コード例 #30
0
        public void GetAllAsync_ShouldReturnSavedGrants()
        {
            int    count   = 10;
            string subject = Guid.NewGuid().ToString();

            GenFu.GenFu.Configure <PersistedGrant>().Fill(x => x.SubjectId, subject);
            var persistedGrants = GenFu.GenFu.ListOf <PersistedGrant>(count);

            using (var session = martenFixture.Store.LightweightSession())
            {
                session.StoreObjects(persistedGrants);
                session.SaveChanges();
            }
            using (var session = martenFixture.Store.LightweightSession())
            {
                var _store      = new PersistedGrantStore(session);
                var foundGrants = _store.GetAllAsync(subject).Result;
                Assert.True(foundGrants.Count() == count);
            }
        }