Browse Source

Make PopulateAsync() public and provide an overload allowing to populate a descriptor

pull/596/head
Kévin Chalet 8 years ago
parent
commit
7fdc84a9b7
  1. 198
      src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs
  2. 110
      src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs
  3. 105
      src/OpenIddict.Core/Managers/OpenIddictScopeManager.cs
  4. 123
      src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs

198
src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs

@ -811,6 +811,108 @@ namespace OpenIddict.Core
return Store.ListAsync(query, state, cancellationToken);
}
/// <summary>
/// Populates the application using the specified descriptor.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="descriptor">The descriptor.</param>
/// <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 PopulateAsync([NotNull] TApplication application,
[NotNull] OpenIddictApplicationDescriptor descriptor, CancellationToken cancellationToken = default)
{
if (application == null)
{
throw new ArgumentNullException(nameof(application));
}
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
await Store.SetClientIdAsync(application, descriptor.ClientId, cancellationToken);
await Store.SetClientSecretAsync(application, descriptor.ClientSecret, cancellationToken);
await Store.SetClientTypeAsync(application, descriptor.Type, cancellationToken);
await Store.SetConsentTypeAsync(application, descriptor.ConsentType, cancellationToken);
await Store.SetDisplayNameAsync(application, descriptor.DisplayName, cancellationToken);
await Store.SetPermissionsAsync(application, ImmutableArray.CreateRange(descriptor.Permissions), cancellationToken);
await Store.SetPostLogoutRedirectUrisAsync(application, ImmutableArray.CreateRange(
descriptor.PostLogoutRedirectUris.Select(address => address.OriginalString)), cancellationToken);
await Store.SetRedirectUrisAsync(application, ImmutableArray.CreateRange(
descriptor.RedirectUris.Select(address => address.OriginalString)), cancellationToken);
}
/// <summary>
/// Populates the specified descriptor using the properties exposed by the application.
/// </summary>
/// <param name="descriptor">The descriptor.</param>
/// <param name="application">The application.</param>
/// <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 PopulateAsync(
[NotNull] OpenIddictApplicationDescriptor descriptor,
[NotNull] TApplication application, CancellationToken cancellationToken = default)
{
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
if (application == null)
{
throw new ArgumentNullException(nameof(application));
}
descriptor.ClientId = await Store.GetClientIdAsync(application, cancellationToken);
descriptor.ClientSecret = await Store.GetClientSecretAsync(application, cancellationToken);
descriptor.ConsentType = await Store.GetConsentTypeAsync(application, cancellationToken);
descriptor.DisplayName = await Store.GetDisplayNameAsync(application, cancellationToken);
descriptor.Type = await Store.GetClientTypeAsync(application, cancellationToken);
descriptor.Permissions.Clear();
descriptor.Permissions.UnionWith(await Store.GetPermissionsAsync(application, cancellationToken));
descriptor.PostLogoutRedirectUris.Clear();
descriptor.RedirectUris.Clear();
foreach (var address in await Store.GetPostLogoutRedirectUrisAsync(application, cancellationToken))
{
// Ensure the address is not null or empty.
if (string.IsNullOrEmpty(address))
{
throw new ArgumentException("Callback URLs cannot be null or empty.");
}
// Ensure the address is a valid absolute URL.
if (!Uri.TryCreate(address, UriKind.Absolute, out Uri uri) || !uri.IsWellFormedOriginalString())
{
throw new ArgumentException("Callback URLs must be valid absolute URLs.");
}
descriptor.PostLogoutRedirectUris.Add(uri);
}
foreach (var address in await Store.GetRedirectUrisAsync(application, cancellationToken))
{
// Ensure the address is not null or empty.
if (string.IsNullOrEmpty(address))
{
throw new ArgumentException("Callback URLs cannot be null or empty.");
}
// Ensure the address is a valid absolute URL.
if (!Uri.TryCreate(address, UriKind.Absolute, out Uri uri) || !uri.IsWellFormedOriginalString())
{
throw new ArgumentException("Callback URLs must be valid absolute URLs.");
}
descriptor.RedirectUris.Add(uri);
}
}
/// <summary>
/// Updates an existing application.
/// </summary>
@ -878,75 +980,33 @@ namespace OpenIddict.Core
/// Updates an existing application.
/// </summary>
/// <param name="application">The application to update.</param>
/// <param name="operation">The delegate used to update the application based on the given descriptor.</param>
/// <param name="descriptor">The descriptor used to update the application.</param>
/// <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 UpdateAsync([NotNull] TApplication application,
[NotNull] Func<OpenIddictApplicationDescriptor, Task> operation, CancellationToken cancellationToken = default)
[NotNull] OpenIddictApplicationDescriptor descriptor, CancellationToken cancellationToken = default)
{
if (operation == null)
{
throw new ArgumentNullException(nameof(operation));
}
// Store the original client secret for later comparison.
var secret = await Store.GetClientSecretAsync(application, cancellationToken);
var descriptor = new OpenIddictApplicationDescriptor
{
ClientId = await Store.GetClientIdAsync(application, cancellationToken),
ClientSecret = secret,
ConsentType = await Store.GetConsentTypeAsync(application, cancellationToken),
DisplayName = await Store.GetDisplayNameAsync(application, cancellationToken),
Type = await Store.GetClientTypeAsync(application, cancellationToken)
};
descriptor.Permissions.UnionWith(await Store.GetPermissionsAsync(application, cancellationToken));
foreach (var address in await Store.GetPostLogoutRedirectUrisAsync(application, cancellationToken))
if (application == null)
{
// Ensure the address is not null or empty.
if (string.IsNullOrEmpty(address))
{
throw new ArgumentException("Callback URLs cannot be null or empty.");
}
// Ensure the address is a valid absolute URL.
if (!Uri.TryCreate(address, UriKind.Absolute, out Uri uri) || !uri.IsWellFormedOriginalString())
{
throw new ArgumentException("Callback URLs must be valid absolute URLs.");
}
descriptor.PostLogoutRedirectUris.Add(uri);
throw new ArgumentNullException(nameof(application));
}
foreach (var address in await Store.GetRedirectUrisAsync(application, cancellationToken))
if (descriptor == null)
{
// Ensure the address is not null or empty.
if (string.IsNullOrEmpty(address))
{
throw new ArgumentException("Callback URLs cannot be null or empty.");
}
// Ensure the address is a valid absolute URL.
if (!Uri.TryCreate(address, UriKind.Absolute, out Uri uri) || !uri.IsWellFormedOriginalString())
{
throw new ArgumentException("Callback URLs must be valid absolute URLs.");
}
descriptor.RedirectUris.Add(uri);
throw new ArgumentNullException(nameof(descriptor));
}
await operation(descriptor);
// Store the original client secret for later comparison.
var comparand = await Store.GetClientSecretAsync(application, cancellationToken);
await PopulateAsync(application, descriptor, cancellationToken);
// If the client secret was updated, re-obfuscate it before persisting the changes.
var comparand = await Store.GetClientSecretAsync(application, cancellationToken);
var secret = await Store.GetClientSecretAsync(application, cancellationToken);
if (!string.Equals(secret, comparand, StringComparison.Ordinal))
{
await UpdateAsync(application, comparand, cancellationToken);
await UpdateAsync(application, secret, cancellationToken);
return;
}
@ -1233,40 +1293,6 @@ namespace OpenIddict.Core
return false;
}
/// <summary>
/// Populates the application using the specified descriptor.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="descriptor">The descriptor.</param>
/// <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>
protected virtual async Task PopulateAsync([NotNull] TApplication application,
[NotNull] OpenIddictApplicationDescriptor descriptor, CancellationToken cancellationToken = default)
{
if (application == null)
{
throw new ArgumentNullException(nameof(application));
}
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
await Store.SetClientIdAsync(application, descriptor.ClientId, cancellationToken);
await Store.SetClientSecretAsync(application, descriptor.ClientSecret, cancellationToken);
await Store.SetClientTypeAsync(application, descriptor.Type, cancellationToken);
await Store.SetConsentTypeAsync(application, descriptor.ConsentType, cancellationToken);
await Store.SetDisplayNameAsync(application, descriptor.DisplayName, cancellationToken);
await Store.SetPermissionsAsync(application, ImmutableArray.CreateRange(descriptor.Permissions), cancellationToken);
await Store.SetPostLogoutRedirectUrisAsync(application, ImmutableArray.CreateRange(
descriptor.PostLogoutRedirectUris.Select(address => address.OriginalString)), cancellationToken);
await Store.SetRedirectUrisAsync(application, ImmutableArray.CreateRange(
descriptor.RedirectUris.Select(address => address.OriginalString)), cancellationToken);
}
/// <summary>
/// Obfuscates the specified client secret so it can be safely stored in a database.
/// By default, this method returns a complex hashed representation computed using PBKDF2.

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

