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.Security.Claims; namespace DigitalData.Core.Security { public static class DIExtensions { private static IServiceCollection AddParamsConfigureOptions(this IServiceCollection services) where TParams : RSAFactoryParams => services.AddSingleton, ParamsConfigureOptions>(); 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. /// /// /// /// The updated with the RSA Factory registered. 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. /// /// /// /// The updated with the RSA Factory registered. public static IServiceCollection AddRSAFactory(this IServiceCollection services, IConfigurationSection section) => services .AddParamsConfigureOptions() .Configure(section) .AddSingleton>(); private static IServiceCollection AddClaimDescriptor(this IServiceCollection services, Func>? claimsMapper = null, Func? subjectMapper = null) { var descriptor = new ClaimDescriptor { CreateClaims = claimsMapper, CreateSubject = subjectMapper }; return services.AddSingleton(sp => Options.Create(descriptor)); } } }