/* * 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.Extensions; using Microsoft.Extensions.Options; using OpenIddict.Core; namespace Microsoft.Extensions.DependencyInjection; /// /// Exposes extensions allowing to register the OpenIddict core services. /// public static class OpenIddictCoreExtensions { /// /// Registers the OpenIddict core services in the DI container. /// /// The services builder used by OpenIddict to register new services. /// This extension can be safely called multiple times. /// The instance. public static OpenIddictCoreBuilder AddCore(this OpenIddictBuilder builder) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } builder.Services.AddLogging(); builder.Services.AddMemoryCache(); builder.Services.AddOptions(); builder.Services.TryAddScoped(typeof(IOpenIddictApplicationCache<>), typeof(OpenIddictApplicationCache<>)); builder.Services.TryAddScoped(typeof(IOpenIddictAuthorizationCache<>), typeof(OpenIddictAuthorizationCache<>)); builder.Services.TryAddScoped(typeof(IOpenIddictScopeCache<>), typeof(OpenIddictScopeCache<>)); builder.Services.TryAddScoped(typeof(IOpenIddictTokenCache<>), typeof(OpenIddictTokenCache<>)); builder.Services.TryAddScoped(typeof(OpenIddictApplicationManager<>)); builder.Services.TryAddScoped(typeof(OpenIddictAuthorizationManager<>)); builder.Services.TryAddScoped(typeof(OpenIddictScopeManager<>)); builder.Services.TryAddScoped(typeof(OpenIddictTokenManager<>)); // Note: default factories for the untyped managers are always registered to make debugging // easier if no store was configured. It is expected that store implementations replace the // registrations with working implementation factories that use the correct entity types. builder.Services.TryAddScoped(static provider => throw new InvalidOperationException(SR.GetResourceString(SR.ID0472))); builder.Services.TryAddScoped(static provider => throw new InvalidOperationException(SR.GetResourceString(SR.ID0472))); builder.Services.TryAddScoped(static provider => throw new InvalidOperationException(SR.GetResourceString(SR.ID0472))); builder.Services.TryAddScoped(static provider => throw new InvalidOperationException(SR.GetResourceString(SR.ID0472))); // Note: TryAddEnumerable() is used here to ensure the initializer is registered only once. builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< IPostConfigureOptions, OpenIddictCoreConfiguration>()); return new OpenIddictCoreBuilder(builder.Services); } /// /// Registers the OpenIddict core services in the DI container. /// /// The services builder used by OpenIddict to register new services. /// The configuration delegate used to configure the core services. /// This extension can be safely called multiple times. /// The instance. public static OpenIddictBuilder AddCore(this OpenIddictBuilder builder, Action configuration) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } if (configuration is null) { throw new ArgumentNullException(nameof(configuration)); } configuration(builder.AddCore()); return builder; } }