@ -803,6 +803,66 @@ namespace OpenIddict.Core
return Store.ListAsync(query, state, cancellationToken);
}
/// <summary>
/// Populates the authorization using the specified descriptor.
/// </summary>
/// <param name="authorization">The authorization.</param>
/// <param name="descriptor">The descriptor.</param>
/// <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 PopulateAsync([NotNull] TAuthorization authorization,
[NotNull] OpenIddictAuthorizationDescriptor descriptor, CancellationToken cancellationToken = default)
{
if (authorization == null)
{
throw new ArgumentNullException(nameof(authorization));
}
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
await Store.SetApplicationIdAsync(authorization, descriptor.ApplicationId, cancellationToken);
await Store.SetScopesAsync(authorization, ImmutableArray.CreateRange(descriptor.Scopes), cancellationToken);
await Store.SetStatusAsync(authorization, descriptor.Status, cancellationToken);
await Store.SetSubjectAsync(authorization, descriptor.Subject, cancellationToken);
await Store.SetTypeAsync(authorization, descriptor.Type, cancellationToken);
}
/// <summary>
/// Populates the specified descriptor using the properties exposed by the authorization.
/// </summary>
/// <param name="descriptor">The descriptor.</param>
/// <param name="authorization">The authorization.</param>
/// <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 PopulateAsync(
[NotNull] OpenIddictAuthorizationDescriptor descriptor,
[NotNull] TAuthorization authorization, CancellationToken cancellationToken = default)
{
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
if (authorization == null)
{
throw new ArgumentNullException(nameof(authorization));
}
descriptor.ApplicationId = await Store.GetApplicationIdAsync(authorization, cancellationToken);
descriptor.Scopes.Clear();
descriptor.Scopes.UnionWith(await Store.GetScopesAsync(authorization, cancellationToken));
descriptor.Status = await Store.GetStatusAsync(authorization, cancellationToken);
descriptor.Subject = await Store.GetSubjectAsync(authorization, cancellationToken);
descriptor.Type = await Store.GetTypeAsync(authorization, cancellationToken);
}
/// <summary>
/// Removes the ad-hoc authorizations that are marked as invalid or have no valid token attached.
/// </summary>
@ -890,33 +950,24 @@ namespace OpenIddict.Core
/// Updates an existing authorization.
/// </summary>
/// <param name="authorization">The authorization to update.</param>
/// <param name="operation">The delegate used to update the authorization based on the given descriptor.</param>
/// <param name="descriptor">The descriptor used to update the authorization.</param>
/// <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 UpdateAsync([NotNull] TAuthorization authorization,
[NotNull] Func<OpenIddictAuthorizationDescriptor, Task> operation, CancellationToken cancellationToken = default)
[NotNull] OpenIddictAuthorizationDescriptor descriptor, CancellationToken cancellationToken = default)
{
if (operation == null)
if (authorization == null)
{
throw new ArgumentNullException(nameof(operation));
throw new ArgumentNullException(nameof(authorization));
}
var descriptor = new OpenIddictAuthorizationDescriptor
{
ApplicationId = await Store.GetApplicationIdAsync(authorization, cancellationToken),
Status = await Store.GetStatusAsync(authorization, cancellationToken),
Subject = await Store.GetSubjectAsync(authorization, cancellationToken),
Type = await Store.GetTypeAsync(authorization, cancellationToken)
};
foreach (var scope in await Store.GetScopesAsync(authorization, cancellationToken))
if (descriptor == null)
{
descriptor.Scopes.Add(scope);
throw new ArgumentNullException(nameof(descriptor));
}
await operation(descriptor);
await PopulateAsync(authorization, descriptor, cancellationToken);
await UpdateAsync(authorization, cancellationToken);
}
@ -984,34 +1035,5 @@ namespace OpenIddict.Core
builder.MoveToImmutable() :
builder.ToImmutable();
}
/// <summary>
/// Populates the authorization using the specified descriptor.
/// </summary>
/// <param name="authorization">The authorization.</param>
/// <param name="descriptor">The descriptor.</param>
/// <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>
protected virtual async Task PopulateAsync([NotNull] TAuthorization authorization,
[NotNull] OpenIddictAuthorizationDescriptor descriptor, CancellationToken cancellationToken = default)
{
if (authorization == null)
{
throw new ArgumentNullException(nameof(authorization));
}
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
await Store.SetApplicationIdAsync(authorization, descriptor.ApplicationId, cancellationToken);
await Store.SetScopesAsync(authorization, ImmutableArray.CreateRange(descriptor.Scopes), cancellationToken);
await Store.SetStatusAsync(authorization, descriptor.Status, cancellationToken);
await Store.SetSubjectAsync(authorization, descriptor.Subject, cancellationToken);
await Store.SetTypeAsync(authorization, descriptor.Type, cancellationToken);
}
}
}

