Replaced all EnvelopeGenerator.GeneratorAPI namespaces with EnvelopeGenerator.API across controllers, models, extensions, middleware, and annotation-related files. Updated using/import statements and namespace declarations accordingly. Added wwwroot folder to project file. Minor code adjustments made for consistency. This unifies API naming for improved clarity and maintainability.
144 lines
5.6 KiB
C#
144 lines
5.6 KiB
C#
using DigitalData.Core.Abstraction.Application;
|
|
using DigitalData.UserManager.Application.Contracts;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using EnvelopeGenerator.API.Models;
|
|
|
|
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 : ControllerBase
|
|
{
|
|
private readonly ILogger<AuthController> _logger;
|
|
[Obsolete("Use MediatR")]
|
|
private readonly IUserService _userService;
|
|
private readonly IDirectorySearchService _dirSearchService;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="AuthController"/> class.
|
|
/// </summary>
|
|
/// <param name="logger">The logger instance.</param>
|
|
/// <param name="userService">The user service instance.</param>
|
|
/// <param name="dirSearchService">The directory search service instance.</param>
|
|
[Obsolete("Use MediatR")]
|
|
public AuthController(ILogger<AuthController> logger, IUserService userService, IDirectorySearchService dirSearchService)
|
|
{
|
|
_logger = logger;
|
|
_userService = userService;
|
|
_dirSearchService = dirSearchService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Authentifiziert einen Benutzer und generiert ein JWT-Token. Wenn 'cookie' wahr ist, wird das Token als HTTP-Only-Cookie zurückgegeben.
|
|
/// </summary>
|
|
/// <param name="login">Benutzeranmeldedaten (Benutzername und Passwort).</param>
|
|
/// <param name="cookie">Wenn wahr, wird das JWT-Token auch als HTTP-Only-Cookie gesendet.</param>
|
|
/// <returns>
|
|
/// Gibt eine HTTP 200 oder 401.
|
|
/// </returns>
|
|
/// <remarks>
|
|
/// Sample request:
|
|
///
|
|
/// POST /api/auth?cookie=true
|
|
/// {
|
|
/// "username": "MaxMustermann",
|
|
/// "password": "Geheim123!"
|
|
/// }
|
|
///
|
|
/// POST /api/auth?cookie=true
|
|
/// {
|
|
/// "id": "1",
|
|
/// "password": "Geheim123!"
|
|
/// }
|
|
///
|
|
/// </remarks>
|
|
/// <response code="200">Erfolgreiche Anmeldung. Gibt das JWT-Token im Antwortkörper oder als Cookie zurück, wenn 'cookie' wahr ist.</response>
|
|
/// <response code="401">Unbefugt. Ungültiger Benutzername oder Passwort.</response>
|
|
[ProducesResponseType(typeof(string), StatusCodes.Status200OK, "text/javascript")]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
public Task<IActionResult> Login([FromBody] Login login, [FromQuery] bool cookie = false)
|
|
{
|
|
// added to configure open API (swagger and scalar)
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Authentifiziert einen Benutzer und generiert ein JWT-Token. Das Token wird als HTTP-only-Cookie zurückgegeben.
|
|
/// </summary>
|
|
/// <param name="login">Benutzeranmeldedaten (Benutzername und Passwort).</param>
|
|
/// <returns>
|
|
/// Gibt eine HTTP 200 oder 401.
|
|
/// </returns>
|
|
/// <remarks>
|
|
/// Sample request:
|
|
///
|
|
/// POST /api/auth/form
|
|
/// {
|
|
/// "username": "MaxMustermann",
|
|
/// "password": "Geheim123!"
|
|
/// }
|
|
///
|
|
/// </remarks>
|
|
/// <response code="200">Erfolgreiche Anmeldung. Gibt das JWT-Token im Antwortkörper oder als Cookie zurück, wenn 'cookie' wahr ist.</response>
|
|
/// <response code="401">Unbefugt. Ungültiger Benutzername oder Passwort.</response>
|
|
[ProducesResponseType(typeof(string), StatusCodes.Status200OK, "text/javascript")]
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route("form")]
|
|
public Task<IActionResult> Login([FromForm] Login login)
|
|
{
|
|
// added to configure open API (swagger and scalar)
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
/// <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]
|
|
[HttpPost("logout")]
|
|
public async Task<IActionResult> Logout()
|
|
{
|
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
return Ok();
|
|
}
|
|
|
|
/// <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();
|
|
} |