feat(MappingProfile): Mapping-Profil hinzugefügt, um TokenDescription in SecurityTokenDescriptor über AutoMapper zu konvertieren

This commit is contained in:
Developer 02 2024-12-20 00:11:33 +01:00
parent ce716d2bab
commit b6b12c7702
2 changed files with 40 additions and 7 deletions

View File

@ -0,0 +1,13 @@
using AutoMapper;
using Microsoft.IdentityModel.Tokens;
namespace DigitalData.Core.Security.Config
{
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<TokenDescription, SecurityTokenDescriptor>();
}
}
}

View File

@ -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<TParams>(this IServiceCollection services) where TParams : RSAFactoryParams
=> services.AddSingleton<IConfigureOptions<TParams>, ParamsConfigureOptions<TParams>>();
private static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams
=> setAsDefault
? services.AddParamsConfigureOptions<TAsymCryptParams>().AddSingleton<IAsymCryptService, AsymCryptService<TAsymCryptParams>>()
: services.AddParamsConfigureOptions<TAsymCryptParams>().AddSingleton<IAsymCryptService<TAsymCryptParams>, AsymCryptService<TAsymCryptParams>>();
{
services.AddParamsConfigureOptions<TAsymCryptParams>().AddMappingProfile();
return setAsDefault
? services.AddSingleton<IAsymCryptService, AsymCryptService<TAsymCryptParams>>()
: services.AddSingleton<IAsymCryptService<TAsymCryptParams>, AsymCryptService<TAsymCryptParams>>();
}
/// <summary>
/// Registers a custom asym crypt service with specified parameters from the given configuration section.
@ -38,7 +57,7 @@ namespace DigitalData.Core.Security
/// <returns></returns>
public static IServiceCollection AddAsymCryptService(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false)
=> services.Configure<AsymCryptParams>(section).AddAsymCryptService<AsymCryptParams>(setAsDefault: setAsDefault);
/// <summary>
/// Registers an asym crypt service with the specified parameters from the given instance. Optionally, sets it as the default factory.
/// </summary>
@ -68,8 +87,9 @@ namespace DigitalData.Core.Security
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
public static IServiceCollection AddRSAFactory(this IServiceCollection services, RSAFactoryParams? factoryParams = null) => services
.AddParamsConfigureOptions<RSAFactoryParams>()
.AddMappingProfile()
.AddScoped<IRSAFactory>(_ => new RSAFactory<RSAFactoryParams>(Options.Create(factoryParams ?? new())));
/// <summary>
/// Registers a custom RSA Factory with specified parameters from the given configuration section.
/// </summary>
@ -81,7 +101,7 @@ namespace DigitalData.Core.Security
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false)
where TRSAFactoryParams : RSAFactoryParams
{
services.AddParamsConfigureOptions<TRSAFactoryParams>().Configure<TRSAFactoryParams>(section);
services.AddMappingProfile().AddParamsConfigureOptions<TRSAFactoryParams>().Configure<TRSAFactoryParams>(section);
return setAsDefault
? services.AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>()
: services.AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
@ -98,7 +118,7 @@ namespace DigitalData.Core.Security
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(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<TRSAFactoryParams>().AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>()
: services.AddParamsConfigureOptions<TRSAFactoryParams>().AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();