From ca4718e1597bc1a26f3bc68fd02fd6d346381266 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Fri, 7 Feb 2025 13:12:27 +0100 Subject: [PATCH] =?UTF-8?q?feat(ControllerBaseExtensions):=20Erstellte=20E?= =?UTF-8?q?rweiterungsmethode=20zum=20Login=20=C3=BCber=20HttpContext=20mi?= =?UTF-8?q?t=20Umschlag=20Empf=C3=A4nger=20und=20Rolle.=20=20-=20Implement?= =?UTF-8?q?iert=20in=20HomeController?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ControllerBaseExtensions.cs | 37 ++++++++++++++++++- .../Controllers/HomeController.cs | 26 ++----------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/EnvelopeGenerator.Web/Controllers/ControllerBaseExtensions.cs b/EnvelopeGenerator.Web/Controllers/ControllerBaseExtensions.cs index 0ad1e9f0..ce6cd783 100644 --- a/EnvelopeGenerator.Web/Controllers/ControllerBaseExtensions.cs +++ b/EnvelopeGenerator.Web/Controllers/ControllerBaseExtensions.cs @@ -1,4 +1,7 @@ -using EnvelopeGenerator.Web.Models; +using EnvelopeGenerator.Application.DTOs.EnvelopeReceiver; +using EnvelopeGenerator.Web.Models; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Mvc; using System.Security.Claims; @@ -6,6 +9,7 @@ namespace EnvelopeGenerator.Web.Controllers { public static class ControllerBaseExtensions { + #region Auth public static string? GetClaimValue(this ControllerBase controller, string claimType) => controller.User.FindFirstValue(claimType); public static string? GetAuthEnvelopeUuid(this ControllerBase controller) => controller.User.FindFirstValue(ClaimTypes.NameIdentifier); @@ -23,7 +27,35 @@ namespace EnvelopeGenerator.Web.Controllers var env_id_str = controller.User.FindFirstValue(EnvelopeClaimTypes.Id); return int.TryParse(env_id_str, out int env_id) ? env_id : null; } + + public static async Task SignInEnvelopeAsync(this HttpContext context, EnvelopeReceiverDto er, string receiverRole) + { + var claims = new List { + new(ClaimTypes.NameIdentifier, er.Envelope!.Uuid), + new(ClaimTypes.Hash, er.Receiver!.Signature), + new(ClaimTypes.Name, er.Name ?? string.Empty), + new(ClaimTypes.Email, er.Receiver.EmailAddress), + new(EnvelopeClaimTypes.Title, er.Envelope.Title), + new(EnvelopeClaimTypes.Id, er.Envelope.Id.ToString()), + new(ClaimTypes.Role, receiverRole) + }; + + var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); + + var authProperties = new AuthenticationProperties + { + AllowRefresh = false, + IsPersistent = false + }; + + await context.SignInAsync( + CookieAuthenticationDefaults.AuthenticationScheme, + new ClaimsPrincipal(claimsIdentity), + authProperties); + } + #endregion + #region View error //TODO: integrate localizer for ready-to-use views public static ViewResult ViewError(this Controller controller, ErrorViewModel errorViewModel) => controller.View("_Error", errorViewModel); @@ -61,5 +93,6 @@ namespace EnvelopeGenerator.Web.Controllers Subtitle = "Ein unerwarteter Fehler ist aufgetreten", Body = "Bitte kontaktieren Sie das IT-Team." }); - } + #endregion + } } \ No newline at end of file diff --git a/EnvelopeGenerator.Web/Controllers/HomeController.cs b/EnvelopeGenerator.Web/Controllers/HomeController.cs index 6535edce..552d284a 100644 --- a/EnvelopeGenerator.Web/Controllers/HomeController.cs +++ b/EnvelopeGenerator.Web/Controllers/HomeController.cs @@ -313,28 +313,8 @@ public class HomeController : ViewControllerBase _logger.LogEnvelopeError(envelopeReceiverId: envelopeReceiverId, message: "No document byte-data was found in ENVELOPE_DOCUMENT table."); return this.ViewDocumentNotFound(); } - - var claims = new List { - new(ClaimTypes.NameIdentifier, uuid), - new(ClaimTypes.Hash, signature), - new(ClaimTypes.Name, er.Name ?? string.Empty), - new(ClaimTypes.Email, er.Receiver.EmailAddress), - new(EnvelopeClaimTypes.Title, er.Envelope.Title), - new(EnvelopeClaimTypes.Id, er.Envelope.Id.ToString()), - new(ClaimTypes.Role, ReceiverRole.FullyAuth) - }; - - var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); - var authProperties = new AuthenticationProperties - { - AllowRefresh = false, - IsPersistent = false - }; - - await HttpContext.SignInAsync( - CookieAuthenticationDefaults.AuthenticationScheme, - new ClaimsPrincipal(claimsIdentity), - authProperties); + + await HttpContext.SignInEnvelopeAsync(er, ReceiverRole.FullyAuth); //add PSPDFKit licence key ViewData["PSPDFKitLicenseKey"] = _configuration["PSPDFKitLicenseKey"]; @@ -347,7 +327,7 @@ public class HomeController : ViewControllerBase return this.ViewInnerServiceError(); } } - + [Authorize(Roles = ReceiverRole.FullyAuth)] [HttpGet("EnvelopeKey/{envelopeReceiverId}/Success")] public async Task EnvelopeSigned(string envelopeReceiverId)