- Methoden eingeführt, um Token-Beschreibungen im DI-Container zu konfigurieren und zu registrieren. - Überladungen zu `AddJwtSignatureHandler` hinzugefügt, um sowohl konfigurationsbasierte als auch Inline-Token-Beschreibungen zu unterstützen.
83 lines
4.4 KiB
C#
83 lines
4.4 KiB
C#
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<TParams>(this IServiceCollection services) where TParams : RSAFactoryParams
|
|
=> services.AddSingleton<IConfigureOptions<TParams>, ParamsConfigureOptions<TParams>>();
|
|
|
|
private static IServiceCollection AddAsymCryptService(this IServiceCollection services) => services
|
|
.AddParamsConfigureOptions<AsymCryptParams>()
|
|
.AddAutoMapper(typeof(MappingProfile).Assembly)
|
|
.AddSingleton<IAsymCryptHandler, AsymCryptHandler>();
|
|
|
|
/// <summary>
|
|
/// Registers a custom asym crypt service with specified parameters from the given configuration section.
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="section"></param>
|
|
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
|
|
public static IServiceCollection AddAsymCryptService(this IServiceCollection services, IConfigurationSection section) => services
|
|
.Configure<AsymCryptParams>(section)
|
|
.AddAsymCryptService();
|
|
|
|
/// <summary>
|
|
/// Registers an asym crypt service with the specified parameters from the given instance.
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
|
|
public static IServiceCollection AddAsymCryptService(this IServiceCollection services, AsymCryptParams? asymCryptParams = null) => services
|
|
.AddSingleton(Options.Create(asymCryptParams ?? new()))
|
|
.AddAsymCryptService();
|
|
|
|
/// <summary>
|
|
/// Registers a custom RSA Factory with specified parameters from the given configuration section.
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="section"></param>
|
|
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
|
|
public static IServiceCollection AddRSAFactory(this IServiceCollection services, IConfigurationSection section) => services
|
|
.AddParamsConfigureOptions<RSAFactoryParams>()
|
|
.Configure<RSAFactoryParams>(section)
|
|
.AddSingleton<IRSAFactory, RSAFactory<RSAFactoryParams>>();
|
|
|
|
private static IServiceCollection AddClaimDescriptor<TPrincipal>(this IServiceCollection services,
|
|
Func<TPrincipal, IDictionary<string, object>>? claimsMapper = null,
|
|
Func<TPrincipal, ClaimsIdentity>? subjectMapper = null)
|
|
{
|
|
var descriptor = new ClaimDescriptor<TPrincipal>
|
|
{
|
|
CreateClaims = claimsMapper,
|
|
CreateSubject = subjectMapper
|
|
};
|
|
|
|
return services.AddSingleton(sp => Options.Create(descriptor));
|
|
}
|
|
|
|
public static IServiceCollection AddTokenDescriptions(this IServiceCollection services, IConfiguration configuration)
|
|
=> services.Configure<IEnumerable<TokenDescription>>(configuration);
|
|
|
|
public static IServiceCollection AddTokenDescriptions(this IServiceCollection services, params TokenDescription[] tokenDescriptions)
|
|
=> services.AddSingleton<IOptions<IEnumerable<TokenDescription>>>(Options.Create(tokenDescriptions));
|
|
|
|
public static IServiceCollection AddJwtSignatureHandler<TPrincipal>(this IServiceCollection services, Func<TPrincipal, IDictionary<string, object>>? claimsMapper = null, Func<TPrincipal, ClaimsIdentity>? 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<IJwtSignatureHandler<TPrincipal>, JwtSignatureHandler<TPrincipal>>();
|
|
}
|
|
}
|
|
} |