16 changed files with 1486 additions and 772 deletions
@ -0,0 +1,399 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.ComponentModel; |
|||
using System.Linq; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using OpenIddict.Models; |
|||
|
|||
namespace OpenIddict.Core |
|||
{ |
|||
/// <summary>
|
|||
/// Provides methods allowing to manage the applications stored in a database.
|
|||
/// Note: this base class can only be used with the default OpenIddict entities.
|
|||
/// </summary>
|
|||
/// <typeparam name="TApplication">The type of the Application entity.</typeparam>
|
|||
/// <typeparam name="TAuthorization">The type of the Authorization entity.</typeparam>
|
|||
/// <typeparam name="TToken">The type of the Token entity.</typeparam>
|
|||
/// <typeparam name="TKey">The type of the entity primary keys.</typeparam>
|
|||
public abstract class OpenIddictApplicationStore<TApplication, TAuthorization, TToken, TKey> : IOpenIddictApplicationStore<TApplication> |
|||
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new() |
|||
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new() |
|||
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new() |
|||
where TKey : IEquatable<TKey> |
|||
{ |
|||
/// <summary>
|
|||
/// Creates a new application.
|
|||
/// </summary>
|
|||
/// <param name="application">The application to create.</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, whose result returns the application.
|
|||
/// </returns>
|
|||
public abstract Task<TApplication> CreateAsync([NotNull] TApplication application, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Creates a new application.
|
|||
/// </summary>
|
|||
/// <param name="descriptor">The application 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, whose result returns the application.
|
|||
/// </returns>
|
|||
public abstract Task<TApplication> CreateAsync([NotNull] OpenIddictApplicationDescriptor descriptor, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Removes an existing application.
|
|||
/// </summary>
|
|||
/// <param name="application">The application to delete.</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 abstract Task DeleteAsync([NotNull] TApplication application, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Retrieves an application using its unique identifier.
|
|||
/// </summary>
|
|||
/// <param name="identifier">The unique identifier associated with 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,
|
|||
/// whose result returns the client application corresponding to the identifier.
|
|||
/// </returns>
|
|||
public virtual Task<TApplication> FindByIdAsync(string identifier, CancellationToken cancellationToken) |
|||
{ |
|||
var key = ConvertIdentifierFromString(identifier); |
|||
|
|||
return GetAsync(applications => applications.Where(application => application.Id.Equals(key)), cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves an application using its client identifier.
|
|||
/// </summary>
|
|||
/// <param name="identifier">The client identifier associated with 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,
|
|||
/// whose result returns the client application corresponding to the identifier.
|
|||
/// </returns>
|
|||
public virtual Task<TApplication> FindByClientIdAsync(string identifier, CancellationToken cancellationToken) |
|||
{ |
|||
return GetAsync(applications => applications.Where(application => application.ClientId.Equals(identifier)), cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves all the applications associated with the specified post_logout_redirect_uri.
|
|||
/// </summary>
|
|||
/// <param name="address">The post_logout_redirect_uri associated with the applications.</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, whose result
|
|||
/// returns the client applications corresponding to the specified post_logout_redirect_uri.
|
|||
/// </returns>
|
|||
public virtual Task<TApplication[]> FindByLogoutRedirectUriAsync(string address, CancellationToken cancellationToken) |
|||
{ |
|||
return ListAsync(applications => applications.Where(application => application.LogoutRedirectUri == address), cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves all the applications associated with the specified redirect_uri.
|
|||
/// </summary>
|
|||
/// <param name="address">The redirect_uri associated with the applications.</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, whose result
|
|||
/// returns the client applications corresponding to the specified redirect_uri.
|
|||
/// </returns>
|
|||
public virtual Task<TApplication[]> FindByRedirectUriAsync(string address, CancellationToken cancellationToken) |
|||
{ |
|||
return ListAsync(applications => applications.Where(application => application.RedirectUri == address), cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Executes the specified query.
|
|||
/// </summary>
|
|||
/// <typeparam name="TResult">The result type.</typeparam>
|
|||
/// <param name="query">The query to execute.</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,
|
|||
/// whose result returns the single element returned when executing the specified query.
|
|||
/// </returns>
|
|||
public abstract Task<TResult> GetAsync<TResult>([NotNull] Func<IQueryable<TApplication>, IQueryable<TResult>> query, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the client identifier associated with an application.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the client identifier associated with the application.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetClientIdAsync([NotNull] TApplication application, CancellationToken cancellationToken) |
|||
{ |
|||
if (application == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(application)); |
|||
} |
|||
|
|||
return Task.FromResult(application.ClientId); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the client secret associated with an application.
|
|||
/// Note: depending on the manager used to create the application,
|
|||
/// the client secret may be hashed for security reasons.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the client secret associated with the application.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetClientSecretAsync([NotNull] TApplication application, CancellationToken cancellationToken) |
|||
{ |
|||
if (application == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(application)); |
|||
} |
|||
|
|||
return Task.FromResult(application.ClientSecret); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the client type associated with an application.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the client type of the application (by default, "public").
|
|||
/// </returns>
|
|||
public virtual Task<string> GetClientTypeAsync([NotNull] TApplication application, CancellationToken cancellationToken) |
|||
{ |
|||
if (application == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(application)); |
|||
} |
|||
|
|||
return Task.FromResult(application.Type); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the display name associated with an application.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the display name associated with the application.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetDisplayNameAsync([NotNull] TApplication application, CancellationToken cancellationToken) |
|||
{ |
|||
if (application == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(application)); |
|||
} |
|||
|
|||
return Task.FromResult(application.DisplayName); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the unique identifier associated with an application.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the unique identifier associated with the application.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetIdAsync([NotNull] TApplication application, CancellationToken cancellationToken) |
|||
{ |
|||
if (application == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(application)); |
|||
} |
|||
|
|||
return Task.FromResult(ConvertIdentifierToString(application.Id)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the logout callback address associated with an application.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the post_logout_redirect_uri associated with the application.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetLogoutRedirectUriAsync([NotNull] TApplication application, CancellationToken cancellationToken) |
|||
{ |
|||
if (application == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(application)); |
|||
} |
|||
|
|||
return Task.FromResult(application.LogoutRedirectUri); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the callback address associated with an application.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the redirect_uri associated with the application.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetRedirectUriAsync([NotNull] TApplication application, CancellationToken cancellationToken) |
|||
{ |
|||
if (application == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(application)); |
|||
} |
|||
|
|||
return Task.FromResult(application.RedirectUri); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the token identifiers associated with an application.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the tokens associated with the application.
|
|||
/// </returns>
|
|||
public virtual async Task<string[]> GetTokensAsync([NotNull] TApplication application, CancellationToken cancellationToken) |
|||
{ |
|||
if (application == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(application)); |
|||
} |
|||
|
|||
var tokens = new List<string>(); |
|||
|
|||
foreach (var identifier in await ListAsync(applications => |
|||
from entity in applications |
|||
where entity.Id.Equals(application.Id) |
|||
from token in entity.Tokens |
|||
select token.Id, cancellationToken)) |
|||
{ |
|||
tokens.Add(ConvertIdentifierToString(identifier)); |
|||
} |
|||
|
|||
return tokens.ToArray(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Executes the specified query.
|
|||
/// </summary>
|
|||
/// <typeparam name="TResult">The result type.</typeparam>
|
|||
/// <param name="query">The query to execute.</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,
|
|||
/// whose result returns all the elements returned when executing the specified query.
|
|||
/// </returns>
|
|||
public abstract Task<TResult[]> ListAsync<TResult>([NotNull] Func<IQueryable<TApplication>, IQueryable<TResult>> query, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Sets the client secret associated with an application.
|
|||
/// Note: depending on the manager used to create the application,
|
|||
/// the client secret may be hashed for security reasons.
|
|||
/// </summary>
|
|||
/// <param name="application">The application.</param>
|
|||
/// <param name="secret">The client secret associated with 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 Task SetClientSecretAsync([NotNull] TApplication application, |
|||
[CanBeNull] string secret, CancellationToken cancellationToken) |
|||
{ |
|||
if (application == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(application)); |
|||
} |
|||
|
|||
application.ClientSecret = secret; |
|||
|
|||
return Task.FromResult(0); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the client type associated with an application.
|
|||
/// </summary>
|
|||
/// <param name="application">The application.</param>
|
|||
/// <param name="type">The client type associated with 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 Task SetClientTypeAsync([NotNull] TApplication application, [NotNull] string type, CancellationToken cancellationToken) |
|||
{ |
|||
if (application == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(application)); |
|||
} |
|||
|
|||
if (string.IsNullOrEmpty(type)) |
|||
{ |
|||
throw new ArgumentException("The client type cannot be null or empty.", nameof(type)); |
|||
} |
|||
|
|||
application.Type = type; |
|||
|
|||
return Task.FromResult(0); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Updates an existing application.
|
|||
/// </summary>
|
|||
/// <param name="application">The application to update.</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 abstract Task UpdateAsync([NotNull] TApplication application, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Converts the provided identifier to a strongly typed key object.
|
|||
/// </summary>
|
|||
/// <param name="identifier">The identifier to convert.</param>
|
|||
/// <returns>An instance of <typeparamref name="TKey"/> representing the provided identifier.</returns>
|
|||
public virtual TKey ConvertIdentifierFromString([CanBeNull] string identifier) |
|||
{ |
|||
if (string.IsNullOrEmpty(identifier)) |
|||
{ |
|||
return default(TKey); |
|||
} |
|||
|
|||
return (TKey) TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(identifier); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the provided identifier to its string representation.
|
|||
/// </summary>
|
|||
/// <param name="identifier">The identifier to convert.</param>
|
|||
/// <returns>A <see cref="string"/> representation of the provided identifier.</returns>
|
|||
public virtual string ConvertIdentifierToString([CanBeNull] TKey identifier) |
|||
{ |
|||
if (Equals(identifier, default(TKey))) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return TypeDescriptor.GetConverter(typeof(TKey)).ConvertToInvariantString(identifier); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,220 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.ComponentModel; |
|||
using System.Linq; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using OpenIddict.Models; |
|||
|
|||
namespace OpenIddict.Core |
|||
{ |
|||
/// <summary>
|
|||
/// Provides methods allowing to manage the authorizations stored in a database.
|
|||
/// Note: this base class can only be used with the default OpenIddict entities.
|
|||
/// </summary>
|
|||
/// <typeparam name="TAuthorization">The type of the Authorization entity.</typeparam>
|
|||
/// <typeparam name="TApplication">The type of the Application entity.</typeparam>
|
|||
/// <typeparam name="TToken">The type of the Token entity.</typeparam>
|
|||
/// <typeparam name="TKey">The type of the entity primary keys.</typeparam>
|
|||
public abstract class OpenIddictAuthorizationStore<TAuthorization, TApplication, TToken, TKey> : IOpenIddictAuthorizationStore<TAuthorization> |
|||
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new() |
|||
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new() |
|||
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new() |
|||
where TKey : IEquatable<TKey> |
|||
{ |
|||
/// <summary>
|
|||
/// Creates a new authorization.
|
|||
/// </summary>
|
|||
/// <param name="authorization">The authorization to create.</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, whose result returns the authorization.
|
|||
/// </returns>
|
|||
public abstract Task<TAuthorization> CreateAsync([NotNull] TAuthorization authorization, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Creates a new authorization.
|
|||
/// </summary>
|
|||
/// <param name="descriptor">The authorization 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, whose result returns the authorization.
|
|||
/// </returns>
|
|||
public abstract Task<TAuthorization> CreateAsync([NotNull] OpenIddictAuthorizationDescriptor descriptor, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Retrieves an authorization using its associated subject/client.
|
|||
/// </summary>
|
|||
/// <param name="subject">The subject associated with the authorization.</param>
|
|||
/// <param name="client">The client associated with 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,
|
|||
/// whose result returns the authorization corresponding to the subject/client.
|
|||
/// </returns>
|
|||
public virtual Task<TAuthorization> FindAsync(string subject, string client, CancellationToken cancellationToken) |
|||
{ |
|||
var key = ConvertIdentifierFromString(client); |
|||
|
|||
return GetAsync(authorizations => |
|||
from authorization in authorizations |
|||
where authorization.Application.Id.Equals(key) |
|||
where authorization.Subject == subject |
|||
select authorization, cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves an authorization using its unique identifier.
|
|||
/// </summary>
|
|||
/// <param name="identifier">The unique identifier associated with 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,
|
|||
/// whose result returns the authorization corresponding to the identifier.
|
|||
/// </returns>
|
|||
public virtual Task<TAuthorization> FindByIdAsync(string identifier, CancellationToken cancellationToken) |
|||
{ |
|||
var key = ConvertIdentifierFromString(identifier); |
|||
|
|||
return GetAsync(authorizations => authorizations.Where(authorization => authorization.Id.Equals(key)), cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Executes the specified query.
|
|||
/// </summary>
|
|||
/// <typeparam name="TResult">The result type.</typeparam>
|
|||
/// <param name="query">The query to execute.</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,
|
|||
/// whose result returns the single element returned when executing the specified query.
|
|||
/// </returns>
|
|||
public abstract Task<TResult> GetAsync<TResult>([NotNull] Func<IQueryable<TAuthorization>, IQueryable<TResult>> query, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the unique identifier associated with an authorization.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the unique identifier associated with the authorization.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetIdAsync([NotNull] TAuthorization authorization, CancellationToken cancellationToken) |
|||
{ |
|||
if (authorization == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(authorization)); |
|||
} |
|||
|
|||
return Task.FromResult(ConvertIdentifierToString(authorization.Id)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the status associated with an authorization.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the status associated with the specified authorization.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetStatusAsync([NotNull] TAuthorization authorization, CancellationToken cancellationToken) |
|||
{ |
|||
return Task.FromResult(authorization.Status); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the subject associated with an authorization.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the subject associated with the specified authorization.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetSubjectAsync([NotNull] TAuthorization authorization, CancellationToken cancellationToken) |
|||
{ |
|||
if (authorization == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(authorization)); |
|||
} |
|||
|
|||
return Task.FromResult(authorization.Subject); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Executes the specified query.
|
|||
/// </summary>
|
|||
/// <typeparam name="TResult">The result type.</typeparam>
|
|||
/// <param name="query">The query to execute.</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,
|
|||
/// whose result returns all the elements returned when executing the specified query.
|
|||
/// </returns>
|
|||
public abstract Task<TResult[]> ListAsync<TResult>([NotNull] Func<IQueryable<TAuthorization>, IQueryable<TResult>> query, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Sets the status associated with an authorization.
|
|||
/// </summary>
|
|||
/// <param name="authorization">The authorization.</param>
|
|||
/// <param name="status">The status associated with 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 Task SetStatusAsync([NotNull] TAuthorization authorization, [NotNull] string status, CancellationToken cancellationToken) |
|||
{ |
|||
authorization.Status = status; |
|||
|
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Updates an existing authorization.
|
|||
/// </summary>
|
|||
/// <param name="authorization">The authorization to update.</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 abstract Task UpdateAsync([NotNull] TAuthorization authorization, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Converts the provided identifier to a strongly typed key object.
|
|||
/// </summary>
|
|||
/// <param name="identifier">The identifier to convert.</param>
|
|||
/// <returns>An instance of <typeparamref name="TKey"/> representing the provided identifier.</returns>
|
|||
public virtual TKey ConvertIdentifierFromString([CanBeNull] string identifier) |
|||
{ |
|||
if (string.IsNullOrEmpty(identifier)) |
|||
{ |
|||
return default(TKey); |
|||
} |
|||
|
|||
return (TKey) TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(identifier); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the provided identifier to its string representation.
|
|||
/// </summary>
|
|||
/// <param name="identifier">The identifier to convert.</param>
|
|||
/// <returns>A <see cref="string"/> representation of the provided identifier.</returns>
|
|||
public virtual string ConvertIdentifierToString([CanBeNull] TKey identifier) |
|||
{ |
|||
if (Equals(identifier, default(TKey))) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return TypeDescriptor.GetConverter(typeof(TKey)).ConvertToInvariantString(identifier); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,81 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.ComponentModel; |
|||
using System.Linq; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using OpenIddict.Models; |
|||
|
|||
namespace OpenIddict.Core |
|||
{ |
|||
/// <summary>
|
|||
/// Provides methods allowing to manage the scopes stored in a database.
|
|||
/// Note: this base class can only be used with the default OpenIddict entities.
|
|||
/// </summary>
|
|||
/// <typeparam name="TScope">The type of the Scope entity.</typeparam>
|
|||
/// <typeparam name="TKey">The type of the entity primary keys.</typeparam>
|
|||
public abstract class OpenIddictScopeStore<TScope, TKey> : IOpenIddictScopeStore<TScope> |
|||
where TScope : OpenIddictScope<TKey>, new() |
|||
where TKey : IEquatable<TKey> |
|||
{ |
|||
/// <summary>
|
|||
/// Executes the specified query.
|
|||
/// </summary>
|
|||
/// <typeparam name="TResult">The result type.</typeparam>
|
|||
/// <param name="query">The query to execute.</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,
|
|||
/// whose result returns the single element returned when executing the specified query.
|
|||
/// </returns>
|
|||
public abstract Task<TResult> GetAsync<TResult>([NotNull] Func<IQueryable<TScope>, IQueryable<TResult>> query, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Executes the specified query.
|
|||
/// </summary>
|
|||
/// <typeparam name="TResult">The result type.</typeparam>
|
|||
/// <param name="query">The query to execute.</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,
|
|||
/// whose result returns all the elements returned when executing the specified query.
|
|||
/// </returns>
|
|||
public abstract Task<TResult[]> ListAsync<TResult>([NotNull] Func<IQueryable<TScope>, IQueryable<TResult>> query, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Converts the provided identifier to a strongly typed key object.
|
|||
/// </summary>
|
|||
/// <param name="identifier">The identifier to convert.</param>
|
|||
/// <returns>An instance of <typeparamref name="TKey"/> representing the provided identifier.</returns>
|
|||
public virtual TKey ConvertIdentifierFromString([CanBeNull] string identifier) |
|||
{ |
|||
if (string.IsNullOrEmpty(identifier)) |
|||
{ |
|||
return default(TKey); |
|||
} |
|||
|
|||
return (TKey) TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(identifier); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the provided identifier to its string representation.
|
|||
/// </summary>
|
|||
/// <param name="identifier">The identifier to convert.</param>
|
|||
/// <returns>A <see cref="string"/> representation of the provided identifier.</returns>
|
|||
public virtual string ConvertIdentifierToString([CanBeNull] TKey identifier) |
|||
{ |
|||
if (Equals(identifier, default(TKey))) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return TypeDescriptor.GetConverter(typeof(TKey)).ConvertToInvariantString(identifier); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,419 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.ComponentModel; |
|||
using System.Linq; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using OpenIddict.Models; |
|||
|
|||
namespace OpenIddict.Core |
|||
{ |
|||
/// <summary>
|
|||
/// Provides methods allowing to manage the tokens stored in a database.
|
|||
/// Note: this base class can only be used with the default OpenIddict entities.
|
|||
/// </summary>
|
|||
/// <typeparam name="TToken">The type of the Token entity.</typeparam>
|
|||
/// <typeparam name="TApplication">The type of the Application entity.</typeparam>
|
|||
/// <typeparam name="TAuthorization">The type of the Authorization entity.</typeparam>
|
|||
/// <typeparam name="TKey">The type of the entity primary keys.</typeparam>
|
|||
public abstract class OpenIddictTokenStore<TToken, TApplication, TAuthorization, TKey> : IOpenIddictTokenStore<TToken> |
|||
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new() |
|||
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new() |
|||
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new() |
|||
where TKey : IEquatable<TKey> |
|||
{ |
|||
/// <summary>
|
|||
/// Creates a new token.
|
|||
/// </summary>
|
|||
/// <param name="token">The token to create.</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, whose result returns the token.
|
|||
/// </returns>
|
|||
public abstract Task<TToken> CreateAsync([NotNull] TToken token, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Creates a new token, which is associated with a particular subject.
|
|||
/// </summary>
|
|||
/// <param name="descriptor">The token 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, whose result returns the token.
|
|||
/// </returns>
|
|||
public abstract Task<TToken> CreateAsync([NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Removes a token.
|
|||
/// </summary>
|
|||
/// <param name="token">The token to delete.</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 abstract Task DeleteAsync([NotNull] TToken token, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the list of tokens corresponding to the specified authorization identifier.
|
|||
/// </summary>
|
|||
/// <param name="identifier">The authorization identifier associated with the tokens.</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,
|
|||
/// whose result returns the tokens corresponding to the specified authorization.
|
|||
/// </returns>
|
|||
public virtual Task<TToken[]> FindByAuthorizationIdAsync(string identifier, CancellationToken cancellationToken) |
|||
{ |
|||
var key = ConvertIdentifierFromString(identifier); |
|||
|
|||
return ListAsync(tokens => tokens.Where(token => token.Authorization.Id.Equals(key)), cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the list of tokens corresponding to the specified hash.
|
|||
/// </summary>
|
|||
/// <param name="hash">The hashed crypto-secure random identifier associated with the tokens.</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,
|
|||
/// whose result returns the tokens corresponding to the specified hash.
|
|||
/// </returns>
|
|||
public virtual Task<TToken> FindByHashAsync(string hash, CancellationToken cancellationToken) |
|||
{ |
|||
return GetAsync(tokens => tokens.Where(token => token.Hash == hash), cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves an token using its unique identifier.
|
|||
/// </summary>
|
|||
/// <param name="identifier">The unique identifier associated with 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,
|
|||
/// whose result returns the token corresponding to the unique identifier.
|
|||
/// </returns>
|
|||
public virtual Task<TToken> FindByIdAsync(string identifier, CancellationToken cancellationToken) |
|||
{ |
|||
var key = ConvertIdentifierFromString(identifier); |
|||
|
|||
return GetAsync(tokens => tokens.Where(token => token.Id.Equals(key)), cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the list of tokens corresponding to the specified subject.
|
|||
/// </summary>
|
|||
/// <param name="subject">The subject associated with the tokens.</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,
|
|||
/// whose result returns the tokens corresponding to the specified subject.
|
|||
/// </returns>
|
|||
public virtual Task<TToken[]> FindBySubjectAsync(string subject, CancellationToken cancellationToken) |
|||
{ |
|||
return ListAsync(tokens => tokens.Where(token => token.Subject == subject), cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Executes the specified query.
|
|||
/// </summary>
|
|||
/// <typeparam name="TResult">The result type.</typeparam>
|
|||
/// <param name="query">The query to execute.</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,
|
|||
/// whose result returns the single element returned when executing the specified query.
|
|||
/// </returns>
|
|||
public abstract Task<TResult> GetAsync<TResult>([NotNull] Func<IQueryable<TToken>, IQueryable<TResult>> query, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the optional authorization identifier associated with a token.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the authorization identifier associated with the token.
|
|||
/// </returns>
|
|||
public virtual async Task<string> GetAuthorizationIdAsync([NotNull] TToken token, CancellationToken cancellationToken) |
|||
{ |
|||
if (token == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(token)); |
|||
} |
|||
|
|||
if (token.Authorization != null) |
|||
{ |
|||
return ConvertIdentifierToString(token.Authorization.Id); |
|||
} |
|||
|
|||
var key = await GetAsync(tokens => |
|||
from element in tokens |
|||
where element.Id.Equals(token.Id) |
|||
select element.Authorization.Id, cancellationToken); |
|||
|
|||
return ConvertIdentifierToString(key); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the ciphertext associated with a token.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the ciphertext associated with the specified token.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetCiphertextAsync([NotNull] TToken token, CancellationToken cancellationToken) |
|||
{ |
|||
if (token == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(token)); |
|||
} |
|||
|
|||
return Task.FromResult(token.Ciphertext); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the creation date associated with a token.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the creation date associated with the specified token.
|
|||
/// </returns>
|
|||
public virtual Task<DateTimeOffset?> GetCreationDateAsync([NotNull] TToken token, CancellationToken cancellationToken) |
|||
{ |
|||
if (token == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(token)); |
|||
} |
|||
|
|||
return Task.FromResult(token.CreationDate); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the expiration date associated with a token.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the expiration date associated with the specified token.
|
|||
/// </returns>
|
|||
public virtual Task<DateTimeOffset?> GetExpirationDateAsync([NotNull] TToken token, CancellationToken cancellationToken) |
|||
{ |
|||
if (token == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(token)); |
|||
} |
|||
|
|||
return Task.FromResult(token.ExpirationDate); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the hashed identifier associated with a token.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the hashed identifier associated with the specified token.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetHashAsync([NotNull] TToken token, CancellationToken cancellationToken) |
|||
{ |
|||
if (token == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(token)); |
|||
} |
|||
|
|||
return Task.FromResult(token.Hash); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the unique identifier associated with a token.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the unique identifier associated with the token.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetIdAsync([NotNull] TToken token, CancellationToken cancellationToken) |
|||
{ |
|||
if (token == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(token)); |
|||
} |
|||
|
|||
return Task.FromResult(ConvertIdentifierToString(token.Id)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the status associated with a token.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the status associated with the specified token.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetStatusAsync([NotNull] TToken token, CancellationToken cancellationToken) |
|||
{ |
|||
if (token == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(token)); |
|||
} |
|||
|
|||
return Task.FromResult(token.Status); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the subject associated with a token.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the subject associated with the specified token.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetSubjectAsync([NotNull] TToken token, CancellationToken cancellationToken) |
|||
{ |
|||
if (token == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(token)); |
|||
} |
|||
|
|||
return Task.FromResult(token.Subject); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the token type associated with a token.
|
|||
/// </summary>
|
|||
/// <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,
|
|||
/// whose result returns the token type associated with the specified token.
|
|||
/// </returns>
|
|||
public virtual Task<string> GetTokenTypeAsync([NotNull] TToken token, CancellationToken cancellationToken) |
|||
{ |
|||
if (token == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(token)); |
|||
} |
|||
|
|||
return Task.FromResult(token.Type); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Executes the specified query.
|
|||
/// </summary>
|
|||
/// <typeparam name="TResult">The result type.</typeparam>
|
|||
/// <param name="query">The query to execute.</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,
|
|||
/// whose result returns all the elements returned when executing the specified query.
|
|||
/// </returns>
|
|||
public abstract Task<TResult[]> ListAsync<TResult>([NotNull] Func<IQueryable<TToken>, IQueryable<TResult>> query, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Sets the authorization associated with a token.
|
|||
/// </summary>
|
|||
/// <param name="token">The token.</param>
|
|||
/// <param name="identifier">The unique identifier associated with 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 abstract Task SetAuthorizationAsync([NotNull] TToken token, [CanBeNull] string identifier, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Sets the client application associated with a token.
|
|||
/// </summary>
|
|||
/// <param name="token">The token.</param>
|
|||
/// <param name="identifier">The unique identifier associated with the client 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 abstract Task SetClientAsync([NotNull] TToken token, [CanBeNull] string identifier, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Sets the expiration date associated with a token.
|
|||
/// </summary>
|
|||
/// <param name="token">The token.</param>
|
|||
/// <param name="date">The date on which the token will no longer be considered valid.</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 Task SetExpirationDateAsync([NotNull] TToken token, |
|||
[CanBeNull] DateTimeOffset? date, CancellationToken cancellationToken) |
|||
{ |
|||
token.ExpirationDate = date; |
|||
|
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the status associated with a token.
|
|||
/// </summary>
|
|||
/// <param name="token">The token.</param>
|
|||
/// <param name="status">The status associated with 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 Task SetStatusAsync([NotNull] TToken token, [NotNull] string status, CancellationToken cancellationToken) |
|||
{ |
|||
token.Status = status; |
|||
|
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Updates an existing token.
|
|||
/// </summary>
|
|||
/// <param name="token">The token to update.</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 abstract Task UpdateAsync([NotNull] TToken token, CancellationToken cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Converts the provided identifier to a strongly typed key object.
|
|||
/// </summary>
|
|||
/// <param name="identifier">The identifier to convert.</param>
|
|||
/// <returns>An instance of <typeparamref name="TKey"/> representing the provided identifier.</returns>
|
|||
public virtual TKey ConvertIdentifierFromString([CanBeNull] string identifier) |
|||
{ |
|||
if (string.IsNullOrEmpty(identifier)) |
|||
{ |
|||
return default(TKey); |
|||
} |
|||
|
|||
return (TKey) TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(identifier); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the provided identifier to its string representation.
|
|||
/// </summary>
|
|||
/// <param name="identifier">The identifier to convert.</param>
|
|||
/// <returns>A <see cref="string"/> representation of the provided identifier.</returns>
|
|||
public virtual string ConvertIdentifierToString([CanBeNull] TKey identifier) |
|||
{ |
|||
if (Equals(identifier, default(TKey))) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return TypeDescriptor.GetConverter(typeof(TKey)).ConvertToInvariantString(identifier); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue