diff --git a/EnvelopeGenerator.API/Controllers/AuthController.cs b/EnvelopeGenerator.API/Controllers/AuthController.cs index b8c89cd0..cec9b1bd 100644 --- a/EnvelopeGenerator.API/Controllers/AuthController.cs +++ b/EnvelopeGenerator.API/Controllers/AuthController.cs @@ -1,10 +1,10 @@ -using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Authorization; +using EnvelopeGenerator.API.Extensions; using EnvelopeGenerator.API.Models; using EnvelopeGenerator.Domain.Constants; -using System.Net; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; namespace EnvelopeGenerator.API.Controllers; @@ -14,7 +14,7 @@ namespace EnvelopeGenerator.API.Controllers; /// [Route("api/[controller]")] [ApiController] -public partial class AuthController(IOptions authTokenKeyOptions) : ControllerBase +public partial class AuthController(IOptions authTokenKeyOptions, IAuthorizationService authService) : ControllerBase { private readonly AuthTokenKeys authTokenKeys = authTokenKeyOptions.Value; @@ -38,9 +38,9 @@ public partial class AuthController(IOptions authTokenKeyOptions) [HttpPost("logout")] public async Task Logout() { - if (User.IsInRole(Role.Sender)) + if (await authService.AuthorizePolicyAsync(User, AuthPolicy.Sender)) Response.Cookies.Delete(authTokenKeys.Cookie); - else if (User.IsInRole(Role.Receiver.Full)) + else if (await authService.AuthorizePolicyAsync(User, AuthPolicy.ReceiverOrReceiverTFA)) await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); else return Unauthorized(); @@ -48,17 +48,6 @@ public partial class AuthController(IOptions authTokenKeyOptions) return Ok(); } - /// - /// - /// - /// - /// - [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(); - /// /// Prüft, ob der Benutzer ein autorisiertes Token hat. /// @@ -71,9 +60,12 @@ public partial class AuthController(IOptions authTokenKeyOptions) /// /// Wenn es einen autorisierten Cookie gibt. /// Wenn kein Cookie vorhanden ist oder nicht autorisierte. - [ProducesResponseType(typeof(string), StatusCodes.Status200OK, "text/javascript")] + [ProducesResponseType(typeof(void), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] + [HttpGet("check")] [Authorize] - [HttpGet] - public IActionResult IsAuthenticated() => Ok(); + public IActionResult Check(string? role = null) + => role is not null && !User.IsInRole(role) + ? Unauthorized() + : Ok(); } \ No newline at end of file