Skip to content

Commit

Permalink
Introduce new APIs in the core managers to allow retrieving and addin…
Browse files Browse the repository at this point in the history
…g additional properties
  • Loading branch information
kevinchalet committed Nov 17, 2020
1 parent b19fdff commit d58cac6
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.Json;

namespace OpenIddict.Abstractions
{
Expand Down Expand Up @@ -34,28 +35,32 @@ public class OpenIddictApplicationDescriptor
/// <summary>
/// Gets the localized display names associated with the application.
/// </summary>
public Dictionary<CultureInfo, string> DisplayNames { get; }
= new Dictionary<CultureInfo, string>();
public Dictionary<CultureInfo, string> DisplayNames { get; } = new();

/// <summary>
/// Gets the permissions associated with the application.
/// </summary>
public HashSet<string> Permissions { get; } = new HashSet<string>(StringComparer.Ordinal);
public HashSet<string> Permissions { get; } = new(StringComparer.Ordinal);

/// <summary>
/// Gets the logout callback URLs associated with the application.
/// </summary>
public HashSet<Uri> PostLogoutRedirectUris { get; } = new HashSet<Uri>();
public HashSet<Uri> PostLogoutRedirectUris { get; } = new();

/// <summary>
/// Gets the additional properties associated with the application.
/// </summary>
public Dictionary<string, JsonElement> Properties { get; } = new(StringComparer.Ordinal);

/// <summary>
/// Gets the callback URLs associated with the application.
/// </summary>
public HashSet<Uri> RedirectUris { get; } = new HashSet<Uri>();
public HashSet<Uri> RedirectUris { get; } = new();

/// <summary>
/// Gets the requirements associated with the application.
/// </summary>
public HashSet<string> Requirements { get; } = new HashSet<string>(StringComparer.Ordinal);
public HashSet<string> Requirements { get; } = new(StringComparer.Ordinal);

/// <summary>
/// Gets or sets the application type associated with the application.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Text.Json;

namespace OpenIddict.Abstractions
{
Expand All @@ -25,10 +26,15 @@ public class OpenIddictAuthorizationDescriptor
/// </summary>
public ClaimsPrincipal? Principal { get; set; }

/// <summary>
/// Gets the additional properties associated with the authorization.
/// </summary>
public Dictionary<string, JsonElement> Properties { get; } = new(StringComparer.Ordinal);

/// <summary>
/// Gets the scopes associated with the authorization.
/// </summary>
public HashSet<string> Scopes { get; } = new HashSet<string>(StringComparer.Ordinal);
public HashSet<string> Scopes { get; } = new(StringComparer.Ordinal);

/// <summary>
/// Gets or sets the status associated with the authorization.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.Json;

namespace OpenIddict.Abstractions
{
Expand All @@ -17,7 +18,7 @@ public class OpenIddictScopeDescriptor
/// <summary>
/// Gets the localized descriptions associated with the scope.
/// </summary>
public Dictionary<CultureInfo, string> Descriptions { get; } = new Dictionary<CultureInfo, string>();
public Dictionary<CultureInfo, string> Descriptions { get; } = new();

/// <summary>
/// Gets or sets the display name associated with the scope.
Expand All @@ -27,16 +28,21 @@ public class OpenIddictScopeDescriptor
/// <summary>
/// Gets the localized display names associated with the scope.
/// </summary>
public Dictionary<CultureInfo, string> DisplayNames { get; } = new Dictionary<CultureInfo, string>();
public Dictionary<CultureInfo, string> DisplayNames { get; } = new();

/// <summary>
/// Gets or sets the unique name associated with the scope.
/// </summary>
public string? Name { get; set; }

/// <summary>
/// Gets the additional properties associated with the scope.
/// </summary>
public Dictionary<string, JsonElement> Properties { get; } = new(StringComparer.Ordinal);

/// <summary>
/// Gets the resources associated with the scope.
/// </summary>
public HashSet<string> Resources { get; } = new HashSet<string>(StringComparer.Ordinal);
public HashSet<string> Resources { get; } = new(StringComparer.Ordinal);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Text.Json;

namespace OpenIddict.Abstractions
{
Expand Down Expand Up @@ -39,6 +41,11 @@ public class OpenIddictTokenDescriptor
/// </summary>
public ClaimsPrincipal? Principal { get; set; }

/// <summary>
/// Gets the additional properties associated with the token.
/// </summary>
public Dictionary<string, JsonElement> Properties { get; } = new(StringComparer.Ordinal);

/// <summary>
/// Gets or sets the redemption date associated with the token.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -275,6 +276,17 @@ ValueTask<TResult> GetAsync<TState, TResult>(
/// </returns>
ValueTask<ImmutableArray<string>> GetPostLogoutRedirectUrisAsync(object application, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves the additional properties 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="ValueTask{TResult}"/> that can be used to monitor the asynchronous operation,
/// whose result returns all the additional properties associated with the application.
/// </returns>
ValueTask<ImmutableDictionary<string, JsonElement>> GetPropertiesAsync(object application, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves the callback addresses associated with an application.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Claims;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -232,6 +233,17 @@ ValueTask<TResult> GetAsync<TState, TResult>(
/// </returns>
ValueTask<string?> GetIdAsync(object authorization, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves the additional properties 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="ValueTask{TResult}"/> that can be used to monitor the asynchronous operation,
/// whose result returns all the additional properties associated with the authorization.
/// </returns>
ValueTask<ImmutableDictionary<string, JsonElement>> GetPropertiesAsync(object authorization, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves the scopes associated with an authorization.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/OpenIddict.Abstractions/Managers/IOpenIddictScopeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -264,6 +265,17 @@ ValueTask<TResult> GetAsync<TState, TResult>(
/// </returns>
ValueTask<string?> GetNameAsync(object scope, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves the additional properties associated with a scope.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> that can be used to monitor the asynchronous operation,
/// whose result returns all the additional properties associated with the scope.
/// </returns>
ValueTask<ImmutableDictionary<string, JsonElement>> GetPropertiesAsync(object scope, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves the resources associated with a scope.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions src/OpenIddict.Abstractions/Managers/IOpenIddictTokenManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -253,6 +255,17 @@ ValueTask<TResult> GetAsync<TState, TResult>(
/// </returns>
ValueTask<string?> GetPayloadAsync(object token, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves the additional properties 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="ValueTask{TResult}"/> that can be used to monitor the asynchronous operation,
/// whose result returns all the additional properties associated with the token.
/// </returns>
ValueTask<ImmutableDictionary<string, JsonElement>> GetPropertiesAsync(object token, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves the redemption date associated with a token.
/// </summary>
Expand Down
32 changes: 32 additions & 0 deletions src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -696,6 +697,26 @@ public virtual ValueTask<ImmutableArray<string>> GetPostLogoutRedirectUrisAsync(
return Store.GetPostLogoutRedirectUrisAsync(application, cancellationToken);
}

/// <summary>
/// Retrieves the additional properties 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="ValueTask{TResult}"/> that can be used to monitor the asynchronous operation,
/// whose result returns all the additional properties associated with the application.
/// </returns>
public virtual ValueTask<ImmutableDictionary<string, JsonElement>> GetPropertiesAsync(
TApplication application, CancellationToken cancellationToken = default)
{
if (application is null)
{
throw new ArgumentNullException(nameof(application));
}

return Store.GetPropertiesAsync(application, cancellationToken);
}

/// <summary>
/// Retrieves the callback addresses associated with an application.
/// </summary>
Expand Down Expand Up @@ -909,6 +930,7 @@ public virtual async ValueTask PopulateAsync(TApplication application,
await Store.SetPermissionsAsync(application, descriptor.Permissions.ToImmutableArray(), cancellationToken);
await Store.SetPostLogoutRedirectUrisAsync(application, ImmutableArray.CreateRange(
descriptor.PostLogoutRedirectUris.Select(address => address.OriginalString)), cancellationToken);
await Store.SetPropertiesAsync(application, descriptor.Properties.ToImmutableDictionary(), cancellationToken);
await Store.SetRedirectUrisAsync(application, ImmutableArray.CreateRange(
descriptor.RedirectUris.Select(address => address.OriginalString)), cancellationToken);
await Store.SetRequirementsAsync(application, descriptor.Requirements.ToImmutableArray(), cancellationToken);
Expand Down Expand Up @@ -971,6 +993,12 @@ public virtual async ValueTask PopulateAsync(
descriptor.PostLogoutRedirectUris.Add(uri);
}

descriptor.Properties.Clear();
foreach (var pair in await Store.GetPropertiesAsync(application, cancellationToken))
{
descriptor.Properties.Add(pair.Key, pair.Value);
}

descriptor.RedirectUris.Clear();
foreach (var address in await Store.GetRedirectUrisAsync(application, cancellationToken))
{
Expand Down Expand Up @@ -1581,6 +1609,10 @@ ValueTask<ImmutableArray<string>> IOpenIddictApplicationManager.GetPermissionsAs
ValueTask<ImmutableArray<string>> IOpenIddictApplicationManager.GetPostLogoutRedirectUrisAsync(object application, CancellationToken cancellationToken)
=> GetPostLogoutRedirectUrisAsync((TApplication) application, cancellationToken);

/// <inheritdoc/>
ValueTask<ImmutableDictionary<string, JsonElement>> IOpenIddictApplicationManager.GetPropertiesAsync(object application, CancellationToken cancellationToken)
=> GetPropertiesAsync((TApplication) application, cancellationToken);

/// <inheritdoc/>
ValueTask<ImmutableArray<string>> IOpenIddictApplicationManager.GetRedirectUrisAsync(object application, CancellationToken cancellationToken)
=> GetRedirectUrisAsync((TApplication) application, cancellationToken);
Expand Down
32 changes: 32 additions & 0 deletions src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Runtime.CompilerServices;
using System.Security.Claims;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -714,6 +715,26 @@ public virtual ValueTask<TResult> GetAsync<TState, TResult>(
return Store.GetIdAsync(authorization, cancellationToken);
}

/// <summary>
/// Retrieves the additional properties 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="ValueTask{TResult}"/> that can be used to monitor the asynchronous operation,
/// whose result returns all the additional properties associated with the authorization.
/// </returns>
public virtual ValueTask<ImmutableDictionary<string, JsonElement>> GetPropertiesAsync(
TAuthorization authorization, CancellationToken cancellationToken = default)
{
if (authorization is null)
{
throw new ArgumentNullException(nameof(authorization));
}

return Store.GetPropertiesAsync(authorization, cancellationToken);
}

/// <summary>
/// Retrieves the scopes associated with an authorization.
/// </summary>
Expand Down Expand Up @@ -933,6 +954,7 @@ public virtual async ValueTask PopulateAsync(TAuthorization authorization,

await Store.SetApplicationIdAsync(authorization, descriptor.ApplicationId, cancellationToken);
await Store.SetCreationDateAsync(authorization, descriptor.CreationDate, cancellationToken);
await Store.SetPropertiesAsync(authorization, descriptor.Properties.ToImmutableDictionary(), cancellationToken);
await Store.SetScopesAsync(authorization, descriptor.Scopes.ToImmutableArray(), cancellationToken);
await Store.SetStatusAsync(authorization, descriptor.Status, cancellationToken);
await Store.SetSubjectAsync(authorization, descriptor.Subject, cancellationToken);
Expand Down Expand Up @@ -969,6 +991,12 @@ public virtual async ValueTask PopulateAsync(
descriptor.Status = await Store.GetStatusAsync(authorization, cancellationToken);
descriptor.Subject = await Store.GetSubjectAsync(authorization, cancellationToken);
descriptor.Type = await Store.GetTypeAsync(authorization, cancellationToken);

descriptor.Properties.Clear();
foreach (var pair in await Store.GetPropertiesAsync(authorization, cancellationToken))
{
descriptor.Properties.Add(pair.Key, pair.Value);
}
}

/// <summary>
Expand Down Expand Up @@ -1231,6 +1259,10 @@ ValueTask<TResult> IOpenIddictAuthorizationManager.GetAsync<TState, TResult>(Fun
ValueTask<string?> IOpenIddictAuthorizationManager.GetIdAsync(object authorization, CancellationToken cancellationToken)
=> GetIdAsync((TAuthorization) authorization, cancellationToken);

/// <inheritdoc/>
ValueTask<ImmutableDictionary<string, JsonElement>> IOpenIddictAuthorizationManager.GetPropertiesAsync(object authorization, CancellationToken cancellationToken)
=> GetPropertiesAsync((TAuthorization) authorization, cancellationToken);

/// <inheritdoc/>
ValueTask<ImmutableArray<string>> IOpenIddictAuthorizationManager.GetScopesAsync(object authorization, CancellationToken cancellationToken)
=> GetScopesAsync((TAuthorization) authorization, cancellationToken);
Expand Down
Loading

0 comments on commit d58cac6

Please sign in to comment.