112 changed files with 2312 additions and 4928 deletions
@ -0,0 +1,32 @@ |
|||
using System.ComponentModel; |
|||
using System.Text.Json; |
|||
using System.Text.Json.Nodes; |
|||
using System.Text.Json.Serialization; |
|||
using System.Text.Json.Serialization.Metadata; |
|||
using Microsoft.IdentityModel.Tokens; |
|||
|
|||
namespace OpenIddict.Abstractions; |
|||
|
|||
/// <summary>
|
|||
/// Exposes <see cref="JsonTypeInfo{T}"/> properties for all
|
|||
/// the OpenIddict types suitable for JSON serialization.
|
|||
/// </summary>
|
|||
[EditorBrowsable(EditorBrowsableState.Never)] |
|||
[JsonSerializable(typeof(bool))] |
|||
[JsonSerializable(typeof(JsonArray))] |
|||
[JsonSerializable(typeof(JsonElement))] |
|||
[JsonSerializable(typeof(JsonNode))] |
|||
[JsonSerializable(typeof(JsonObject))] |
|||
[JsonSerializable(typeof(JsonValue))] |
|||
[JsonSerializable(typeof(JsonWebKey))] |
|||
[JsonSerializable(typeof(JsonWebKeySet))] |
|||
[JsonSerializable(typeof(long))] |
|||
[JsonSerializable(typeof(OpenIddictMessage), TypeInfoPropertyName = "Message")] |
|||
[JsonSerializable(typeof(OpenIddictParameter), TypeInfoPropertyName = "Parameter")] |
|||
[JsonSerializable(typeof(OpenIddictRequest), TypeInfoPropertyName = "Request")] |
|||
[JsonSerializable(typeof(OpenIddictResponse), TypeInfoPropertyName = "Response")] |
|||
[JsonSerializable(typeof(string))] |
|||
[JsonSerializable(typeof(string[]))] |
|||
public partial class OpenIddictSerializer : JsonSerializerContext |
|||
{ |
|||
} |
|||
@ -1,15 +0,0 @@ |
|||
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; |
|||
} |
|||
@ -1,15 +0,0 @@ |
|||
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; |
|||
} |
|||
@ -1,15 +0,0 @@ |
|||
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; |
|||
} |
|||
@ -1,15 +0,0 @@ |
|||
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,24 +0,0 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace OpenIddict.Core; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an application store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictApplicationStoreResolver : IOpenIddictApplicationStoreResolver |
|||
{ |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictApplicationStoreResolver(IServiceProvider provider) |
|||
=> _provider = provider ?? throw new ArgumentNullException(nameof(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 |
|||
=> _provider.GetService<IOpenIddictApplicationStore<TApplication>>() ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0228)); |
|||
} |
|||
@ -1,24 +0,0 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace OpenIddict.Core; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an authorization store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictAuthorizationStoreResolver : IOpenIddictAuthorizationStoreResolver |
|||
{ |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictAuthorizationStoreResolver(IServiceProvider provider) |
|||
=> _provider = provider ?? throw new ArgumentNullException(nameof(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 |
|||
=> _provider.GetService<IOpenIddictAuthorizationStore<TAuthorization>>() ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0229)); |
|||
} |
|||
@ -1,24 +0,0 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace OpenIddict.Core; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a scope store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictScopeStoreResolver : IOpenIddictScopeStoreResolver |
|||
{ |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictScopeStoreResolver(IServiceProvider provider) |
|||
=> _provider = provider ?? throw new ArgumentNullException(nameof(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 |
|||
=> _provider.GetService<IOpenIddictScopeStore<TScope>>() ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0230)); |
|||
} |
|||
@ -1,24 +0,0 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace OpenIddict.Core; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a token store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictTokenStoreResolver : IOpenIddictTokenStoreResolver |
|||
{ |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictTokenStoreResolver(IServiceProvider provider) |
|||
=> _provider = provider ?? throw new ArgumentNullException(nameof(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 |
|||
=> _provider.GetService<IOpenIddictTokenStore<TToken>>() ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0231)); |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
/* |
|||
* 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.ComponentModel; |
|||
|
|||
namespace OpenIddict.EntityFramework; |
|||
|
|||
/// <summary>
|
|||
/// Exposes the Entity Framework context used by the OpenIddict stores.
|
|||
/// </summary>
|
|||
[EditorBrowsable(EditorBrowsableState.Advanced)] |
|||
public interface IOpenIddictEntityFrameworkContext |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the <see cref="DbContext"/>.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// A <see cref="ValueTask{TResult}"/> that can be used to monitor the
|
|||
/// asynchronous operation, whose result returns the <see cref="DbContext"/>.
|
|||
/// </returns>
|
|||
ValueTask<DbContext> GetDbContextAsync(CancellationToken cancellationToken); |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
/* |
|||
* 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.ComponentModel; |
|||
|
|||
namespace OpenIddict.EntityFramework; |
|||
|
|||
/// <inheritdoc/>
|
|||
[EditorBrowsable(EditorBrowsableState.Advanced)] |
|||
public sealed class OpenIddictEntityFrameworkContext<TContext> : IOpenIddictEntityFrameworkContext |
|||
where TContext : DbContext |
|||
{ |
|||
private readonly TContext? _context; |
|||
|
|||
/// <summary>
|
|||
/// Creates a new instance of the <see cref="OpenIddictEntityFrameworkContext{TContext}"/> class.
|
|||
/// </summary>
|
|||
public OpenIddictEntityFrameworkContext() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Creates a new instance of the <see cref="OpenIddictEntityFrameworkContext{TContext}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="context">The Entity Framework Core context, if available.</param>
|
|||
public OpenIddictEntityFrameworkContext(TContext? context) => _context = context; |
|||
|
|||
/// <inheritdoc/>
|
|||
public ValueTask<DbContext> GetDbContextAsync(CancellationToken cancellationToken) |
|||
{ |
|||
if (cancellationToken.IsCancellationRequested) |
|||
{ |
|||
return new(Task.FromCanceled<DbContext>(cancellationToken)); |
|||
} |
|||
|
|||
if (_context is not DbContext context) |
|||
{ |
|||
return new(Task.FromException<DbContext>(new InvalidOperationException(SR.GetResourceString(SR.ID0470)))); |
|||
} |
|||
|
|||
return new(context); |
|||
} |
|||
} |
|||
@ -1,72 +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.Collections.Concurrent; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using OpenIddict.EntityFramework.Models; |
|||
using OpenIddict.Extensions; |
|||
|
|||
namespace OpenIddict.EntityFramework; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an application store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictEntityFrameworkApplicationStoreResolver : IOpenIddictApplicationStoreResolver |
|||
{ |
|||
private readonly TypeResolutionCache _cache; |
|||
private readonly IOptionsMonitor<OpenIddictEntityFrameworkOptions> _options; |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictEntityFrameworkApplicationStoreResolver( |
|||
TypeResolutionCache cache, |
|||
IOptionsMonitor<OpenIddictEntityFrameworkOptions> options, |
|||
IServiceProvider provider) |
|||
{ |
|||
_cache = cache ?? throw new ArgumentNullException(nameof(cache)); |
|||
_options = options ?? throw new ArgumentNullException(nameof(options)); |
|||
_provider = provider ?? throw new ArgumentNullException(nameof(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 is not null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TApplication), key => |
|||
{ |
|||
var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkApplication<,,>)) ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0234)); |
|||
|
|||
var context = _options.CurrentValue.DbContextType ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0235)); |
|||
|
|||
return typeof(OpenIddictEntityFrameworkApplicationStore<,,,,>).MakeGenericType( |
|||
/* TApplication: */ key, |
|||
/* TAuthorization: */ root.GenericTypeArguments[1], |
|||
/* TToken: */ root.GenericTypeArguments[2], |
|||
/* TContext: */ context, |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictApplicationStore<TApplication>) _provider.GetRequiredService(type); |
|||
} |
|||
|
|||
// Note: Entity Framework resolvers are registered as scoped dependencies as their inner
|
|||
// service provider must be able to resolve scoped services (typically, the store they return).
|
|||
// To avoid having to declare a static type resolution cache, a special cache service is used
|
|||
// here and registered as a singleton dependency so that its content persists beyond the scope.
|
|||
public sealed class TypeResolutionCache : ConcurrentDictionary<Type, Type> { } |
|||
} |
|||
@ -1,72 +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.Collections.Concurrent; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using OpenIddict.EntityFramework.Models; |
|||
using OpenIddict.Extensions; |
|||
|
|||
namespace OpenIddict.EntityFramework; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an authorization store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictEntityFrameworkAuthorizationStoreResolver : IOpenIddictAuthorizationStoreResolver |
|||
{ |
|||
private readonly TypeResolutionCache _cache; |
|||
private readonly IOptionsMonitor<OpenIddictEntityFrameworkOptions> _options; |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictEntityFrameworkAuthorizationStoreResolver( |
|||
TypeResolutionCache cache, |
|||
IOptionsMonitor<OpenIddictEntityFrameworkOptions> options, |
|||
IServiceProvider provider) |
|||
{ |
|||
_cache = cache ?? throw new ArgumentNullException(nameof(cache)); |
|||
_options = options ?? throw new ArgumentNullException(nameof(options)); |
|||
_provider = provider ?? throw new ArgumentNullException(nameof(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 is not null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TAuthorization), key => |
|||
{ |
|||
var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkAuthorization<,,>)) ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0236)); |
|||
|
|||
var context = _options.CurrentValue.DbContextType ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0235)); |
|||
|
|||
return typeof(OpenIddictEntityFrameworkAuthorizationStore<,,,,>).MakeGenericType( |
|||
/* TAuthorization: */ key, |
|||
/* TApplication: */ root.GenericTypeArguments[1], |
|||
/* TToken: */ root.GenericTypeArguments[2], |
|||
/* TContext: */ context, |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictAuthorizationStore<TAuthorization>) _provider.GetRequiredService(type); |
|||
} |
|||
|
|||
// Note: Entity Framework resolvers are registered as scoped dependencies as their inner
|
|||
// service provider must be able to resolve scoped services (typically, the store they return).
|
|||
// To avoid having to declare a static type resolution cache, a special cache service is used
|
|||
// here and registered as a singleton dependency so that its content persists beyond the scope.
|
|||
public sealed class TypeResolutionCache : ConcurrentDictionary<Type, Type> { } |
|||
} |
|||
@ -1,70 +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.Collections.Concurrent; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using OpenIddict.EntityFramework.Models; |
|||
using OpenIddict.Extensions; |
|||
|
|||
namespace OpenIddict.EntityFramework; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a scope store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictEntityFrameworkScopeStoreResolver : IOpenIddictScopeStoreResolver |
|||
{ |
|||
private readonly TypeResolutionCache _cache; |
|||
private readonly IOptionsMonitor<OpenIddictEntityFrameworkOptions> _options; |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictEntityFrameworkScopeStoreResolver( |
|||
TypeResolutionCache cache, |
|||
IOptionsMonitor<OpenIddictEntityFrameworkOptions> options, |
|||
IServiceProvider provider) |
|||
{ |
|||
_cache = cache ?? throw new ArgumentNullException(nameof(cache)); |
|||
_options = options ?? throw new ArgumentNullException(nameof(options)); |
|||
_provider = provider ?? throw new ArgumentNullException(nameof(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 is not null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TScope), key => |
|||
{ |
|||
var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkScope<>)) ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0237)); |
|||
|
|||
var context = _options.CurrentValue.DbContextType ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0235)); |
|||
|
|||
return typeof(OpenIddictEntityFrameworkScopeStore<,,>).MakeGenericType( |
|||
/* TScope: */ key, |
|||
/* TContext: */ context, |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictScopeStore<TScope>) _provider.GetRequiredService(type); |
|||
} |
|||
|
|||
// Note: Entity Framework resolvers are registered as scoped dependencies as their inner
|
|||
// service provider must be able to resolve scoped services (typically, the store they return).
|
|||
// To avoid having to declare a static type resolution cache, a special cache service is used
|
|||
// here and registered as a singleton dependency so that its content persists beyond the scope.
|
|||
public sealed class TypeResolutionCache : ConcurrentDictionary<Type, Type> { } |
|||
} |
|||
@ -1,72 +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.Collections.Concurrent; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using OpenIddict.EntityFramework.Models; |
|||
using OpenIddict.Extensions; |
|||
|
|||
namespace OpenIddict.EntityFramework; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a token store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictEntityFrameworkTokenStoreResolver : IOpenIddictTokenStoreResolver |
|||
{ |
|||
private readonly TypeResolutionCache _cache; |
|||
private readonly IOptionsMonitor<OpenIddictEntityFrameworkOptions> _options; |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictEntityFrameworkTokenStoreResolver( |
|||
TypeResolutionCache cache, |
|||
IOptionsMonitor<OpenIddictEntityFrameworkOptions> options, |
|||
IServiceProvider provider) |
|||
{ |
|||
_cache = cache ?? throw new ArgumentNullException(nameof(cache)); |
|||
_options = options ?? throw new ArgumentNullException(nameof(options)); |
|||
_provider = provider ?? throw new ArgumentNullException(nameof(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 is not null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TToken), key => |
|||
{ |
|||
var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkToken<,,>)) ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0238)); |
|||
|
|||
var context = _options.CurrentValue.DbContextType ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0235)); |
|||
|
|||
return typeof(OpenIddictEntityFrameworkTokenStore<,,,,>).MakeGenericType( |
|||
/* TToken: */ key, |
|||
/* TApplication: */ root.GenericTypeArguments[1], |
|||
/* TAuthorization: */ root.GenericTypeArguments[2], |
|||
/* TContext: */ context, |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictTokenStore<TToken>) _provider.GetRequiredService(type); |
|||
} |
|||
|
|||
// Note: Entity Framework resolvers are registered as scoped dependencies as their inner
|
|||
// service provider must be able to resolve scoped services (typically, the store they return).
|
|||
// To avoid having to declare a static type resolution cache, a special cache service is used
|
|||
// here and registered as a singleton dependency so that its content persists beyond the scope.
|
|||
public sealed class TypeResolutionCache : ConcurrentDictionary<Type, Type> { } |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
/* |
|||
* 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.ComponentModel; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore; |
|||
|
|||
/// <summary>
|
|||
/// Exposes the Entity Framework Core context used by the OpenIddict stores.
|
|||
/// </summary>
|
|||
[EditorBrowsable(EditorBrowsableState.Advanced)] |
|||
public interface IOpenIddictEntityFrameworkCoreContext |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the <see cref="DbContext"/>.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// A <see cref="ValueTask{TResult}"/> that can be used to monitor the
|
|||
/// asynchronous operation, whose result returns the <see cref="DbContext"/>.
|
|||
/// </returns>
|
|||
ValueTask<DbContext> GetDbContextAsync(CancellationToken cancellationToken); |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
/* |
|||
* 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.ComponentModel; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore; |
|||
|
|||
/// <inheritdoc/>
|
|||
[EditorBrowsable(EditorBrowsableState.Advanced)] |
|||
public sealed class OpenIddictEntityFrameworkCoreContext<TContext> : IOpenIddictEntityFrameworkCoreContext |
|||
where TContext : DbContext |
|||
{ |
|||
private readonly TContext? _context; |
|||
|
|||
/// <summary>
|
|||
/// Creates a new instance of the <see cref="OpenIddictEntityFrameworkCoreContext{TContext}"/> class.
|
|||
/// </summary>
|
|||
public OpenIddictEntityFrameworkCoreContext() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Creates a new instance of the <see cref="OpenIddictEntityFrameworkCoreContext{TContext}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="context">The Entity Framework Core context, if available.</param>
|
|||
public OpenIddictEntityFrameworkCoreContext(TContext? context) => _context = context; |
|||
|
|||
/// <inheritdoc/>
|
|||
public ValueTask<DbContext> GetDbContextAsync(CancellationToken cancellationToken) |
|||
{ |
|||
if (cancellationToken.IsCancellationRequested) |
|||
{ |
|||
return new(Task.FromCanceled<DbContext>(cancellationToken)); |
|||
} |
|||
|
|||
if (_context is not DbContext context) |
|||
{ |
|||
return new(Task.FromException<DbContext>(new InvalidOperationException(SR.GetResourceString(SR.ID0471)))); |
|||
} |
|||
|
|||
return new(context); |
|||
} |
|||
} |
|||
@ -1,72 +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.Collections.Concurrent; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using OpenIddict.EntityFrameworkCore.Models; |
|||
using OpenIddict.Extensions; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an application store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictEntityFrameworkCoreApplicationStoreResolver : IOpenIddictApplicationStoreResolver |
|||
{ |
|||
private readonly TypeResolutionCache _cache; |
|||
private readonly IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions> _options; |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictEntityFrameworkCoreApplicationStoreResolver( |
|||
TypeResolutionCache cache, |
|||
IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions> options, |
|||
IServiceProvider provider) |
|||
{ |
|||
_cache = cache ?? throw new ArgumentNullException(nameof(cache)); |
|||
_options = options ?? throw new ArgumentNullException(nameof(options)); |
|||
_provider = provider ?? throw new ArgumentNullException(nameof(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 is not null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TApplication), key => |
|||
{ |
|||
var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkCoreApplication<,,>)) ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0252)); |
|||
|
|||
var context = _options.CurrentValue.DbContextType ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0253)); |
|||
|
|||
return typeof(OpenIddictEntityFrameworkCoreApplicationStore<,,,,>).MakeGenericType( |
|||
/* TApplication: */ key, |
|||
/* TAuthorization: */ root.GenericTypeArguments[1], |
|||
/* TToken: */ root.GenericTypeArguments[2], |
|||
/* TContext: */ context, |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictApplicationStore<TApplication>) _provider.GetRequiredService(type); |
|||
} |
|||
|
|||
// Note: Entity Framework Core resolvers are registered as scoped dependencies as their inner
|
|||
// service provider must be able to resolve scoped services (typically, the store they return).
|
|||
// To avoid having to declare a static type resolution cache, a special cache service is used
|
|||
// here and registered as a singleton dependency so that its content persists beyond the scope.
|
|||
public sealed class TypeResolutionCache : ConcurrentDictionary<Type, Type> { } |
|||
} |
|||
@ -1,72 +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.Collections.Concurrent; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using OpenIddict.EntityFrameworkCore.Models; |
|||
using OpenIddict.Extensions; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an authorization store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictEntityFrameworkCoreAuthorizationStoreResolver : IOpenIddictAuthorizationStoreResolver |
|||
{ |
|||
private readonly TypeResolutionCache _cache; |
|||
private readonly IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions> _options; |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictEntityFrameworkCoreAuthorizationStoreResolver( |
|||
TypeResolutionCache cache, |
|||
IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions> options, |
|||
IServiceProvider provider) |
|||
{ |
|||
_cache = cache ?? throw new ArgumentNullException(nameof(cache)); |
|||
_options = options ?? throw new ArgumentNullException(nameof(options)); |
|||
_provider = provider ?? throw new ArgumentNullException(nameof(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 is not null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TAuthorization), key => |
|||
{ |
|||
var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkCoreAuthorization<,,>)) ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0254)); |
|||
|
|||
var context = _options.CurrentValue.DbContextType ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0253)); |
|||
|
|||
return typeof(OpenIddictEntityFrameworkCoreAuthorizationStore<,,,,>).MakeGenericType( |
|||
/* TAuthorization: */ key, |
|||
/* TApplication: */ root.GenericTypeArguments[1], |
|||
/* TToken: */ root.GenericTypeArguments[2], |
|||
/* TContext: */ context, |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictAuthorizationStore<TAuthorization>) _provider.GetRequiredService(type); |
|||
} |
|||
|
|||
// Note: Entity Framework Core resolvers are registered as scoped dependencies as their inner
|
|||
// service provider must be able to resolve scoped services (typically, the store they return).
|
|||
// To avoid having to declare a static type resolution cache, a special cache service is used
|
|||
// here and registered as a singleton dependency so that its content persists beyond the scope.
|
|||
public sealed class TypeResolutionCache : ConcurrentDictionary<Type, Type> { } |
|||
} |
|||
@ -1,70 +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.Collections.Concurrent; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using OpenIddict.EntityFrameworkCore.Models; |
|||
using OpenIddict.Extensions; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a scope store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictEntityFrameworkCoreScopeStoreResolver : IOpenIddictScopeStoreResolver |
|||
{ |
|||
private readonly TypeResolutionCache _cache; |
|||
private readonly IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions> _options; |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictEntityFrameworkCoreScopeStoreResolver( |
|||
TypeResolutionCache cache, |
|||
IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions> options, |
|||
IServiceProvider provider) |
|||
{ |
|||
_cache = cache ?? throw new ArgumentNullException(nameof(cache)); |
|||
_options = options ?? throw new ArgumentNullException(nameof(options)); |
|||
_provider = provider ?? throw new ArgumentNullException(nameof(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 is not null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TScope), key => |
|||
{ |
|||
var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkCoreScope<>)) ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0255)); |
|||
|
|||
var context = _options.CurrentValue.DbContextType ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0253)); |
|||
|
|||
return typeof(OpenIddictEntityFrameworkCoreScopeStore<,,>).MakeGenericType( |
|||
/* TScope: */ key, |
|||
/* TContext: */ context, |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictScopeStore<TScope>) _provider.GetRequiredService(type); |
|||
} |
|||
|
|||
// Note: Entity Framework Core resolvers are registered as scoped dependencies as their inner
|
|||
// service provider must be able to resolve scoped services (typically, the store they return).
|
|||
// To avoid having to declare a static type resolution cache, a special cache service is used
|
|||
// here and registered as a singleton dependency so that its content persists beyond the scope.
|
|||
public sealed class TypeResolutionCache : ConcurrentDictionary<Type, Type> { } |
|||
} |
|||
@ -1,72 +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.Collections.Concurrent; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using OpenIddict.EntityFrameworkCore.Models; |
|||
using OpenIddict.Extensions; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a token store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictEntityFrameworkCoreTokenStoreResolver : IOpenIddictTokenStoreResolver |
|||
{ |
|||
private readonly TypeResolutionCache _cache; |
|||
private readonly IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions> _options; |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictEntityFrameworkCoreTokenStoreResolver( |
|||
TypeResolutionCache cache, |
|||
IOptionsMonitor<OpenIddictEntityFrameworkCoreOptions> options, |
|||
IServiceProvider provider) |
|||
{ |
|||
_cache = cache ?? throw new ArgumentNullException(nameof(cache)); |
|||
_options = options ?? throw new ArgumentNullException(nameof(options)); |
|||
_provider = provider ?? throw new ArgumentNullException(nameof(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 is not null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TToken), key => |
|||
{ |
|||
var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkCoreToken<,,>)) ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0256)); |
|||
|
|||
var context = _options.CurrentValue.DbContextType ?? |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0253)); |
|||
|
|||
return typeof(OpenIddictEntityFrameworkCoreTokenStore<,,,,>).MakeGenericType( |
|||
/* TToken: */ key, |
|||
/* TApplication: */ root.GenericTypeArguments[1], |
|||
/* TAuthorization: */ root.GenericTypeArguments[2], |
|||
/* TContext: */ context, |
|||
/* TKey: */ root.GenericTypeArguments[0]); |
|||
}); |
|||
|
|||
return (IOpenIddictTokenStore<TToken>) _provider.GetRequiredService(type); |
|||
} |
|||
|
|||
// Note: Entity Framework Core resolvers are registered as scoped dependencies as their inner
|
|||
// service provider must be able to resolve scoped services (typically, the store they return).
|
|||
// To avoid having to declare a static type resolution cache, a special cache service is used
|
|||
// here and registered as a singleton dependency so that its content persists beyond the scope.
|
|||
public sealed class TypeResolutionCache : ConcurrentDictionary<Type, Type> { } |
|||
} |
|||
@ -1,50 +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.Collections.Concurrent; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.MongoDb.Models; |
|||
|
|||
namespace OpenIddict.MongoDb; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an application store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictMongoDbApplicationStoreResolver : IOpenIddictApplicationStoreResolver |
|||
{ |
|||
private readonly ConcurrentDictionary<Type, Type> _cache = new(); |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictMongoDbApplicationStoreResolver(IServiceProvider provider) |
|||
=> _provider = provider ?? throw new ArgumentNullException(nameof(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 is not null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TApplication), key => |
|||
{ |
|||
if (!typeof(OpenIddictMongoDbApplication).IsAssignableFrom(key)) |
|||
{ |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0257)); |
|||
} |
|||
|
|||
return typeof(OpenIddictMongoDbApplicationStore<>).MakeGenericType(key); |
|||
}); |
|||
|
|||
return (IOpenIddictApplicationStore<TApplication>) _provider.GetRequiredService(type); |
|||
} |
|||
} |
|||
@ -1,50 +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.Collections.Concurrent; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.MongoDb.Models; |
|||
|
|||
namespace OpenIddict.MongoDb; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve an authorization store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictMongoDbAuthorizationStoreResolver : IOpenIddictAuthorizationStoreResolver |
|||
{ |
|||
private readonly ConcurrentDictionary<Type, Type> _cache = new(); |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictMongoDbAuthorizationStoreResolver(IServiceProvider provider) |
|||
=> _provider = provider ?? throw new ArgumentNullException(nameof(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 is not null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TAuthorization), key => |
|||
{ |
|||
if (!typeof(OpenIddictMongoDbAuthorization).IsAssignableFrom(key)) |
|||
{ |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0258)); |
|||
} |
|||
|
|||
return typeof(OpenIddictMongoDbAuthorizationStore<>).MakeGenericType(key); |
|||
}); |
|||
|
|||
return (IOpenIddictAuthorizationStore<TAuthorization>) _provider.GetRequiredService(type); |
|||
} |
|||
} |
|||
@ -1,50 +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.Collections.Concurrent; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.MongoDb.Models; |
|||
|
|||
namespace OpenIddict.MongoDb; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a scope store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictMongoDbScopeStoreResolver : IOpenIddictScopeStoreResolver |
|||
{ |
|||
private readonly ConcurrentDictionary<Type, Type> _cache = new(); |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictMongoDbScopeStoreResolver(IServiceProvider provider) |
|||
=> _provider = provider ?? throw new ArgumentNullException(nameof(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 is not null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TScope), key => |
|||
{ |
|||
if (!typeof(OpenIddictMongoDbScope).IsAssignableFrom(key)) |
|||
{ |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0259)); |
|||
} |
|||
|
|||
return typeof(OpenIddictMongoDbScopeStore<>).MakeGenericType(key); |
|||
}); |
|||
|
|||
return (IOpenIddictScopeStore<TScope>) _provider.GetRequiredService(type); |
|||
} |
|||
} |
|||
@ -1,50 +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.Collections.Concurrent; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using OpenIddict.MongoDb.Models; |
|||
|
|||
namespace OpenIddict.MongoDb; |
|||
|
|||
/// <summary>
|
|||
/// Exposes a method allowing to resolve a token store.
|
|||
/// </summary>
|
|||
public sealed class OpenIddictMongoDbTokenStoreResolver : IOpenIddictTokenStoreResolver |
|||
{ |
|||
private readonly ConcurrentDictionary<Type, Type> _cache = new(); |
|||
private readonly IServiceProvider _provider; |
|||
|
|||
public OpenIddictMongoDbTokenStoreResolver(IServiceProvider provider) |
|||
=> _provider = provider ?? throw new ArgumentNullException(nameof(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 is not null) |
|||
{ |
|||
return store; |
|||
} |
|||
|
|||
var type = _cache.GetOrAdd(typeof(TToken), key => |
|||
{ |
|||
if (!typeof(OpenIddictMongoDbToken).IsAssignableFrom(key)) |
|||
{ |
|||
throw new InvalidOperationException(SR.GetResourceString(SR.ID0260)); |
|||
} |
|||
|
|||
return typeof(OpenIddictMongoDbTokenStore<>).MakeGenericType(key); |
|||
}); |
|||
|
|||
return (IOpenIddictTokenStore<TToken>) _provider.GetRequiredService(type); |
|||
} |
|||
} |
|||
@ -1,44 +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 Microsoft.Extensions.DependencyInjection; |
|||
using Moq; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Core.Tests; |
|||
|
|||
public class OpenIddictApplicationStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenStoreCannotBeFound() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictApplicationStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(resolver.Get<OpenIddictApplication>); |
|||
|
|||
Assert.Equal(SR.GetResourceString(SR.ID0228), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictApplicationStore<OpenIddictApplication>>()); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictApplicationStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<OpenIddictApplication>()); |
|||
} |
|||
|
|||
public class OpenIddictApplication { } |
|||
} |
|||
@ -1,44 +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 Microsoft.Extensions.DependencyInjection; |
|||
using Moq; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Core.Tests; |
|||
|
|||
public class OpenIddictAuthorizationStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenStoreCannotBeFound() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictAuthorizationStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(resolver.Get<OpenIddictAuthorization>); |
|||
|
|||
Assert.Equal(SR.GetResourceString(SR.ID0229), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictAuthorizationStore<OpenIddictAuthorization>>()); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictAuthorizationStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<OpenIddictAuthorization>()); |
|||
} |
|||
|
|||
public class OpenIddictAuthorization { } |
|||
} |
|||
@ -1,44 +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 Microsoft.Extensions.DependencyInjection; |
|||
using Moq; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Core.Tests; |
|||
|
|||
public class OpenIddictScopeStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenStoreCannotBeFound() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictScopeStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(resolver.Get<OpenIddictScope>); |
|||
|
|||
Assert.Equal(SR.GetResourceString(SR.ID0230), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictScopeStore<OpenIddictScope>>()); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictScopeStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<OpenIddictScope>()); |
|||
} |
|||
|
|||
public class OpenIddictScope { } |
|||
} |
|||
@ -1,44 +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 Microsoft.Extensions.DependencyInjection; |
|||
using Moq; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Core.Tests; |
|||
|
|||
public class OpenIddictTokenStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenStoreCannotBeFound() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictTokenStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(resolver.Get<OpenIddictToken>); |
|||
|
|||
Assert.Equal(SR.GetResourceString(SR.ID0231), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictTokenStore<OpenIddictToken>>()); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictTokenStoreResolver(provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<OpenIddictToken>()); |
|||
} |
|||
|
|||
public class OpenIddictToken { } |
|||
} |
|||
@ -1,105 +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.Data.Entity; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using OpenIddict.EntityFramework.Models; |
|||
using Xunit; |
|||
using static OpenIddict.EntityFramework.OpenIddictEntityFrameworkApplicationStoreResolver; |
|||
|
|||
namespace OpenIddict.EntityFramework.Tests; |
|||
|
|||
public class OpenIddictEntityFrameworkApplicationStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictApplicationStore<CustomApplication>>()); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkApplicationStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<CustomApplication>()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionForInvalidEntityType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkApplicationStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(resolver.Get<CustomApplication>); |
|||
|
|||
Assert.Equal(SR.GetResourceString(SR.ID0234), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenDbContextTypeIsNotAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = null |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkApplicationStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(resolver.Get<OpenIddictEntityFrameworkApplication>); |
|||
|
|||
Assert.Equal(SR.GetResourceString(SR.ID0235), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictApplicationStore<CustomApplication>>()); |
|||
services.AddSingleton(CreateStore()); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = typeof(DbContext) |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkApplicationStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<MyApplication>()); |
|||
} |
|||
|
|||
private static OpenIddictEntityFrameworkApplicationStore<MyApplication, MyAuthorization, MyToken, DbContext, long> CreateStore() |
|||
=> new Mock<OpenIddictEntityFrameworkApplicationStore<MyApplication, MyAuthorization, MyToken, DbContext, long>>( |
|||
Mock.Of<IMemoryCache>(), |
|||
Mock.Of<DbContext>(), |
|||
Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>()).Object; |
|||
|
|||
public class CustomApplication { } |
|||
|
|||
public class MyApplication : OpenIddictEntityFrameworkApplication<long, MyAuthorization, MyToken> { } |
|||
public class MyAuthorization : OpenIddictEntityFrameworkAuthorization<long, MyApplication, MyToken> { } |
|||
public class MyScope : OpenIddictEntityFrameworkScope<long> { } |
|||
public class MyToken : OpenIddictEntityFrameworkToken<long, MyApplication, MyAuthorization> { } |
|||
} |
|||
@ -1,105 +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.Data.Entity; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using OpenIddict.EntityFramework.Models; |
|||
using Xunit; |
|||
using static OpenIddict.EntityFramework.OpenIddictEntityFrameworkAuthorizationStoreResolver; |
|||
|
|||
namespace OpenIddict.EntityFramework.Tests; |
|||
|
|||
public class OpenIddictEntityFrameworkAuthorizationStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictAuthorizationStore<CustomAuthorization>>()); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkAuthorizationStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<CustomAuthorization>()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionForInvalidEntityType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkAuthorizationStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(resolver.Get<CustomAuthorization>); |
|||
|
|||
Assert.Equal(SR.GetResourceString(SR.ID0236), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenDbContextTypeIsNotAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = null |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkAuthorizationStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(resolver.Get<OpenIddictEntityFrameworkAuthorization>); |
|||
|
|||
Assert.Equal(SR.GetResourceString(SR.ID0235), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictAuthorizationStore<CustomAuthorization>>()); |
|||
services.AddSingleton(CreateStore()); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = typeof(DbContext) |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkAuthorizationStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<MyAuthorization>()); |
|||
} |
|||
|
|||
private static OpenIddictEntityFrameworkAuthorizationStore<MyAuthorization, MyApplication, MyToken, DbContext, long> CreateStore() |
|||
=> new Mock<OpenIddictEntityFrameworkAuthorizationStore<MyAuthorization, MyApplication, MyToken, DbContext, long>>( |
|||
Mock.Of<IMemoryCache>(), |
|||
Mock.Of<DbContext>(), |
|||
Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>()).Object; |
|||
|
|||
public class CustomAuthorization { } |
|||
|
|||
public class MyApplication : OpenIddictEntityFrameworkApplication<long, MyAuthorization, MyToken> { } |
|||
public class MyAuthorization : OpenIddictEntityFrameworkAuthorization<long, MyApplication, MyToken> { } |
|||
public class MyScope : OpenIddictEntityFrameworkScope<long> { } |
|||
public class MyToken : OpenIddictEntityFrameworkToken<long, MyApplication, MyAuthorization> { } |
|||
} |
|||
@ -1,105 +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.Data.Entity; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using OpenIddict.EntityFramework.Models; |
|||
using Xunit; |
|||
using static OpenIddict.EntityFramework.OpenIddictEntityFrameworkScopeStoreResolver; |
|||
|
|||
namespace OpenIddict.EntityFramework.Tests; |
|||
|
|||
public class OpenIddictEntityFrameworkScopeStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictScopeStore<CustomScope>>()); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkScopeStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<CustomScope>()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionForInvalidEntityType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkScopeStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(resolver.Get<CustomScope>); |
|||
|
|||
Assert.Equal(SR.GetResourceString(SR.ID0237), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenDbContextTypeIsNotAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = null |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkScopeStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(resolver.Get<OpenIddictEntityFrameworkScope>); |
|||
|
|||
Assert.Equal(SR.GetResourceString(SR.ID0235), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictScopeStore<CustomScope>>()); |
|||
services.AddSingleton(CreateStore()); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = typeof(DbContext) |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkScopeStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<MyScope>()); |
|||
} |
|||
|
|||
private static OpenIddictEntityFrameworkScopeStore<MyScope, DbContext, long> CreateStore() |
|||
=> new Mock<OpenIddictEntityFrameworkScopeStore<MyScope, DbContext, long>>( |
|||
Mock.Of<IMemoryCache>(), |
|||
Mock.Of<DbContext>(), |
|||
Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>()).Object; |
|||
|
|||
public class CustomScope { } |
|||
|
|||
public class MyApplication : OpenIddictEntityFrameworkApplication<long, MyAuthorization, MyToken> { } |
|||
public class MyAuthorization : OpenIddictEntityFrameworkAuthorization<long, MyApplication, MyToken> { } |
|||
public class MyScope : OpenIddictEntityFrameworkScope<long> { } |
|||
public class MyToken : OpenIddictEntityFrameworkToken<long, MyApplication, MyAuthorization> { } |
|||
} |
|||
@ -1,105 +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.Data.Entity; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using OpenIddict.EntityFramework.Models; |
|||
using Xunit; |
|||
using static OpenIddict.EntityFramework.OpenIddictEntityFrameworkTokenStoreResolver; |
|||
|
|||
namespace OpenIddict.EntityFramework.Tests; |
|||
|
|||
public class OpenIddictEntityFrameworkTokenStoreResolverTests |
|||
{ |
|||
[Fact] |
|||
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictTokenStore<CustomToken>>()); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkTokenStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<CustomToken>()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionForInvalidEntityType() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>(); |
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkTokenStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(resolver.Get<CustomToken>); |
|||
|
|||
Assert.Equal(SR.GetResourceString(SR.ID0238), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ThrowsAnExceptionWhenDbContextTypeIsNotAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = null |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkTokenStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<InvalidOperationException>(resolver.Get<OpenIddictEntityFrameworkToken>); |
|||
|
|||
Assert.Equal(SR.GetResourceString(SR.ID0235), exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(Mock.Of<IOpenIddictTokenStore<CustomToken>>()); |
|||
services.AddSingleton(CreateStore()); |
|||
|
|||
var options = Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>( |
|||
mock => mock.CurrentValue == new OpenIddictEntityFrameworkOptions |
|||
{ |
|||
DbContextType = typeof(DbContext) |
|||
}); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
var resolver = new OpenIddictEntityFrameworkTokenStoreResolver(new TypeResolutionCache(), options, provider); |
|||
|
|||
// Act and assert
|
|||
Assert.NotNull(resolver.Get<MyToken>()); |
|||
} |
|||
|
|||
private static OpenIddictEntityFrameworkTokenStore<MyToken, MyApplication, MyAuthorization, DbContext, long> CreateStore() |
|||
=> new Mock<OpenIddictEntityFrameworkTokenStore<MyToken, MyApplication, MyAuthorization, DbContext, long>>( |
|||
Mock.Of<IMemoryCache>(), |
|||
Mock.Of<DbContext>(), |
|||
Mock.Of<IOptionsMonitor<OpenIddictEntityFrameworkOptions>>()).Object; |
|||
|
|||
public class CustomToken { } |
|||
|
|||
public class MyApplication : OpenIddictEntityFrameworkApplication<long, MyAuthorization, MyToken> { } |
|||
public class MyAuthorization : OpenIddictEntityFrameworkAuthorization<long, MyApplication, MyToken> { } |
|||
public class MyScope : OpenIddictEntityFrameworkScope<long> { } |
|||
public class MyToken : OpenIddictEntityFrameworkToken<long, MyApplication, MyAuthorization> { } |
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue