From 4874079b69ae028c3eb678e9300ae4240a41822e Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Tue, 7 Jan 2025 10:21:25 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20TokenParams-Kaliber=20erstellt,=20um=20T?= =?UTF-8?q?oken-Beschreibungen=20=C3=BCber=20IOptions=20zu=20konfigurieren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Config/TokenParams.cs | 7 ++++++ DigitalData.Core.Security/DIExtensions.cs | 23 ++++++++----------- .../JwtSignatureHandler.cs | 8 +++---- 3 files changed, 21 insertions(+), 17 deletions(-) create mode 100644 DigitalData.Core.Security/Config/TokenParams.cs diff --git a/DigitalData.Core.Security/Config/TokenParams.cs b/DigitalData.Core.Security/Config/TokenParams.cs new file mode 100644 index 0000000..442c7d2 --- /dev/null +++ b/DigitalData.Core.Security/Config/TokenParams.cs @@ -0,0 +1,7 @@ +namespace DigitalData.Core.Security.Config +{ + public class TokenParams + { + public required IEnumerable Descriptions { get; init; } + } +} \ No newline at end of file diff --git a/DigitalData.Core.Security/DIExtensions.cs b/DigitalData.Core.Security/DIExtensions.cs index 09ddd79..8d528e6 100644 --- a/DigitalData.Core.Security/DIExtensions.cs +++ b/DigitalData.Core.Security/DIExtensions.cs @@ -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>(configuration); + public static IServiceCollection AddTokenParams(this IServiceCollection services, IConfiguration configuration) + => services.Configure(configuration); - public static IServiceCollection AddTokenDescriptions(this IServiceCollection services, params TokenDescription[] tokenDescriptions) - => services.AddSingleton>>(Options.Create(tokenDescriptions)); + public static IServiceCollection AddTokenParams(this IServiceCollection services, TokenParams tokenParams) + => services.AddSingleton(Options.Create(tokenParams)); - public static IServiceCollection AddJwtSignatureHandler(this IServiceCollection services, Func>? claimsMapper = null, Func? subjectMapper = null, IConfiguration? tokenDescriptionconfig = null, params TokenDescription[]? tokenDescriptions) - { - if (tokenDescriptionconfig is not null) - services.AddTokenDescriptions(tokenDescriptionconfig); - - if (tokenDescriptions is not null) - services.AddTokenDescriptions(tokenDescriptions); + public static IServiceCollection AddTokenParams(this IServiceCollection services, params TokenDescription[] descriptions) + => services.AddSingleton(Options.Create(new() { Descriptions = descriptions })); - return services + public static IServiceCollection AddJwtSignatureHandler(this IServiceCollection services, + Func>? claimsMapper = null, + Func? subjectMapper = null) + => services .AddClaimDescriptor(claimsMapper: claimsMapper, subjectMapper: subjectMapper) .AddSingleton, JwtSignatureHandler>(); - } } } \ No newline at end of file diff --git a/DigitalData.Core.Security/JwtSignatureHandler.cs b/DigitalData.Core.Security/JwtSignatureHandler.cs index 07adde3..0f71e3e 100644 --- a/DigitalData.Core.Security/JwtSignatureHandler.cs +++ b/DigitalData.Core.Security/JwtSignatureHandler.cs @@ -13,15 +13,15 @@ namespace DigitalData.Core.Security private readonly IMapper _mapper; - private readonly IEnumerable? _tokenDescriptions; + private readonly TokenParams? _params; private readonly IAsymCryptHandler _cryptHandler; - public JwtSignatureHandler(IOptions> claimDescriptorOptions, IMapper mapper, IOptions>? tokenDescriptionOptions, IAsymCryptHandler asymCryptHandler) + public JwtSignatureHandler(IOptions> claimDescriptorOptions, IMapper mapper, IOptions? 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