refactor(AsymCryptService): Nicht-generische Schnittstelle erstellt.

- Geordnete DI-Registrierungsmethoden.
This commit is contained in:
Developer 02 2024-12-13 14:44:09 +01:00
parent 82aa8d1143
commit 644283cf8f
2 changed files with 41 additions and 11 deletions

View File

@ -1,9 +1,11 @@
namespace DigitalData.Core.Abstractions.Security namespace DigitalData.Core.Abstractions.Security
{ {
public interface IAsymCryptService<TParams> : IRSAFactory<TParams> public interface IAsymCryptService : IRSAFactory
{ {
public IEnumerable<IRSADecryptor> Decryptors { get; } public IEnumerable<IRSADecryptor> Decryptors { get; }
public IEnumerable<IRSAEncryptor> Encryptors { get; } public IEnumerable<IRSAEncryptor> Encryptors { get; }
} }
public interface IAsymCryptService<TParams> : IAsymCryptService, IRSAFactory<TParams> { }
} }

View File

@ -20,27 +20,55 @@ namespace DigitalData.Core.Security
options.Converters.Add(new JsonStringEnumConverter()); options.Converters.Add(new JsonStringEnumConverter());
return options; return options;
} }
private static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams
=> setAsDefault
? services.AddScoped<IAsymCryptService, AsymCryptService<TAsymCryptParams>>()
: services.AddScoped<IAsymCryptService<TAsymCryptParams>, AsymCryptService<TAsymCryptParams>>();
private static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services) where TAsymCryptParams : AsymCryptParams /// <summary>
=> services.AddScoped<IAsymCryptService<TAsymCryptParams>, AsymCryptService<TAsymCryptParams>>(); /// Registers a custom asym crypt service with specified parameters from the given configuration section.
/// </summary>
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, IConfigurationSection section) /// <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 where TAsymCryptParams : AsymCryptParams
=> services.Configure<TAsymCryptParams>(section).AddAsymCryptService<TAsymCryptParams>(); => services.Configure<TAsymCryptParams>(section).AddAsymCryptService<TAsymCryptParams>(setAsDefault: setAsDefault);
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, TAsymCryptParams param) /// <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 where TAsymCryptParams : AsymCryptParams
=> services.AddSingleton(Options.Create(param)).AddAsymCryptService<TAsymCryptParams>(); => 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> /// <summary>
/// Registers default RSA Factory instance with default params /// Registers default RSA Factory instance with default params
/// </summary> /// </summary>
/// <param name="services"></param> /// <param name="services"></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) => services.AddScoped(_ => Instance.RSAFactory); public static IServiceCollection AddRSAFactory(this IServiceCollection services, RSAFactoryParams? factoryParams = null)
=> services.AddScoped<IRSAFactory>(_ => new RSAFactory<RSAFactoryParams>(Options.Create(factoryParams ?? new())));
/// <summary> /// <summary>
/// Registers a custom RSA Factory instance with specified parameters from the given configuration section. /// Registers a custom RSA Factory with specified parameters from the given configuration section.
/// </summary> /// </summary>
/// <typeparam name="TRSAFactoryParams"></typeparam> /// <typeparam name="TRSAFactoryParams"></typeparam>
/// <param name="services"></param> /// <param name="services"></param>