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

View File

@ -13,15 +13,15 @@ namespace DigitalData.Core.Security
private readonly IMapper _mapper;
private readonly IEnumerable<TokenDescription>? _tokenDescriptions;
private readonly TokenParams? _params;
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;
_mapper = mapper;
_tokenDescriptions = tokenDescriptionOptions?.Value;
_params = tokenParamOptions?.Value;
_cryptHandler = asymCryptHandler;
}
@ -47,7 +47,7 @@ namespace DigitalData.Core.Security
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}'.");
description.SigningCredentials = _cryptHandler.Decryptors