feat(MappingProfile): Mapping-Profil hinzugefügt, um TokenDescription in SecurityTokenDescriptor über AutoMapper zu konvertieren
This commit is contained in:
parent
ce716d2bab
commit
b6b12c7702
13
DigitalData.Core.Security/Config/MappingProfile.cs
Normal file
13
DigitalData.Core.Security/Config/MappingProfile.cs
Normal 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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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.
|
||||
@ -68,6 +87,7 @@ 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>
|
||||
@ -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>>();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user