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

@@ -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";
}
}