using EnvelopeGenerator.Application.Dto.EnvelopeReceiver; using EnvelopeGenerator.Web.Models; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Mvc; using System.Security.Claims; 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); public static string? GetAuthReceiverSignature(this ControllerBase controller) => controller.User.FindFirstValue(ClaimTypes.Hash); public static string? GetAuthReceiverName(this ControllerBase controller) => controller.User.FindFirstValue(ClaimTypes.Name); public static string? GetAuthReceiverMail(this ControllerBase controller) => controller.User.FindFirstValue(ClaimTypes.Email); public static string? GetAuthEnvelopeTitle(this ControllerBase controller) => controller.User.FindFirstValue(EnvelopeClaimTypes.Title); public static int? GetAuthEnvelopeId(this ControllerBase controller) { 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 //TODO: integrate to global exception handler middleware public static ViewResult ViewError(this Controller controller, ErrorViewModel errorViewModel) => controller.View("_Error", errorViewModel); public static ViewResult ViewError404(this Controller controller) => controller.ViewError(new() { Title = "404", Subtitle = "Die von Ihnen gesuchte Seite ist nicht verfügbar", Body = "Sie können derzeit nur an Sie gerichtete Briefe einsehen und unterschreiben.", }); public static ViewResult ViewEnvelopeNotFound(this Controller controller) => controller.ViewError(new() { Title = "404", Subtitle = "Document not found", Body = "Wenn Sie diese URL in Ihrer E-Mail erhalten haben, wenden Sie sich bitte an das IT-Team." }); public static ViewResult ViewDocumentNotFound(this Controller controller) => controller.ViewError(new() { Title = "404", Subtitle = "Umschlag nicht gefunden", Body = "Wenn Sie diese URL in Ihrer E-Mail erhalten haben, wenden Sie sich bitte an das IT-Team." }); public static ViewResult ViewAccessCodeNotSent(this Controller controller) => controller.ViewError(new() { Title = "500", Subtitle = "Der Zugangscode konnte nicht gesendet werden", Body = "Bitte kontaktieren Sie das IT-Team." }); public static ViewResult ViewInnerServiceError(this Controller controller) => controller.ViewError(new() { Title = "500", Subtitle = "Ein unerwarteter Fehler ist aufgetreten", Body = "Bitte kontaktieren Sie das IT-Team." }); #endregion } }