89 changed files with 3432 additions and 2702 deletions
@ -0,0 +1,23 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\build\packages.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
</PropertyGroup> |
|||
|
|||
<PropertyGroup> |
|||
<Description>OpenIddict's abstractions.</Description> |
|||
<Authors>Kévin Chalet</Authors> |
|||
<PackageTags>aspnetcore;authentication;jwt;openidconnect;openiddict;security</PackageTags> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="JetBrains.Annotations" Version="$(JetBrainsVersion)" PrivateAssets="All" /> |
|||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(AspNetCoreVersion)" /> |
|||
<PackageReference Include="Newtonsoft.Json" Version="$(JsonNetVersion)" /> |
|||
<PackageReference Include="System.Collections.Immutable" Version="$(ImmutableCollectionsVersion)" /> |
|||
<PackageReference Include="System.Threading.Tasks.Extensions" Version="$(TasksExtensionsVersion)" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,38 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.ComponentModel; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
/// <summary>
|
|||
/// Provides a shared entry point allowing to configure the OpenIddict services.
|
|||
/// </summary>
|
|||
public class OpenIddictBuilder |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of <see cref="OpenIddictBuilder"/>.
|
|||
/// </summary>
|
|||
/// <param name="services">The services collection.</param>
|
|||
public OpenIddictBuilder([NotNull] IServiceCollection services) |
|||
{ |
|||
if (services == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(services)); |
|||
} |
|||
|
|||
Services = services; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the services collection.
|
|||
/// </summary>
|
|||
[EditorBrowsable(EditorBrowsableState.Never)] |
|||
public IServiceCollection Services { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,56 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
public static class OpenIddictExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Provides a common entry point for registering the OpenIddict services.
|
|||
/// </summary>
|
|||
/// <param name="services">The services collection.</param>
|
|||
/// <remarks>This extension can be safely called multiple times.</remarks>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public static OpenIddictBuilder AddOpenIddict([NotNull] this IServiceCollection services) |
|||
{ |
|||
if (services == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(services)); |
|||
} |
|||
|
|||
return new OpenIddictBuilder(services); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Provides a common entry point for registering the OpenIddict services.
|
|||
/// </summary>
|
|||
/// <param name="services">The services collection.</param>
|
|||
/// <param name="configuration">The configuration delegate used to register new services.</param>
|
|||
/// <remarks>This extension can be safely called multiple times.</remarks>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public static IServiceCollection AddOpenIddict( |
|||
[NotNull] this IServiceCollection services, |
|||
[NotNull] Action<OpenIddictBuilder> configuration) |
|||
{ |
|||
if (services == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(services)); |
|||
} |
|||
|
|||
if (configuration == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(configuration)); |
|||
} |
|||
|
|||
configuration(services.AddOpenIddict()); |
|||
|
|||
return services; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using System; |
|||
|
|||
namespace OpenIddict.Abstractions |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an application store.
|
|||
/// </summary>
|
|||
public interface IOpenIddictApplicationStoreResolver |
|||
{ |
|||
/// <summary>
|
|||
/// Returns an application store compatible with the specified application type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TApplication">The type of the Application entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictApplicationStore{TApplication}"/>.</returns>
|
|||
IOpenIddictApplicationStore<TApplication> Get<TApplication>() where TApplication : class; |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using System; |
|||
|
|||
namespace OpenIddict.Abstractions |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an authorization store.
|
|||
/// </summary>
|
|||
public interface IOpenIddictAuthorizationStoreResolver |
|||
{ |
|||
/// <summary>
|
|||
/// Returns an authorization store compatible with the specified authorization type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TAuthorization">The type of the Authorization entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictAuthorizationStore{TAuthorization}"/>.</returns>
|
|||
IOpenIddictAuthorizationStore<TAuthorization> Get<TAuthorization>() where TAuthorization : class; |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using System; |
|||
|
|||
namespace OpenIddict.Abstractions |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a scope store.
|
|||
/// </summary>
|
|||
public interface IOpenIddictScopeStoreResolver |
|||
{ |
|||
/// <summary>
|
|||
/// Returns a scope store compatible with the specified scope type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TScope">The type of the Scope entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictScopeStore{TScope}"/>.</returns>
|
|||
IOpenIddictScopeStore<TScope> Get<TScope>() where TScope : class; |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using System; |
|||
|
|||
namespace OpenIddict.Abstractions |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a token store.
|
|||
/// </summary>
|
|||
public interface IOpenIddictTokenStoreResolver |
|||
{ |
|||
/// <summary>
|
|||
/// Returns a token store compatible with the specified token type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TToken">The type of the Token entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictTokenStore{TToken}"/>.</returns>
|
|||
IOpenIddictTokenStore<TToken> Get<TToken>() where TToken : class; |
|||
} |
|||
} |
|||
@ -1,327 +0,0 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.ComponentModel; |
|||
using JetBrains.Annotations; |
|||
using OpenIddict.Core; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes the necessary methods required to configure OpenIddict.
|
|||
/// </summary>
|
|||
public class OpenIddictBuilder |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of <see cref="OpenIddictBuilder"/>.
|
|||
/// </summary>
|
|||
/// <param name="services">The services collection.</param>
|
|||
public OpenIddictBuilder([NotNull] IServiceCollection services) |
|||
{ |
|||
if (services == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(services)); |
|||
} |
|||
|
|||
Services = services; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the type corresponding to the Application entity.
|
|||
/// </summary>
|
|||
[EditorBrowsable(EditorBrowsableState.Never)] |
|||
public Type ApplicationType { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the type corresponding to the Authorization entity.
|
|||
/// </summary>
|
|||
[EditorBrowsable(EditorBrowsableState.Never)] |
|||
public Type AuthorizationType { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the type corresponding to the Scope entity.
|
|||
/// </summary>
|
|||
[EditorBrowsable(EditorBrowsableState.Never)] |
|||
public Type ScopeType { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the type corresponding to the Token entity.
|
|||
/// </summary>
|
|||
[EditorBrowsable(EditorBrowsableState.Never)] |
|||
public Type TokenType { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the services collection.
|
|||
/// </summary>
|
|||
[EditorBrowsable(EditorBrowsableState.Never)] |
|||
public IServiceCollection Services { get; } |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom application manager derived from
|
|||
/// <see cref="OpenIddictApplicationManager{TApplication}"/>.
|
|||
/// </summary>
|
|||
/// <typeparam name="TManager">The type of the custom manager.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public OpenIddictBuilder AddApplicationManager<TManager>() where TManager : class |
|||
=> AddApplicationManager(typeof(TManager)); |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom application manager derived from
|
|||
/// <see cref="OpenIddictApplicationManager{TApplication}"/>.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom manager.</param>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public virtual OpenIddictBuilder AddApplicationManager([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var contract = typeof(OpenIddictApplicationManager<>).MakeGenericType(ApplicationType); |
|||
if (!contract.IsAssignableFrom(type)) |
|||
{ |
|||
throw new InvalidOperationException("The specified type is invalid."); |
|||
} |
|||
|
|||
Services.AddScoped(contract, type); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom application store derived from
|
|||
/// <see cref="IOpenIddictApplicationStore{TApplication}"/>.
|
|||
/// </summary>
|
|||
/// <typeparam name="TStore">The type of the custom store.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public OpenIddictBuilder AddApplicationStore<TStore>() where TStore : class |
|||
=> AddApplicationStore(typeof(TStore)); |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom application store derived from
|
|||
/// <see cref="IOpenIddictApplicationStore{TApplication}"/>.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom store.</param>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public virtual OpenIddictBuilder AddApplicationStore([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var contract = typeof(IOpenIddictApplicationStore<>).MakeGenericType(ApplicationType); |
|||
if (!contract.IsAssignableFrom(type)) |
|||
{ |
|||
throw new InvalidOperationException("The specified type is invalid."); |
|||
} |
|||
|
|||
Services.AddScoped(contract, type); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom authorization manager derived from
|
|||
/// <see cref="OpenIddictAuthorizationManager{TAuthorization}"/>.
|
|||
/// </summary>
|
|||
/// <typeparam name="TManager">The type of the custom manager.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public OpenIddictBuilder AddAuthorizationManager<TManager>() where TManager : class |
|||
=> AddAuthorizationManager(typeof(TManager)); |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom authorization manager derived from
|
|||
/// <see cref="OpenIddictAuthorizationManager{TAuthorization}"/>.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom manager.</param>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public virtual OpenIddictBuilder AddAuthorizationManager([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var contract = typeof(OpenIddictAuthorizationManager<>).MakeGenericType(AuthorizationType); |
|||
if (!contract.IsAssignableFrom(type)) |
|||
{ |
|||
throw new InvalidOperationException("The specified type is invalid."); |
|||
} |
|||
|
|||
Services.AddScoped(contract, type); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom authorization store derived from
|
|||
/// <see cref="IOpenIddictAuthorizationStore{TAuthorization}"/>.
|
|||
/// </summary>
|
|||
/// <typeparam name="TStore">The type of the custom store.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public OpenIddictBuilder AddAuthorizationStore<TStore>() where TStore : class |
|||
=> AddAuthorizationStore(typeof(TStore)); |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom authorization store derived from
|
|||
/// <see cref="IOpenIddictAuthorizationStore{TAuthorization}"/>.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom store.</param>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public virtual OpenIddictBuilder AddAuthorizationStore([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var contract = typeof(IOpenIddictAuthorizationStore<>).MakeGenericType(AuthorizationType); |
|||
if (!contract.IsAssignableFrom(type)) |
|||
{ |
|||
throw new InvalidOperationException("The specified type is invalid."); |
|||
} |
|||
|
|||
Services.AddScoped(contract, type); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom scope manager derived from
|
|||
/// <see cref="OpenIddictScopeManager{TScope}"/>.
|
|||
/// </summary>
|
|||
/// <typeparam name="TManager">The type of the custom manager.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public OpenIddictBuilder AddScopeManager<TManager>() where TManager : class |
|||
=> AddScopeManager(typeof(TManager)); |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom scope manager derived from
|
|||
/// <see cref="OpenIddictScopeManager{TScope}"/>.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom manager.</param>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public virtual OpenIddictBuilder AddScopeManager([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var contract = typeof(OpenIddictScopeManager<>).MakeGenericType(ScopeType); |
|||
if (!contract.IsAssignableFrom(type)) |
|||
{ |
|||
throw new InvalidOperationException("The specified type is invalid."); |
|||
} |
|||
|
|||
Services.AddScoped(contract, type); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom scope store derived from
|
|||
/// <see cref="IOpenIddictScopeStore{TScope}"/>.
|
|||
/// </summary>
|
|||
/// <typeparam name="TStore">The type of the custom store.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public OpenIddictBuilder AddScopeStore<TStore>() where TStore : class |
|||
=> AddScopeStore(typeof(TStore)); |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom scope store derived from
|
|||
/// <see cref="IOpenIddictScopeStore{TScope}"/>.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom store.</param>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public virtual OpenIddictBuilder AddScopeStore([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var contract = typeof(IOpenIddictScopeStore<>).MakeGenericType(ScopeType); |
|||
if (!contract.IsAssignableFrom(type)) |
|||
{ |
|||
throw new InvalidOperationException("The specified type is invalid."); |
|||
} |
|||
|
|||
Services.AddScoped(contract, type); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom token manager derived from
|
|||
/// <see cref="OpenIddictTokenManager{TToken}"/>.
|
|||
/// </summary>
|
|||
/// <typeparam name="TManager">The type of the custom manager.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public OpenIddictBuilder AddTokenManager<TManager>() where TManager : class |
|||
=> AddTokenManager(typeof(TManager)); |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom token manager derived from
|
|||
/// <see cref="OpenIddictTokenManager{TToken}"/>.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom manager.</param>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public virtual OpenIddictBuilder AddTokenManager([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var contract = typeof(OpenIddictTokenManager<>).MakeGenericType(TokenType); |
|||
if (!contract.IsAssignableFrom(type)) |
|||
{ |
|||
throw new InvalidOperationException("The specified type is invalid."); |
|||
} |
|||
|
|||
Services.AddScoped(contract, type); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom token store derived from
|
|||
/// <see cref="IOpenIddictTokenStore{TToken}"/>.
|
|||
/// </summary>
|
|||
/// <typeparam name="TStore">The type of the custom store.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public OpenIddictBuilder AddTokenStore<TStore>() where TStore : class |
|||
=> AddTokenStore(typeof(TStore)); |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom token store derived from
|
|||
/// <see cref="IOpenIddictTokenStore{TToken}"/>.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom store.</param>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public virtual OpenIddictBuilder AddTokenStore([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var contract = typeof(IOpenIddictTokenStore<>).MakeGenericType(TokenType); |
|||
if (!contract.IsAssignableFrom(type)) |
|||
{ |
|||
throw new InvalidOperationException("The specified type is invalid."); |
|||
} |
|||
|
|||
Services.AddScoped(contract, type); |
|||
|
|||
return this; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,628 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.ComponentModel; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection.Extensions; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.Core; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes the necessary methods required to configure the OpenIddict core services.
|
|||
/// </summary>
|
|||
public class OpenIddictCoreBuilder |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of <see cref="OpenIddictCoreBuilder"/>.
|
|||
/// </summary>
|
|||
/// <param name="services">The services collection.</param>
|
|||
public OpenIddictCoreBuilder([NotNull] IServiceCollection services) |
|||
{ |
|||
if (services == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(services)); |
|||
} |
|||
|
|||
Services = services; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the services collection.
|
|||
/// </summary>
|
|||
[EditorBrowsable(EditorBrowsableState.Never)] |
|||
public IServiceCollection Services { get; } |
|||
|
|||
/// <summary>
|
|||
/// Amends the default OpenIddict core configuration.
|
|||
/// </summary>
|
|||
/// <param name="configuration">The delegate used to configure the OpenIddict options.</param>
|
|||
/// <remarks>This extension can be safely called multiple times.</remarks>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder Configure([NotNull] Action<OpenIddictCoreOptions> configuration) |
|||
{ |
|||
if (configuration == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(configuration)); |
|||
} |
|||
|
|||
Services.Configure(configuration); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom application store by a custom implementation derived
|
|||
/// from <see cref="IOpenIddictApplicationStore{TApplication}"/>.
|
|||
/// Note: when using this overload, the application store
|
|||
/// must be either a non-generic or closed generic service.
|
|||
/// </summary>
|
|||
/// <typeparam name="TStore">The type of the custom store.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder AddApplicationStore<TStore>() where TStore : class |
|||
=> AddApplicationStore(typeof(TStore)); |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom application store by a custom implementation derived
|
|||
/// from <see cref="IOpenIddictApplicationStore{TApplication}"/>.
|
|||
/// Note: when using this overload, the application store can be
|
|||
/// either a non-generic, a closed or an open generic service.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom store.</param>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder AddApplicationStore([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(type, typeof(IOpenIddictApplicationStore<>)); |
|||
if (root == null) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
// Note: managers can be either open generics (e.g OpenIddictApplicationStore<>)
|
|||
// or closed generics (e.g OpenIddictApplicationStore<OpenIddictApplication>).
|
|||
if (type.IsGenericTypeDefinition) |
|||
{ |
|||
if (type.GetGenericArguments().Length != 1) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(IOpenIddictApplicationStore<>), type)); |
|||
} |
|||
|
|||
else |
|||
{ |
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(IOpenIddictApplicationStore<>) |
|||
.MakeGenericType(root.GenericTypeArguments[0]), type)); |
|||
} |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom authorization store by a custom implementation derived
|
|||
/// from <see cref="IOpenIddictAuthorizationStore{TAuthorization}"/>.
|
|||
/// Note: when using this overload, the authorization store
|
|||
/// must be either a non-generic or closed generic service.
|
|||
/// </summary>
|
|||
/// <typeparam name="TStore">The type of the custom store.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder AddAuthorizationStore<TStore>() where TStore : class |
|||
=> AddAuthorizationStore(typeof(TStore)); |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom authorization store by a custom implementation derived
|
|||
/// from <see cref="IOpenIddictAuthorizationStore{TAuthorization}"/>.
|
|||
/// Note: when using this overload, the authorization store can be
|
|||
/// either a non-generic, a closed or an open generic service.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom store.</param>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder AddAuthorizationStore([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(type, typeof(IOpenIddictAuthorizationStore<>)); |
|||
if (root == null) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
// Note: managers can be either open generics (e.g OpenIddictAuthorizationStore<>)
|
|||
// or closed generics (e.g OpenIddictAuthorizationStore<OpenIddictAuthorization>).
|
|||
if (type.IsGenericTypeDefinition) |
|||
{ |
|||
if (type.GetGenericArguments().Length != 1) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(IOpenIddictAuthorizationStore<>), type)); |
|||
} |
|||
|
|||
else |
|||
{ |
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(IOpenIddictAuthorizationStore<>) |
|||
.MakeGenericType(root.GenericTypeArguments[0]), type)); |
|||
} |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom scope store by a custom implementation derived
|
|||
/// from <see cref="IOpenIddictScopeStore{TScope}"/>.
|
|||
/// Note: when using this overload, the scope store
|
|||
/// must be either a non-generic or closed generic service.
|
|||
/// </summary>
|
|||
/// <typeparam name="TStore">The type of the custom store.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder AddScopeStore<TStore>() where TStore : class |
|||
=> AddScopeStore(typeof(TStore)); |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom scope store by a custom implementation derived
|
|||
/// from <see cref="IOpenIddictScopeStore{TScope}"/>.
|
|||
/// Note: when using this overload, the scope store can be
|
|||
/// either a non-generic, a closed or an open generic service.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom store.</param>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder AddScopeStore([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(type, typeof(IOpenIddictScopeStore<>)); |
|||
if (root == null) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
// Note: managers can be either open generics (e.g OpenIddictScopeStore<>)
|
|||
// or closed generics (e.g OpenIddictScopeStore<OpenIddictScope>).
|
|||
if (type.IsGenericTypeDefinition) |
|||
{ |
|||
if (type.GetGenericArguments().Length != 1) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(IOpenIddictScopeStore<>), type)); |
|||
} |
|||
|
|||
else |
|||
{ |
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(IOpenIddictScopeStore<>) |
|||
.MakeGenericType(root.GenericTypeArguments[0]), type)); |
|||
} |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom token store by a custom implementation derived
|
|||
/// from <see cref="IOpenIddictTokenStore{TToken}"/>.
|
|||
/// Note: when using this overload, the token store
|
|||
/// must be either a non-generic or closed generic service.
|
|||
/// </summary>
|
|||
/// <typeparam name="TStore">The type of the custom store.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder AddTokenStore<TStore>() where TStore : class |
|||
=> AddTokenStore(typeof(TStore)); |
|||
|
|||
/// <summary>
|
|||
/// Adds a custom token store by a custom implementation derived
|
|||
/// from <see cref="IOpenIddictTokenStore{TToken}"/>.
|
|||
/// Note: when using this overload, the token store can be
|
|||
/// either a non-generic, a closed or an open generic service.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom store.</param>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder AddTokenStore([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(type, typeof(IOpenIddictTokenStore<>)); |
|||
if (root == null) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
// Note: managers can be either open generics (e.g OpenIddictTokenStore<>)
|
|||
// or closed generics (e.g OpenIddictTokenStore<OpenIddictToken>).
|
|||
if (type.IsGenericTypeDefinition) |
|||
{ |
|||
if (type.GetGenericArguments().Length != 1) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(IOpenIddictTokenStore<>), type)); |
|||
} |
|||
|
|||
else |
|||
{ |
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(IOpenIddictTokenStore<>) |
|||
.MakeGenericType(root.GenericTypeArguments[0]), type)); |
|||
} |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Replace the default application manager by a custom manager derived
|
|||
/// from <see cref="OpenIddictApplicationManager{TApplication}"/>.
|
|||
/// Note: when using this overload, the application manager can be
|
|||
/// either a non-generic, a closed or an open generic service.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom manager.</param>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceApplicationManager([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(type, typeof(OpenIddictApplicationManager<>)); |
|||
if (root == null) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
// Note: managers can be either open generics (e.g OpenIddictApplicationManager<>)
|
|||
// or closed generics (e.g OpenIddictApplicationManager<OpenIddictApplication>).
|
|||
if (type.IsGenericTypeDefinition) |
|||
{ |
|||
if (type.GetGenericArguments().Length != 1) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(OpenIddictApplicationManager<>), type)); |
|||
} |
|||
|
|||
else |
|||
{ |
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(OpenIddictApplicationManager<>) |
|||
.MakeGenericType(root.GenericTypeArguments[0]), type)); |
|||
} |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Replaces the default application store resolver by a custom implementation.
|
|||
/// </summary>
|
|||
/// <typeparam name="TResolver">The type of the custom store.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceApplicationStoreResolver<TResolver>() |
|||
where TResolver : IOpenIddictApplicationStoreResolver |
|||
=> ReplaceApplicationStoreResolver(typeof(TResolver)); |
|||
|
|||
/// <summary>
|
|||
/// Replaces the default application store resolver by a custom implementation.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom store.</param>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceApplicationStoreResolver([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
if (!typeof(IOpenIddictApplicationStoreResolver).IsAssignableFrom(type)) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(IOpenIddictApplicationStoreResolver), type)); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Replace the default application manager by a custom manager derived
|
|||
/// from <see cref="OpenIddictApplicationManager{TApplication}"/>.
|
|||
/// Note: when using this overload, the application manager
|
|||
/// must be either a non-generic or closed generic service.
|
|||
/// </summary>
|
|||
/// <typeparam name="TManager">The type of the custom manager.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceApplicationManager<TManager>() where TManager : class |
|||
=> ReplaceApplicationManager(typeof(TManager)); |
|||
|
|||
/// <summary>
|
|||
/// Replace the default authorization manager by a custom manager derived
|
|||
/// from <see cref="OpenIddictAuthorizationManager{TAuthorization}"/>.
|
|||
/// Note: when using this overload, the authorization manager
|
|||
/// must be either a non-generic or closed generic service.
|
|||
/// </summary>
|
|||
/// <typeparam name="TManager">The type of the custom manager.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceAuthorizationManager<TManager>() where TManager : class |
|||
=> ReplaceAuthorizationManager(typeof(TManager)); |
|||
|
|||
/// <summary>
|
|||
/// Replace the default authorization manager by a custom manager derived
|
|||
/// from <see cref="OpenIddictAuthorizationManager{TAuthorization}"/>.
|
|||
/// Note: when using this overload, the authorization manager can be
|
|||
/// either a non-generic, a closed or an open generic service.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom manager.</param>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceAuthorizationManager([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(type, typeof(OpenIddictAuthorizationManager<>)); |
|||
if (root == null) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
// Note: managers can be either open generics (e.g OpenIddictAuthorizationManager<>)
|
|||
// or closed generics (e.g OpenIddictAuthorizationManager<OpenIddictAuthorization>).
|
|||
if (type.IsGenericTypeDefinition) |
|||
{ |
|||
if (type.GetGenericArguments().Length != 1) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(OpenIddictAuthorizationManager<>), type)); |
|||
} |
|||
|
|||
else |
|||
{ |
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(OpenIddictAuthorizationManager<>) |
|||
.MakeGenericType(root.GenericTypeArguments[0]), type)); |
|||
} |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Replaces the default authorization store resolver by a custom implementation.
|
|||
/// </summary>
|
|||
/// <typeparam name="TResolver">The type of the custom store.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceAuthorizationStoreResolver<TResolver>() |
|||
where TResolver : IOpenIddictAuthorizationStoreResolver |
|||
=> ReplaceAuthorizationStoreResolver(typeof(TResolver)); |
|||
|
|||
/// <summary>
|
|||
/// Replaces the default authorization store resolver by a custom implementation.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom store.</param>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceAuthorizationStoreResolver([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
if (!typeof(IOpenIddictAuthorizationStoreResolver).IsAssignableFrom(type)) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(IOpenIddictAuthorizationStoreResolver), type)); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Replace the default scope manager by a custom manager
|
|||
/// derived from <see cref="OpenIddictScopeManager{TScope}"/>.
|
|||
/// Note: when using this overload, the scope manager
|
|||
/// must be either a non-generic or closed generic service.
|
|||
/// </summary>
|
|||
/// <typeparam name="TManager">The type of the custom manager.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceScopeManager<TManager>() where TManager : class |
|||
=> ReplaceScopeManager(typeof(TManager)); |
|||
|
|||
/// <summary>
|
|||
/// Replace the default scope manager by a custom manager
|
|||
/// derived from <see cref="OpenIddictScopeManager{TScope}"/>.
|
|||
/// Note: when using this overload, the scope manager can be
|
|||
/// either a non-generic, a closed or an open generic service.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom manager.</param>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceScopeManager([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(type, typeof(OpenIddictScopeManager<>)); |
|||
if (root == null) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
// Note: managers can be either open generics (e.g OpenIddictScopeManager<>)
|
|||
// or closed generics (e.g OpenIddictScopeManager<OpenIddictScope>).
|
|||
if (type.IsGenericTypeDefinition) |
|||
{ |
|||
if (type.GetGenericArguments().Length != 1) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(OpenIddictScopeManager<>), type)); |
|||
} |
|||
|
|||
else |
|||
{ |
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(OpenIddictScopeManager<>) |
|||
.MakeGenericType(root.GenericTypeArguments[0]), type)); |
|||
} |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Replaces the default scope store resolver by a custom implementation.
|
|||
/// </summary>
|
|||
/// <typeparam name="TResolver">The type of the custom store.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceScopeStoreResolver<TResolver>() |
|||
where TResolver : IOpenIddictScopeStoreResolver |
|||
=> ReplaceScopeStoreResolver(typeof(TResolver)); |
|||
|
|||
/// <summary>
|
|||
/// Replaces the default scope store resolver by a custom implementation.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom store.</param>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceScopeStoreResolver([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
if (!typeof(IOpenIddictScopeStoreResolver).IsAssignableFrom(type)) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(IOpenIddictScopeStoreResolver), type)); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Replace the default token manager by a custom manager
|
|||
/// derived from <see cref="OpenIddictTokenManager{TToken}"/>.
|
|||
/// Note: when using this overload, the token manager
|
|||
/// must be either a non-generic or closed generic service.
|
|||
/// </summary>
|
|||
/// <typeparam name="TManager">The type of the custom manager.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceTokenManager<TManager>() where TManager : class |
|||
=> ReplaceTokenManager(typeof(TManager)); |
|||
|
|||
/// <summary>
|
|||
/// Replace the default token manager by a custom manager
|
|||
/// derived from <see cref="OpenIddictTokenManager{TToken}"/>.
|
|||
/// Note: when using this overload, the token manager can be
|
|||
/// either a non-generic, a closed or an open generic service.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom manager.</param>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceTokenManager([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(type, typeof(OpenIddictTokenManager<>)); |
|||
if (root == null) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
// Note: managers can be either open generics (e.g OpenIddictTokenManager<>)
|
|||
// or closed generics (e.g OpenIddictTokenManager<OpenIddictToken>).
|
|||
if (type.IsGenericTypeDefinition) |
|||
{ |
|||
if (type.GetGenericArguments().Length != 1) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(OpenIddictTokenManager<>), type)); |
|||
} |
|||
|
|||
else |
|||
{ |
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(OpenIddictTokenManager<>) |
|||
.MakeGenericType(root.GenericTypeArguments[0]), type)); |
|||
} |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Replaces the default token store resolver by a custom implementation.
|
|||
/// </summary>
|
|||
/// <typeparam name="TResolver">The type of the custom store.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceTokenStoreResolver<TResolver>() |
|||
where TResolver : IOpenIddictTokenStoreResolver |
|||
=> ReplaceTokenStoreResolver(typeof(TResolver)); |
|||
|
|||
/// <summary>
|
|||
/// Replaces the default token store resolver by a custom implementation.
|
|||
/// </summary>
|
|||
/// <param name="type">The type of the custom store.</param>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder ReplaceTokenStoreResolver([NotNull] Type type) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
if (!typeof(IOpenIddictTokenStoreResolver).IsAssignableFrom(type)) |
|||
{ |
|||
throw new ArgumentException("The specified type is invalid.", nameof(type)); |
|||
} |
|||
|
|||
Services.Replace(ServiceDescriptor.Scoped(typeof(IOpenIddictTokenStoreResolver), type)); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Configures OpenIddict to use the specified entities.
|
|||
/// </summary>
|
|||
/// <typeparam name="TApplication">The type corresponding to the Application entity.</typeparam>
|
|||
/// <typeparam name="TAuthorization">The type corresponding to the Authorization entity.</typeparam>
|
|||
/// <typeparam name="TScope">The type corresponding to the Scope entity.</typeparam>
|
|||
/// <typeparam name="TToken">The type corresponding to the Token entity.</typeparam>
|
|||
/// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
|
|||
public OpenIddictCoreBuilder UseCustomModels<TApplication, TAuthorization, TScope, TToken>() |
|||
where TApplication : class |
|||
where TAuthorization : class |
|||
where TScope : class |
|||
where TToken : class |
|||
=> Configure(options => |
|||
{ |
|||
options.DefaultApplicationType = typeof(TApplication); |
|||
options.DefaultAuthorizationType = typeof(TAuthorization); |
|||
options.DefaultScopeType = typeof(TScope); |
|||
options.DefaultTokenType = typeof(TToken); |
|||
}); |
|||
} |
|||
} |
|||
@ -0,0 +1,73 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection.Extensions; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.Core; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
public static class OpenIddictCoreExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Registers the OpenIddict core services in the DI container.
|
|||
/// </summary>
|
|||
/// <param name="builder">The services builder used by OpenIddict to register new services.</param>
|
|||
/// <remarks>This extension can be safely called multiple times.</remarks>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public static OpenIddictCoreBuilder AddCore([NotNull] this OpenIddictBuilder builder) |
|||
{ |
|||
if (builder == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(builder)); |
|||
} |
|||
|
|||
builder.Services.AddDistributedMemoryCache(); |
|||
builder.Services.AddMemoryCache(); |
|||
builder.Services.AddOptions(); |
|||
|
|||
builder.Services.TryAddScoped(typeof(OpenIddictApplicationManager<>)); |
|||
builder.Services.TryAddScoped(typeof(OpenIddictAuthorizationManager<>)); |
|||
builder.Services.TryAddScoped(typeof(OpenIddictScopeManager<>)); |
|||
builder.Services.TryAddScoped(typeof(OpenIddictTokenManager<>)); |
|||
|
|||
builder.Services.TryAddScoped<IOpenIddictApplicationStoreResolver, OpenIddictApplicationStoreResolver>(); |
|||
builder.Services.TryAddScoped<IOpenIddictAuthorizationStoreResolver, OpenIddictAuthorizationStoreResolver>(); |
|||
builder.Services.TryAddScoped<IOpenIddictScopeStoreResolver, OpenIddictScopeStoreResolver>(); |
|||
builder.Services.TryAddScoped<IOpenIddictTokenStoreResolver, OpenIddictTokenStoreResolver>(); |
|||
|
|||
return new OpenIddictCoreBuilder(builder.Services); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers the OpenIddict core services in the DI container.
|
|||
/// </summary>
|
|||
/// <param name="builder">The services builder used by OpenIddict to register new services.</param>
|
|||
/// <param name="configuration">The configuration delegate used to configure the core services.</param>
|
|||
/// <remarks>This extension can be safely called multiple times.</remarks>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public static OpenIddictBuilder AddCore( |
|||
[NotNull] this OpenIddictBuilder builder, |
|||
[NotNull] Action<OpenIddictCoreBuilder> configuration) |
|||
{ |
|||
if (builder == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(builder)); |
|||
} |
|||
|
|||
if (configuration == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(configuration)); |
|||
} |
|||
|
|||
configuration(builder.AddCore()); |
|||
|
|||
return builder; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,67 @@ |
|||
using System; |
|||
using System.ComponentModel; |
|||
|
|||
namespace OpenIddict.Core |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes common helpers used by the OpenIddict assemblies.
|
|||
/// </summary>
|
|||
[EditorBrowsable(EditorBrowsableState.Never)] |
|||
public static class OpenIddictCoreHelpers |
|||
{ |
|||
/// <summary>
|
|||
/// Finds the base type that matches the specified generic type definition.
|
|||
/// </summary>
|
|||
/// <param name="type">The type to introspect.</param>
|
|||
/// <param name="definition">The generic type definition.</param>
|
|||
/// <returns>A <see cref="Type"/> instance if the base type was found, <c>null</c> otherwise.</returns>
|
|||
public static Type FindGenericBaseType(Type type, Type definition) |
|||
{ |
|||
if (type == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(type)); |
|||
} |
|||
|
|||
if (definition == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(definition)); |
|||
} |
|||
|
|||
if (!definition.IsGenericTypeDefinition) |
|||
{ |
|||
throw new ArgumentException("The second parameter must be a generic type definition.", nameof(definition)); |
|||
} |
|||
|
|||
for (var candidate = type; candidate != null; candidate = candidate.BaseType) |
|||
{ |
|||
if (!candidate.IsGenericType && !candidate.IsConstructedGenericType) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
if (candidate.GetGenericTypeDefinition() == definition) |
|||
{ |
|||
return candidate; |
|||
} |
|||
|
|||
if (definition.IsInterface) |
|||
{ |
|||
foreach (var contract in candidate.GetInterfaces()) |
|||
{ |
|||
if (!contract.IsGenericType && !contract.IsConstructedGenericType) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
if (contract.GetGenericTypeDefinition() == definition) |
|||
{ |
|||
return contract; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
|
|||
namespace OpenIddict.Core |
|||
{ |
|||
public class OpenIddictCoreOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Gets or sets the type corresponding to the Application entity.
|
|||
/// </summary>
|
|||
public Type DefaultApplicationType { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the type corresponding to the Authorization entity.
|
|||
/// </summary>
|
|||
public Type DefaultAuthorizationType { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the type corresponding to the Scope entity.
|
|||
/// </summary>
|
|||
public Type DefaultScopeType { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the type corresponding to the Token entity.
|
|||
/// </summary>
|
|||
public Type DefaultTokenType { get; set; } |
|||
} |
|||
} |
|||
@ -1,89 +0,0 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection.Extensions; |
|||
using OpenIddict.Core; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
public static class OpenIddictExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Registers the default OpenIddict services in the DI container, using the specified entities.
|
|||
/// </summary>
|
|||
/// <typeparam name="TApplication">The type of the Application entity.</typeparam>
|
|||
/// <typeparam name="TAuthorization">The type of the Authorization entity.</typeparam>
|
|||
/// <typeparam name="TScope">The type of the Scope entity.</typeparam>
|
|||
/// <typeparam name="TToken">The type of the Token entity.</typeparam>
|
|||
/// <param name="services">The services collection.</param>
|
|||
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
|
|||
public static OpenIddictBuilder AddOpenIddict<TApplication, TAuthorization, TScope, TToken>([NotNull] this IServiceCollection services) |
|||
where TApplication : class |
|||
where TAuthorization : class |
|||
where TScope : class |
|||
where TToken : class |
|||
{ |
|||
if (services == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(services)); |
|||
} |
|||
|
|||
services.AddDistributedMemoryCache(); |
|||
services.AddMemoryCache(); |
|||
services.AddOptions(); |
|||
|
|||
// Register the OpenIddict core services in the DI container.
|
|||
services.TryAddScoped<OpenIddictApplicationManager<TApplication>>(); |
|||
services.TryAddScoped<OpenIddictAuthorizationManager<TAuthorization>>(); |
|||
services.TryAddScoped<OpenIddictScopeManager<TScope>>(); |
|||
services.TryAddScoped<OpenIddictTokenManager<TToken>>(); |
|||
|
|||
return new OpenIddictBuilder(services) |
|||
{ |
|||
ApplicationType = typeof(TApplication), |
|||
AuthorizationType = typeof(TAuthorization), |
|||
ScopeType = typeof(TScope), |
|||
TokenType = typeof(TToken) |
|||
}; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers the default OpenIddict services in the DI container, using the specified entities.
|
|||
/// </summary>
|
|||
/// <typeparam name="TApplication">The type of the Application entity.</typeparam>
|
|||
/// <typeparam name="TAuthorization">The type of the Authorization entity.</typeparam>
|
|||
/// <typeparam name="TScope">The type of the Scope entity.</typeparam>
|
|||
/// <typeparam name="TToken">The type of the Token entity.</typeparam>
|
|||
/// <param name="services">The services collection.</param>
|
|||
/// <param name="configuration">The configuration delegate used to register new services.</param>
|
|||
/// <returns>The <see cref="IServiceCollection"/>.</returns>
|
|||
public static IServiceCollection AddOpenIddict<TApplication, TAuthorization, TScope, TToken>( |
|||
[NotNull] this IServiceCollection services, |
|||
[NotNull] Action<OpenIddictBuilder> configuration) |
|||
where TApplication : class |
|||
where TAuthorization : class |
|||
where TScope : class |
|||
where TToken : class |
|||
{ |
|||
if (services == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(services)); |
|||
} |
|||
|
|||
if (configuration == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(configuration)); |
|||
} |
|||
|
|||
// Register the OpenIddict core services and invoke the configuration delegate.
|
|||
configuration(services.AddOpenIddict<TApplication, TAuthorization, TScope, TToken>()); |
|||
|
|||
return services; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
using System; |
|||
using System.Text; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.Abstractions; |
|||
|
|||
namespace OpenIddict.Core |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an application store.
|
|||
/// </summary>
|
|||
public class OpenIddictApplicationStoreResolver : IOpenIddictApplicationStoreResolver |
|||
{ |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictApplicationStoreResolver([NotNull] IServiceProvider provider) |
|||
{ |
|||
_provider = provider; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns an application store compatible with the specified application type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TApplication">The type of the Application entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictApplicationStore{TApplication}"/>.</returns>
|
|||
public IOpenIddictApplicationStore<TApplication> Get<TApplication>() where TApplication : class |
|||
{ |
|||
var store = _provider.GetService<IOpenIddictApplicationStore<TApplication>>(); |
|||
if (store == null) |
|||
{ |
|||
throw new InvalidOperationException(new StringBuilder() |
|||
.AppendLine("No application store has been registered in the dependency injection container.") |
|||
.Append("To register the Entity Framework Core stores, reference the 'OpenIddict.EntityFrameworkCore' ") |
|||
.AppendLine("package and call 'services.AddOpenIddict().AddCore().AddEntityFrameworkCoreStores()'.") |
|||
.Append("To register a custom store, create an implementation of 'IOpenIddictApplicationStore' and ") |
|||
.Append("use 'services.AddOpenIddict().AddCore().AddApplicationStore()' to add it to the DI container.") |
|||
.ToString()); |
|||
} |
|||
|
|||
return store; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
using System; |
|||
using System.Text; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.Abstractions; |
|||
|
|||
namespace OpenIddict.Core |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an authorization store.
|
|||
/// </summary>
|
|||
public class OpenIddictAuthorizationStoreResolver : IOpenIddictAuthorizationStoreResolver |
|||
{ |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictAuthorizationStoreResolver([NotNull] IServiceProvider provider) |
|||
{ |
|||
_provider = provider; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns an authorization store compatible with the specified authorization type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TAuthorization">The type of the Authorization entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictAuthorizationStore{TAuthorization}"/>.</returns>
|
|||
public IOpenIddictAuthorizationStore<TAuthorization> Get<TAuthorization>() where TAuthorization : class |
|||
{ |
|||
var store = _provider.GetService<IOpenIddictAuthorizationStore<TAuthorization>>(); |
|||
if (store == null) |
|||
{ |
|||
throw new InvalidOperationException(new StringBuilder() |
|||
.AppendLine("No authorization store has been registered in the dependency injection container.") |
|||
.Append("To register the Entity Framework Core stores, reference the 'OpenIddict.EntityFrameworkCore' ") |
|||
.AppendLine("package and call 'services.AddOpenIddict().AddCore().AddEntityFrameworkCoreStores()'.") |
|||
.Append("To register a custom store, create an implementation of 'IOpenIddictAuthorizationStore' and ") |
|||
.Append("use 'services.AddOpenIddict().AddCore().AddAuthorizationStore()' to add it to the DI container.") |
|||
.ToString()); |
|||
} |
|||
|
|||
return store; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
using System; |
|||
using System.Text; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.Abstractions; |
|||
|
|||
namespace OpenIddict.Core |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a scope store.
|
|||
/// </summary>
|
|||
public class OpenIddictScopeStoreResolver : IOpenIddictScopeStoreResolver |
|||
{ |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictScopeStoreResolver([NotNull] IServiceProvider provider) |
|||
{ |
|||
_provider = provider; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a scope store compatible with the specified scope type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TScope">The type of the Scope entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictScopeStore{TScope}"/>.</returns>
|
|||
public IOpenIddictScopeStore<TScope> Get<TScope>() where TScope : class |
|||
{ |
|||
var store = _provider.GetService<IOpenIddictScopeStore<TScope>>(); |
|||
if (store == null) |
|||
{ |
|||
throw new InvalidOperationException(new StringBuilder() |
|||
.AppendLine("No scope store has been registered in the dependency injection container.") |
|||
.Append("To register the Entity Framework Core stores, reference the 'OpenIddict.EntityFrameworkCore' ") |
|||
.AppendLine("package and call 'services.AddOpenIddict().AddCore().AddEntityFrameworkCoreStores()'.") |
|||
.Append("To register a custom store, create an implementation of 'IOpenIddictScopeStore' and ") |
|||
.Append("use 'services.AddOpenIddict().AddCore().AddScopeStore()' to add it to the DI container.") |
|||
.ToString()); |
|||
} |
|||
|
|||
return store; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
using System; |
|||
using System.Text; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.Abstractions; |
|||
|
|||
namespace OpenIddict.Core |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a token store.
|
|||
/// </summary>
|
|||
public class OpenIddictTokenStoreResolver : IOpenIddictTokenStoreResolver |
|||
{ |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictTokenStoreResolver([NotNull] IServiceProvider provider) |
|||
{ |
|||
_provider = provider; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a token store compatible with the specified token type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TToken">The type of the Token entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictTokenStore{TToken}"/>.</returns>
|
|||
public IOpenIddictTokenStore<TToken> Get<TToken>() where TToken : class |
|||
{ |
|||
var store = _provider.GetService<IOpenIddictTokenStore<TToken>>(); |
|||
if (store == null) |
|||
{ |
|||
throw new InvalidOperationException(new StringBuilder() |
|||
.AppendLine("No token store factory has been registered in the dependency injection container.") |
|||
.Append("To register the Entity Framework Core stores, reference the 'OpenIddict.EntityFrameworkCore' ") |
|||
.AppendLine("package and call 'services.AddOpenIddict().AddCore().AddEntityFrameworkCoreStores()'.") |
|||
.Append("To register a custom store, create an implementation of 'IOpenIddictTokenStore' and ") |
|||
.Append("use 'services.AddOpenIddict().AddCore().AddTokenStore()' to add it to the DI container.") |
|||
.ToString()); |
|||
} |
|||
|
|||
return store; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Data.Entity; |
|||
using System.Text; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.Core; |
|||
using OpenIddict.Models; |
|||
|
|||
namespace OpenIddict.EntityFramework |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an application store.
|
|||
/// </summary>
|
|||
public class OpenIddictApplicationStoreResolver<TContext> : IOpenIddictApplicationStoreResolver |
|||
where TContext : DbContext |
|||
{ |
|||
private static readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>(); |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictApplicationStoreResolver([NotNull] IServiceProvider provider) |
|||
{ |
|||
_provider = provider; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns an application store compatible with the specified application type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TApplication">The type of the Application entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictApplicationStore{TApplication}"/>.</returns>
|
|||
public IOpenIddictApplicationStore<TApplication> Get<TApplication>() where TApplication : class |
|||
{ |
|||
var store = _provider.GetService<IOpenIddictApplicationStore<TApplication>>(); |
|||
if (store != null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TApplication), key => |
|||
{ |
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(key, typeof(OpenIddictApplication<,,>)); |
|||
if (root == null) |
|||
{ |
|||
throw new InvalidOperationException(new StringBuilder() |
|||
.AppendLine("The specified application type is not compatible with the Entity Framework 6.x stores.") |
|||
.Append("When enabling the Entity Framework 6.x stores, make sure you use the built-in generic ") |
|||
.Append("'OpenIddictApplication' entity (from the 'OpenIddict.Models' package) or a custom entity ") |
|||
.Append("that inherits from the generic 'OpenIddictApplication' entity.") |
|||
.ToString()); |
|||
} |
|||
|
|||
return typeof(OpenIddictApplicationStore<,,,,>).MakeGenericType( |
|||
/* TApplication: */ key, |
|||
/* TAuthorization: */ root.GenericTypeArguments[1], |
|||
/* TToken: */ root.GenericTypeArguments[2], |
|||
/* TContext: */ typeof(TContext), |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictApplicationStore<TApplication>) _provider.GetRequiredService(type); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Data.Entity; |
|||
using System.Text; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.Core; |
|||
using OpenIddict.Models; |
|||
|
|||
namespace OpenIddict.EntityFramework |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an authorization store.
|
|||
/// </summary>
|
|||
public class OpenIddictAuthorizationStoreResolver<TContext> : IOpenIddictAuthorizationStoreResolver |
|||
where TContext : DbContext |
|||
{ |
|||
private static readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>(); |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictAuthorizationStoreResolver([NotNull] IServiceProvider provider) |
|||
{ |
|||
_provider = provider; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns an authorization store compatible with the specified authorization type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TAuthorization">The type of the Authorization entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictAuthorizationStore{TAuthorization}"/>.</returns>
|
|||
public IOpenIddictAuthorizationStore<TAuthorization> Get<TAuthorization>() where TAuthorization : class |
|||
{ |
|||
var store = _provider.GetService<IOpenIddictAuthorizationStore<TAuthorization>>(); |
|||
if (store != null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TAuthorization), key => |
|||
{ |
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(key, typeof(OpenIddictAuthorization<,,>)); |
|||
if (root == null) |
|||
{ |
|||
throw new InvalidOperationException(new StringBuilder() |
|||
.AppendLine("The specified authorization type is not compatible with the Entity Framework 6.x stores.") |
|||
.Append("When enabling the Entity Framework 6.x stores, make sure you use the built-in generic ") |
|||
.Append("'OpenIddictAuthorization' entity (from the 'OpenIddict.Models' package) or a custom entity ") |
|||
.Append("that inherits from the generic 'OpenIddictAuthorization' entity.") |
|||
.ToString()); |
|||
} |
|||
|
|||
return typeof(OpenIddictAuthorizationStore<,,,,>).MakeGenericType( |
|||
/* TAuthorization: */ key, |
|||
/* TApplication: */ root.GenericTypeArguments[1], |
|||
/* TToken: */ root.GenericTypeArguments[2], |
|||
/* TContext: */ typeof(TContext), |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictAuthorizationStore<TAuthorization>) _provider.GetRequiredService(type); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Data.Entity; |
|||
using System.Text; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.Core; |
|||
using OpenIddict.Models; |
|||
|
|||
namespace OpenIddict.EntityFramework |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a scope store.
|
|||
/// </summary>
|
|||
public class OpenIddictScopeStoreResolver<TContext> : IOpenIddictScopeStoreResolver |
|||
where TContext : DbContext |
|||
{ |
|||
private static readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>(); |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictScopeStoreResolver([NotNull] IServiceProvider provider) |
|||
{ |
|||
_provider = provider; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a scope store compatible with the specified scope type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TScope">The type of the Scope entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictScopeStore{TScope}"/>.</returns>
|
|||
public IOpenIddictScopeStore<TScope> Get<TScope>() where TScope : class |
|||
{ |
|||
var store = _provider.GetService<IOpenIddictScopeStore<TScope>>(); |
|||
if (store != null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TScope), key => |
|||
{ |
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(key, typeof(OpenIddictScope<>)); |
|||
if (root == null) |
|||
{ |
|||
throw new InvalidOperationException(new StringBuilder() |
|||
.AppendLine("The specified scope type is not compatible with the Entity Framework 6.x stores.") |
|||
.Append("When enabling the Entity Framework 6.x stores, make sure you use the built-in generic ") |
|||
.Append("'OpenIdScope' entity (from the 'OpenIddict.Models' package) or a custom entity ") |
|||
.Append("that inherits from the generic 'OpenIddictScope' entity.") |
|||
.ToString()); |
|||
} |
|||
|
|||
return typeof(OpenIddictScopeStore<,,>).MakeGenericType( |
|||
/* TScope: */ key, |
|||
/* TContext: */ typeof(TContext), |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictScopeStore<TScope>) _provider.GetRequiredService(type); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Data.Entity; |
|||
using System.Text; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.Core; |
|||
using OpenIddict.Models; |
|||
|
|||
namespace OpenIddict.EntityFramework |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a token store.
|
|||
/// </summary>
|
|||
public class OpenIddictTokenStoreResolver<TContext> : IOpenIddictTokenStoreResolver |
|||
where TContext : DbContext |
|||
{ |
|||
private static readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>(); |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictTokenStoreResolver([NotNull] IServiceProvider provider) |
|||
{ |
|||
_provider = provider; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a token store compatible with the specified token type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TToken">The type of the Token entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictTokenStore{TToken}"/>.</returns>
|
|||
public IOpenIddictTokenStore<TToken> Get<TToken>() where TToken : class |
|||
{ |
|||
var store = _provider.GetService<IOpenIddictTokenStore<TToken>>(); |
|||
if (store != null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TToken), key => |
|||
{ |
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(key, typeof(OpenIddictToken<,,>)); |
|||
if (root == null) |
|||
{ |
|||
throw new InvalidOperationException(new StringBuilder() |
|||
.AppendLine("The specified token type is not compatible with the Entity Framework 6.x stores.") |
|||
.Append("When enabling the Entity Framework 6.x stores, make sure you use the built-in generic ") |
|||
.Append("'OpenIddictToken' entity (from the 'OpenIddict.Models' package) or a custom entity ") |
|||
.Append("that inherits from the generic 'OpenIddictToken' entity.") |
|||
.ToString()); |
|||
} |
|||
|
|||
return typeof(OpenIddictTokenStore<,,,,>).MakeGenericType( |
|||
/* TToken: */ key, |
|||
/* TApplication: */ root.GenericTypeArguments[1], |
|||
/* TAuthorization: */ root.GenericTypeArguments[2], |
|||
/* TContext: */ typeof(TContext), |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictTokenStore<TToken>) _provider.GetRequiredService(type); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Text; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.Core; |
|||
using OpenIddict.Models; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an application store.
|
|||
/// </summary>
|
|||
public class OpenIddictApplicationStoreResolver<TContext> : IOpenIddictApplicationStoreResolver |
|||
where TContext : DbContext |
|||
{ |
|||
private static readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>(); |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictApplicationStoreResolver([NotNull] IServiceProvider provider) |
|||
{ |
|||
_provider = provider; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns an application store compatible with the specified application type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TApplication">The type of the Application entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictApplicationStore{TApplication}"/>.</returns>
|
|||
public IOpenIddictApplicationStore<TApplication> Get<TApplication>() where TApplication : class |
|||
{ |
|||
var store = _provider.GetService<IOpenIddictApplicationStore<TApplication>>(); |
|||
if (store != null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TApplication), key => |
|||
{ |
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(key, typeof(OpenIddictApplication<,,>)); |
|||
if (root == null) |
|||
{ |
|||
throw new InvalidOperationException(new StringBuilder() |
|||
.AppendLine("The specified application type is not compatible with the Entity Framework Core stores.") |
|||
.Append("When enabling the Entity Framework Core stores, make sure you use the built-in generic ") |
|||
.Append("'OpenIddictApplication' entity (from the 'OpenIddict.Models' package) or a custom entity ") |
|||
.Append("that inherits from the generic 'OpenIddictApplication' entity.") |
|||
.ToString()); |
|||
} |
|||
|
|||
return typeof(OpenIddictApplicationStore<,,,,>).MakeGenericType( |
|||
/* TApplication: */ key, |
|||
/* TAuthorization: */ root.GenericTypeArguments[1], |
|||
/* TToken: */ root.GenericTypeArguments[2], |
|||
/* TContext: */ typeof(TContext), |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictApplicationStore<TApplication>) _provider.GetRequiredService(type); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Text; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.Core; |
|||
using OpenIddict.Models; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an authorization store.
|
|||
/// </summary>
|
|||
public class OpenIddictAuthorizationStoreResolver<TContext> : IOpenIddictAuthorizationStoreResolver |
|||
where TContext : DbContext |
|||
{ |
|||
private static readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>(); |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictAuthorizationStoreResolver([NotNull] IServiceProvider provider) |
|||
{ |
|||
_provider = provider; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns an authorization store compatible with the specified authorization type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TAuthorization">The type of the Authorization entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictAuthorizationStore{TAuthorization}"/>.</returns>
|
|||
public IOpenIddictAuthorizationStore<TAuthorization> Get<TAuthorization>() where TAuthorization : class |
|||
{ |
|||
var store = _provider.GetService<IOpenIddictAuthorizationStore<TAuthorization>>(); |
|||
if (store != null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TAuthorization), key => |
|||
{ |
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(key, typeof(OpenIddictAuthorization<,,>)); |
|||
if (root == null) |
|||
{ |
|||
throw new InvalidOperationException(new StringBuilder() |
|||
.AppendLine("The specified authorization type is not compatible with the Entity Framework Core stores.") |
|||
.Append("When enabling the Entity Framework Core stores, make sure you use the built-in generic ") |
|||
.Append("'OpenIddictAuthorization' entity (from the 'OpenIddict.Models' package) or a custom entity ") |
|||
.Append("that inherits from the generic 'OpenIddictAuthorization' entity.") |
|||
.ToString()); |
|||
} |
|||
|
|||
return typeof(OpenIddictAuthorizationStore<,,,,>).MakeGenericType( |
|||
/* TAuthorization: */ key, |
|||
/* TApplication: */ root.GenericTypeArguments[1], |
|||
/* TToken: */ root.GenericTypeArguments[2], |
|||
/* TContext: */ typeof(TContext), |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictAuthorizationStore<TAuthorization>) _provider.GetRequiredService(type); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Text; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.Core; |
|||
using OpenIddict.Models; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a scope store.
|
|||
/// </summary>
|
|||
public class OpenIddictScopeStoreResolver<TContext> : IOpenIddictScopeStoreResolver |
|||
where TContext : DbContext |
|||
{ |
|||
private static readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>(); |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictScopeStoreResolver([NotNull] IServiceProvider provider) |
|||
{ |
|||
_provider = provider; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a scope store compatible with the specified scope type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TScope">The type of the Scope entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictScopeStore{TScope}"/>.</returns>
|
|||
public IOpenIddictScopeStore<TScope> Get<TScope>() where TScope : class |
|||
{ |
|||
var store = _provider.GetService<IOpenIddictScopeStore<TScope>>(); |
|||
if (store != null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TScope), key => |
|||
{ |
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(key, typeof(OpenIddictScope<>)); |
|||
if (root == null) |
|||
{ |
|||
throw new InvalidOperationException(new StringBuilder() |
|||
.AppendLine("The specified scope type is not compatible with the Entity Framework Core stores.") |
|||
.Append("When enabling the Entity Framework Core stores, make sure you use the built-in generic ") |
|||
.Append("'OpenIdScope' entity (from the 'OpenIddict.Models' package) or a custom entity ") |
|||
.Append("that inherits from the generic 'OpenIddictScope' entity.") |
|||
.ToString()); |
|||
} |
|||
|
|||
return typeof(OpenIddictScopeStore<,,>).MakeGenericType( |
|||
/* TScope: */ key, |
|||
/* TContext: */ typeof(TContext), |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictScopeStore<TScope>) _provider.GetRequiredService(type); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Text; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.Abstractions; |
|||
using OpenIddict.Core; |
|||
using OpenIddict.Models; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a token store.
|
|||
/// </summary>
|
|||
public class OpenIddictTokenStoreResolver<TContext> : IOpenIddictTokenStoreResolver |
|||
where TContext : DbContext |
|||
{ |
|||
private static readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>(); |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictTokenStoreResolver([NotNull] IServiceProvider provider) |
|||
{ |
|||
_provider = provider; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a token store compatible with the specified token type or throws an
|
|||
/// <see cref="InvalidOperationException"/> if no store can be built using the specified type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TToken">The type of the Token entity.</typeparam>
|
|||
/// <returns>An <see cref="IOpenIddictTokenStore{TToken}"/>.</returns>
|
|||
public IOpenIddictTokenStore<TToken> Get<TToken>() where TToken : class |
|||
{ |
|||
var store = _provider.GetService<IOpenIddictTokenStore<TToken>>(); |
|||
if (store != null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TToken), key => |
|||
{ |
|||
var root = OpenIddictCoreHelpers.FindGenericBaseType(key, typeof(OpenIddictToken<,,>)); |
|||
if (root == null) |
|||
{ |
|||
throw new InvalidOperationException(new StringBuilder() |
|||
.AppendLine("The specified token type is not compatible with the Entity Framework Core stores.") |
|||
.Append("When enabling the Entity Framework Core stores, make sure you use the built-in generic ") |
|||
.Append("'OpenIddictToken' entity (from the 'OpenIddict.Models' package) or a custom entity ") |
|||
.Append("that inherits from the generic 'OpenIddictToken' entity.") |
|||
.ToString()); |
|||
} |
|||
|
|||
return typeof(OpenIddictTokenStore<,,,,>).MakeGenericType( |
|||
/* TToken: */ key, |
|||
/* TApplication: */ root.GenericTypeArguments[1], |
|||
/* TAuthorization: */ root.GenericTypeArguments[2], |
|||
/* TContext: */ typeof(TContext), |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictTokenStore<TToken>) _provider.GetRequiredService(type); |
|||
} |
|||
} |
|||
} |
|||
File diff suppressed because it is too large
@ -0,0 +1,653 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.ComponentModel; |
|||
using System.IdentityModel.Tokens.Jwt; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using System.Security.Cryptography.X509Certificates; |
|||
using System.Threading; |
|||
using AspNet.Security.OpenIdConnect.Primitives; |
|||
using AspNet.Security.OpenIdConnect.Server; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.AspNetCore.DataProtection; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.IdentityModel.Tokens; |
|||
using OpenIddict.Core; |
|||
using OpenIddict.Server; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes the necessary methods required to configure the OpenIddict server services.
|
|||
/// </summary>
|
|||
public class OpenIddictServerBuilder |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of <see cref="OpenIddictServerBuilder"/>.
|
|||
/// </summary>
|
|||
/// <param name="services">The services collection.</param>
|
|||
public OpenIddictServerBuilder([NotNull] IServiceCollection services) |
|||
{ |
|||
if (services == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(services)); |
|||
} |
|||
|
|||
Services = services; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the services collection.
|
|||
/// </summary>
|
|||
[EditorBrowsable(EditorBrowsableState.Never)] |
|||
public IServiceCollection Services { get; } |
|||
|
|||
/// <summary>
|
|||
/// Amends the default OpenIddict server configuration.
|
|||
/// </summary>
|
|||
/// <param name="configuration">The delegate used to configure the OpenIddict options.</param>
|
|||
/// <remarks>This extension can be safely called multiple times.</remarks>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder Configure([NotNull] Action<OpenIddictServerOptions> configuration) |
|||
{ |
|||
if (configuration == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(configuration)); |
|||
} |
|||
|
|||
Services.Configure(OpenIdConnectServerDefaults.AuthenticationScheme, configuration); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers (and generates if necessary) a user-specific development
|
|||
/// certificate used to sign the JWT tokens issued by OpenIddict.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AddDevelopmentSigningCertificate() |
|||
=> Configure(options => options.SigningCredentials.AddDevelopmentCertificate()); |
|||
|
|||
/// <summary>
|
|||
/// Registers (and generates if necessary) a user-specific development
|
|||
/// certificate used to sign the JWT tokens issued by OpenIddict.
|
|||
/// </summary>
|
|||
/// <param name="subject">The subject name associated with the certificate.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AddDevelopmentSigningCertificate([NotNull] X500DistinguishedName subject) |
|||
{ |
|||
if (subject == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(subject)); |
|||
} |
|||
|
|||
return Configure(options => options.SigningCredentials.AddDevelopmentCertificate(subject)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers a new ephemeral key used to sign the JWT tokens issued by OpenIddict: the key
|
|||
/// is discarded when the application shuts down and tokens signed using this key are
|
|||
/// automatically invalidated. This method should only be used during development.
|
|||
/// On production, using a X.509 certificate stored in the machine store is recommended.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AddEphemeralSigningKey() |
|||
=> Configure(options => options.SigningCredentials.AddEphemeralKey()); |
|||
|
|||
/// <summary>
|
|||
/// Registers a new ephemeral key used to sign the JWT tokens issued by OpenIddict: the key
|
|||
/// is discarded when the application shuts down and tokens signed using this key are
|
|||
/// automatically invalidated. This method should only be used during development.
|
|||
/// On production, using a X.509 certificate stored in the machine store is recommended.
|
|||
/// </summary>
|
|||
/// <param name="algorithm">The algorithm associated with the signing key.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AddEphemeralSigningKey([NotNull] string algorithm) |
|||
{ |
|||
if (string.IsNullOrEmpty(algorithm)) |
|||
{ |
|||
throw new ArgumentException("The algorithm cannot be null or empty.", nameof(algorithm)); |
|||
} |
|||
|
|||
return Configure(options => options.SigningCredentials.AddEphemeralKey(algorithm)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers a <see cref="SecurityKey"/> used to encrypt the JWT access tokens issued by OpenIddict.
|
|||
/// </summary>
|
|||
/// <param name="key">The security key.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AddEncryptingKey([NotNull] SecurityKey key) |
|||
{ |
|||
if (key == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(key)); |
|||
} |
|||
|
|||
return Configure(options => options.EncryptingCredentials.AddKey(key)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers a <see cref="X509Certificate2"/> that is used to sign the JWT tokens issued by OpenIddict.
|
|||
/// </summary>
|
|||
/// <param name="certificate">The certificate used to sign the security tokens issued by the server.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AddSigningCertificate([NotNull] X509Certificate2 certificate) |
|||
{ |
|||
if (certificate == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(certificate)); |
|||
} |
|||
|
|||
if (!certificate.HasPrivateKey) |
|||
{ |
|||
throw new InvalidOperationException("The certificate doesn't contain the required private key."); |
|||
} |
|||
|
|||
return Configure(options => options.SigningCredentials.AddCertificate(certificate)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers a <see cref="X509Certificate2"/> retrieved from an
|
|||
/// embedded resource and used to sign the JWT tokens issued by OpenIddict.
|
|||
/// </summary>
|
|||
/// <param name="assembly">The assembly containing the certificate.</param>
|
|||
/// <param name="resource">The name of the embedded resource.</param>
|
|||
/// <param name="password">The password used to open the certificate.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AddSigningCertificate( |
|||
[NotNull] Assembly assembly, [NotNull] string resource, [NotNull] string password) |
|||
{ |
|||
if (assembly == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(assembly)); |
|||
} |
|||
|
|||
if (string.IsNullOrEmpty(resource)) |
|||
{ |
|||
throw new ArgumentNullException(nameof(resource)); |
|||
} |
|||
|
|||
if (string.IsNullOrEmpty(password)) |
|||
{ |
|||
throw new ArgumentNullException(nameof(password)); |
|||
} |
|||
|
|||
return Configure(options => options.SigningCredentials.AddCertificate(assembly, resource, password)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers a <see cref="X509Certificate2"/> extracted from a
|
|||
/// stream and used to sign the JWT tokens issued by OpenIddict.
|
|||
/// </summary>
|
|||
/// <param name="stream">The stream containing the certificate.</param>
|
|||
/// <param name="password">The password used to open the certificate.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AddSigningCertificate([NotNull] Stream stream, [NotNull] string password) |
|||
{ |
|||
if (stream == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(stream)); |
|||
} |
|||
|
|||
if (string.IsNullOrEmpty(password)) |
|||
{ |
|||
throw new ArgumentNullException(nameof(password)); |
|||
} |
|||
|
|||
return Configure(options => options.SigningCredentials.AddCertificate(stream, password)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers a <see cref="X509Certificate2"/> extracted from a
|
|||
/// stream and used to sign the JWT tokens issued by OpenIddict.
|
|||
/// </summary>
|
|||
/// <param name="stream">The stream containing the certificate.</param>
|
|||
/// <param name="password">The password used to open the certificate.</param>
|
|||
/// <param name="flags">
|
|||
/// An enumeration of flags indicating how and where
|
|||
/// to store the private key of the certificate.
|
|||
/// </param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AddSigningCertificate( |
|||
[NotNull] Stream stream, [NotNull] string password, X509KeyStorageFlags flags) |
|||
{ |
|||
if (stream == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(stream)); |
|||
} |
|||
|
|||
if (string.IsNullOrEmpty(password)) |
|||
{ |
|||
throw new ArgumentNullException(nameof(password)); |
|||
} |
|||
|
|||
return Configure(options => options.SigningCredentials.AddCertificate(stream, password, flags)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers a <see cref="X509Certificate2"/> retrieved from the X.509
|
|||
/// machine store and used to sign the JWT tokens issued by OpenIddict.
|
|||
/// </summary>
|
|||
/// <param name="thumbprint">The thumbprint of the certificate used to identify it in the X.509 store.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AddSigningCertificate([NotNull] string thumbprint) |
|||
{ |
|||
if (string.IsNullOrEmpty(thumbprint)) |
|||
{ |
|||
throw new ArgumentNullException(nameof(thumbprint)); |
|||
} |
|||
|
|||
return Configure(options => options.SigningCredentials.AddCertificate(thumbprint)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers a <see cref="X509Certificate2"/> retrieved from the given
|
|||
/// X.509 store and used to sign the JWT tokens issued by OpenIddict.
|
|||
/// </summary>
|
|||
/// <param name="thumbprint">The thumbprint of the certificate used to identify it in the X.509 store.</param>
|
|||
/// <param name="name">The name of the X.509 store.</param>
|
|||
/// <param name="location">The location of the X.509 store.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AddSigningCertificate( |
|||
[NotNull] string thumbprint, StoreName name, StoreLocation location) |
|||
{ |
|||
if (string.IsNullOrEmpty(thumbprint)) |
|||
{ |
|||
throw new ArgumentNullException(nameof(thumbprint)); |
|||
} |
|||
|
|||
return Configure(options => options.SigningCredentials.AddCertificate(thumbprint, name, location)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers a <see cref="SecurityKey"/> used to sign the JWT tokens issued by OpenIddict.
|
|||
/// Note: using <see cref="RsaSecurityKey"/> asymmetric keys is recommended on production.
|
|||
/// </summary>
|
|||
/// <param name="key">The security key.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AddSigningKey([NotNull] SecurityKey key) |
|||
{ |
|||
if (key == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(key)); |
|||
} |
|||
|
|||
return Configure(options => options.SigningCredentials.AddKey(key)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Enables authorization code flow support. For more information
|
|||
/// about this specific OAuth2/OpenID Connect flow, visit
|
|||
/// https://tools.ietf.org/html/rfc6749#section-4.1 and
|
|||
/// http://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AllowAuthorizationCodeFlow() |
|||
=> Configure(options => options.GrantTypes.Add(OpenIdConnectConstants.GrantTypes.AuthorizationCode)); |
|||
|
|||
/// <summary>
|
|||
/// Enables client credentials flow support. For more information about this
|
|||
/// specific OAuth2 flow, visit https://tools.ietf.org/html/rfc6749#section-4.4.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AllowClientCredentialsFlow() |
|||
=> Configure(options => options.GrantTypes.Add(OpenIdConnectConstants.GrantTypes.ClientCredentials)); |
|||
|
|||
/// <summary>
|
|||
/// Enables custom grant type support.
|
|||
/// </summary>
|
|||
/// <param name="type">The grant type associated with the flow.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AllowCustomFlow([NotNull] string type) |
|||
{ |
|||
if (string.IsNullOrEmpty(type)) |
|||
{ |
|||
throw new ArgumentException("The grant type cannot be null or empty.", nameof(type)); |
|||
} |
|||
|
|||
return Configure(options => options.GrantTypes.Add(type)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Enables implicit flow support. For more information
|
|||
/// about this specific OAuth2/OpenID Connect flow, visit
|
|||
/// https://tools.ietf.org/html/rfc6749#section-4.2 and
|
|||
/// http://openid.net/specs/openid-connect-core-1_0.html#ImplicitFlowAuth.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AllowImplicitFlow() |
|||
=> Configure(options => options.GrantTypes.Add(OpenIdConnectConstants.GrantTypes.Implicit)); |
|||
|
|||
/// <summary>
|
|||
/// Enables password flow support. For more information about this specific
|
|||
/// OAuth2 flow, visit https://tools.ietf.org/html/rfc6749#section-4.3.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AllowPasswordFlow() |
|||
=> Configure(options => options.GrantTypes.Add(OpenIdConnectConstants.GrantTypes.Password)); |
|||
|
|||
/// <summary>
|
|||
/// Enables refresh token flow support. For more information about this
|
|||
/// specific OAuth2 flow, visit https://tools.ietf.org/html/rfc6749#section-6.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder AllowRefreshTokenFlow() |
|||
=> Configure(options => options.GrantTypes.Add(OpenIdConnectConstants.GrantTypes.RefreshToken)); |
|||
|
|||
/// <summary>
|
|||
/// Disables the configuration endpoint.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder DisableConfigurationEndpoint() |
|||
=> Configure(options => options.ConfigurationEndpointPath = PathString.Empty); |
|||
|
|||
/// <summary>
|
|||
/// Disables the cryptography endpoint.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder DisableCryptographyEndpoint() |
|||
=> Configure(options => options.CryptographyEndpointPath = PathString.Empty); |
|||
|
|||
/// <summary>
|
|||
/// Disables the HTTPS requirement during development.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder DisableHttpsRequirement() |
|||
=> Configure(options => options.AllowInsecureHttp = true); |
|||
|
|||
/// <summary>
|
|||
/// Disables sliding expiration. When using this option, refresh tokens
|
|||
/// are issued with a fixed expiration date: when it expires, a complete
|
|||
/// authorization flow must be started to retrieve a new refresh token.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder DisableSlidingExpiration() |
|||
=> Configure(options => options.UseSlidingExpiration = false); |
|||
|
|||
/// <summary>
|
|||
/// Disables token revocation, so that authorization code and
|
|||
/// refresh tokens are never stored and cannot be revoked.
|
|||
/// Using this option is generally not recommended.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder DisableTokenRevocation() |
|||
=> Configure(options => options.DisableTokenRevocation = true); |
|||
|
|||
/// <summary>
|
|||
/// Enables the authorization endpoint.
|
|||
/// </summary>
|
|||
/// <param name="path">The relative path of the authorization endpoint.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder EnableAuthorizationEndpoint(PathString path) |
|||
{ |
|||
if (!path.HasValue) |
|||
{ |
|||
throw new ArgumentException("The path cannot be empty.", nameof(path)); |
|||
} |
|||
|
|||
return Configure(options => options.AuthorizationEndpointPath = path); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Enables the introspection endpoint.
|
|||
/// </summary>
|
|||
/// <param name="path">The relative path of the logout endpoint.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder EnableIntrospectionEndpoint(PathString path) |
|||
{ |
|||
if (!path.HasValue) |
|||
{ |
|||
throw new ArgumentException("The path cannot be empty.", nameof(path)); |
|||
} |
|||
|
|||
return Configure(options => options.IntrospectionEndpointPath = path); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Enables the logout endpoint.
|
|||
/// </summary>
|
|||
/// <param name="path">The relative path of the logout endpoint.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder EnableLogoutEndpoint(PathString path) |
|||
{ |
|||
if (!path.HasValue) |
|||
{ |
|||
throw new ArgumentException("The path cannot be empty.", nameof(path)); |
|||
} |
|||
|
|||
return Configure(options => options.LogoutEndpointPath = path); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Enables request caching, so that both authorization and logout requests
|
|||
/// are automatically stored in the distributed cache, which allows flowing
|
|||
/// large payloads across requests. Enabling this option is recommended
|
|||
/// when using external authentication providers or when large GET or POST
|
|||
/// OpenID Connect authorization requests support is required.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder EnableRequestCaching() |
|||
=> Configure(options => options.EnableRequestCaching = true); |
|||
|
|||
/// <summary>
|
|||
/// Enables the revocation endpoint.
|
|||
/// </summary>
|
|||
/// <param name="path">The relative path of the revocation endpoint.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder EnableRevocationEndpoint( PathString path) |
|||
{ |
|||
if (!path.HasValue) |
|||
{ |
|||
throw new ArgumentException("The path cannot be empty.", nameof(path)); |
|||
} |
|||
|
|||
return Configure(options => options.RevocationEndpointPath = path); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Rejects authorization and token requests that specify scopes that have not been
|
|||
/// registered using <see cref="RegisterScopes(string[])"/> or
|
|||
/// <see cref="OpenIddictScopeManager{TScope}.CreateAsync(TScope, CancellationToken)"/>.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder EnableScopeValidation() |
|||
=> Configure(options => options.EnableScopeValidation = true); |
|||
|
|||
/// <summary>
|
|||
/// Enables the token endpoint.
|
|||
/// </summary>
|
|||
/// <param name="path">The relative path of the token endpoint.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder EnableTokenEndpoint(PathString path) |
|||
{ |
|||
if (!path.HasValue) |
|||
{ |
|||
throw new ArgumentException("The path cannot be empty.", nameof(path)); |
|||
} |
|||
|
|||
return Configure(options => options.TokenEndpointPath = path); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Enables the userinfo endpoint.
|
|||
/// </summary>
|
|||
/// <param name="path">The relative path of the userinfo endpoint.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder EnableUserinfoEndpoint(PathString path) |
|||
{ |
|||
if (!path.HasValue) |
|||
{ |
|||
throw new ArgumentException("The path cannot be empty.", nameof(path)); |
|||
} |
|||
|
|||
return Configure(options => options.UserinfoEndpointPath = path); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Makes client identification mandatory so that token and revocation
|
|||
/// requests that don't specify a client_id are automatically rejected.
|
|||
/// Note: enabling this option doesn't prevent public clients from using
|
|||
/// the token and revocation endpoints, but specifying a client_id is required.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder RequireClientIdentification() |
|||
=> Configure(options => options.RequireClientIdentification = true); |
|||
|
|||
/// <summary>
|
|||
/// Sets the access token lifetime, after which client applications must retrieve
|
|||
/// a new access token by making a grant_type=refresh_token token request
|
|||
/// or a prompt=none authorization request, depending on the selected flow.
|
|||
/// Using long-lived access tokens or tokens that never expire is not recommended.
|
|||
/// </summary>
|
|||
/// <param name="lifetime">The access token lifetime.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder SetAccessTokenLifetime(TimeSpan lifetime) |
|||
=> Configure(options => options.AccessTokenLifetime = lifetime); |
|||
|
|||
/// <summary>
|
|||
/// Sets the authorization code lifetime, after which client applications
|
|||
/// are unable to send a grant_type=authorization_code token request.
|
|||
/// Using short-lived authorization codes is strongly recommended.
|
|||
/// </summary>
|
|||
/// <param name="lifetime">The authorization code lifetime.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder SetAuthorizationCodeLifetime(TimeSpan lifetime) |
|||
=> Configure(options => options.AuthorizationCodeLifetime = lifetime); |
|||
|
|||
/// <summary>
|
|||
/// Sets the identity token lifetime, after which client
|
|||
/// applications should refuse processing identity tokens.
|
|||
/// </summary>
|
|||
/// <param name="lifetime">The identity token lifetime.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder SetIdentityTokenLifetime(TimeSpan lifetime) |
|||
=> Configure(options => options.IdentityTokenLifetime = lifetime); |
|||
|
|||
/// <summary>
|
|||
/// Sets the refresh token lifetime, after which client applications must get
|
|||
/// a new authorization from the user. When sliding expiration is enabled,
|
|||
/// a new refresh token is always issued to the client application,
|
|||
/// which prolongs the validity period of the refresh token.
|
|||
/// </summary>
|
|||
/// <param name="lifetime">The refresh token lifetime.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder SetRefreshTokenLifetime(TimeSpan lifetime) |
|||
=> Configure(options => options.RefreshTokenLifetime = lifetime); |
|||
|
|||
/// <summary>
|
|||
/// Sets the issuer address, which is used as the base address
|
|||
/// for the endpoint URIs returned from the discovery endpoint.
|
|||
/// </summary>
|
|||
/// <param name="address">The issuer address.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder SetIssuer([NotNull] Uri address) |
|||
{ |
|||
if (address == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(address)); |
|||
} |
|||
|
|||
return Configure(options => options.Issuer = address); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers the specified claims as supported claims so
|
|||
/// they can be returned as part of the discovery document.
|
|||
/// </summary>
|
|||
/// <param name="claims">The supported claims.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder RegisterClaims([NotNull] params string[] claims) |
|||
{ |
|||
if (claims == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(claims)); |
|||
} |
|||
|
|||
if (claims.Any(claim => string.IsNullOrEmpty(claim))) |
|||
{ |
|||
throw new ArgumentException("Claims cannot be null or empty.", nameof(claims)); |
|||
} |
|||
|
|||
return Configure(options => options.Claims.UnionWith(claims)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers the specified scopes as supported scopes so
|
|||
/// they can be returned as part of the discovery document.
|
|||
/// </summary>
|
|||
/// <param name="scopes">The supported scopes.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder RegisterScopes([NotNull] params string[] scopes) |
|||
{ |
|||
if (scopes == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(scopes)); |
|||
} |
|||
|
|||
if (scopes.Any(scope => string.IsNullOrEmpty(scope))) |
|||
{ |
|||
throw new ArgumentException("Scopes cannot be null or empty.", nameof(scopes)); |
|||
} |
|||
|
|||
return Configure(options => options.Scopes.UnionWith(scopes)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Configures OpenIddict to use a specific data protection provider
|
|||
/// instead of relying on the default instance provided by the DI container.
|
|||
/// </summary>
|
|||
/// <param name="provider">The data protection provider used to create token protectors.</param>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder UseDataProtectionProvider([NotNull] IDataProtectionProvider provider) |
|||
{ |
|||
if (provider == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(provider)); |
|||
} |
|||
|
|||
return Configure(options => options.DataProtectionProvider = provider); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets JWT as the default token format for access tokens.
|
|||
/// Note: this option cannot be used when using reference tokens.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder UseJsonWebTokens() |
|||
=> Configure(options => options.AccessTokenHandler = new JwtSecurityTokenHandler |
|||
{ |
|||
InboundClaimTypeMap = new Dictionary<string, string>(), |
|||
OutboundClaimTypeMap = new Dictionary<string, string>() |
|||
}); |
|||
|
|||
/// <summary>
|
|||
/// Configures OpenIddict to use reference tokens, so that authorization codes,
|
|||
/// access tokens and refresh tokens are stored as ciphertext in the database
|
|||
/// (only an identifier is returned to the client application). Enabling this option
|
|||
/// is useful to keep track of all the issued tokens, when storing a very large
|
|||
/// number of claims in the authorization codes, access tokens and refresh tokens
|
|||
/// or when immediate revocation of reference access tokens is desired.
|
|||
/// Note: this option cannot be used when configuring JWT as the access token format.
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder UseReferenceTokens() |
|||
=> Configure(options => options.UseReferenceTokens = true); |
|||
|
|||
/// <summary>
|
|||
/// Configures OpenIddict to use rolling refresh tokens. When this option is enabled,
|
|||
/// a new refresh token is always issued for each refresh token request (and the previous
|
|||
/// one is automatically revoked unless token revocation was explicitly disabled).
|
|||
/// </summary>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public OpenIddictServerBuilder UseRollingTokens() |
|||
=> Configure(options => options.UseRollingTokens = true); |
|||
} |
|||
} |
|||
@ -0,0 +1,123 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.Text; |
|||
using AspNet.Security.OpenIdConnect.Server; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.AspNetCore.Authentication; |
|||
using Microsoft.Extensions.DependencyInjection.Extensions; |
|||
using Microsoft.Extensions.Options; |
|||
using OpenIddict.Server; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
public static class OpenIddictServerExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Registers the OpenIddict token server services in the DI container.
|
|||
/// </summary>
|
|||
/// <param name="builder">The services builder used by OpenIddict to register new services.</param>
|
|||
/// <remarks>This extension can be safely called multiple times.</remarks>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public static OpenIddictServerBuilder AddServer([NotNull] this OpenIddictBuilder builder) |
|||
{ |
|||
if (builder == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(builder)); |
|||
} |
|||
|
|||
builder.Services.AddAuthentication(); |
|||
|
|||
// Register the options initializers used by the OpenID Connect server handler and OpenIddict.
|
|||
// Note: TryAddEnumerable() is used here to ensure the initializers are only registered once.
|
|||
builder.Services.TryAddEnumerable(new[] |
|||
{ |
|||
ServiceDescriptor.Singleton<IPostConfigureOptions<OpenIddictServerOptions>, |
|||
OpenIddictServerInitializer>(), |
|||
ServiceDescriptor.Singleton<IPostConfigureOptions<OpenIddictServerOptions>, |
|||
OpenIdConnectServerInitializer>() |
|||
}); |
|||
|
|||
// Register the OpenIddict handler/provider.
|
|||
builder.Services.TryAddScoped(typeof(OpenIddictServerProvider<,,,>)); |
|||
builder.Services.TryAddScoped<OpenIddictServerHandler>(); |
|||
builder.Services.TryAddScoped(provider => |
|||
{ |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictServerOptions>>() |
|||
.Get(OpenIdConnectServerDefaults.AuthenticationScheme); |
|||
|
|||
if (options == null) |
|||
{ |
|||
throw new InvalidOperationException("The OpenIddict validation options cannot be resolved."); |
|||
} |
|||
|
|||
if (options.ApplicationType == null || options.AuthorizationType == null || |
|||
options.ScopeType == null || options.TokenType == null) |
|||
{ |
|||
throw new InvalidOperationException(new StringBuilder() |
|||
.AppendLine("The entity types must be configured for the token server services to work correctly.") |
|||
.Append("To configure the entities, use either 'services.AddOpenIddict().AddCore().UseDefaultModels()' ") |
|||
.Append("or 'services.AddOpenIddict().AddCore().UseCustomModels()'.") |
|||
.ToString()); |
|||
} |
|||
|
|||
var type = typeof(OpenIddictServerProvider<,,,>).MakeGenericType( |
|||
/* TApplication: */ options.ApplicationType, |
|||
/* TAuthorization: */ options.AuthorizationType, |
|||
/* TScope: */ options.ScopeType, |
|||
/* TToken: */ options.TokenType); |
|||
|
|||
return (OpenIdConnectServerProvider) provider.GetRequiredService(type); |
|||
}); |
|||
|
|||
// Register the OpenID Connect server handler in the authentication options,
|
|||
// so it can be discovered by the default authentication handler provider.
|
|||
builder.Services.Configure<AuthenticationOptions>(options => |
|||
{ |
|||
// Note: this method is guaranteed to be idempotent. To prevent multiple schemes from being
|
|||
// registered (which would result in an exception being thrown), a manual check is made here.
|
|||
if (options.SchemeMap.ContainsKey(OpenIdConnectServerDefaults.AuthenticationScheme)) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
options.AddScheme(OpenIdConnectServerDefaults.AuthenticationScheme, scheme => |
|||
{ |
|||
scheme.HandlerType = typeof(OpenIddictServerHandler); |
|||
}); |
|||
}); |
|||
|
|||
return new OpenIddictServerBuilder(builder.Services); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers the OpenIddict token server services in the DI container.
|
|||
/// </summary>
|
|||
/// <param name="builder">The services builder used by OpenIddict to register new services.</param>
|
|||
/// <param name="configuration">The configuration delegate used to configure the server services.</param>
|
|||
/// <remarks>This extension can be safely called multiple times.</remarks>
|
|||
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
|
|||
public static OpenIddictBuilder AddServer( |
|||
[NotNull] this OpenIddictBuilder builder, |
|||
[NotNull] Action<OpenIddictServerBuilder> configuration) |
|||
{ |
|||
if (builder == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(builder)); |
|||
} |
|||
|
|||
if (configuration == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(configuration)); |
|||
} |
|||
|
|||
configuration(builder.AddServer()); |
|||
|
|||
return builder; |
|||
} |
|||
} |
|||
} |
|||
@ -1,329 +0,0 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
using Moq; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Core.Tests |
|||
{ |
|||
public class OpenIddictBuilderTests |
|||
{ |
|||
[Fact] |
|||
public void AddApplicationManager_ThrowsAnExceptionForInvalidManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.ApplicationType = typeof(object); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => builder.AddApplicationManager(typeof(object))); |
|||
|
|||
Assert.Equal("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddApplicationManager_OverridesDefaultManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.ApplicationType = typeof(object); |
|||
|
|||
var type = new Mock<OpenIddictApplicationManager<object>>( |
|||
Mock.Of<IOpenIddictApplicationStore<object>>(), |
|||
Mock.Of<ILogger<OpenIddictApplicationManager<object>>>()).Object.GetType(); |
|||
|
|||
// Act
|
|||
builder.AddApplicationManager(type); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var manager = provider.GetRequiredService<OpenIddictApplicationManager<object>>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType(type, manager); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddApplicationStore_ThrowsAnExceptionForInvalidStore() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.ApplicationType = typeof(object); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => builder.AddApplicationStore(typeof(object))); |
|||
|
|||
Assert.Equal("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddApplicationStore_OverridesDefaultManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.ApplicationType = typeof(object); |
|||
|
|||
var type = Mock.Of<IOpenIddictApplicationStore<object>>().GetType(); |
|||
|
|||
// Act
|
|||
builder.AddApplicationStore(type); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var store = provider.GetRequiredService<IOpenIddictApplicationStore<object>>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType(type, store); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddAuthorizationManager_ThrowsAnExceptionForInvalidManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.AuthorizationType = typeof(object); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => builder.AddAuthorizationManager(typeof(object))); |
|||
|
|||
Assert.Equal("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddAuthorizationManager_OverridesDefaultManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.AuthorizationType = typeof(object); |
|||
|
|||
var type = new Mock<OpenIddictAuthorizationManager<object>>( |
|||
Mock.Of<IOpenIddictAuthorizationStore<object>>(), |
|||
Mock.Of<ILogger<OpenIddictAuthorizationManager<object>>>()).Object.GetType(); |
|||
|
|||
// Act
|
|||
builder.AddAuthorizationManager(type); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var manager = provider.GetRequiredService<OpenIddictAuthorizationManager<object>>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType(type, manager); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddAuthorizationStore_ThrowsAnExceptionForInvalidStore() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.AuthorizationType = typeof(object); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => builder.AddAuthorizationStore(typeof(object))); |
|||
|
|||
Assert.Equal("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddAuthorizationStore_OverridesDefaultManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.AuthorizationType = typeof(object); |
|||
|
|||
var type = Mock.Of<IOpenIddictAuthorizationStore<object>>().GetType(); |
|||
|
|||
// Act
|
|||
builder.AddAuthorizationStore(type); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var store = provider.GetRequiredService<IOpenIddictAuthorizationStore<object>>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType(type, store); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddScopeManager_ThrowsAnExceptionForInvalidManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.ScopeType = typeof(object); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => builder.AddScopeManager(typeof(object))); |
|||
|
|||
Assert.Equal("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddScopeManager_OverridesDefaultManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.ScopeType = typeof(object); |
|||
|
|||
var type = new Mock<OpenIddictScopeManager<object>>( |
|||
Mock.Of<IOpenIddictScopeStore<object>>(), |
|||
Mock.Of<ILogger<OpenIddictScopeManager<object>>>()).Object.GetType(); |
|||
|
|||
// Act
|
|||
builder.AddScopeManager(type); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var manager = provider.GetRequiredService<OpenIddictScopeManager<object>>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType(type, manager); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddScopeStore_ThrowsAnExceptionForInvalidStore() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.ScopeType = typeof(object); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => builder.AddScopeStore(typeof(object))); |
|||
|
|||
Assert.Equal("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddScopeStore_OverridesDefaultManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.ScopeType = typeof(object); |
|||
|
|||
var type = Mock.Of<IOpenIddictScopeStore<object>>().GetType(); |
|||
|
|||
// Act
|
|||
builder.AddScopeStore(type); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var store = provider.GetRequiredService<IOpenIddictScopeStore<object>>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType(type, store); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddTokenManager_ThrowsAnExceptionForInvalidManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.TokenType = typeof(object); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => builder.AddTokenManager(typeof(object))); |
|||
|
|||
Assert.Equal("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddTokenManager_OverridesDefaultManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.TokenType = typeof(object); |
|||
|
|||
var type = new Mock<OpenIddictTokenManager<object>>( |
|||
Mock.Of<IOpenIddictTokenStore<object>>(), |
|||
Mock.Of<ILogger<OpenIddictTokenManager<object>>>()).Object.GetType(); |
|||
|
|||
// Act
|
|||
builder.AddTokenManager(type); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var manager = provider.GetRequiredService<OpenIddictTokenManager<object>>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType(type, manager); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddTokenStore_ThrowsAnExceptionForInvalidStore() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.TokenType = typeof(object); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(() => builder.AddTokenStore(typeof(object))); |
|||
|
|||
Assert.Equal("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddTokenStore_OverridesDefaultManager() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictBuilder(services); |
|||
builder.TokenType = typeof(object); |
|||
|
|||
var type = Mock.Of<IOpenIddictTokenStore<object>>().GetType(); |
|||
|
|||
// Act
|
|||
builder.AddTokenStore(type); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var store = provider.GetRequiredService<IOpenIddictTokenStore<object>>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType(type, store); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,487 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using OpenIddict.Abstractions; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Core.Tests |
|||
{ |
|||
public class OpenIddictCoreBuilderTests |
|||
{ |
|||
[Fact] |
|||
public void ReplaceApplicationManager_ThrowsAnExceptionForInvalidManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentException>(() => builder.ReplaceApplicationManager(typeof(object))); |
|||
|
|||
Assert.Equal("type", exception.ParamName); |
|||
Assert.StartsWith("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceApplicationManager_OverridesDefaultOpenGenericManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceApplicationManager(typeof(OpenGenericApplicationManager<>)); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => |
|||
service.ServiceType == typeof(OpenIddictApplicationManager<>) && |
|||
service.ImplementationType == typeof(OpenGenericApplicationManager<>)); |
|||
Assert.DoesNotContain(services, service => |
|||
service.ServiceType == typeof(OpenIddictApplicationManager<>) && |
|||
service.ImplementationType == typeof(OpenIddictApplicationManager<>)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceApplicationManager_AddsClosedGenericManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceApplicationManager(typeof(ClosedGenericApplicationManager)); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => |
|||
service.ServiceType == typeof(OpenIddictApplicationManager<CustomApplication>) && |
|||
service.ImplementationType == typeof(ClosedGenericApplicationManager)); |
|||
Assert.Contains(services, service => |
|||
service.ServiceType == typeof(OpenIddictApplicationManager<>) && |
|||
service.ImplementationType == typeof(OpenIddictApplicationManager<>)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceApplicationStoreResolver_ThrowsAnExceptionForInvalidStoreResolver() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentException>(() => builder.ReplaceApplicationStoreResolver(typeof(object))); |
|||
|
|||
Assert.Equal("type", exception.ParamName); |
|||
Assert.StartsWith("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceApplicationStoreResolver_OverridesDefaultManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
var type = Mock.Of<IOpenIddictApplicationStoreResolver>().GetType(); |
|||
|
|||
// Act
|
|||
builder.ReplaceApplicationStoreResolver(type); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var store = provider.GetRequiredService<IOpenIddictApplicationStoreResolver>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType(type, store); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceAuthorizationManager_ThrowsAnExceptionForInvalidManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentException>(() => builder.ReplaceAuthorizationManager(typeof(object))); |
|||
|
|||
Assert.Equal("type", exception.ParamName); |
|||
Assert.StartsWith("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceAuthorizationManager_OverridesDefaultOpenGenericManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceAuthorizationManager(typeof(OpenGenericAuthorizationManager<>)); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => |
|||
service.ServiceType == typeof(OpenIddictAuthorizationManager<>) && |
|||
service.ImplementationType == typeof(OpenGenericAuthorizationManager<>)); |
|||
Assert.DoesNotContain(services, service => |
|||
service.ServiceType == typeof(OpenIddictAuthorizationManager<>) && |
|||
service.ImplementationType == typeof(OpenIddictAuthorizationManager<>)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceAuthorizationManager_AddsClosedGenericManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceAuthorizationManager(typeof(ClosedGenericAuthorizationManager)); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => |
|||
service.ServiceType == typeof(OpenIddictAuthorizationManager<CustomAuthorization>) && |
|||
service.ImplementationType == typeof(ClosedGenericAuthorizationManager)); |
|||
Assert.Contains(services, service => |
|||
service.ServiceType == typeof(OpenIddictAuthorizationManager<>) && |
|||
service.ImplementationType == typeof(OpenIddictAuthorizationManager<>)); |
|||
} |
|||
[Fact] |
|||
public void ReplaceAuthorizationStoreResolver_ThrowsAnExceptionForInvalidStoreResolver() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentException>(() => builder.ReplaceAuthorizationStoreResolver(typeof(object))); |
|||
|
|||
Assert.Equal("type", exception.ParamName); |
|||
Assert.StartsWith("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceAuthorizationStoreResolver_OverridesDefaultManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
var type = Mock.Of<IOpenIddictAuthorizationStoreResolver>().GetType(); |
|||
|
|||
// Act
|
|||
builder.ReplaceAuthorizationStoreResolver(type); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var store = provider.GetRequiredService<IOpenIddictAuthorizationStoreResolver>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType(type, store); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceScopeManager_ThrowsAnExceptionForInvalidManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentException>(() => builder.ReplaceScopeManager(typeof(object))); |
|||
|
|||
Assert.Equal("type", exception.ParamName); |
|||
Assert.StartsWith("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceScopeManager_OverridesDefaultOpenGenericManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceScopeManager(typeof(OpenGenericScopeManager<>)); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => |
|||
service.ServiceType == typeof(OpenIddictScopeManager<>) && |
|||
service.ImplementationType == typeof(OpenGenericScopeManager<>)); |
|||
Assert.DoesNotContain(services, service => |
|||
service.ServiceType == typeof(OpenIddictScopeManager<>) && |
|||
service.ImplementationType == typeof(OpenIddictScopeManager<>)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceScopeManager_AddsClosedGenericManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceScopeManager(typeof(ClosedGenericScopeManager)); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => |
|||
service.ServiceType == typeof(OpenIddictScopeManager<CustomScope>) && |
|||
service.ImplementationType == typeof(ClosedGenericScopeManager)); |
|||
Assert.Contains(services, service => |
|||
service.ServiceType == typeof(OpenIddictScopeManager<>) && |
|||
service.ImplementationType == typeof(OpenIddictScopeManager<>)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceScopeStoreResolver_ThrowsAnExceptionForInvalidStoreResolver() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentException>(() => builder.ReplaceScopeStoreResolver(typeof(object))); |
|||
|
|||
Assert.Equal("type", exception.ParamName); |
|||
Assert.StartsWith("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceScopeStoreResolver_OverridesDefaultManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
var type = Mock.Of<IOpenIddictScopeStoreResolver>().GetType(); |
|||
|
|||
// Act
|
|||
builder.ReplaceScopeStoreResolver(type); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var store = provider.GetRequiredService<IOpenIddictScopeStoreResolver>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType(type, store); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceTokenManager_ThrowsAnExceptionForInvalidManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentException>(() => builder.ReplaceTokenManager(typeof(object))); |
|||
|
|||
Assert.Equal("type", exception.ParamName); |
|||
Assert.StartsWith("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceTokenManager_OverridesDefaultOpenGenericManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceTokenManager(typeof(OpenGenericTokenManager<>)); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => |
|||
service.ServiceType == typeof(OpenIddictTokenManager<>) && |
|||
service.ImplementationType == typeof(OpenGenericTokenManager<>)); |
|||
Assert.DoesNotContain(services, service => |
|||
service.ServiceType == typeof(OpenIddictTokenManager<>) && |
|||
service.ImplementationType == typeof(OpenIddictTokenManager<>)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceTokenManager_AddsClosedGenericManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.ReplaceTokenManager(typeof(ClosedGenericTokenManager)); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => |
|||
service.ServiceType == typeof(OpenIddictTokenManager<CustomToken>) && |
|||
service.ImplementationType == typeof(ClosedGenericTokenManager)); |
|||
Assert.Contains(services, service => |
|||
service.ServiceType == typeof(OpenIddictTokenManager<>) && |
|||
service.ImplementationType == typeof(OpenIddictTokenManager<>)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceTokenStoreResolver_ThrowsAnExceptionForInvalidStoreResolver() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentException>(() => builder.ReplaceTokenStoreResolver(typeof(object))); |
|||
|
|||
Assert.Equal("type", exception.ParamName); |
|||
Assert.StartsWith("The specified type is invalid.", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReplaceTokenStoreResolver_OverridesDefaultManager() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
var type = Mock.Of<IOpenIddictTokenStoreResolver>().GetType(); |
|||
|
|||
// Act
|
|||
builder.ReplaceTokenStoreResolver(type); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var store = provider.GetRequiredService<IOpenIddictTokenStoreResolver>(); |
|||
|
|||
// Assert
|
|||
Assert.IsType(type, store); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseCustomModels_CustomEntitiesAreCorrectlySet() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
services.AddOpenIddict().AddCore() |
|||
.UseCustomModels<CustomApplication, CustomAuthorization, CustomScope, CustomToken>(); |
|||
|
|||
// Assert
|
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictCoreOptions>>().CurrentValue; |
|||
|
|||
Assert.Equal(typeof(CustomApplication), options.DefaultApplicationType); |
|||
Assert.Equal(typeof(CustomAuthorization), options.DefaultAuthorizationType); |
|||
Assert.Equal(typeof(CustomScope), options.DefaultScopeType); |
|||
Assert.Equal(typeof(CustomToken), options.DefaultTokenType); |
|||
} |
|||
|
|||
private static OpenIddictCoreBuilder CreateBuilder(IServiceCollection services) |
|||
=> services.AddOpenIddict().AddCore(); |
|||
|
|||
private static IServiceCollection CreateServices() |
|||
{ |
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
return services; |
|||
} |
|||
|
|||
public class CustomApplication { } |
|||
public class CustomAuthorization { } |
|||
public class CustomScope { } |
|||
public class CustomToken { } |
|||
|
|||
private class ClosedGenericApplicationManager : OpenIddictApplicationManager<CustomApplication> |
|||
{ |
|||
public ClosedGenericApplicationManager( |
|||
IOpenIddictApplicationStoreResolver resolver, |
|||
ILogger<OpenIddictApplicationManager<CustomApplication>> logger, |
|||
IOptionsMonitor<OpenIddictCoreOptions> options) |
|||
: base(resolver, logger, options) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
private class OpenGenericApplicationManager<TApplication> : OpenIddictApplicationManager<TApplication> |
|||
where TApplication : class |
|||
{ |
|||
public OpenGenericApplicationManager( |
|||
IOpenIddictApplicationStoreResolver resolver, |
|||
ILogger<OpenIddictApplicationManager<TApplication>> logger, |
|||
IOptionsMonitor<OpenIddictCoreOptions> options) |
|||
: base(resolver, logger, options) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
private class ClosedGenericAuthorizationManager : OpenIddictAuthorizationManager<CustomAuthorization> |
|||
{ |
|||
public ClosedGenericAuthorizationManager( |
|||
IOpenIddictAuthorizationStoreResolver resolver, |
|||
ILogger<OpenIddictAuthorizationManager<CustomAuthorization>> logger, |
|||
IOptionsMonitor<OpenIddictCoreOptions> options) |
|||
: base(resolver, logger, options) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
private class OpenGenericAuthorizationManager<TAuthorization> : OpenIddictAuthorizationManager<TAuthorization> |
|||
where TAuthorization : class |
|||
{ |
|||
public OpenGenericAuthorizationManager( |
|||
IOpenIddictAuthorizationStoreResolver resolver, |
|||
ILogger<OpenIddictAuthorizationManager<TAuthorization>> logger, |
|||
IOptionsMonitor<OpenIddictCoreOptions> options) |
|||
: base(resolver, logger, options) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
private class ClosedGenericScopeManager : OpenIddictScopeManager<CustomScope> |
|||
{ |
|||
public ClosedGenericScopeManager( |
|||
IOpenIddictScopeStoreResolver resolver, |
|||
ILogger<OpenIddictScopeManager<CustomScope>> logger, |
|||
IOptionsMonitor<OpenIddictCoreOptions> options) |
|||
: base(resolver, logger, options) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
private class OpenGenericScopeManager<TScope> : OpenIddictScopeManager<TScope> |
|||
where TScope : class |
|||
{ |
|||
public OpenGenericScopeManager( |
|||
IOpenIddictScopeStoreResolver resolver, |
|||
ILogger<OpenIddictScopeManager<TScope>> logger, |
|||
IOptionsMonitor<OpenIddictCoreOptions> options) |
|||
: base(resolver, logger, options) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
private class ClosedGenericTokenManager : OpenIddictTokenManager<CustomToken> |
|||
{ |
|||
public ClosedGenericTokenManager( |
|||
IOpenIddictTokenStoreResolver resolver, |
|||
ILogger<OpenIddictTokenManager<CustomToken>> logger, |
|||
IOptionsMonitor<OpenIddictCoreOptions> options) |
|||
: base(resolver, logger, options) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
private class OpenGenericTokenManager<TToken> : OpenIddictTokenManager<TToken> |
|||
where TToken : class |
|||
{ |
|||
public OpenGenericTokenManager( |
|||
IOpenIddictTokenStoreResolver resolver, |
|||
ILogger<OpenIddictTokenManager<TToken>> logger, |
|||
IOptionsMonitor<OpenIddictCoreOptions> options) |
|||
: base(resolver, logger, options) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,48 +0,0 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Core.Tests |
|||
{ |
|||
public class OpenIddictExtensionsTests |
|||
{ |
|||
[Fact] |
|||
public void AddOpenIddict_CustomEntitiesAreCorrectlySet() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
// Act
|
|||
var builder = services.AddOpenIddict<object, object, object, object>(); |
|||
|
|||
// Assert
|
|||
Assert.Equal(typeof(object), builder.ApplicationType); |
|||
Assert.Equal(typeof(object), builder.AuthorizationType); |
|||
Assert.Equal(typeof(object), builder.ScopeType); |
|||
Assert.Equal(typeof(object), builder.TokenType); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(typeof(OpenIddictApplicationManager<object>))] |
|||
[InlineData(typeof(OpenIddictAuthorizationManager<object>))] |
|||
[InlineData(typeof(OpenIddictScopeManager<object>))] |
|||
[InlineData(typeof(OpenIddictTokenManager<object>))] |
|||
public void AddOpenIddict_ManagersForCustomEntitiesAreCorrectlyRegistered(Type type) |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
// Act
|
|||
services.AddOpenIddict<object, object, object, object>(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(services, service => service.ServiceType == type); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue