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