/*
* 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.Client;
using OpenIddict.Client.WebIntegration;
namespace Microsoft.Extensions.DependencyInjection;
///
/// Exposes extensions allowing to register the OpenIddict client Web integration services.
///
public static partial class OpenIddictClientWebIntegrationExtensions
{
///
/// Registers the OpenIddict client Web integration 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 OpenIddictClientWebIntegrationBuilder UseWebProviders(this OpenIddictClientBuilder builder)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
// Register the System.Net.Http integration.
builder.UseSystemNetHttp();
// Register the built-in event handlers used by the OpenIddict client Web components.
// Note: the order used here is not important, as the actual order is set in the options.
builder.Services.TryAdd(OpenIddictClientWebIntegrationHandlers.DefaultHandlers
.Select(descriptor => descriptor.ServiceDescriptor));
// Note: TryAddEnumerable() is used here to ensure the initializers are registered only once.
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<
IConfigureOptions, OpenIddictClientWebIntegrationConfiguration>());
// Note: the IPostConfigureOptions service responsible for populating
// the client registrations MUST be registered before OpenIddictClientConfiguration to ensure
// the registrations are correctly populated before being validated.
if (!builder.Services.Any(static descriptor =>
descriptor.ServiceType == typeof(IPostConfigureOptions) &&
descriptor.ImplementationType == typeof(OpenIddictClientWebIntegrationConfiguration)))
{
builder.Services.Insert(0, ServiceDescriptor.Singleton<
IPostConfigureOptions, OpenIddictClientWebIntegrationConfiguration>());
}
return new OpenIddictClientWebIntegrationBuilder(builder.Services);
}
///
/// Registers the OpenIddict client Web integration services in the DI container.
///
/// The services builder used by OpenIddict to register new services.
/// The configuration delegate used to configure the validation services.
/// This extension can be safely called multiple times.
/// The instance.
public static OpenIddictClientBuilder UseWebProviders(
this OpenIddictClientBuilder builder, Action configuration)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
configuration(builder.UseWebProviders());
return builder;
}
}