diff --git a/DigitalData.Core.Security/Config/MappingProfile.cs b/DigitalData.Core.Security/Config/MappingProfile.cs new file mode 100644 index 0000000..17ba5eb --- /dev/null +++ b/DigitalData.Core.Security/Config/MappingProfile.cs @@ -0,0 +1,13 @@ +using AutoMapper; +using Microsoft.IdentityModel.Tokens; + +namespace DigitalData.Core.Security.Config +{ + public class MappingProfile : Profile + { + public MappingProfile() + { + CreateMap(); + } + } +} \ No newline at end of file diff --git a/DigitalData.Core.Security/DIExtensions.cs b/DigitalData.Core.Security/DIExtensions.cs index 985368a..27cb935 100644 --- a/DigitalData.Core.Security/DIExtensions.cs +++ b/DigitalData.Core.Security/DIExtensions.cs @@ -9,13 +9,32 @@ 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); + } + } + 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 - => setAsDefault - ? services.AddParamsConfigureOptions().AddSingleton>() - : services.AddParamsConfigureOptions().AddSingleton, AsymCryptService>(); + { + services.AddParamsConfigureOptions().AddMappingProfile(); + return setAsDefault + ? services.AddSingleton>() + : services.AddSingleton, AsymCryptService>(); + } /// /// Registers a custom asym crypt service with specified parameters from the given configuration section. @@ -38,7 +57,7 @@ namespace DigitalData.Core.Security /// 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. /// @@ -68,8 +87,9 @@ namespace DigitalData.Core.Security /// 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()))); - + /// /// Registers a custom RSA Factory with specified parameters from the given configuration section. /// @@ -81,7 +101,7 @@ namespace DigitalData.Core.Security public static IServiceCollection AddRSAFactory(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false) where TRSAFactoryParams : RSAFactoryParams { - services.AddParamsConfigureOptions().Configure(section); + services.AddMappingProfile().AddParamsConfigureOptions().Configure(section); return setAsDefault ? services.AddSingleton>() : services.AddSingleton, RSAFactory>(); @@ -98,7 +118,7 @@ namespace DigitalData.Core.Security public static IServiceCollection AddRSAFactory(this IServiceCollection services, TRSAFactoryParams rsaParams, bool setAsDefault = false) where TRSAFactoryParams : RSAFactoryParams { - services.AddSingleton(Options.Create(rsaParams)); + services.AddMappingProfile().AddSingleton(Options.Create(rsaParams)); return setAsDefault ? services.AddParamsConfigureOptions().AddSingleton>() : services.AddParamsConfigureOptions().AddSingleton, RSAFactory>();