public async Task Should_Not_Remove_Still_Valid_Grant(TestDatabase testDb)
        {
            var validGrant = new Models.PersistedGrant
            {
                Key        = Guid.NewGuid().ToString(),
                ClientId   = "test-app",
                Type       = "reference",
                SubjectId  = "42",
                Expiration = DateTime.UtcNow.AddDays(3),
                Data       = "testdata"
            };

            using (var session = testDb.OpenSession())
            {
                await session.SaveAsync(validGrant.ToEntity());

                await session.FlushAsync();
            }

            await CreateTokenCleanupServiceSut(testDb, TestOperationalStoreOptions).RemoveExpiredGrantsAsync();

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

            await CleanupTestDataAsync(testDb);
        }
コード例 #2
0
        public async Task StoreAsync(Models.PersistedGrant grant)
        {
            try
            {
                if (grant == null)
                {
                    throw new ArgumentNullException("grant cant be null");
                }

                var ncachePersistantGrant = grant.ToEntity();

                await _repository.AddAsync(ncachePersistantGrant);

                if (_isDebugLoggingEnabled)
                {
                    _logger.LogDebug("Grant {key} stored", grant.Key);
                }
            }
            catch (Exception ex)
            {
                if (_isErrorLoggingEnabled)
                {
                    _logger.LogError(
                        ex,
                        $"something went wrong with StoreAsync for " +
                        $"grant key {grant.Key}");
                }
                throw;
            }
        }
        public void Map()
        {
            var model  = new Models.PersistedGrant();
            var entity = PersistedGrantMappers.ToEntity(model);

            model = PersistedGrantMappers.ToModel(entity);

            // Assert
            Assert.IsNotNull(entity);
            Assert.IsNotNull(model);
        }
コード例 #4
0
 private PersistedGrant Map(Models.PersistedGrant grant)
 {
     return(new PersistedGrant
     {
         ClientId = grant.ClientId,
         CreationTime = grant.CreationTime,
         Data = grant.Data,
         Expiration = grant.Expiration,
         Key = grant.Key,
         SubjectId = grant.SubjectId,
         Type = grant.Type
     });
 }
コード例 #5
0
        public async Task StoreAsync(Models.PersistedGrant grant)
        {
            using (var connection = new SqlConnection(_dapperStoreOptions.DbConnectionString))
            {
                await RemoveAsync(grant.Key);

                var sql = $@"
                INSERT 
                INTO PersistedGrant(K, Type, SubjectId, ClientId, CreationTime, Expiration, Data) 
                VALUES(@Key, @Type, @SubjectId, @ClientId, @CreationTime, @Expiration, @Data);
                ";
                await connection.ExecuteAsync(sql, new { grant.Key, grant.Type, grant.SubjectId, grant.ClientId, grant.CreationTime, grant.Expiration, grant.Data });
            }
        }
        public void ToEntity_MapsKeyWithDefaultHash()
        {
            const string key = "some-key";

            var persistedGrant = new Models.PersistedGrant()
            {
                Key = key
            };

            var entity = persistedGrant.ToEntity();

            var expectedKeyValue = CryptographyHelper.CreateHash(key);

            Assert.Equal(expectedKeyValue, entity.Key);
        }
コード例 #7
0
        private Models.PersistedGrant Map(Models.PersistedGrant existing, PersistedGrant grant)
        {
            if (existing == null)
            {
                existing = new Models.PersistedGrant();
            }

            existing.ClientId     = grant.ClientId;
            existing.CreationTime = grant.CreationTime;
            existing.Data         = grant.Data;
            existing.Expiration   = grant.Expiration;
            existing.Key          = grant.Key;
            existing.SubjectId    = grant.SubjectId;
            existing.Type         = grant.Type;

            return(existing);
        }
コード例 #8
0
        public void Map()
        {
            // Arrange
            var mapperConfiguration = new MapperConfiguration(expression => { expression.AddProfile <PersistedGrantMapperProfile>(); });
            var mapper = new AutoMapperWrapper(new Mapper(mapperConfiguration));
            var model  = new Models.PersistedGrant();

            // Act
            var entity = mapper.Map <Entities.PersistedGrant>(model);

            model = mapper.Map <Models.PersistedGrant>(entity);

            // Assert
            Assert.NotNull(entity);
            Assert.NotNull(model);
            mapperConfiguration.AssertConfigurationIsValid();
        }
コード例 #9
0
 public static Entities.PersistedGrant ToEntity(this Models.PersistedGrant model)
 {
     return(model == null ? null : Mapper.Map <Entities.PersistedGrant>(model));
 }
