Browse Source

Allow the authorization/token status to be directly set on the descriptor

pull/474/head
Kévin Chalet 9 years ago
parent
commit
e88a1cb2ba
  1. 5
      src/OpenIddict.Core/Descriptors/OpenIddictAuthorizationDescriptor.cs
  2. 5
      src/OpenIddict.Core/Descriptors/OpenIddictTokenDescriptor.cs
  3. 6
      src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs
  4. 48
      src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs
  5. 1
      src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs
  6. 1
      src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs
  7. 1
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs
  8. 1
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs
  9. 2
      src/OpenIddict.Models/OpenIddictAuthorization.cs
  10. 2
      src/OpenIddict.Models/OpenIddictToken.cs
  11. 2
      src/OpenIddict/OpenIddictProvider.Serialization.cs

5
src/OpenIddict.Core/Descriptors/OpenIddictAuthorizationDescriptor.cs

@ -17,6 +17,11 @@ namespace OpenIddict.Core
/// </summary> /// </summary>
public IEnumerable<string> Scopes { get; set; } public IEnumerable<string> Scopes { get; set; }
/// <summary>
/// Gets or sets the status associated with the authorization.
/// </summary>
public string Status { get; set; }
/// <summary> /// <summary>
/// Gets or sets the subject associated with the authorization. /// Gets or sets the subject associated with the authorization.
/// </summary> /// </summary>

5
src/OpenIddict.Core/Descriptors/OpenIddictTokenDescriptor.cs

@ -37,6 +37,11 @@ namespace OpenIddict.Core
/// </summary> /// </summary>
public string Hash { get; set; } public string Hash { get; set; }
/// <summary>
/// Gets or sets the status associated with the token.
/// </summary>
public string Status { get; set; }
/// <summary> /// <summary>
/// Gets or sets the subject associated with the token. /// Gets or sets the subject associated with the token.
/// </summary> /// </summary>

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

@ -221,6 +221,7 @@ namespace OpenIddict.Core
var descriptor = new OpenIddictAuthorizationDescriptor var descriptor = new OpenIddictAuthorizationDescriptor
{ {
Status = await Store.GetStatusAsync(authorization, cancellationToken),
Subject = await Store.GetSubjectAsync(authorization, cancellationToken) Subject = await Store.GetSubjectAsync(authorization, cancellationToken)
}; };
@ -242,6 +243,11 @@ namespace OpenIddict.Core
throw new ArgumentNullException(nameof(descriptor)); 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)) if (string.IsNullOrEmpty(descriptor.Subject))
{ {
throw new ArgumentException("The subject cannot be null or empty."); throw new ArgumentException("The subject cannot be null or empty.");

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

@ -65,14 +65,15 @@ namespace OpenIddict.Core
/// <returns> /// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation, whose result returns the token. /// A <see cref="Task"/> that can be used to monitor the asynchronous operation, whose result returns the token.
/// </returns> /// </returns>
public virtual Task<TToken> CreateAsync([NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken) public virtual async Task<TToken> CreateAsync([NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken)
{ {
if (descriptor == null) if (descriptor == null)
{ {
throw new ArgumentNullException(nameof(descriptor)); throw new ArgumentNullException(nameof(descriptor));
} }
return Store.CreateAsync(descriptor, cancellationToken); await ValidateAsync(descriptor, cancellationToken);
return await Store.CreateAsync(descriptor, cancellationToken);
} }
/// <summary> /// <summary>
@ -505,22 +506,53 @@ namespace OpenIddict.Core
throw new ArgumentNullException(nameof(token)); throw new ArgumentNullException(nameof(token));
} }
var type = await Store.GetTokenTypeAsync(token, cancellationToken); var descriptor = new OpenIddictTokenDescriptor
if (string.IsNullOrEmpty(type))
{ {
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);
}
/// <summary>
/// Validates the token descriptor to ensure it's in a consistent state.
/// </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.
/// </returns>
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) && if (!string.Equals(descriptor.Type, OpenIdConnectConstants.TokenTypeHints.AuthorizationCode, StringComparison.OrdinalIgnoreCase) &&
!string.Equals(type, OpenIdConnectConstants.TokenTypeHints.RefreshToken, 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."); 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."); throw new ArgumentException("The subject cannot be null or empty.");
} }
return Task.CompletedTask;
} }
} }
} }

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

