Browse Source

Update OpenIddictAuthorizationManager/OpenIddictTokenManager.PruneInvalidAsync() to re-throw exceptions

pull/567/head
Kévin Chalet 8 years ago
parent
commit
176af6281a
  1. 50
      src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs
  2. 51
      src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs
  3. 2
      src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs
  4. 2
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs

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

@ -671,38 +671,48 @@ namespace OpenIddict.Core
/// </returns>
public virtual async Task PruneInvalidAsync(CancellationToken cancellationToken = default)
{
ImmutableArray<TAuthorization> authorizations;
IList<Exception> exceptions = null;
var authorizations = new List<TAuthorization>();
do
// First, start retrieving the invalid authorizations 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.
authorizations = await ListInvalidAsync(100, 0, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
foreach (var authorization in authorizations)
var results = await ListAsync(100, offset, cancellationToken);
if (results.IsEmpty)
{
cancellationToken.ThrowIfCancellationRequested();
break;
}
try
{
await DeleteAsync(authorization, cancellationToken);
authorizations.AddRange(results);
}
Logger.LogDebug("The authorization {AuthorizationId} was successfully removed from the database.",
await GetIdAsync(authorization, cancellationToken));
}
// Then, remove the invalid authorizations one by one.
foreach (var authorization in authorizations)
{
cancellationToken.ThrowIfCancellationRequested();
catch (Exception exception)
try
{
await DeleteAsync(authorization, cancellationToken);
}
catch (Exception exception)
{
if (exceptions == null)
{
Logger.LogDebug(exception,
"An error occurred while removing the authorization {AuthorizationId} from the database.",
await GetIdAsync(authorization, cancellationToken));
exceptions = new List<Exception>(capacity: 1);
}
}
cancellationToken.ThrowIfCancellationRequested();
exceptions.Add(exception);
}
}
while (!authorizations.IsDefaultOrEmpty);
if (exceptions != null)
{
throw new AggregateException("An error occurred while pruning authorizations.", exceptions);
}
}
/// <summary>

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

@ -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>

2
src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs

@ -163,7 +163,7 @@ namespace OpenIddict.EntityFramework
Task<List<TToken>> ListTokensAsync()
=> (from token in Tokens
where token.Application.Id.Equals(authorization.Id)
where token.Authorization.Id.Equals(authorization.Id)
select token).ToListAsync(cancellationToken);
// Remove all the tokens associated with the application.

2
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs

@ -163,7 +163,7 @@ namespace OpenIddict.EntityFrameworkCore
Task<List<TToken>> ListTokensAsync()
=> (from token in Tokens
where token.Application.Id.Equals(authorization.Id)
where token.Authorization.Id.Equals(authorization.Id)
select token).ToListAsync(cancellationToken);
// Remove all the tokens associated with the application.

Loading…
Cancel
Save