コード例 #10
0
 /// <summary>
 /// Updates an entity from a model.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="entity">The entity.</param>
 public static void UpdateEntity(this Models.PersistedGrant model, Entities.PersistedGrant entity)
 {
     Mapper.Map(model, entity);
 }
コード例 #11
0
 public static void UpdateEntity(this Models.PersistedGrant token, Documents.PersistedGrant target)
 {
     Mapper.Map(token, target);
 }
コード例 #12
0
 public static Documents.PersistedGrant ToDocument(this Models.PersistedGrant token)
 {
     return(token == null ? null : Mapper.Map <Documents.PersistedGrant>(token));
 }
コード例 #13
0
        public async Task StoreAsync(Models.PersistedGrant grant)
        {
            try
            {
                if (grant == null)
                {
                    throw new ArgumentNullException(
                              nameof(grant),
                              "grant cant be null");
                }

                await _inner.StoreAsync(grant);

                var cacheKey = Utilities.CreateCachedPersistedGrantStoreKey(grant);
                var item     = new CacheItem(grant.ToEntity())
                {
                    Tags       = Utilities.CreateCachePersistedGrantStoreTags(grant),
                    Expiration =
                        Utilities.DetermineCachePersistedGrantStoreExpiration(
                            grant, _handle.options)
                };

                var result = await _setFallBackPolicy
                             .ExecuteAsync(async() =>
                {
                    await _handle.cache.InsertAsync(cacheKey, item)
                    .ConfigureAwait(false);
                    return(true);
                }).ConfigureAwait(false);


                if (result)
                {
                    if (_debugLoggingEnabled)
                    {
                        _logger.LogDebug(
                            $"Persisted grant with key {grant.Key} " +
                            $"successfully stored");
                    }
                }
                else
                {
                    if (_errorLoggingEnabled)
                    {
                        _logger.LogError(
                            $"Caching problems with persistant store cache");
                    }
                }
            }
            catch (Exception ex)
            {
                if (_errorLoggingEnabled)
                {
                    _logger.LogError(
                        ex,
                        $"something went wrong with StoreAsync " +
                        $"for  grant key {grant.Key}");
                }
                throw;
            }
        }
コード例 #14
0
        public async Task <Models.PersistedGrant> GetAsync(string key)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(key))
                {
                    throw new ArgumentNullException(
                              "key cant be null or white space", nameof(key));
                }

                var cacheKey = Utilities.CreateCachedPersistedGrantStoreKey(key);

                var cachedPersistedGrant = await _getPersistedGrantPolicy
                                           .ExecuteAsync(async() =>
                {
                    return(await Task.Run(() =>
                                          _handle.cache.Get <PersistedGrant>(cacheKey)
                                          ).ConfigureAwait(false));
                }).ConfigureAwait(false);

                Models.PersistedGrant persistedGrant = null;

                if (cachedPersistedGrant == null)
                {
                    persistedGrant = await _inner.GetAsync(key);

                    if (persistedGrant != null)
                    {
                        if (_debugLoggingEnabled)
                        {
                            _logger.LogDebug(
                                $"Cache Miss: Persisted grant {key}" +
                                $"acquired from IPersistedGrantStore");
                        }
                        var item = new CacheItem(persistedGrant.ToEntity())
                        {
                            Tags =
                                Utilities.CreateCachePersistedGrantStoreTags(
                                    persistedGrant),
                            Expiration =
                                Utilities.DetermineCachePersistedGrantStoreExpiration
                                (
                                    persistedGrant,
                                    _handle.options)
                        };

                        bool result = await _setFallBackPolicy
                                      .ExecuteAsync(async() =>
                        {
                            await _handle.cache.InsertAsync(cacheKey, item)
                            .ConfigureAwait(false);
                            return(true);
                        }).ConfigureAwait(false);

                        if (_errorLoggingEnabled && !result)
                        {
                            _logger.LogError(
                                $"Caching problems with persistant store cache");
                        }
                    }
                    else
                    {
                        if (_debugLoggingEnabled)
                        {
                            _logger.LogDebug(
                                $"Persisted grant {key} not found");
                        }
                    }
                }
                else
                {
                    persistedGrant = cachedPersistedGrant.ToModel();
                    if (_debugLoggingEnabled)
                    {
                        _logger.LogDebug(
                            $"Cache Hit: Persisted grant {key}" +
                            $"acquired from NCache");
                    }
                }

                return(persistedGrant);
            }
            catch (Exception ex)
            {
                if (_errorLoggingEnabled)
                {
                    _logger.LogError(
                        ex,
                        $"something went wrong with GetAsync for {key}");
                }
                throw;
            }
        }