@ -126,6 +126,7 @@ namespace OpenIddict.EntityFramework
var authorization = new TAuthorization var authorization = new TAuthorization
{ {
Scope = string.Join(" ", descriptor.Scopes), Scope = string.Join(" ", descriptor.Scopes),
Status = descriptor.Status,
Subject = descriptor.Subject Subject = descriptor.Subject
}; };

1
src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs

@ -134,6 +134,7 @@ namespace OpenIddict.EntityFramework
CreationDate = descriptor.CreationDate, CreationDate = descriptor.CreationDate,
ExpirationDate = descriptor.ExpirationDate, ExpirationDate = descriptor.ExpirationDate,
Hash = descriptor.Hash, Hash = descriptor.Hash,
Status = descriptor.Status,
Subject = descriptor.Subject, Subject = descriptor.Subject,
Type = descriptor.Type Type = descriptor.Type
}; };

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

@ -125,6 +125,7 @@ namespace OpenIddict.EntityFrameworkCore
var authorization = new TAuthorization var authorization = new TAuthorization
{ {
Scope = string.Join(" ", descriptor.Scopes), Scope = string.Join(" ", descriptor.Scopes),
Status = descriptor.Status,
Subject = descriptor.Subject Subject = descriptor.Subject
}; };

1
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs

@ -133,6 +133,7 @@ namespace OpenIddict.EntityFrameworkCore
CreationDate = descriptor.CreationDate, CreationDate = descriptor.CreationDate,
ExpirationDate = descriptor.ExpirationDate, ExpirationDate = descriptor.ExpirationDate,
Hash = descriptor.Hash, Hash = descriptor.Hash,
Status = descriptor.Status,
Subject = descriptor.Subject, Subject = descriptor.Subject,
Type = descriptor.Type Type = descriptor.Type
}; };

2
src/OpenIddict.Models/OpenIddictAuthorization.cs

@ -53,7 +53,7 @@ namespace OpenIddict.Models
/// <summary> /// <summary>
/// Gets or sets the status of the current authorization. /// Gets or sets the status of the current authorization.
/// </summary> /// </summary>
public virtual string Status { get; set; } = "valid"; public virtual string Status { get; set; }
/// <summary> /// <summary>
/// Gets or sets the subject associated with the current authorization. /// Gets or sets the subject associated with the current authorization.

2
src/OpenIddict.Models/OpenIddictToken.cs

@ -78,7 +78,7 @@ namespace OpenIddict.Models
/// <summary> /// <summary>
/// Gets or sets the status of the current token. /// Gets or sets the status of the current token.
/// </summary> /// </summary>
public virtual string Status { get; set; } = "valid"; public virtual string Status { get; set; }
/// <summary> /// <summary>
/// Gets or sets the subject associated with the current token. /// Gets or sets the subject associated with the current token.

2
src/OpenIddict/OpenIddictProvider.Serialization.cs

@ -184,6 +184,7 @@ namespace OpenIddict
{ {
CreationDate = ticket.Properties.IssuedUtc, CreationDate = ticket.Properties.IssuedUtc,
ExpirationDate = ticket.Properties.ExpiresUtc, ExpirationDate = ticket.Properties.ExpiresUtc,
Status = OpenIddictConstants.Statuses.Valid,
Subject = ticket.Principal.GetClaim(OpenIdConnectConstants.Claims.Subject), Subject = ticket.Principal.GetClaim(OpenIdConnectConstants.Claims.Subject),
Type = type Type = type
}; };
@ -259,6 +260,7 @@ namespace OpenIddict
{ {
ApplicationId = descriptor.ApplicationId, ApplicationId = descriptor.ApplicationId,
Scopes = request.GetScopes(), Scopes = request.GetScopes(),
Status = OpenIddictConstants.Statuses.Valid,
Subject = descriptor.Subject Subject = descriptor.Subject
}, context.RequestAborted); }, context.RequestAborted);

Loading…
Cancel
Save