Browse Source

Introduce OpenIddictAuthorizationManager.PruneInvalidAsync() and OpenIddictTokenManager.PruneInvalidAsync()

pull/555/head
Kévin Chalet 8 years ago
parent
commit
418cbc9d3d
  1. 43
      src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs
  2. 43
      src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs

43
src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs

@ -531,6 +531,49 @@ namespace OpenIddict.Core
return Store.ListInvalidAsync(count, offset, cancellationToken);
}
/// <summary>
/// Removes the ad-hoc authorizations that are marked as invalid or have no valid token attached.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation.
/// </returns>
public virtual async Task PruneInvalidAsync(CancellationToken cancellationToken = default)
{
ImmutableArray<TAuthorization> authorizations;
do
{
// Note: don't use an offset here, as the elements returned by this method
// are progressively removed from the database immediately after calling it.
authorizations = await ListInvalidAsync(100, 0, cancellationToken);
foreach (var authorization in authorizations)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
await DeleteAsync(authorization, cancellationToken);
Logger.LogDebug("The authorization {AuthorizationId} was successfully removed from the database.",
await GetIdAsync(authorization, cancellationToken));
}
catch (Exception exception)
{
Logger.LogDebug(exception,
"An error occurred while removing the authorization {AuthorizationId} from the database.",
await GetIdAsync(authorization, cancellationToken));
}
}
cancellationToken.ThrowIfCancellationRequested();
}
while (!authorizations.IsDefaultOrEmpty);
}
/// <summary>
/// Revokes an authorization.
/// </summary>

43
src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs

@ -623,6 +623,49 @@ namespace OpenIddict.Core
}
}
/// <summary>
/// Removes the tokens that are marked as expired or invalid.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation.
/// </returns>
public virtual async Task PruneInvalidAsync(CancellationToken cancellationToken = default)
{
ImmutableArray<TToken> tokens;
do
{
// 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);
foreach (var token in tokens)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
await DeleteAsync(token, cancellationToken);
Logger.LogDebug("The token {TokenId} was successfully removed from the database.",
await GetIdAsync(token, cancellationToken));
}
catch (Exception exception)
{
Logger.LogDebug(exception,
"An error occurred while removing the token {TokenId} from the database.",
await GetIdAsync(token, cancellationToken));
}
}
cancellationToken.ThrowIfCancellationRequested();
}
while (!tokens.IsDefaultOrEmpty);
}
/// <summary>
/// Redeems a token.
/// </summary>

Loading…
Cancel
Save