fix: TokenParams-Kaliber erstellt, um Token-Beschreibungen über IOptions zu konfigurieren

This commit is contained in:
Developer 02 2025-01-07 10:21:25 +01:00
parent 15e909064f
commit 4874079b69
3 changed files with 21 additions and 17 deletions

View File

@ -0,0 +1,7 @@
namespace DigitalData.Core.Security.Config
{
public class TokenParams
{
public required IEnumerable<TokenDescription> Descriptions { get; init; }
}
}

View File

@ -61,23 +61,20 @@ namespace DigitalData.Core.Security
return services.AddSingleton(sp => Options.Create(descriptor)); return services.AddSingleton(sp => Options.Create(descriptor));
} }
public static IServiceCollection AddTokenDescriptions(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddTokenParams(this IServiceCollection services, IConfiguration configuration)
=> services.Configure<IEnumerable<TokenDescription>>(configuration); => services.Configure<TokenParams>(configuration);
public static IServiceCollection AddTokenDescriptions(this IServiceCollection services, params TokenDescription[] tokenDescriptions) public static IServiceCollection AddTokenParams(this IServiceCollection services, TokenParams tokenParams)
=> services.AddSingleton<IOptions<IEnumerable<TokenDescription>>>(Options.Create(tokenDescriptions)); => services.AddSingleton(Options.Create(tokenParams));
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) public static IServiceCollection AddTokenParams(this IServiceCollection services, params TokenDescription[] descriptions)
{ => services.AddSingleton(Options.Create<TokenParams>(new() { Descriptions = descriptions }));
if (tokenDescriptionconfig is not null)
services.AddTokenDescriptions(tokenDescriptionconfig);
if (tokenDescriptions is not null) public static IServiceCollection AddJwtSignatureHandler<TPrincipal>(this IServiceCollection services,
services.AddTokenDescriptions(tokenDescriptions); Func<TPrincipal, IDictionary<string, object>>? claimsMapper = null,
Func<TPrincipal, ClaimsIdentity>? subjectMapper = null)
return services => services
.AddClaimDescriptor(claimsMapper: claimsMapper, subjectMapper: subjectMapper) .AddClaimDescriptor(claimsMapper: claimsMapper, subjectMapper: subjectMapper)
.AddSingleton<IJwtSignatureHandler<TPrincipal>, JwtSignatureHandler<TPrincipal>>(); .AddSingleton<IJwtSignatureHandler<TPrincipal>, JwtSignatureHandler<TPrincipal>>();
} }
} }
}

View File

@ -13,15 +13,15 @@ namespace DigitalData.Core.Security
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly IEnumerable<TokenDescription>? _tokenDescriptions; private readonly TokenParams? _params;
private readonly IAsymCryptHandler _cryptHandler; private readonly IAsymCryptHandler _cryptHandler;
public JwtSignatureHandler(IOptions<ClaimDescriptor<TPrincipal>> claimDescriptorOptions, IMapper mapper, IOptions<IEnumerable<TokenDescription>>? tokenDescriptionOptions, IAsymCryptHandler asymCryptHandler) public JwtSignatureHandler(IOptions<ClaimDescriptor<TPrincipal>> claimDescriptorOptions, IMapper mapper, IOptions<TokenParams>? tokenParamOptions, IAsymCryptHandler asymCryptHandler)
{ {
_claimDescriptor = claimDescriptorOptions.Value; _claimDescriptor = claimDescriptorOptions.Value;
_mapper = mapper; _mapper = mapper;
_tokenDescriptions = tokenDescriptionOptions?.Value; _params = tokenParamOptions?.Value;
_cryptHandler = asymCryptHandler; _cryptHandler = asymCryptHandler;
} }
@ -47,7 +47,7 @@ namespace DigitalData.Core.Security
public SecurityToken CreateToken(TPrincipal subject, string apiRoute) public SecurityToken CreateToken(TPrincipal subject, string apiRoute)
{ {
var description = _tokenDescriptions?.SingleOrDefault(description => description.ApiRoute == apiRoute) var description = _params?.Descriptions.SingleOrDefault(description => description.ApiRoute == apiRoute)
?? throw new InvalidOperationException($"No or multiple token description found for api route '{apiRoute}'."); ?? throw new InvalidOperationException($"No or multiple token description found for api route '{apiRoute}'.");
description.SigningCredentials = _cryptHandler.Decryptors description.SigningCredentials = _cryptHandler.Decryptors