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>
public IEnumerable<string> Scopes { get; set; }
/// <summary>
/// Gets or sets the status associated with the authorization.
/// </summary>
public string Status { get; set; }
/// <summary>
/// Gets or sets the subject associated with the authorization.
/// </summary>

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

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

6
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.");

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

@ -65,14 +65,15 @@ namespace OpenIddict.Core
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation, whose result returns the token.
/// </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)
{
throw new ArgumentNullException(nameof(descriptor));
}
return Store.CreateAsync(descriptor, cancellationToken);
await ValidateAsync(descriptor, cancellationToken);
return await Store.CreateAsync(descriptor, cancellationToken);
}
/// <summary>
@ -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);
}
/// <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) &&
!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;
}
}
}

1
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
};

1
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
};

1
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
};

1
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
};

2
src/OpenIddict.Models/OpenIddictAuthorization.cs

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

2
src/OpenIddict.Models/OpenIddictToken.cs

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

Loading…
Cancel
Save