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().Any()) options.Converters.Add(new HashAlgorithmNameConverter()); if (!options.Converters.OfType().Any()) 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>(); /// /// 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 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.AddScoped(_ => new RSAFactory(Options.Create(factoryParams ?? new()))); /// /// 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.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.AddSingleton(Options.Create(rsaParams)); return setAsDefault ? services.AddSingleton>() : services.AddSingleton, RSAFactory>(); } } }