Cookie-basierte automatische Autorisierung wurde konfiguriert. Middlevare für Benutzerberechtigung hinzugefügt

This commit is contained in:
Developer 02
2024-04-15 17:24:27 +02:00
parent 49cfeb28d9
commit 87c839549a
31 changed files with 1111 additions and 44 deletions

View File

@@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
namespace EnvelopeGenerator.Web.Controllers
{
public static class ControllerBaseExtensions
{
public static (string EnvelopeUuid, string ReceiverSignature)? GetAuthenticatedEnvelopeDetails(this ControllerBase controller)
{
if(controller?.User?.Identity?.IsAuthenticated ?? false)
{
var envelopeUuid = controller.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
var receiverSignature = controller.User.FindFirst(ClaimTypes.Hash)?.Value;
if (!string.IsNullOrEmpty(envelopeUuid) && !string.IsNullOrEmpty(receiverSignature))
return (EnvelopeUuid: envelopeUuid, ReceiverSignature: receiverSignature);
}
return null;
}
}
}

View File

@@ -1,12 +1,14 @@
using EnvelopeGenerator.Application.Contracts;
using EnvelopeGenerator.Application.DTOs;
using EnvelopeGenerator.Application.Services;
using EnvelopeGenerator.Common;
using EnvelopeGenerator.Web.Models;
using EnvelopeGenerator.Web.Services;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using System.Diagnostics;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
namespace EnvelopeGenerator.Web.Controllers
{
@@ -62,7 +64,7 @@ namespace EnvelopeGenerator.Web.Controllers
}
[HttpGet("/EnvelopeKey/{envelopeReceiverId}")]
public async Task<IActionResult> ShowEnvelope([FromRoute] string envelopeReceiverId)
public async Task<IActionResult> SendAccessCode([FromRoute] string envelopeReceiverId)
{
EnvelopeResponse response = await envelopeOldService.LoadEnvelope(envelopeReceiverId);
@@ -76,18 +78,20 @@ namespace EnvelopeGenerator.Web.Controllers
bool actionResult = database.Services.actionService.RequestAccessCode(response.Envelope, response.Receiver);
bool result = database.Services.emailService.SendDocumentAccessCodeReceivedEmail(response.Envelope, response.Receiver);
}
}
return Redirect($"/EnvelopeKey/{envelopeReceiverId}/Locked");
}
else
{
ViewData["EnvelopeKey"] = envelopeReceiverId;
return View();
}
return Redirect($"/EnvelopeKey/{envelopeReceiverId}/Locked");
}
[HttpGet("/EnvelopeKey/{envelopeReceiverId}/Locked")]
public IActionResult EnvelopeLocked([FromRoute] string envelopeReceiverId)
{
ViewData["EnvelopeKey"] = envelopeReceiverId;
return View();
}
[HttpPost("/EnvelopeKey/{envelopeReceiverId}/Locked")]
public async Task<IActionResult> ShowEnvelope([FromRoute] string envelopeReceiverId, [FromForm] string access_code)
public async Task<IActionResult> LogInEnvelope([FromRoute] string envelopeReceiverId, [FromForm] string access_code)
{
var decodedId = envelopeReceiverId.DecodeEnvelopeReceiverId();
@@ -99,16 +103,15 @@ namespace EnvelopeGenerator.Web.Controllers
if (verification.IsSuccess)
{
if (envelopeOldService.ReceiverAlreadySigned(response.Envelope, response.Receiver.Id) == true)
if (envelopeOldService.ReceiverAlreadySigned(response.Envelope, response.Receiver.Id))
{
return Problem(statusCode: 403);
return Redirect("/EnvelopeKey/{envelopeReceiverId}/Success");
}
var envelope = await _envelopeService.ReadByUuidAsync(uuid: decodedId.EnvelopeUuid, signature: decodedId.ReceiverSignature, withAll: true);
database.Services.actionService.EnterCorrectAccessCode(response.Envelope, response.Receiver); //for history
ViewData["EnvelopeKey"] = envelopeReceiverId;
ViewData["EnvelopeResponse"] = response;
ViewData["EnvelopeResponse"] = response;
if (response.Envelope.Documents.Count() > 0)
{
@@ -119,6 +122,22 @@ namespace EnvelopeGenerator.Web.Controllers
else
ViewData["DocumentBytes"] = null;
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, decodedId.EnvelopeUuid),
new Claim(ClaimTypes.Hash, decodedId.ReceiverSignature),
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties
{
};
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
authProperties);
return View("ShowEnvelope", envelope);
}
else
@@ -129,19 +148,21 @@ namespace EnvelopeGenerator.Web.Controllers
}
}
[HttpGet("/EnvelopeKey/{envelopeReceiverId}/Locked")]
public async Task<IActionResult> EnvelopeLocked([FromRoute] string envelopeReceiverId)
[HttpGet("/EnvelopeKey/{envelopeReceiverId}/Success")]
public async Task<IActionResult> EnvelopeSigned(string envelopeReceiverId)
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
ViewData["EnvelopeKey"] = envelopeReceiverId;
return View();
}
[HttpGet("/EnvelopeKey/{EnvelopeReceiverId}/Success")]
public IActionResult EnvelopeSigned()
[Authorize]
[HttpGet("IsAuthenticated")]
public IActionResult IsAuthenticated()
{
ViewData["EnvelopeKey"] = HttpContext.Request.RouteValues["EnvelopeReceiverId"];
return View();
var envelopeUuid = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
var receiverSignature = User.FindFirst(ClaimTypes.Hash)?.Value;
return Ok(new { EnvelopeUuid = envelopeUuid, ReceiverSignature = receiverSignature });
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
@@ -149,5 +170,9 @@ namespace EnvelopeGenerator.Web.Controllers
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
[Authorize]
[HttpGet("test")]
public string Test() => "Test";
}
}

View File

@@ -0,0 +1,42 @@
using EnvelopeGenerator.Application.Contracts;
using EnvelopeGenerator.Application.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
namespace EnvelopeGenerator.Web.Controllers.Test
{
[ApiController]
[Route("api/test/[controller]")]
public class TestAuthController : ControllerBase
{
private readonly IJWTService<string> _authService;
public TestAuthController(IJWTService<string> authService)
{
_authService = authService;
}
[HttpPost]
public IActionResult ProvideToken([FromQuery] string value)
{
var token = _authService.GenerateToken(value);
return Ok(token);
}
[HttpGet]
public IActionResult GetSecurityToken([FromQuery] string token)
{
var sToken = _authService.ReadSecurityToken(token);
return Ok(sToken);
}
[HttpGet("Username")]
[Authorize]
public IActionResult Getname()
{
var username = User.Claims?.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
return Ok(username);
}
}
}

View File

@@ -5,7 +5,6 @@ using Microsoft.AspNetCore.Mvc;
namespace EnvelopeGenerator.Web.Controllers.Test
{
//[NonController]
[ApiController]
[Route("api/test/[controller]")]
public class TestControllerBase<TOriginalController, TCRUDService, TCRUDRepository, TDto, TEntity, TId> : BasicCRUDControllerBase<TOriginalController, TCRUDService, TCRUDRepository, TDto, TEntity, TId> where TOriginalController : CRUDControllerBase<TOriginalController, TCRUDService, TCRUDRepository, TDto, TDto, TDto, TEntity, TId> where TCRUDService : ICRUDService<TCRUDRepository, TDto, TDto, TDto, TEntity, TId> where TCRUDRepository : ICRUDRepository<TEntity, TId> where TDto : class where TEntity : class