Developer 02 7da93c6719 refactor(DIExtensions): Verbesserung der Registrierung von AsymCrypt-Diensten und Vereinfachung von Overloads
- `AddAsymCryptService` aktualisiert, um eine Standardimplementierung mit `AsymCryptParams` ohne generische Typen bereitzustellen.
- Neue Überladung von `AddAsymCryptService` hinzugefügt, die eine `IConfigurationSection` für Standardparameter akzeptiert.
- Lebensdauer der Service-Registrierungen für `IAsymCryptService` von `Scoped` auf `Singleton` geändert, um Konsistenz und geringeren Overhead zu gewährleisten.
2024-12-16 09:44:51 +01:00

114 lines
6.9 KiB
C#

using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Config;
using DigitalData.Core.Security.Cryptographer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace DigitalData.Core.Security
{
public static class DIExtensions
{
public static JsonSerializerOptions AddCryptographerConverter(this JsonSerializerOptions options)
{
if (!options.Converters.OfType<HashAlgorithmNameConverter>().Any())
options.Converters.Add(new HashAlgorithmNameConverter());
if (!options.Converters.OfType<JsonStringEnumConverter>().Any())
options.Converters.Add(new JsonStringEnumConverter());
return options;
}
private static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams
=> setAsDefault
? services.AddSingleton<IAsymCryptService, AsymCryptService<TAsymCryptParams>>()
: services.AddSingleton<IAsymCryptService<TAsymCryptParams>, AsymCryptService<TAsymCryptParams>>();
/// <summary>
/// Registers a custom asym crypt service with specified parameters from the given configuration section.
/// </summary>
/// <typeparam name="TAsymCryptParams"></typeparam>
/// <param name="services"></param>
/// <param name="section"></param>
/// <param name="setAsDefault">If true, the factory is registered as the default <see cref="IRSAFactory"/>. Otherwise, it is registered as <see cref="IRSAFactory{TRSAFactoryParams}"/>.</param>
/// <returns></returns>
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false)
where TAsymCryptParams : AsymCryptParams
=> services.Configure<TAsymCryptParams>(section).AddAsymCryptService<TAsymCryptParams>(setAsDefault: setAsDefault);
/// <summary>
/// Registers a custom asym crypt service with default parameters from the given configuration section.
/// </summary>
/// <param name="services"></param>
/// <param name="section"></param>
/// <param name="setAsDefault"></param>
/// <returns></returns>
public static IServiceCollection AddAsymCryptService(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false)
=> services.Configure<AsymCryptParams>(section).AddAsymCryptService<AsymCryptParams>(setAsDefault: setAsDefault);
/// <summary>
/// Registers an asym crypt service with the specified parameters from the given instance. Optionally, sets it as the default factory.
/// </summary>
/// <typeparam name="TAsymCryptParams"></typeparam>
/// <param name="services"></param>
/// <param name="param"></param>
/// <param name="setAsDefault">If true, the factory is registered as the default <see cref="IRSAFactory"/>. Otherwise, it is registered as <see cref="IRSAFactory{TRSAFactoryParams}"/>.</param>
/// <returns></returns>
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, TAsymCryptParams param, bool setAsDefault = false)
where TAsymCryptParams : AsymCryptParams
=> services.AddSingleton(Options.Create(param)).AddAsymCryptService<TAsymCryptParams>(setAsDefault: setAsDefault);
/// <summary>
/// Registers default asym crypt service with the specified parameters from the given instance.
/// </summary>
/// <param name="services"></param>
/// <param name="param"></param>
/// <returns></returns>
public static IServiceCollection AddAsymCryptService(this IServiceCollection services, AsymCryptParams param) => services.AddAsymCryptService(param: param, setAsDefault: true);
/// <summary>
/// Registers default RSA Factory instance with default params
/// </summary>
/// <param name="services"></param>
/// <param name="factoryParams"></param>
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
public static IServiceCollection AddRSAFactory(this IServiceCollection services, RSAFactoryParams? factoryParams = null)
=> services.AddScoped<IRSAFactory>(_ => new RSAFactory<RSAFactoryParams>(Options.Create(factoryParams ?? new())));
/// <summary>
/// Registers a custom RSA Factory with specified parameters from the given configuration section.
/// </summary>
/// <typeparam name="TRSAFactoryParams"></typeparam>
/// <param name="services"></param>
/// <param name="section"></param>
/// <param name="setAsDefault">If true, the factory is registered as the default <see cref="IRSAFactory"/>. Otherwise, it is registered as <see cref="IRSAFactory{TRSAFactoryParams}"/>.</param>
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false)
where TRSAFactoryParams : RSAFactoryParams
{
services.Configure<TRSAFactoryParams>(section);
return setAsDefault
? services.AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>()
: services.AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
}
/// <summary>
/// Registers an RSA Factory with the specified parameters from the given instance. Optionally, sets it as the default factory.
/// </summary>
/// <typeparam name="TRSAFactoryParams">The type of the RSA factory parameters.</typeparam>
/// <param name="services"></param>
/// <param name="rsaParams"></param>
/// <param name="setAsDefault">If true, the factory is registered as the default <see cref="IRSAFactory"/>. Otherwise, it is registered as <see cref="IRSAFactory{TRSAFactoryParams}"/>.</param>
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, TRSAFactoryParams rsaParams, bool setAsDefault = false)
where TRSAFactoryParams : RSAFactoryParams
{
services.AddSingleton(Options.Create(rsaParams));
return setAsDefault
? services.AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>()
: services.AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
}
}
}