From 4f57d16b66f4cd048e83df607bd34da6beed9194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Sun, 4 Feb 2018 20:58:37 +0100 Subject: [PATCH] Introduce new methods in the authorization manager --- .../OpenIddictAuthorizationManager.cs | 150 ++++++++++++++---- .../Stores/IOpenIddictAuthorizationStore.cs | 16 -- .../Stores/OpenIddictAuthorizationStore.cs | 40 ----- 3 files changed, 121 insertions(+), 85 deletions(-) diff --git a/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs b/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs index 8e1bc2cb..340a38da 100644 --- a/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs +++ b/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs @@ -191,35 +191,6 @@ namespace OpenIddict.Core return Store.FindAsync(subject, client, cancellationToken); } - /// - /// Retrieves the authorizations corresponding to the specified subject, associated with - /// the application identifier and for which the specified scopes have been granted. - /// - /// The subject associated with the authorization. - /// The client associated with the authorization. - /// The minimal scopes associated with the authorization. - /// The that can be used to abort the operation. - /// - /// A that can be used to monitor the asynchronous operation, whose result - /// returns the authorizations corresponding to the specified subject/client/scopes. - /// - public virtual Task> FindAsync( - [NotNull] string subject, [NotNull] string client, - ImmutableArray scopes, CancellationToken cancellationToken) - { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException("The subject cannot be null or empty.", nameof(subject)); - } - - if (string.IsNullOrEmpty(client)) - { - throw new ArgumentException("The client identifier cannot be null or empty.", nameof(client)); - } - - return Store.FindAsync(subject, client, scopes, cancellationToken); - } - /// /// Retrieves an authorization using its unique identifier. /// @@ -318,6 +289,25 @@ namespace OpenIddict.Core return Store.GetIdAsync(authorization, cancellationToken); } + /// + /// Retrieves the scopes associated with an authorization. + /// + /// The authorization. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation, + /// whose result returns the scopes associated with the specified authorization. + /// + public virtual Task> GetScopesAsync([NotNull] TAuthorization authorization, CancellationToken cancellationToken = default) + { + if (authorization == null) + { + throw new ArgumentNullException(nameof(authorization)); + } + + return Store.GetScopesAsync(authorization, cancellationToken); + } + /// /// Retrieves the status associated with an authorization. /// @@ -337,6 +327,108 @@ namespace OpenIddict.Core return Store.GetStatusAsync(authorization, cancellationToken); } + /// + /// Retrieves the subject associated with an authorization. + /// + /// The authorization. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation, + /// whose result returns the subject associated with the specified authorization. + /// + public virtual Task GetSubjectAsync([NotNull] TAuthorization authorization, CancellationToken cancellationToken = default) + { + if (authorization == null) + { + throw new ArgumentNullException(nameof(authorization)); + } + + return Store.GetSubjectAsync(authorization, cancellationToken); + } + + /// + /// Retrieves the type associated with an authorization. + /// + /// The authorization. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation, + /// whose result returns the type associated with the specified authorization. + /// + public virtual Task GetTypeAsync([NotNull] TAuthorization authorization, CancellationToken cancellationToken = default) + { + if (authorization == null) + { + throw new ArgumentNullException(nameof(authorization)); + } + + return Store.GetTypeAsync(authorization, cancellationToken); + } + + /// + /// Determines whether the specified scopes are included in the authorization. + /// + /// The authorization. + /// The scopes. + /// The that can be used to abort the operation. + /// true if the scopes are included in the authorization, false otherwise. + public virtual async Task HasScopesAsync([NotNull] TAuthorization authorization, + ImmutableArray scopes, CancellationToken cancellationToken = default) + { + if (authorization == null) + { + throw new ArgumentNullException(nameof(authorization)); + } + + return (await Store.GetScopesAsync(authorization, cancellationToken)) + .ToImmutableHashSet(StringComparer.Ordinal) + .IsSupersetOf(scopes); + } + + /// + /// Determines whether a given authorization is ad hoc. + /// + /// The authorization. + /// The that can be used to abort the operation. + /// true if the authorization is ad hoc, false otherwise. + public async Task IsAdHocAsync([NotNull] TAuthorization authorization, CancellationToken cancellationToken = default) + { + if (authorization == null) + { + throw new ArgumentNullException(nameof(authorization)); + } + + var type = await GetTypeAsync(authorization, cancellationToken); + if (string.IsNullOrEmpty(type)) + { + return false; + } + + return string.Equals(type, OpenIddictConstants.AuthorizationTypes.AdHoc, StringComparison.OrdinalIgnoreCase); + } + + /// + /// Determines whether a given authorization is permanent. + /// + /// The authorization. + /// The that can be used to abort the operation. + /// true if the authorization is permanent, false otherwise. + public async Task IsPermanentAsync([NotNull] TAuthorization authorization, CancellationToken cancellationToken = default) + { + if (authorization == null) + { + throw new ArgumentNullException(nameof(authorization)); + } + + var type = await GetTypeAsync(authorization, cancellationToken); + if (string.IsNullOrEmpty(type)) + { + return false; + } + + return string.Equals(type, OpenIddictConstants.AuthorizationTypes.Permanent, StringComparison.OrdinalIgnoreCase); + } + /// /// Determines whether a given authorization has been revoked. /// diff --git a/src/OpenIddict.Core/Stores/IOpenIddictAuthorizationStore.cs b/src/OpenIddict.Core/Stores/IOpenIddictAuthorizationStore.cs index 2f792847..7c97cac9 100644 --- a/src/OpenIddict.Core/Stores/IOpenIddictAuthorizationStore.cs +++ b/src/OpenIddict.Core/Stores/IOpenIddictAuthorizationStore.cs @@ -75,22 +75,6 @@ namespace OpenIddict.Core /// Task> FindAsync([NotNull] string subject, [NotNull] string client, CancellationToken cancellationToken); - /// - /// Retrieves the authorizations corresponding to the specified subject, associated with - /// the application identifier and for which the specified scopes have been granted. - /// - /// The subject associated with the authorization. - /// The client associated with the authorization. - /// The minimal scopes associated with the authorization. - /// The that can be used to abort the operation. - /// - /// A that can be used to monitor the asynchronous operation, whose result - /// returns the authorizations corresponding to the specified subject/client/scopes. - /// - Task> FindAsync( - [NotNull] string subject, [NotNull] string client, - ImmutableArray scopes, CancellationToken cancellationToken); - /// /// Retrieves an authorization using its unique identifier. /// diff --git a/src/OpenIddict.Core/Stores/OpenIddictAuthorizationStore.cs b/src/OpenIddict.Core/Stores/OpenIddictAuthorizationStore.cs index be3934c5..1b1e7fb2 100644 --- a/src/OpenIddict.Core/Stores/OpenIddictAuthorizationStore.cs +++ b/src/OpenIddict.Core/Stores/OpenIddictAuthorizationStore.cs @@ -112,46 +112,6 @@ namespace OpenIddict.Core (key: ConvertIdentifierFromString(client), principal: subject), cancellationToken); } - /// - /// Retrieves the authorizations corresponding to the specified subject, associated with - /// the application identifier and for which the specified scopes have been granted. - /// - /// The subject associated with the authorization. - /// The client associated with the authorization. - /// The minimal scopes associated with the authorization. - /// The that can be used to abort the operation. - /// - /// A that can be used to monitor the asynchronous operation, whose result - /// returns the authorizations corresponding to the specified subject/client/scopes. - /// - public virtual async Task> FindAsync( - [NotNull] string subject, [NotNull] string client, - ImmutableArray scopes, CancellationToken cancellationToken) - { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException("The subject cannot be null or empty.", nameof(subject)); - } - - if (string.IsNullOrEmpty(client)) - { - throw new ArgumentException("The client cannot be null or empty.", nameof(client)); - } - - var builder = ImmutableArray.CreateBuilder(); - - foreach (var authorization in await FindAsync(subject, client, cancellationToken)) - { - var set = new HashSet(await GetScopesAsync(authorization, cancellationToken), StringComparer.Ordinal); - if (set.IsSupersetOf(scopes)) - { - builder.Add(authorization); - } - } - - return builder.ToImmutable(); - } - /// /// Retrieves an authorization using its unique identifier. ///