|
|
|
@ -5,6 +5,7 @@ |
|
|
|
*/ |
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Collections.Immutable; |
|
|
|
using System.ComponentModel.DataAnnotations; |
|
|
|
using System.Linq; |
|
|
|
@ -638,38 +639,48 @@ namespace OpenIddict.Core |
|
|
|
/// </returns>
|
|
|
|
public virtual async Task PruneInvalidAsync(CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
ImmutableArray<TToken> tokens; |
|
|
|
IList<Exception> exceptions = null; |
|
|
|
var tokens = new List<TToken>(); |
|
|
|
|
|
|
|
do |
|
|
|
// First, start retrieving the invalid tokens from the database.
|
|
|
|
for (var offset = 0; offset < 10_000; offset = offset + 100) |
|
|
|
{ |
|
|
|
// Note: don't use an offset here, as the elements returned by this method
|
|
|
|
// are progressively removed from the database immediately after calling it.
|
|
|
|
tokens = await ListInvalidAsync(100, 0, cancellationToken); |
|
|
|
cancellationToken.ThrowIfCancellationRequested(); |
|
|
|
|
|
|
|
foreach (var token in tokens) |
|
|
|
var results = await ListInvalidAsync(100, offset, cancellationToken); |
|
|
|
if (results.IsEmpty) |
|
|
|
{ |
|
|
|
cancellationToken.ThrowIfCancellationRequested(); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
await DeleteAsync(token, cancellationToken); |
|
|
|
tokens.AddRange(results); |
|
|
|
} |
|
|
|
|
|
|
|
Logger.LogDebug("The token {TokenId} was successfully removed from the database.", |
|
|
|
await GetIdAsync(token, cancellationToken)); |
|
|
|
} |
|
|
|
// Then, remove the invalid tokens one by one.
|
|
|
|
foreach (var token in tokens) |
|
|
|
{ |
|
|
|
cancellationToken.ThrowIfCancellationRequested(); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
await DeleteAsync(token, cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
catch (Exception exception) |
|
|
|
catch (Exception exception) |
|
|
|
{ |
|
|
|
if (exceptions == null) |
|
|
|
{ |
|
|
|
Logger.LogDebug(exception, |
|
|
|
"An error occurred while removing the token {TokenId} from the database.", |
|
|
|
await GetIdAsync(token, cancellationToken)); |
|
|
|
exceptions = new List<Exception>(capacity: 1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested(); |
|
|
|
exceptions.Add(exception); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
while (!tokens.IsDefaultOrEmpty); |
|
|
|
if (exceptions != null) |
|
|
|
{ |
|
|
|
throw new AggregateException("An error occurred while pruning tokens.", exceptions); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|