/*
* 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.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using OpenIddict.Client;
using OpenIddict.Client.AspNetCore;
using OpenIddict.Client.AspNetCore.Bff;
using static OpenIddict.Client.AspNetCore.Bff.OpenIddictClientAspNetCoreBffModels;
namespace Microsoft.Extensions.DependencyInjection;
///
/// Exposes extensions allowing to register the OpenIddict backend-for-frontend (BFF) services.
///
public static class OpenIddictClientAspNetCoreBffExtensions
{
///
/// Registers the OpenIddict backend-for-frontend (BFF) 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 OpenIddictClientAspNetCoreBffBuilder UseBff(this OpenIddictClientBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
// The BFF components rely on the OpenIddict client ASP.NET Core integration.
builder.UseAspNetCore();
builder.Services.AddHttpContextAccessor();
builder.Services.TryAddSingleton();
builder.Services.TryAddSingleton();
builder.Services.TryAddEnumerable(ServiceDescriptor.Scoped<
IOpenIddictClientAspNetCoreBffBackchannelLogoutHandler, OpenIddictClientAspNetCoreBffSessionStoreLogoutHandler>());
// Note: TryAddEnumerable() is used here to ensure the initializers are only registered once.
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<
IPostConfigureOptions, OpenIddictClientAspNetCoreBffConfiguration>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<
IPostConfigureOptions, OpenIddictClientAspNetCoreBffConfiguration>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<
IPostConfigureOptions, OpenIddictClientAspNetCoreBffConfiguration>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<
IValidateOptions, OpenIddictClientAspNetCoreBffConfiguration>());
return new OpenIddictClientAspNetCoreBffBuilder(builder.Services);
}
///
/// Registers the OpenIddict backend-for-frontend (BFF) services in the DI container.
///
/// The services builder used by OpenIddict to register new services.
/// The configuration delegate used to configure the BFF services.
/// This extension can be safely called multiple times.
/// The instance.
public static OpenIddictClientBuilder UseBff(
this OpenIddictClientBuilder builder, Action configuration)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(configuration);
configuration(builder.UseBff());
return builder;
}
///
/// Adds a handler attaching the access token of the current user to the requests sent by the HTTP client.
///
/// The HTTP client builder.
/// The instance.
public static IHttpClientBuilder AddOpenIddictBffUserAccessTokenHandler(this IHttpClientBuilder builder)
=> builder.AddOpenIddictBffAccessTokenHandler(OpenIddictClientAspNetCoreBffTokenType.User);
///
/// Adds a handler attaching an access token obtained using the client
/// credentials grant to the requests sent by the HTTP client.
///
/// The HTTP client builder.
/// The client access token request, if applicable.
/// The instance.
public static IHttpClientBuilder AddOpenIddictBffClientAccessTokenHandler(
this IHttpClientBuilder builder, ClientAccessTokenRequest? request = null)
=> builder.AddOpenIddictBffAccessTokenHandler(OpenIddictClientAspNetCoreBffTokenType.Client, request);
///
/// Adds a handler attaching the specified type of access token to the requests sent by the HTTP client.
///
/// The HTTP client builder.
/// The type of access token.
/// The client access token request, if applicable.
/// The instance.
public static IHttpClientBuilder AddOpenIddictBffAccessTokenHandler(this IHttpClientBuilder builder,
OpenIddictClientAspNetCoreBffTokenType type, ClientAccessTokenRequest? request = null)
{
ArgumentNullException.ThrowIfNull(builder);
return builder.AddHttpMessageHandler(provider => new OpenIddictClientAspNetCoreBffAccessTokenHandler(
provider.GetRequiredService(),
provider.GetService() ??
throw new InvalidOperationException(SR.GetResourceString(SR.ID0561)),
type, request));
}
///
/// Registers the YARP transforms attaching the user or client access tokens to proxied requests
/// (see ).
///
/// The reverse proxy builder.
/// The instance.
public static IReverseProxyBuilder AddOpenIddictBffTransforms(this IReverseProxyBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
return builder.AddTransforms();
}
}