Example #1
0
    /// <summary>
    /// Removes the expired persisted grants.
    /// </summary>
    /// <returns></returns>
    protected virtual async Task RemoveExpiredPersistedGrantsAsync(CancellationToken cancellationToken = default)
    {
        var found = Int32.MaxValue;

        while (found >= _options.TokenCleanupBatchSize)
        {
            var expiredGrants = await _persistedGrantDbContext.PersistedGrants
                                .Where(x => x.Expiration < DateTime.UtcNow)
                                .OrderBy(x => x.Expiration)
                                .Take(_options.TokenCleanupBatchSize)
                                .ToArrayAsync(cancellationToken);

            found = expiredGrants.Length;

            if (found > 0)
            {
                _logger.LogInformation("Removing {grantCount} expired grants", found);

                _persistedGrantDbContext.PersistedGrants.RemoveRange(expiredGrants);

                var list = await _persistedGrantDbContext.SaveChangesWithConcurrencyCheckAsync <Entities.PersistedGrant>(_logger, cancellationToken);

                expiredGrants = expiredGrants.Except(list).ToArray();

                if (_operationalStoreNotification != null)
                {
                    await _operationalStoreNotification.PersistedGrantsRemovedAsync(expiredGrants);
                }
            }
        }
    }
        /// <summary>
        /// Removes the expired persisted grants.
        /// </summary>
        /// <returns></returns>
        protected virtual async Task RemoveExpiredPersistedGrantsAsync()
        {
            var found = Int32.MaxValue;

            while (found >= _options.TokenCleanupBatchSize)
            {
                var expiredGrants = await _persistedGrantDbContext.PersistedGrants
                                    .Where(x => x.Expiration < DateTime.UtcNow)
                                    .OrderBy(x => x.Expiration)
                                    .Take(_options.TokenCleanupBatchSize)
                                    .ToArrayAsync();

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

                if (found > 0)
                {
                    _persistedGrantDbContext.PersistedGrants.RemoveRange(expiredGrants);
                    await SaveChangesAsync();

                    if (_operationalStoreNotification != null)
                    {
                        await _operationalStoreNotification.PersistedGrantsRemovedAsync(expiredGrants);
                    }
                }
            }
        }
        protected virtual async Task RemoveGrantsAsync()
        {
            int found = int.MaxValue;

            while (found >= _options.TokenCleanupBatchSize)
            {
                DocumentSnapshot[] expiredGrants = (await _persistedGrantDbContext.PersistedGrants
                                                    .WhereLessThan(nameof(PersistedGrant.Expiration), DateTime.UtcNow)
                                                    .Limit(_options.TokenCleanupBatchSize)
                                                    .GetSnapshotAsync()
                                                    .ConfigureAwait(false))
                                                   .ToArray();

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

                if (found <= 0)
                {
                    continue;
                }

                IEnumerable <PersistedGrant> removedGrants = expiredGrants.Select(x => x.ConvertTo <PersistedGrant>());

                foreach (DocumentSnapshot expiredGrant in expiredGrants)
                {
                    await expiredGrant.Reference.DeleteAsync(Precondition.None);
                }

                if (_operationalStoreNotification != null)
                {
                    await _operationalStoreNotification.PersistedGrantsRemovedAsync(removedGrants);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Removes the stale persisted grants.
        /// </summary>
        /// <returns></returns>
        protected virtual async Task RemoveGrantsAsync()
        {
            var           found = Int32.MaxValue;
            WriteBatch    batch;
            QuerySnapshot expiredGrants;

            while (found >= _options.TokenCleanupBatchSize)
            {
                expiredGrants = await _persistedGrantDbContext.PersistedGrants
                                .WhereLessThan("Expiration", DateTime.UtcNow)
                                .Limit(_options.TokenCleanupBatchSize)
                                .GetSnapshotAsync()
                                .ConfigureAwait(false);

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

                if (found > 0)
                {
                    batch = _persistedGrantDbContext.StartBatch();
                    foreach (var doc in expiredGrants)
                    {
                        batch.Delete(doc.Reference);
                    }
                    await batch.CommitAsync();

                    if (_operationalStoreNotification != null)
                    {
                        await _operationalStoreNotification.PersistedGrantsRemovedAsync(expiredGrants.Select(x => x.ConvertTo <Entities.PersistedGrant>()));
                    }
                }
            }
        }
Example #5
0
        /// <summary>
        /// Removes the stale persisted grants.
        /// </summary>
        /// <returns></returns>
        protected virtual async Task RemoveGrantsAsync()
        {
            var found = Int32.MaxValue;

            while (found >= _options.TokenCleanupBatchSize)
            {
                var expiredGrants = await _freeSql.Select <Entities.PersistedGrant>()
                                    .Where(x => x.Expiration < DateTime.UtcNow)
                                    .OrderBy(x => x.Key)
                                    .Take(_options.TokenCleanupBatchSize)
                                    .ToListAsync();

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

                if (found > 0)
                {
                    await _freeSql.Delete <Entities.PersistedGrant>(expiredGrants).ExecuteAffrowsAsync();

                    if (_operationalStoreNotification != null)
                    {
                        await _operationalStoreNotification.PersistedGrantsRemovedAsync(expiredGrants);
                    }
                }
            }
        }
        /// <summary>
        /// Removes the stale persisted grants.
        /// </summary>
        /// <returns></returns>
        protected virtual async Task RemoveGrantsAsync()
        {
            var found = Int32.MaxValue;

            while (found >= options.TokenCleanupBatchSize)
            {
                var expiredGrants = await unitOfWork.Query <XpoPersistedGrant>()
                                    .Where(x => x.Expiration < DateTime.UtcNow)
                                    .OrderBy(x => x.Key)
                                    .Take(options.TokenCleanupBatchSize)
                                    .ToArrayAsync();

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

                if (found > 0)
                {
                    await unitOfWork.DeleteAsync(expiredGrants);
                    await SaveChangesAsync();

                    if (operationalStoreNotification != null)
                    {
                        await operationalStoreNotification.PersistedGrantsRemovedAsync(expiredGrants);
                    }
                }
            }
        }
Example #7
0
        public async Task RemoveExpiredGrantsAsync()
        {
            try
            {
                var tenants = await _adminServices.GetAllTenantsAsync();

                foreach (var tenant in tenants)
                {
                    var context = GetTenantContext(tenant.Name);

                    var found = Int32.MaxValue;

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

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

                        if (found > 0)
                        {
                            context.PersistedGrants.RemoveRange(expiredGrants);
                            await context.SaveChangesAsync();

                            if (_operationalStoreNotification != null)
                            {
                                await _operationalStoreNotification.PersistedGrantsRemovedAsync(expiredGrants);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Exception removing expired grants: {exception}", ex.Message);
            }
        }
Example #8
0
        /// <summary>
        /// Removes the stale persisted grants.
        /// </summary>
        protected virtual async Task RemoveGrantsAsync()
        {
            const string deleteExpiredGrantsHql = "delete PersistedGrant pg where pg.ID in (:expiredGrantsIDs)";

            var expiredTokenFound = int.MaxValue;

            while (expiredTokenFound >= _options.TokenCleanupBatchSize)
            {
                using (var tx = _session.BeginTransaction())
                {
                    var expiredGrantsQuery = _session.QueryOver <PersistedGrant>()
                                             .Where(g => g.Expiration < DateTime.UtcNow)
                                             .OrderBy(g => g.Expiration).Asc
                                             .Take(_options.TokenCleanupBatchSize);

                    var expiredGrants = await expiredGrantsQuery.ListAsync();

                    var expiredGrantsIDs = expiredGrants.Select(pg => pg.ID).ToArray();
                    expiredTokenFound = expiredGrantsIDs.Length;

                    if (expiredTokenFound > 0)
                    {
                        _logger.LogInformation($"Removing {expiredTokenFound} expired grants");

                        await _session.CreateQuery(deleteExpiredGrantsHql)
                        .SetParameterList("expiredGrantsIDs", expiredGrantsIDs)
                        .ExecuteUpdateAsync();

                        await tx.CommitAsync();

                        if (_operationalStoreNotification != null)
                        {
                            await _operationalStoreNotification.PersistedGrantsRemovedAsync(expiredGrants);
                        }
                    }
                }
            }
        }
Example #9
0
        /// <summary>
        /// Removes the stale persisted grants.
        /// </summary>
        /// <returns></returns>
        protected virtual async Task RemoveGrantsAsync()
        {
            var found = Int32.MaxValue;

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

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

                if (found > 0)
                {
                    _persistedGrantDbContext.PersistedGrants.RemoveRange(expiredGrants);
                    try
                    {
                        await _persistedGrantDbContext.SaveChangesAsync();

                        if (_operationalStoreNotification != null)
                        {
                            await _operationalStoreNotification.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);
                    }
                }
            }
        }