feat(ParamsConfigureOptions): Erstellt, um nach der Konfiguration über appsettings initialisiert zu werden.

- DI Extension Methoden wurden entsprechend bearbeitet.
This commit is contained in:
Developer 02 2024-12-16 17:20:40 +01:00
parent 155eb563d1
commit 154478c318
2 changed files with 29 additions and 15 deletions

View File

@ -0,0 +1,9 @@
using Microsoft.Extensions.Options;
namespace DigitalData.Core.Security.Config
{
public class ParamsConfigureOptions<TParams> : IConfigureOptions<TParams> where TParams : RSAFactoryParams
{
public void Configure(TParams options) => options.Init();
}
}

View File

@ -20,11 +20,14 @@ namespace DigitalData.Core.Security
options.Converters.Add(new JsonStringEnumConverter()); options.Converters.Add(new JsonStringEnumConverter());
return options; return options;
} }
private static IServiceCollection AddParamsConfigureOptions<TParams>(this IServiceCollection services) where TParams : RSAFactoryParams
=> services.AddSingleton<IConfigureOptions<TParams>, ParamsConfigureOptions<TParams>>();
private static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams private static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams
=> setAsDefault => setAsDefault
? services.AddSingleton<IAsymCryptService, AsymCryptService<TAsymCryptParams>>() ? services.AddParamsConfigureOptions<TAsymCryptParams>().AddSingleton<IAsymCryptService, AsymCryptService<TAsymCryptParams>>()
: services.AddSingleton<IAsymCryptService<TAsymCryptParams>, AsymCryptService<TAsymCryptParams>>(); : services.AddParamsConfigureOptions<TAsymCryptParams>().AddSingleton<IAsymCryptService<TAsymCryptParams>, AsymCryptService<TAsymCryptParams>>();
/// <summary> /// <summary>
/// Registers a custom asym crypt service with specified parameters from the given configuration section. /// Registers a custom asym crypt service with specified parameters from the given configuration section.
@ -34,9 +37,9 @@ namespace DigitalData.Core.Security
/// <param name="section"></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> /// <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> /// <returns></returns>
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false) public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams => services
where TAsymCryptParams : AsymCryptParams .Configure<TAsymCryptParams>(section)
=> services.Configure<TAsymCryptParams>(section).AddAsymCryptService<TAsymCryptParams>(setAsDefault: setAsDefault); .AddAsymCryptService<TAsymCryptParams>(setAsDefault: setAsDefault);
/// <summary> /// <summary>
/// Registers a custom asym crypt service with default parameters from the given configuration section. /// Registers a custom asym crypt service with default parameters from the given configuration section.
@ -56,9 +59,9 @@ namespace DigitalData.Core.Security
/// <param name="param"></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> /// <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> /// <returns></returns>
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, TAsymCryptParams param, bool setAsDefault = false) public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, TAsymCryptParams param, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams => services
where TAsymCryptParams : AsymCryptParams .AddSingleton(Options.Create(param))
=> services.AddSingleton(Options.Create(param)).AddAsymCryptService<TAsymCryptParams>(setAsDefault: setAsDefault); .AddAsymCryptService<TAsymCryptParams>(setAsDefault: setAsDefault);
/// <summary> /// <summary>
/// Registers default asym crypt service with the specified parameters from the given instance. /// Registers default asym crypt service with the specified parameters from the given instance.
@ -66,7 +69,8 @@ namespace DigitalData.Core.Security
/// <param name="services"></param> /// <param name="services"></param>
/// <param name="param"></param> /// <param name="param"></param>
/// <returns></returns> /// <returns></returns>
public static IServiceCollection AddAsymCryptService(this IServiceCollection services, AsymCryptParams param) => services.AddAsymCryptService(param: param, setAsDefault: true); public static IServiceCollection AddAsymCryptService(this IServiceCollection services, AsymCryptParams param) => services
.AddAsymCryptService(param: param, setAsDefault: true);
/// <summary> /// <summary>
/// Registers default RSA Factory instance with default params /// Registers default RSA Factory instance with default params
@ -74,8 +78,9 @@ namespace DigitalData.Core.Security
/// <param name="services"></param> /// <param name="services"></param>
/// <param name="factoryParams"></param> /// <param name="factoryParams"></param>
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns> /// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
public static IServiceCollection AddRSAFactory(this IServiceCollection services, RSAFactoryParams? factoryParams = null) public static IServiceCollection AddRSAFactory(this IServiceCollection services, RSAFactoryParams? factoryParams = null) => services
=> services.AddScoped<IRSAFactory>(_ => new RSAFactory<RSAFactoryParams>(Options.Create(factoryParams ?? new()))); .AddParamsConfigureOptions<RSAFactoryParams>()
.AddScoped<IRSAFactory>(_ => new RSAFactory<RSAFactoryParams>(Options.Create(factoryParams ?? new())));
/// <summary> /// <summary>
/// Registers a custom RSA Factory with specified parameters from the given configuration section. /// Registers a custom RSA Factory with specified parameters from the given configuration section.
@ -88,7 +93,7 @@ namespace DigitalData.Core.Security
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false) public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false)
where TRSAFactoryParams : RSAFactoryParams where TRSAFactoryParams : RSAFactoryParams
{ {
services.Configure<TRSAFactoryParams>(section); services.AddParamsConfigureOptions<TRSAFactoryParams>().Configure<TRSAFactoryParams>(section);
return setAsDefault return setAsDefault
? services.AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>() ? services.AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>()
: services.AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>(); : services.AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
@ -107,8 +112,8 @@ namespace DigitalData.Core.Security
{ {
services.AddSingleton(Options.Create(rsaParams)); services.AddSingleton(Options.Create(rsaParams));
return setAsDefault return setAsDefault
? services.AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>() ? services.AddParamsConfigureOptions<TRSAFactoryParams>().AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>()
: services.AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>(); : services.AddParamsConfigureOptions<TRSAFactoryParams>().AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
} }
} }
} }