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.DependencyInjection.Extensions; 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) where TAsymCryptParams : AsymCryptParams { services.TryAddScoped, AsymCryptService>(); return services; } public static IServiceCollection AddAsymCryptService(this IServiceCollection services, IConfigurationSection section) where TAsymCryptParams : AsymCryptParams => services.Configure(section).AddAsymCryptService(); public static IServiceCollection AddAsymCryptService(this IServiceCollection services, TAsymCryptParams param) where TAsymCryptParams : AsymCryptParams => services.AddSingleton(Options.Create(param)).AddAsymCryptService(); private static IServiceCollection AddRSAFactory(this IServiceCollection services) where TRSAFactoryParams : RSAFactoryParams { services.TryAddScoped, RSAFactory>(); return services; } public static IServiceCollection AddRSAFactory(this IServiceCollection services, IConfigurationSection section) where TRSAFactoryParams : RSAFactoryParams => services.Configure(section).AddRSAFactory(); public static IServiceCollection AddRSAFactory(this IServiceCollection services, TRSAFactoryParams param) where TRSAFactoryParams : RSAFactoryParams => services.AddSingleton(Options.Create(param)).AddRSAFactory(); } }