Example #1
0
 public TenantAwareTokenCleanupService(
     OperationalStoreOptions options,
     IAdminServices adminServices,
     ITenantAwareConfigurationDbContextAccessor tenantAwareConfigurationDbContextAccessor,
     ILogger <TenantAwareTokenCleanupService> logger,
     IOperationalStoreNotification operationalStoreNotification = null)
 {
     _options       = options;
     _adminServices = adminServices;
     _tenantAwareConfigurationDbContextAccessor = tenantAwareConfigurationDbContextAccessor;
     _logger = logger;
     _operationalStoreNotification = operationalStoreNotification;
 }
Example #2
0
        /// <summary>
        /// Constructor for TokenCleanupService.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="persistedGrantDbContext"></param>
        /// <param name="operationalStoreNotification"></param>
        /// <param name="logger"></param>
        public TokenCleanupService(
            OperationalStoreOptions options,
            IPersistedGrantDbContext persistedGrantDbContext,
            ILogger <TokenCleanupService> logger,
            IOperationalStoreNotification 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");
            }

            _persistedGrantDbContext = persistedGrantDbContext ?? throw new ArgumentNullException(nameof(persistedGrantDbContext));
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));

            _operationalStoreNotification = operationalStoreNotification;
        }
Example #3
0
        /// <summary>
        /// Constructor for TokenCleanupService.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="persistedGrantDbContext"></param>
        /// <param name="operationalStoreNotification"></param>
        /// <param name="logger"></param>
        public TokenCleanupService(
            OperationalStoreOptions options,
            IFreeSql <OperationalDb> freeSql,
            ILogger <TokenCleanupService> logger,
            IOperationalStoreNotification 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");
            }

            _freeSql = freeSql ?? throw new ArgumentNullException(nameof(freeSql));
            _logger  = logger ?? throw new ArgumentNullException(nameof(logger));

            _operationalStoreNotification = operationalStoreNotification;
        }
Example #4
0
        /// <summary>
        /// Constructor for TokenCleanupService.
        /// </summary>
        public TokenCleanupService(
            OperationalStoreOptions options,
            IStatelessSession session,
            ILogger <TokenCleanupService> logger,
            IOperationalStoreNotification 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");
            }

            _session = session ?? throw new ArgumentNullException(nameof(session));
            _logger  = logger ?? throw new ArgumentNullException(nameof(logger));

            _operationalStoreNotification = operationalStoreNotification;
        }
        /// <summary>
        /// Constructor for TokenCleanupService.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="unitOfWork"></param>
        /// <param name="operationalStoreNotification"></param>
        /// <param name="logger"></param>
        public TokenCleanupService(
            OperationalStoreOptions options,
            UnitOfWork unitOfWork,
            ILogger <TokenCleanupService> logger,
            IOperationalStoreNotification operationalStoreNotification = null)
        {
            this.options = options ?? throw new ArgumentNullException(nameof(options));
            if (this.options.TokenCleanupBatchSize < 1)
            {
                throw new ArgumentException("Token cleanup batch size interval must be at least 1");
            }

            this.unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));
            this.logger     = logger ?? throw new ArgumentNullException(nameof(logger));

            this.operationalStoreNotification = operationalStoreNotification;
        }
        private async Task RemoveGrants(IPersistedGrantDbContext context, IOperationalStoreNotification tokenCleanupNotification)
        {
            var found = Int32.MaxValue;

            while (found >= _options.TokenCleanupBatchSize)
            {
                var expiredGrants = context.PersistedGrants
                                    .Where(x => x.Expiration < DateTime.UtcNow)
                                    .OrderBy(x => x.Key)
                                    .Take(_options.TokenCleanupBatchSize)
                                    .ToArray();

                found = expiredGrants.Length;
                _logger.LogInformation("Removing {grantCount} grants", found);

                if (found > 0)
                {
                    context.PersistedGrants.RemoveRange(expiredGrants);
                    try
                    {
                        context.SaveChanges();

                        if (tokenCleanupNotification != null)
                        {
                            await tokenCleanupNotification.PersistedGrantsRemovedAsync(expiredGrants);
                        }
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        // we get this if/when someone else already deleted the records
                        // we want to essentially ignore this, and keep working
                        _logger.LogDebug("Concurrency exception removing expired grants: {exception}", ex.Message);
                    }
                }
            }
        }