diff --git a/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs b/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs index 0f85ab1..5d9b6da 100644 --- a/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs +++ b/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs @@ -10,6 +10,4 @@ IEnumerable Encryptors { get; } } - - public interface IAsymCryptService : IAsymCryptService, IRSAFactory { } } \ No newline at end of file diff --git a/DigitalData.Core.Abstractions/Security/IRSAFactory.cs b/DigitalData.Core.Abstractions/Security/IRSAFactory.cs index 3e7233e..a1e4bb9 100644 --- a/DigitalData.Core.Abstractions/Security/IRSAFactory.cs +++ b/DigitalData.Core.Abstractions/Security/IRSAFactory.cs @@ -20,6 +20,4 @@ namespace DigitalData.Core.Abstractions.Security IRSADecryptor CreateDecryptor(string pem, string? issuer = null, string? audience = null, bool encrypt = false, RSAEncryptionPadding? padding = null); } - - public interface IRSAFactory : IRSAFactory { } } \ No newline at end of file diff --git a/DigitalData.Core.Security/AsymCryptService.cs b/DigitalData.Core.Security/AsymCryptService.cs index 41539a2..3c363f2 100644 --- a/DigitalData.Core.Security/AsymCryptService.cs +++ b/DigitalData.Core.Security/AsymCryptService.cs @@ -7,8 +7,7 @@ using System.Collections; namespace DigitalData.Core.Security { - public class AsymCryptService : RSAFactory, IAsymCryptService, IRSAFactory, IEnumerable - where TAsymCryptParams : AsymCryptParams + public class AsymCryptService : RSAFactory, IAsymCryptService, IRSAFactory, IEnumerable { public IEnumerable Decryptors { get; } @@ -38,7 +37,7 @@ namespace DigitalData.Core.Security index, $"The index {index} is out of range. The valid indices for {GetType()} are between 0 and {Decryptors.Count() - 1} (inclusive). Please ensure the index is within this range."); - public AsymCryptService(IOptions options, ILogger>? logger = null) : base(options) + public AsymCryptService(IOptions options, ILogger? logger = null) : base(options) { logger?.LogInformation("Core.Secrets version: {Version}, Created on: {CreationDate}.", Secrets.Version, Secrets.CreationDate.ToString("dd.MM.yyyy")); diff --git a/DigitalData.Core.Security/Cryptographer/RSAFactory.cs b/DigitalData.Core.Security/Cryptographer/RSAFactory.cs index 609fbe5..299035a 100644 --- a/DigitalData.Core.Security/Cryptographer/RSAFactory.cs +++ b/DigitalData.Core.Security/Cryptographer/RSAFactory.cs @@ -5,7 +5,7 @@ using System.Security.Cryptography; namespace DigitalData.Core.Security.Cryptographer { - public class RSAFactory : IRSAFactory where TRSAFactoryParams : RSAFactoryParams + public class RSAFactory : IRSAFactory where TRSAFactoryParams : RSAFactoryParams { protected readonly TRSAFactoryParams _params; diff --git a/DigitalData.Core.Security/DIExtensions.cs b/DigitalData.Core.Security/DIExtensions.cs index 2d57ced..70f1b5c 100644 --- a/DigitalData.Core.Security/DIExtensions.cs +++ b/DigitalData.Core.Security/DIExtensions.cs @@ -10,122 +10,43 @@ namespace DigitalData.Core.Security { public static class DIExtensions { - private static (bool Added, object Lock) _mappingProfile = (false, new()); - private static IServiceCollection AddMappingProfile(this IServiceCollection services) - { - if (_mappingProfile.Added) - return services; - - lock (_mappingProfile.Lock) - { - if (_mappingProfile.Added) - return services; - - _mappingProfile.Added = true; - return services - .AddAutoMapper(typeof(MappingProfile).Assembly) - .AddSingleton(); - } - } - private static IServiceCollection AddParamsConfigureOptions(this IServiceCollection services) where TParams : RSAFactoryParams => services.AddSingleton, ParamsConfigureOptions>(); - private static IServiceCollection AddAsymCryptService(this IServiceCollection services, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams - { - services.AddParamsConfigureOptions().AddMappingProfile(); - return setAsDefault - ? services.AddSingleton>() - : services.AddSingleton, AsymCryptService>(); - } + private static IServiceCollection AddAsymCryptService(this IServiceCollection services) => services + .AddParamsConfigureOptions() + .AddAutoMapper(typeof(MappingProfile).Assembly) + .AddSingleton(); /// /// 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(setAsDefault: setAsDefault); - - /// - /// Registers a custom asym crypt service with default parameters from the given configuration section. - /// - /// - /// - /// - /// - public static IServiceCollection AddAsymCryptService(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false) - => services.Configure(section).AddAsymCryptService(setAsDefault: setAsDefault); - - /// - /// 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(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, RSAFactoryParams? factoryParams = null) => services - .AddParamsConfigureOptions() - .AddMappingProfile() - .AddScoped(_ => new RSAFactory(Options.Create(factoryParams ?? new()))); + public static IServiceCollection AddAsymCryptService(this IServiceCollection services, IConfigurationSection section) => services + .Configure(section) + .AddAsymCryptService(); + + /// + /// Registers an asym crypt service with the specified parameters from the given instance. + /// + /// + /// The updated with the RSA Factory registered. + public static IServiceCollection AddAsymCryptService(this IServiceCollection services, AsymCryptParams? asymCryptParams = null) => services + .AddSingleton(Options.Create(asymCryptParams ?? new())) + .AddAsymCryptService(); /// /// Registers a custom RSA Factory with specified parameters from the given configuration section. /// - /// /// /// - /// If true, the factory is registered as the default . Otherwise, it is registered as . /// The updated with the RSA Factory registered. - public static IServiceCollection AddRSAFactory(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false) - where TRSAFactoryParams : RSAFactoryParams - { - services.AddMappingProfile().AddParamsConfigureOptions().Configure(section); - return setAsDefault - ? services.AddSingleton>() - : services.AddSingleton, RSAFactory>(); - } - - /// - /// Registers an RSA Factory with the specified parameters from the given instance. Optionally, sets it as the default factory. - /// - /// The type of the RSA factory parameters. - /// - /// - /// If true, the factory is registered as the default . Otherwise, it is registered as . - /// The updated with the RSA Factory registered. - public static IServiceCollection AddRSAFactory(this IServiceCollection services, TRSAFactoryParams rsaParams, bool setAsDefault = false) - where TRSAFactoryParams : RSAFactoryParams - { - services.AddMappingProfile().AddSingleton(Options.Create(rsaParams)); - return setAsDefault - ? services.AddParamsConfigureOptions().AddSingleton>() - : services.AddParamsConfigureOptions().AddSingleton, RSAFactory>(); - } + public static IServiceCollection AddRSAFactory(this IServiceCollection services, IConfigurationSection section) => services + .AddParamsConfigureOptions() + .Configure(section) + .AddSingleton>(); private static IServiceCollection AddClaimDescriptor(this IServiceCollection services, Func>? claimsMapper = null,