Exemple #1
0
    /// <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 <>));
            if (root is null)
            {
                throw new InvalidOperationException(SR.GetResourceString(SR.ID0237));
            }

            var context = _options.CurrentValue.DbContextType;
            if (context is null)
            {
                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));
    }
Exemple #2
0
        /// <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 = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkCoreToken <, ,>));
                if (root == null)
                {
                    throw new InvalidOperationException(SR.GetResourceString(SR.ID1255));
                }

                var context = _options.CurrentValue.DbContextType;
                if (context == null)
                {
                    throw new InvalidOperationException(SR.GetResourceString(SR.ID1252));
                }

                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));
        }
Exemple #3
0
        /// <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 = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictApplication <, ,>));
                if (root == null)
                {
                    throw new InvalidOperationException(new StringBuilder()
                                                        .AppendLine("The specified application type is not compatible with the NHibernate stores.")
                                                        .Append("When enabling the NHibernate stores, make sure you use the built-in ")
                                                        .Append("'OpenIddictApplication' entity (from the 'OpenIddict.NHibernate.Models' package) ")
                                                        .Append("or a custom entity that inherits from the generic 'OpenIddictApplication' entity.")
                                                        .ToString());
                }

                return(typeof(OpenIddictApplicationStore <, , ,>).MakeGenericType(
                           /* TApplication: */ key,
                           /* TAuthorization: */ root.GenericTypeArguments[1],
                           /* TToken: */ root.GenericTypeArguments[2],
                           /* TKey: */ root.GenericTypeArguments[0]));
            });

            return((IOpenIddictApplicationStore <TApplication>)_provider.GetRequiredService(type));
        }
Exemple #4
0
        /// <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>
        /// <param name="lifetime">The lifetime of the registered service.</param>
        /// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
        public OpenIddictCoreBuilder AddApplicationStore(
            [NotNull] Type type, ServiceLifetime lifetime = ServiceLifetime.Scoped)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var root = OpenIddictHelpers.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(new ServiceDescriptor(typeof(IOpenIddictApplicationStore <>), type, lifetime));
            }

            else
            {
                Services.Replace(new ServiceDescriptor(typeof(IOpenIddictApplicationStore <>)
                                                       .MakeGenericType(root.GenericTypeArguments[0]), type, lifetime));
            }

            return(this);
        }
        /// <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 = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkCoreApplication <, ,>));
                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 ")
                                                        .Append("'OpenIddictEntityFrameworkCoreApplication' entity or a custom entity that inherits ")
                                                        .Append("from the generic 'OpenIddictEntityFrameworkCoreApplication' entity.")
                                                        .ToString());
                }

                var context = _options.CurrentValue.DbContextType;
                if (context == null)
                {
                    throw new InvalidOperationException(new StringBuilder()
                                                        .AppendLine("No Entity Framework Core context was specified in the OpenIddict options.")
                                                        .Append("To configure the OpenIddict Entity Framework Core stores to use a specific 'DbContext', ")
                                                        .Append("use 'options.UseEntityFrameworkCore().UseDbContext<TContext>()'.")
                                                        .ToString());
                }

                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));
        }
Exemple #6
0
        /// <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>
        /// <param name="lifetime">The lifetime of the registered service.</param>
        /// <returns>The <see cref="OpenIddictCoreBuilder"/>.</returns>
        public OpenIddictCoreBuilder ReplaceTokenManager(
            [NotNull] Type type, ServiceLifetime lifetime = ServiceLifetime.Scoped)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var root = OpenIddictHelpers.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(new ServiceDescriptor(type, type, lifetime));
                Services.Replace(new ServiceDescriptor(typeof(OpenIddictTokenManager <>), type, lifetime));
            }

            else
            {
                object ResolveManager(IServiceProvider provider)
                => provider.GetRequiredService(typeof(OpenIddictTokenManager <>)
                                               .MakeGenericType(root.GenericTypeArguments[0]));

                Services.Replace(new ServiceDescriptor(type, ResolveManager, lifetime));
                Services.Replace(new ServiceDescriptor(typeof(OpenIddictTokenManager <>)
                                                       .MakeGenericType(root.GenericTypeArguments[0]), type, lifetime));
            }

            return(this);
        }
Exemple #7
0
        /// <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 = OpenIddictHelpers.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 ")
                                                        .Append("'OpenIddictScope' entity (from the 'OpenIddict.EntityFramework.Models' package) ")
                                                        .Append("or a custom entity that inherits from the generic 'OpenIddictScope' entity.")
                                                        .ToString());
                }

                var context = _options.CurrentValue.DbContextType;
                if (context == null)
                {
                    throw new InvalidOperationException(new StringBuilder()
                                                        .AppendLine("No Entity Framework 6.x context was specified in the OpenIddict options.")
                                                        .Append("To configure the OpenIddict Entity Framework 6.x stores to use a specific 'DbContext', ")
                                                        .Append("use 'options.UseEntityFramework().UseDbContext<TContext>()'.")
                                                        .ToString());
                }

                return(typeof(OpenIddictScopeStore <, ,>).MakeGenericType(
                           /* TScope: */ key,
                           /* TContext: */ context,
                           /* TKey: */ root.GenericTypeArguments[0]));
            });

            return((IOpenIddictScopeStore <TScope>)_provider.GetRequiredService(type));
        }