/*
* 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.ComponentModel;
using Microsoft.AspNetCore.DataProtection;
using OpenIddict.Client.DataProtection;
namespace Microsoft.Extensions.DependencyInjection;
///
/// Exposes the necessary methods required to configure the
/// OpenIddict ASP.NET Core Data Protection integration.
///
public sealed class OpenIddictClientDataProtectionBuilder
{
///
/// Initializes a new instance of .
///
/// The services collection.
public OpenIddictClientDataProtectionBuilder(IServiceCollection services)
=> Services = services ?? throw new ArgumentNullException(nameof(services));
///
/// Gets the services collection.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public IServiceCollection Services { get; }
///
/// Amends the default OpenIddict client ASP.NET Core Data Protection configuration.
///
/// The delegate used to configure the OpenIddict options.
/// This extension can be safely called multiple times.
/// The instance.
public OpenIddictClientDataProtectionBuilder Configure(Action configuration)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
Services.Configure(configuration);
return this;
}
///
/// Configures OpenIddict to use a specific data protection provider
/// instead of relying on the default instance provided by the DI container.
///
/// The data protection provider used to create token protectors.
/// The instance.
public OpenIddictClientDataProtectionBuilder UseDataProtectionProvider(IDataProtectionProvider provider)
{
if (provider is null)
{
throw new ArgumentNullException(nameof(provider));
}
return Configure(options => options.DataProtectionProvider = provider);
}
///
/// Configures OpenIddict to use a specific formatter instead of relying on the default instance.
///
/// The formatter used to read and write tokens.
/// The instance.
public OpenIddictClientDataProtectionBuilder UseFormatter(IOpenIddictClientDataProtectionFormatter formatter)
{
if (formatter is null)
{
throw new ArgumentNullException(nameof(formatter));
}
return Configure(options => options.Formatter = formatter);
}
///
/// Configures OpenIddict to use the default token format (JWT) when issuing new state tokens.
///
/// The instance.
public OpenIddictClientDataProtectionBuilder PreferDefaultStateTokenFormat()
=> Configure(options => options.PreferDefaultStateTokenFormat = true);
///
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object? obj) => base.Equals(obj);
///
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => base.GetHashCode();
///
[EditorBrowsable(EditorBrowsableState.Never)]
public override string? ToString() => base.ToString();
}