move extension extensions dir
This commit is contained in:
parent
7a011930df
commit
51b96e2a81
@ -1,119 +0,0 @@
|
|||||||
using EnvelopeGenerator.Application.Dto.EnvelopeReceiver;
|
|
||||||
using EnvelopeGenerator.Web.Models;
|
|
||||||
using Microsoft.AspNetCore.Authentication;
|
|
||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
||||||
using Microsoft.AspNetCore.Localization;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using System.Security.Claims;
|
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Web.Controllers
|
|
||||||
{
|
|
||||||
public static class ControllerBaseExtensions
|
|
||||||
{
|
|
||||||
#region Auth
|
|
||||||
public static string? GetClaimValue(this ClaimsPrincipal user, string claimType) => user.FindFirstValue(claimType);
|
|
||||||
|
|
||||||
public static string? GetAuthEnvelopeUuid(this ClaimsPrincipal user) => user.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
||||||
|
|
||||||
public static string? GetAuthReceiverSignature(this ClaimsPrincipal user) => user.FindFirstValue(ClaimTypes.Hash);
|
|
||||||
|
|
||||||
public static string? GetAuthReceiverName(this ClaimsPrincipal user) => user.FindFirstValue(ClaimTypes.Name);
|
|
||||||
|
|
||||||
public static string? GetAuthReceiverMail(this ClaimsPrincipal user) => user.FindFirstValue(ClaimTypes.Email);
|
|
||||||
|
|
||||||
public static string? GetAuthEnvelopeTitle(this ClaimsPrincipal user) => user.FindFirstValue(EnvelopeClaimTypes.Title);
|
|
||||||
|
|
||||||
public static int? GetAuthEnvelopeId(this ClaimsPrincipal user)
|
|
||||||
{
|
|
||||||
var env_id_str = 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<Claim> {
|
|
||||||
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 Cookie
|
|
||||||
public static string? GetCulture(this IRequestCookieCollection cookies)
|
|
||||||
=> cookies[CookieRequestCultureProvider.DefaultCookieName];
|
|
||||||
|
|
||||||
public static void SetCulture(this IResponseCookies cookies, string culture)
|
|
||||||
{
|
|
||||||
var cookieOptions = new CookieOptions
|
|
||||||
{
|
|
||||||
Secure = false,
|
|
||||||
SameSite = SameSiteMode.Strict,
|
|
||||||
HttpOnly = true
|
|
||||||
};
|
|
||||||
cookies.Append(
|
|
||||||
CookieRequestCultureProvider.DefaultCookieName,
|
|
||||||
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
|
|
||||||
cookieOptions);
|
|
||||||
}
|
|
||||||
#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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -9,6 +9,7 @@ using static EnvelopeGenerator.Domain.Constants;
|
|||||||
using EnvelopeGenerator.Domain.Entities;
|
using EnvelopeGenerator.Domain.Entities;
|
||||||
using EnvelopeGenerator.Domain;
|
using EnvelopeGenerator.Domain;
|
||||||
using DigitalData.Core.Abstraction.Application.DTO;
|
using DigitalData.Core.Abstraction.Application.DTO;
|
||||||
|
using EnvelopeGenerator.Web.Extensions;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Web.Controllers;
|
namespace EnvelopeGenerator.Web.Controllers;
|
||||||
|
|
||||||
|
|||||||
@ -7,6 +7,7 @@ using EnvelopeGenerator.Application.EnvelopeReceivers.Queries;
|
|||||||
using EnvelopeGenerator.Application.Interfaces.Services;
|
using EnvelopeGenerator.Application.Interfaces.Services;
|
||||||
using EnvelopeGenerator.Application.Resources;
|
using EnvelopeGenerator.Application.Resources;
|
||||||
using EnvelopeGenerator.Extensions;
|
using EnvelopeGenerator.Extensions;
|
||||||
|
using EnvelopeGenerator.Web.Extensions;
|
||||||
using EnvelopeGenerator.Web.Models;
|
using EnvelopeGenerator.Web.Models;
|
||||||
using Ganss.Xss;
|
using Ganss.Xss;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
|
|||||||
@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Authorization;
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using static EnvelopeGenerator.Domain.Constants;
|
using static EnvelopeGenerator.Domain.Constants;
|
||||||
|
using EnvelopeGenerator.Web.Extensions;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Web.Controllers
|
namespace EnvelopeGenerator.Web.Controllers
|
||||||
{
|
{
|
||||||
|
|||||||
@ -12,6 +12,7 @@ using Microsoft.AspNetCore.Authentication;
|
|||||||
using EnvelopeGenerator.Application.Interfaces.Services;
|
using EnvelopeGenerator.Application.Interfaces.Services;
|
||||||
using DigitalData.Core.Abstraction.Application.DTO;
|
using DigitalData.Core.Abstraction.Application.DTO;
|
||||||
using static EnvelopeGenerator.Domain.Constants;
|
using static EnvelopeGenerator.Domain.Constants;
|
||||||
|
using EnvelopeGenerator.Web.Extensions;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Web.Controllers;
|
namespace EnvelopeGenerator.Web.Controllers;
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
namespace EnvelopeGenerator.Web;
|
namespace EnvelopeGenerator.Web.Extensions;
|
||||||
|
|
||||||
public static class StringExtensions
|
public static class StringExtensions
|
||||||
{
|
{
|
||||||
118
EnvelopeGenerator.Web/Extensions/WebExtensions.cs
Normal file
118
EnvelopeGenerator.Web/Extensions/WebExtensions.cs
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
using EnvelopeGenerator.Application.Dto.EnvelopeReceiver;
|
||||||
|
using EnvelopeGenerator.Web.Models;
|
||||||
|
using Microsoft.AspNetCore.Authentication;
|
||||||
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||||
|
using Microsoft.AspNetCore.Localization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Security.Claims;
|
||||||
|
|
||||||
|
namespace EnvelopeGenerator.Web.Extensions;
|
||||||
|
|
||||||
|
public static class WebExtensions
|
||||||
|
{
|
||||||
|
#region Auth
|
||||||
|
public static string? GetClaimValue(this ClaimsPrincipal user, string claimType) => user.FindFirstValue(claimType);
|
||||||
|
|
||||||
|
public static string? GetAuthEnvelopeUuid(this ClaimsPrincipal user) => user.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||||
|
|
||||||
|
public static string? GetAuthReceiverSignature(this ClaimsPrincipal user) => user.FindFirstValue(ClaimTypes.Hash);
|
||||||
|
|
||||||
|
public static string? GetAuthReceiverName(this ClaimsPrincipal user) => user.FindFirstValue(ClaimTypes.Name);
|
||||||
|
|
||||||
|
public static string? GetAuthReceiverMail(this ClaimsPrincipal user) => user.FindFirstValue(ClaimTypes.Email);
|
||||||
|
|
||||||
|
public static string? GetAuthEnvelopeTitle(this ClaimsPrincipal user) => user.FindFirstValue(EnvelopeClaimTypes.Title);
|
||||||
|
|
||||||
|
public static int? GetAuthEnvelopeId(this ClaimsPrincipal user)
|
||||||
|
{
|
||||||
|
var env_id_str = 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<Claim> {
|
||||||
|
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 Cookie
|
||||||
|
public static string? GetCulture(this IRequestCookieCollection cookies)
|
||||||
|
=> cookies[CookieRequestCultureProvider.DefaultCookieName];
|
||||||
|
|
||||||
|
public static void SetCulture(this IResponseCookies cookies, string culture)
|
||||||
|
{
|
||||||
|
var cookieOptions = new CookieOptions
|
||||||
|
{
|
||||||
|
Secure = false,
|
||||||
|
SameSite = SameSiteMode.Strict,
|
||||||
|
HttpOnly = true
|
||||||
|
};
|
||||||
|
cookies.Append(
|
||||||
|
CookieRequestCultureProvider.DefaultCookieName,
|
||||||
|
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
|
||||||
|
cookieOptions);
|
||||||
|
}
|
||||||
|
#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
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
using EnvelopeGenerator.Web.Controllers;
|
using EnvelopeGenerator.Web.Extensions;
|
||||||
using EnvelopeGenerator.Web.Models;
|
using EnvelopeGenerator.Web.Models;
|
||||||
using Microsoft.AspNetCore.Localization;
|
using Microsoft.AspNetCore.Localization;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
@using EnvelopeGenerator.Application.Dto.EnvelopeReceiver;
|
@using EnvelopeGenerator.Application.Dto.EnvelopeReceiver;
|
||||||
@using Newtonsoft.Json
|
@using Newtonsoft.Json
|
||||||
|
@using EnvelopeGenerator.Web.Extensions;
|
||||||
@model Auth;
|
@model Auth;
|
||||||
@{
|
@{
|
||||||
//TODO: Create view model
|
//TODO: Create view model
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
}
|
}
|
||||||
@using DigitalData.Core.Abstraction.Application.DTO;
|
@using DigitalData.Core.Abstraction.Application.DTO;
|
||||||
@using EnvelopeGenerator.Application.Dto.EnvelopeReceiver
|
@using EnvelopeGenerator.Application.Dto.EnvelopeReceiver
|
||||||
|
@using EnvelopeGenerator.Web.Extensions
|
||||||
@using Newtonsoft.Json
|
@using Newtonsoft.Json
|
||||||
@using Newtonsoft.Json.Serialization
|
@using Newtonsoft.Json.Serialization
|
||||||
@model EnvelopeReceiverDto;
|
@model EnvelopeReceiverDto;
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
@using EnvelopeGenerator.Application.Dto;
|
@using EnvelopeGenerator.Application.Dto;
|
||||||
@using EnvelopeGenerator.Application.Dto.EnvelopeReceiver
|
@using EnvelopeGenerator.Application.Dto.EnvelopeReceiver
|
||||||
@using EnvelopeGenerator.Application.Dto.EnvelopeReceiverReadOnly
|
@using EnvelopeGenerator.Application.Dto.EnvelopeReceiverReadOnly
|
||||||
|
@using EnvelopeGenerator.Web.Extensions
|
||||||
@using Newtonsoft.Json
|
@using Newtonsoft.Json
|
||||||
@using Newtonsoft.Json.Serialization
|
@using Newtonsoft.Json.Serialization
|
||||||
@model EnvelopeReceiverDto;
|
@model EnvelopeReceiverDto;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user