You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
3.0 KiB
67 lines
3.0 KiB
/*
|
|
* 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.Concurrent;
|
|
using System.Text;
|
|
using JetBrains.Annotations;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using OpenIddict.Abstractions;
|
|
using OpenIddict.Core;
|
|
using OpenIddict.Extensions;
|
|
using OpenIddict.NHibernate.Models;
|
|
|
|
namespace OpenIddict.NHibernate
|
|
{
|
|
/// <summary>
|
|
/// Exposes a method allowing to resolve an authorization store.
|
|
/// </summary>
|
|
public class OpenIddictAuthorizationStoreResolver : IOpenIddictAuthorizationStoreResolver
|
|
{
|
|
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 = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictAuthorization<,,>));
|
|
if (root == null)
|
|
{
|
|
throw new InvalidOperationException(new StringBuilder()
|
|
.AppendLine("The specified authorization type is not compatible with the NHibernate stores.")
|
|
.Append("When enabling the NHibernate stores, make sure you use the built-in ")
|
|
.Append("'OpenIddictAuthorization' entity (from the 'OpenIddict.NHibernate.Models' package) ")
|
|
.Append("or a custom entity that inherits from the generic 'OpenIddictAuthorization' entity.")
|
|
.ToString());
|
|
}
|
|
|
|
return typeof(OpenIddictAuthorizationStore<,,,>).MakeGenericType(
|
|
/* TAuthorization: */ key,
|
|
/* TApplication: */ root.GenericTypeArguments[1],
|
|
/* TToken: */ root.GenericTypeArguments[2],
|
|
/* TKey: */ root.GenericTypeArguments[0]);
|
|
});
|
|
|
|
return (IOpenIddictAuthorizationStore<TAuthorization>) _provider.GetRequiredService(type);
|
|
}
|
|
}
|
|
}
|
|
|