105
src/OpenIddict.Core/Managers/OpenIddictScopeManager.cs

@ -468,6 +468,64 @@ namespace OpenIddict.Core
return set.ToImmutableArray();
}
/// <summary>
/// Populates the scope using the specified descriptor.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="descriptor">The descriptor.</param>
/// <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 PopulateAsync([NotNull] TScope scope,
[NotNull] OpenIddictScopeDescriptor descriptor, CancellationToken cancellationToken = default)
{
if (scope == null)
{
throw new ArgumentNullException(nameof(scope));
}
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
await Store.SetDescriptionAsync(scope, descriptor.Description, cancellationToken);
await Store.SetDisplayNameAsync(scope, descriptor.DisplayName, cancellationToken);
await Store.SetNameAsync(scope, descriptor.Name, cancellationToken);
await Store.SetResourcesAsync(scope, descriptor.Resources.ToImmutableArray(), cancellationToken);
}
/// <summary>
/// Populates the specified descriptor using the properties exposed by the scope.
/// </summary>
/// <param name="descriptor">The descriptor.</param>
/// <param name="scope">The scope.</param>
/// <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 PopulateAsync(
[NotNull] OpenIddictScopeDescriptor descriptor,
[NotNull] TScope scope, CancellationToken cancellationToken = default)
{
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
if (scope == null)
{
throw new ArgumentNullException(nameof(scope));
}
descriptor.Description = await Store.GetDescriptionAsync(scope, cancellationToken);
descriptor.DisplayName = await Store.GetDisplayNameAsync(scope, cancellationToken);
descriptor.Name = await Store.GetNameAsync(scope, cancellationToken);
descriptor.Resources.Clear();
descriptor.Resources.UnionWith(await Store.GetResourcesAsync(scope, cancellationToken));
}
/// <summary>
/// Updates an existing scope.
/// </summary>
@ -496,29 +554,24 @@ namespace OpenIddict.Core
/// Updates an existing scope.
/// </summary>
/// <param name="scope">The scope to update.</param>
/// <param name="operation">The delegate used to update the scope based on the given descriptor.</param>
/// <param name="descriptor">The descriptor used to update the scope.</param>
/// <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 UpdateAsync([NotNull] TScope scope,
[NotNull] Func<OpenIddictScopeDescriptor, Task> operation, CancellationToken cancellationToken = default)
[NotNull] OpenIddictScopeDescriptor descriptor, CancellationToken cancellationToken = default)
{
if (operation == null)
if (scope == null)
{
throw new ArgumentNullException(nameof(operation));
throw new ArgumentNullException(nameof(scope));
}
var descriptor = new OpenIddictScopeDescriptor
if (descriptor == null)
{
Description = await Store.GetDescriptionAsync(scope, cancellationToken),
DisplayName = await Store.GetDisplayNameAsync(scope, cancellationToken),
Name = await Store.GetNameAsync(scope, cancellationToken)
};
descriptor.Resources.UnionWith(await Store.GetResourcesAsync(scope, cancellationToken));
throw new ArgumentNullException(nameof(descriptor));
}
await operation(descriptor);
await PopulateAsync(scope, descriptor, cancellationToken);
await UpdateAsync(scope, cancellationToken);
}
@ -574,33 +627,5 @@ namespace OpenIddict.Core
builder.MoveToImmutable() :
builder.ToImmutable();
}
/// <summary>
/// Populates the scope using the specified descriptor.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="descriptor">The descriptor.</param>
/// <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>
protected virtual async Task PopulateAsync([NotNull] TScope scope,
[NotNull] OpenIddictScopeDescriptor descriptor, CancellationToken cancellationToken = default)
{
if (scope == null)
{
throw new ArgumentNullException(nameof(scope));
}
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
await Store.SetDescriptionAsync(scope, descriptor.Description, cancellationToken);
await Store.SetDisplayNameAsync(scope, descriptor.DisplayName, cancellationToken);
await Store.SetNameAsync(scope, descriptor.Name, cancellationToken);
await Store.SetResourcesAsync(scope, descriptor.Resources.ToImmutableArray(), cancellationToken);
}
}
}

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

@ -693,6 +693,73 @@ namespace OpenIddict.Core
}
}
/// <summary>
/// Populates the token using the specified descriptor.
/// </summary>
/// <param name="token">The token.</param>
/// <param name="descriptor">The descriptor.</param>
/// <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 PopulateAsync([NotNull] TToken token,
[NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken = default)
{
if (token == null)
{
throw new ArgumentNullException(nameof(token));
}
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
await Store.SetApplicationIdAsync(token, descriptor.ApplicationId, cancellationToken);
await Store.SetAuthorizationIdAsync(token, descriptor.AuthorizationId, cancellationToken);
await Store.SetCreationDateAsync(token, descriptor.CreationDate, cancellationToken);
await Store.SetExpirationDateAsync(token, descriptor.ExpirationDate, cancellationToken);
await Store.SetPayloadAsync(token, descriptor.Payload, cancellationToken);
await Store.SetReferenceIdAsync(token, descriptor.ReferenceId, cancellationToken);
await Store.SetStatusAsync(token, descriptor.Status, cancellationToken);
await Store.SetSubjectAsync(token, descriptor.Subject, cancellationToken);
await Store.SetTokenTypeAsync(token, descriptor.Type, cancellationToken);
}
/// <summary>
/// Populates the specified descriptor using the properties exposed by the token.
/// </summary>
/// <param name="descriptor">The descriptor.</param>
/// <param name="token">The token.</param>
/// <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 PopulateAsync(
[NotNull] OpenIddictTokenDescriptor descriptor,
[NotNull] TToken token, CancellationToken cancellationToken = default)
{
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
if (token == null)
{
throw new ArgumentNullException(nameof(token));
}
descriptor.ApplicationId = await Store.GetApplicationIdAsync(token, cancellationToken);
descriptor.AuthorizationId = await Store.GetAuthorizationIdAsync(token, cancellationToken);
descriptor.CreationDate = await Store.GetCreationDateAsync(token, cancellationToken);
descriptor.ExpirationDate = await Store.GetExpirationDateAsync(token, cancellationToken);
descriptor.Payload = await Store.GetPayloadAsync(token, cancellationToken);
descriptor.ReferenceId = await Store.GetReferenceIdAsync(token, cancellationToken);
descriptor.Status = await Store.GetStatusAsync(token, cancellationToken);
descriptor.Subject = await Store.GetSubjectAsync(token, cancellationToken);
descriptor.Type = await Store.GetTokenTypeAsync(token, cancellationToken);
}
/// <summary>
/// Removes the tokens that are marked as expired or invalid.
/// </summary>
@ -815,33 +882,24 @@ namespace OpenIddict.Core
/// Updates an existing token.
/// </summary>
/// <param name="token">The token to update.</param>
/// <param name="operation">The delegate used to update the token based on the given descriptor.</param>
/// <param name="descriptor">The descriptor used to update the token.</param>
/// <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 UpdateAsync([NotNull] TToken token,
[NotNull] Func<OpenIddictTokenDescriptor, Task> operation, CancellationToken cancellationToken = default)
[NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken = default)
{
if (operation == null)
if (token == null)
{
throw new ArgumentNullException(nameof(operation));
throw new ArgumentNullException(nameof(token));
}
var descriptor = new OpenIddictTokenDescriptor
if (descriptor == null)
{
ApplicationId = await Store.GetApplicationIdAsync(token, cancellationToken),
AuthorizationId = await Store.GetAuthorizationIdAsync(token, cancellationToken),
CreationDate = await Store.GetCreationDateAsync(token, cancellationToken),
ExpirationDate = await Store.GetExpirationDateAsync(token, cancellationToken),
Payload = await Store.GetPayloadAsync(token, cancellationToken),
ReferenceId = await Store.GetReferenceIdAsync(token, cancellationToken),
Status = await Store.GetStatusAsync(token, cancellationToken),
Subject = await Store.GetSubjectAsync(token, cancellationToken),
Type = await Store.GetTokenTypeAsync(token, cancellationToken)
};
throw new ArgumentNullException(nameof(descriptor));
}
await operation(descriptor);
await PopulateAsync(token, descriptor, cancellationToken);
await UpdateAsync(token, cancellationToken);
}
@ -910,38 +968,5 @@ namespace OpenIddict.Core
builder.MoveToImmutable() :
builder.ToImmutable();
}
/// <summary>
/// Populates the token using the specified descriptor.
/// </summary>
/// <param name="token">The token.</param>
/// <param name="descriptor">The descriptor.</param>
/// <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>
protected virtual async Task PopulateAsync([NotNull] TToken token,
[NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken = default)
{
if (token == null)
{
throw new ArgumentNullException(nameof(token));
}
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
await Store.SetApplicationIdAsync(token, descriptor.ApplicationId, cancellationToken);
await Store.SetAuthorizationIdAsync(token, descriptor.AuthorizationId, cancellationToken);
await Store.SetCreationDateAsync(token, descriptor.CreationDate, cancellationToken);
await Store.SetExpirationDateAsync(token, descriptor.ExpirationDate, cancellationToken);
await Store.SetPayloadAsync(token, descriptor.Payload, cancellationToken);
await Store.SetReferenceIdAsync(token, descriptor.ReferenceId, cancellationToken);
await Store.SetStatusAsync(token, descriptor.Status, cancellationToken);
await Store.SetSubjectAsync(token, descriptor.Subject, cancellationToken);
await Store.SetTokenTypeAsync(token, descriptor.Type, cancellationToken);
}
}
}
Loading…
Cancel
Save