diff --git a/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs b/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs index 81088ee..be07e1d 100644 --- a/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs +++ b/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs @@ -1,9 +1,11 @@ namespace DigitalData.Core.Abstractions.Security { - public interface IAsymCryptService : IRSAFactory + public interface IAsymCryptService : IRSAFactory { public IEnumerable Decryptors { get; } public IEnumerable Encryptors { get; } } + + public interface IAsymCryptService : IAsymCryptService, IRSAFactory { } } \ No newline at end of file diff --git a/DigitalData.Core.Security/DIExtensions.cs b/DigitalData.Core.Security/DIExtensions.cs index b9a32dc..2adbd54 100644 --- a/DigitalData.Core.Security/DIExtensions.cs +++ b/DigitalData.Core.Security/DIExtensions.cs @@ -20,27 +20,55 @@ namespace DigitalData.Core.Security options.Converters.Add(new JsonStringEnumConverter()); return options; } + + private static IServiceCollection AddAsymCryptService(this IServiceCollection services, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams + => setAsDefault + ? services.AddScoped>() + : services.AddScoped, AsymCryptService>(); - private static IServiceCollection AddAsymCryptService(this IServiceCollection services) where TAsymCryptParams : AsymCryptParams - => services.AddScoped, AsymCryptService>(); - - public static IServiceCollection AddAsymCryptService(this IServiceCollection services, IConfigurationSection section) + /// + /// Registers a custom asym crypt service with specified parameters from the given configuration section. + /// + /// + /// + /// + /// If true, the factory is registered as the default . Otherwise, it is registered as . + /// + public static IServiceCollection AddAsymCryptService(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams - => services.Configure(section).AddAsymCryptService(); + => services.Configure(section).AddAsymCryptService(setAsDefault: setAsDefault); - public static IServiceCollection AddAsymCryptService(this IServiceCollection services, TAsymCryptParams param) + /// + /// Registers an asym crypt service with the specified parameters from the given instance. Optionally, sets it as the default factory. + /// + /// + /// + /// + /// If true, the factory is registered as the default . Otherwise, it is registered as . + /// + public static IServiceCollection AddAsymCryptService(this IServiceCollection services, TAsymCryptParams param, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams - => services.AddSingleton(Options.Create(param)).AddAsymCryptService(); + => services.AddSingleton(Options.Create(param)).AddAsymCryptService(setAsDefault: setAsDefault); + + /// + /// Registers default asym crypt service with the specified parameters from the given instance. + /// + /// + /// + /// + public static IServiceCollection AddAsymCryptService(this IServiceCollection services, AsymCryptParams param) => services.AddAsymCryptService(param: param, setAsDefault: true); /// /// Registers default RSA Factory instance with default params /// /// + /// /// The updated with the RSA Factory registered. - public static IServiceCollection AddRSAFactory(this IServiceCollection services) => services.AddScoped(_ => Instance.RSAFactory); - + public static IServiceCollection AddRSAFactory(this IServiceCollection services, RSAFactoryParams? factoryParams = null) + => services.AddScoped(_ => new RSAFactory(Options.Create(factoryParams ?? new()))); + /// - /// 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. /// /// ///