From 06260e0edb72e225a4845b8b69426967ad64adad Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Fri, 20 Dec 2024 01:51:48 +0100 Subject: [PATCH] =?UTF-8?q?feat(JwtSignatureService):=20Erstellt=20mit=20g?= =?UTF-8?q?rundlegender=20Funktionalit=C3=A4t,=20um=20mit=20Token=20Beschr?= =?UTF-8?q?eibung=20erstellen=20zu=20k=C3=B6nnen.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JwtSignatureService.cs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 DigitalData.Core.Security/JwtSignatureService.cs diff --git a/DigitalData.Core.Security/JwtSignatureService.cs b/DigitalData.Core.Security/JwtSignatureService.cs new file mode 100644 index 0000000..2aa5b29 --- /dev/null +++ b/DigitalData.Core.Security/JwtSignatureService.cs @@ -0,0 +1,30 @@ +using DigitalData.Core.Security.Config; +using Microsoft.Extensions.Options; +using Microsoft.IdentityModel.Tokens; +using System.IdentityModel.Tokens.Jwt; + +namespace DigitalData.Core.Security +{ + public class JwtSignatureService : JwtSecurityTokenHandler + { + private readonly ClaimDescriptor _claimDescriptor; + + private readonly TokenDescriptorProvider _descriptorProvider; + + public JwtSignatureService(IOptions> claimDescriptorOptions, TokenDescriptorProvider descriptorProvider) + { + _claimDescriptor = claimDescriptorOptions.Value; + _descriptorProvider = descriptorProvider; + } + + public SecurityToken CreateToken(TPrincipal subject, TokenDescription description) + { + var descriptor = _descriptorProvider.Create(description: description); + descriptor.Claims = _claimDescriptor.CreateClaims?.Invoke(subject); + descriptor.Subject = _claimDescriptor.CreateSubject?.Invoke(subject); + return CreateToken(descriptor); + } + + public string CreateAndWriteToken(TPrincipal subject, TokenDescription description) => WriteToken(CreateToken(subject, description)); + } +} \ No newline at end of file