Renamed receiver roles FullyAuth → Receiver.Full and PreAuth → Receiver.TFA across the codebase for improved clarity and consistency. Updated all usages, [Authorize] attributes, role checks, authentication logic, and authorization policies to use the new role names. Marked old constants as obsolete and pointed them to the new values. This change enhances code readability and groups receiver roles under the Receiver static class.
79 lines
3.0 KiB
C#
79 lines
3.0 KiB
C#
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using EnvelopeGenerator.API.Models;
|
|
using EnvelopeGenerator.Domain.Constants;
|
|
using System.Net;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace EnvelopeGenerator.API.Controllers;
|
|
|
|
/// <summary>
|
|
/// Controller verantwortlich für die Benutzer-Authentifizierung, einschließlich Anmelden, Abmelden und Überprüfung des Authentifizierungsstatus.
|
|
/// </summary>
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public partial class AuthController(IOptions<AuthTokenKeys> authTokenKeyOptions) : ControllerBase
|
|
{
|
|
private readonly AuthTokenKeys authTokenKeys = authTokenKeyOptions.Value;
|
|
|
|
/// <summary>
|
|
/// Entfernt das Authentifizierungs-Cookie des Benutzers (AuthCookie)
|
|
/// </summary>
|
|
/// <returns>
|
|
/// Gibt eine HTTP 200 oder 401.
|
|
/// </returns>
|
|
/// <remarks>
|
|
/// Sample request:
|
|
///
|
|
/// POST /api/auth/logout
|
|
///
|
|
/// </remarks>
|
|
/// <response code="200">Erfolgreich gelöscht, wenn der Benutzer ein berechtigtes Cookie hat.</response>
|
|
/// <response code="401">Wenn es kein zugelassenes Cookie gibt, wird „nicht zugelassen“ zurückgegeben.</response>
|
|
[ProducesResponseType(typeof(string), StatusCodes.Status200OK, "text/javascript")]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
|
[Authorize(Policy = AuthPolicy.SenderOrReceiver)]
|
|
[HttpPost("logout")]
|
|
public async Task<IActionResult> Logout()
|
|
{
|
|
if (User.IsInRole(Role.Sender))
|
|
Response.Cookies.Delete(authTokenKeys.Cookie);
|
|
else if (User.IsInRole(Role.Receiver.Full))
|
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
else
|
|
return Unauthorized();
|
|
|
|
return Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="role"></param>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
|
[HttpGet("check")]
|
|
[Authorize(Policy = AuthPolicy.SenderOrReceiver)]
|
|
public IActionResult Check([FromQuery] string role) => User.IsInRole(role) ? Ok() : Unauthorized();
|
|
|
|
/// <summary>
|
|
/// Prüft, ob der Benutzer ein autorisiertes Token hat.
|
|
/// </summary>
|
|
/// <returns>Wenn ein autorisiertes Token vorhanden ist HTTP 200 asynchron 401</returns>
|
|
/// <remarks>
|
|
/// Sample request:
|
|
///
|
|
/// GET /api/auth
|
|
///
|
|
/// </remarks>
|
|
/// <response code="200">Wenn es einen autorisierten Cookie gibt.</response>
|
|
/// <response code="401">Wenn kein Cookie vorhanden ist oder nicht autorisierte.</response>
|
|
[ProducesResponseType(typeof(string), StatusCodes.Status200OK, "text/javascript")]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
|
[Authorize]
|
|
[HttpGet]
|
|
public IActionResult IsAuthenticated() => Ok();
|
|
} |