Skip to content

Commit 6cb8af0

Browse files
committed
Update the query used for authorizations pruning to exclude all authorizations that still have tokens attached
1 parent 42d6369 commit 6cb8af0

File tree

6 files changed

+14
-18
lines changed

6 files changed

+14
-18
lines changed

src/OpenIddict.Abstractions/Managers/IOpenIddictAuthorizationManager.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,11 @@ IAsyncEnumerable<TResult> ListAsync<TState, TResult>(
347347
ValueTask PopulateAsync(object authorization, OpenIddictAuthorizationDescriptor descriptor, CancellationToken cancellationToken = default);
348348

349349
/// <summary>
350-
/// Removes the authorizations that are marked as invalid and the ad-hoc ones that have no token attached.
350+
/// Removes the authorizations that are marked as invalid and don't have any token attached.
351351
/// Only authorizations created before the specified <paramref name="threshold"/> are removed.
352352
/// </summary>
353353
/// <remarks>
354-
/// To ensure ad-hoc authorizations that no longer have any valid/non-expired token
355-
/// attached are correctly removed, the tokens should always be pruned first.
354+
/// Since authorizations with tokens still attached are not deleted, tokens should always be pruned first.
356355
/// </remarks>
357356
/// <param name="threshold">The date before which authorizations are not pruned.</param>
358357
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>

src/OpenIddict.Abstractions/Stores/IOpenIddictAuthorizationStore.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,11 @@ IAsyncEnumerable<TResult> ListAsync<TState, TResult>(
232232
TState state, CancellationToken cancellationToken);
233233

234234
/// <summary>
235-
/// Removes the authorizations that are marked as invalid and the ad-hoc ones that have no token attached.
235+
/// Removes the authorizations that are marked as invalid and don't have any token attached.
236236
/// Only authorizations created before the specified <paramref name="threshold"/> are removed.
237237
/// </summary>
238238
/// <remarks>
239-
/// To ensure ad-hoc authorizations that no longer have any valid/non-expired token
240-
/// attached are correctly removed, the tokens should always be pruned first.
239+
/// Since authorizations with tokens still attached are not deleted, tokens should always be pruned first.
241240
/// </remarks>
242241
/// <param name="threshold">The date before which authorizations are not pruned.</param>
243242
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>

src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,8 @@ public virtual async ValueTask<long> PruneAsync(DateTimeOffset threshold, Cancel
521521
var authorizations =
522522
await (from authorization in Authorizations.Include(authorization => authorization.Tokens)
523523
where authorization.CreationDate < date
524-
where authorization.Status != Statuses.Valid ||
525-
(authorization.Type == AuthorizationTypes.AdHoc && !authorization.Tokens.Any())
524+
where authorization.Status != Statuses.Valid || authorization.Type == AuthorizationTypes.AdHoc
525+
where !authorization.Tokens.Any()
526526
orderby authorization.Id
527527
select authorization).Take(1_000).ToListAsync(cancellationToken);
528528

src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,8 @@ public virtual async ValueTask<long> PruneAsync(DateTimeOffset threshold, Cancel
605605
var count = await
606606
(from authorization in Authorizations
607607
where authorization.CreationDate < date
608-
where authorization.Status != Statuses.Valid ||
609-
(authorization.Type == AuthorizationTypes.AdHoc && !authorization.Tokens.Any())
608+
where authorization.Status != Statuses.Valid || authorization.Type == AuthorizationTypes.AdHoc
609+
where !authorization.Tokens.Any()
610610
orderby authorization.Id
611611
select authorization).Take(1_000).ExecuteDeleteAsync(cancellationToken);
612612

@@ -643,8 +643,8 @@ orderby authorization.Id
643643
var authorizations = await
644644
(from authorization in Authorizations.Include(authorization => authorization.Tokens).AsTracking()
645645
where authorization.CreationDate < date
646-
where authorization.Status != Statuses.Valid ||
647-
(authorization.Type == AuthorizationTypes.AdHoc && !authorization.Tokens.Any())
646+
where authorization.Status != Statuses.Valid || authorization.Type == AuthorizationTypes.AdHoc
647+
where !authorization.Tokens.Any()
648648
orderby authorization.Id
649649
select authorization).Take(1_000).ToListAsync(cancellationToken);
650650

src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ public virtual async ValueTask<long> PruneAsync(DateTimeOffset threshold, Cancel
422422
join token in database.GetCollection<OpenIddictMongoDbToken>(Options.CurrentValue.TokensCollectionName).AsQueryable()
423423
on authorization.Id equals token.AuthorizationId into tokens
424424
where authorization.CreationDate < threshold.UtcDateTime
425-
where authorization.Status != Statuses.Valid ||
426-
(authorization.Type == AuthorizationTypes.AdHoc && !tokens.Any())
425+
where authorization.Status != Statuses.Valid || authorization.Type == AuthorizationTypes.AdHoc
426+
where !tokens.Any()
427427
select authorization.Id).ToListAsync(cancellationToken);
428428

429429
// Note: to avoid generating delete requests with very large filters, a buffer is used here and the

src/OpenIddict.Quartz/OpenIddictQuartzJob.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ public async Task Execute(IJobExecutionContext context)
5959

6060
try
6161
{
62-
// Note: this background task is responsible for automatically removing orphaned tokens/authorizations
63-
// (i.e tokens that are no longer valid and ad-hoc authorizations that have no valid tokens associated).
64-
// Import: since tokens associated to ad-hoc authorizations are not removed as part of the same operation,
65-
// the tokens MUST be deleted before removing the ad-hoc authorizations that no longer have any token.
62+
// Important: since authorizations that still have tokens attached are never
63+
// pruned, the tokens MUST be deleted before deleting the authorizations.
6664

6765
if (!_options.CurrentValue.DisableTokenPruning)
6866
{

0 commit comments

Comments
 (0)