diff --git a/src/OpenIddict.Core/Descriptors/OpenIddictAuthorizationDescriptor.cs b/src/OpenIddict.Core/Descriptors/OpenIddictAuthorizationDescriptor.cs index 927661c8..a9338799 100644 --- a/src/OpenIddict.Core/Descriptors/OpenIddictAuthorizationDescriptor.cs +++ b/src/OpenIddict.Core/Descriptors/OpenIddictAuthorizationDescriptor.cs @@ -17,6 +17,11 @@ namespace OpenIddict.Core /// public IEnumerable Scopes { get; set; } + /// + /// Gets or sets the status associated with the authorization. + /// + public string Status { get; set; } + /// /// Gets or sets the subject associated with the authorization. /// diff --git a/src/OpenIddict.Core/Descriptors/OpenIddictTokenDescriptor.cs b/src/OpenIddict.Core/Descriptors/OpenIddictTokenDescriptor.cs index 724935f5..258713a9 100644 --- a/src/OpenIddict.Core/Descriptors/OpenIddictTokenDescriptor.cs +++ b/src/OpenIddict.Core/Descriptors/OpenIddictTokenDescriptor.cs @@ -37,6 +37,11 @@ namespace OpenIddict.Core /// public string Hash { get; set; } + /// + /// Gets or sets the status associated with the token. + /// + public string Status { get; set; } + /// /// Gets or sets the subject associated with the token. /// diff --git a/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs b/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs index 44737b82..34f4c18f 100644 --- a/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs +++ b/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs @@ -221,6 +221,7 @@ namespace OpenIddict.Core var descriptor = new OpenIddictAuthorizationDescriptor { + Status = await Store.GetStatusAsync(authorization, cancellationToken), Subject = await Store.GetSubjectAsync(authorization, cancellationToken) }; @@ -242,6 +243,11 @@ namespace OpenIddict.Core throw new ArgumentNullException(nameof(descriptor)); } + if (string.IsNullOrEmpty(descriptor.Status)) + { + throw new ArgumentException("The status cannot be null or empty."); + } + if (string.IsNullOrEmpty(descriptor.Subject)) { throw new ArgumentException("The subject cannot be null or empty."); diff --git a/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs b/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs index 9774e570..f6bdb666 100644 --- a/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs +++ b/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs @@ -65,14 +65,15 @@ namespace OpenIddict.Core /// /// A that can be used to monitor the asynchronous operation, whose result returns the token. /// - public virtual Task CreateAsync([NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken) + public virtual async Task CreateAsync([NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken) { if (descriptor == null) { throw new ArgumentNullException(nameof(descriptor)); } - return Store.CreateAsync(descriptor, cancellationToken); + await ValidateAsync(descriptor, cancellationToken); + return await Store.CreateAsync(descriptor, cancellationToken); } /// @@ -505,22 +506,53 @@ namespace OpenIddict.Core throw new ArgumentNullException(nameof(token)); } - var type = await Store.GetTokenTypeAsync(token, cancellationToken); - if (string.IsNullOrEmpty(type)) + var descriptor = new OpenIddictTokenDescriptor { - throw new ArgumentException("The token type cannot be null or empty.", nameof(token)); + Status = await Store.GetStatusAsync(token, cancellationToken), + Subject = await Store.GetSubjectAsync(token, cancellationToken), + Type = await Store.GetTokenTypeAsync(token, cancellationToken) + }; + + await ValidateAsync(descriptor, cancellationToken); + } + + /// + /// Validates the token descriptor to ensure it's in a consistent state. + /// + /// The token descriptor. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation. + /// + protected virtual Task ValidateAsync([NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken) + { + if (descriptor == null) + { + throw new ArgumentNullException(nameof(descriptor)); + } + + if (string.IsNullOrEmpty(descriptor.Type)) + { + throw new ArgumentException("The token type cannot be null or empty.", nameof(descriptor)); } - if (!string.Equals(type, OpenIdConnectConstants.TokenTypeHints.AuthorizationCode, StringComparison.OrdinalIgnoreCase) && - !string.Equals(type, OpenIdConnectConstants.TokenTypeHints.RefreshToken, StringComparison.OrdinalIgnoreCase)) + if (!string.Equals(descriptor.Type, OpenIdConnectConstants.TokenTypeHints.AuthorizationCode, StringComparison.OrdinalIgnoreCase) && + !string.Equals(descriptor.Type, OpenIdConnectConstants.TokenTypeHints.RefreshToken, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException("The specified token type is not supported by the default token manager."); } - if (string.IsNullOrEmpty(await Store.GetSubjectAsync(token, cancellationToken))) + if (string.IsNullOrEmpty(descriptor.Status)) + { + throw new ArgumentException("The status cannot be null or empty."); + } + + if (string.IsNullOrEmpty(descriptor.Subject)) { throw new ArgumentException("The subject cannot be null or empty."); } + + return Task.CompletedTask; } } } \ No newline at end of file diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs index af65eca1..cfdadabf 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs @@ -126,6 +126,7 @@ namespace OpenIddict.EntityFramework var authorization = new TAuthorization { Scope = string.Join(" ", descriptor.Scopes), + Status = descriptor.Status, Subject = descriptor.Subject }; diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs index 886200b7..3443c103 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs @@ -134,6 +134,7 @@ namespace OpenIddict.EntityFramework CreationDate = descriptor.CreationDate, ExpirationDate = descriptor.ExpirationDate, Hash = descriptor.Hash, + Status = descriptor.Status, Subject = descriptor.Subject, Type = descriptor.Type }; diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs index 6a1b7c83..bf938412 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs @@ -125,6 +125,7 @@ namespace OpenIddict.EntityFrameworkCore var authorization = new TAuthorization { Scope = string.Join(" ", descriptor.Scopes), + Status = descriptor.Status, Subject = descriptor.Subject }; diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs index 39515293..516d784f 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs @@ -133,6 +133,7 @@ namespace OpenIddict.EntityFrameworkCore CreationDate = descriptor.CreationDate, ExpirationDate = descriptor.ExpirationDate, Hash = descriptor.Hash, + Status = descriptor.Status, Subject = descriptor.Subject, Type = descriptor.Type }; diff --git a/src/OpenIddict.Models/OpenIddictAuthorization.cs b/src/OpenIddict.Models/OpenIddictAuthorization.cs index 2da90c02..2ff98b8e 100644 --- a/src/OpenIddict.Models/OpenIddictAuthorization.cs +++ b/src/OpenIddict.Models/OpenIddictAuthorization.cs @@ -53,7 +53,7 @@ namespace OpenIddict.Models /// /// Gets or sets the status of the current authorization. /// - public virtual string Status { get; set; } = "valid"; + public virtual string Status { get; set; } /// /// Gets or sets the subject associated with the current authorization. diff --git a/src/OpenIddict.Models/OpenIddictToken.cs b/src/OpenIddict.Models/OpenIddictToken.cs index aeef2b6a..bf2026e7 100644 --- a/src/OpenIddict.Models/OpenIddictToken.cs +++ b/src/OpenIddict.Models/OpenIddictToken.cs @@ -78,7 +78,7 @@ namespace OpenIddict.Models /// /// Gets or sets the status of the current token. /// - public virtual string Status { get; set; } = "valid"; + public virtual string Status { get; set; } /// /// Gets or sets the subject associated with the current token. diff --git a/src/OpenIddict/OpenIddictProvider.Serialization.cs b/src/OpenIddict/OpenIddictProvider.Serialization.cs index a9ca6009..60e7ba89 100644 --- a/src/OpenIddict/OpenIddictProvider.Serialization.cs +++ b/src/OpenIddict/OpenIddictProvider.Serialization.cs @@ -184,6 +184,7 @@ namespace OpenIddict { CreationDate = ticket.Properties.IssuedUtc, ExpirationDate = ticket.Properties.ExpiresUtc, + Status = OpenIddictConstants.Statuses.Valid, Subject = ticket.Principal.GetClaim(OpenIdConnectConstants.Claims.Subject), Type = type }; @@ -259,6 +260,7 @@ namespace OpenIddict { ApplicationId = descriptor.ApplicationId, Scopes = request.GetScopes(), + Status = OpenIddictConstants.Statuses.Valid, Subject = descriptor.Subject }, context.RequestAborted);