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)); } public static IServiceCollection AddTokenDescriptions(this IServiceCollection services, IConfiguration configuration) => services.Configure>(configuration); public static IServiceCollection AddTokenDescriptions(this IServiceCollection services, params TokenDescription[] tokenDescriptions) => services.AddSingleton>>(Options.Create(tokenDescriptions)); public static IServiceCollection AddJwtSignatureHandler(this IServiceCollection services, Func>? claimsMapper = null, Func? subjectMapper = null, IConfiguration? tokenDescriptionconfig = null, params TokenDescription[]? tokenDescriptions) { if (tokenDescriptionconfig is not null) services.AddTokenDescriptions(tokenDescriptionconfig); if (tokenDescriptions is not null) services.AddTokenDescriptions(tokenDescriptions); return services .AddClaimDescriptor(claimsMapper: claimsMapper, subjectMapper: subjectMapper) .AddSingleton, JwtSignatureHandler>(); } } }