92 lines
4.4 KiB
C#
92 lines
4.4 KiB
C#
using Helpdesk.Api.Infrastructure;
|
||||
|
|
using Helpdesk.Application.Abstractions;
|
|||
|
|
using Helpdesk.Application.Auth;
|
|||
|
|
using Helpdesk.Application.Users;
|
|||
|
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
using Microsoft.AspNetCore.RateLimiting;
|
|||
|
|
|
|||
|
|
namespace Helpdesk.Api.Controllers;
|
|||
|
|
|
|||
|
|
/// <summary>Registrace, přihlášení a správa tokenů.</summary>
|
|||
|
|
[ApiController]
|
|||
|
|
[Route("api/auth")]
|
|||
|
|
[EnableRateLimiting(RateLimitPolicies.Auth)]
|
|||
|
|
[Produces("application/json")]
|
|||
|
|
public sealed class AuthController(AuthService auth) : ControllerBase
|
|||
|
|
{
|
|||
|
|
/// <summary>Registrace e-mailem a heslem. Role (student / vyučující) se určí podle domény e-mailu.</summary>
|
|||
|
|
/// <remarks>Po registraci přijde e-mail s odkazem pro ověření; bez ověření se nelze přihlásit.</remarks>
|
|||
|
|
[HttpPost("register")]
|
|||
|
|
[ProducesResponseType<UserDto>(StatusCodes.Status201Created)]
|
|||
|
|
[ProducesResponseType<ValidationProblemDetails>(StatusCodes.Status400BadRequest)]
|
|||
|
|
[ProducesResponseType<ProblemDetails>(StatusCodes.Status409Conflict)]
|
|||
|
|
public async Task<ActionResult<UserDto>> Register(RegisterRequest request, CancellationToken ct)
|
|||
|
|
{
|
|||
|
|
var user = await auth.RegisterAsync(request, ct);
|
|||
|
|
return Created("/api/users/me", user);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>Ověření e-mailu tokenem z registračního e-mailu (jednorázový).</summary>
|
|||
|
|
[HttpPost("confirm-email")]
|
|||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|||
|
|
[ProducesResponseType<ProblemDetails>(StatusCodes.Status400BadRequest)]
|
|||
|
|
public async Task<IActionResult> ConfirmEmail(EmailTokenRequest request, CancellationToken ct)
|
|||
|
|
{
|
|||
|
|
await auth.ConfirmEmailAsync(request, ct);
|
|||
|
|
return NoContent();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>Přihlášení – vrací access token (15 min) a refresh token (14 dní).</summary>
|
|||
|
|
[HttpPost("login")]
|
|||
|
|
[ProducesResponseType<AuthResponse>(StatusCodes.Status200OK)]
|
|||
|
|
[ProducesResponseType<ProblemDetails>(StatusCodes.Status401Unauthorized)]
|
|||
|
|
[ProducesResponseType<ProblemDetails>(StatusCodes.Status403Forbidden)]
|
|||
|
|
public Task<AuthResponse> Login(LoginRequest request, CancellationToken ct) => auth.LoginAsync(request, ct);
|
|||
|
|
|
|||
|
|
/// <summary>Výměna refresh tokenu za nový pár tokenů. Použitý refresh token se zneplatní.</summary>
|
|||
|
|
/// <remarks>Opakované použití už vyměněného refresh tokenu zneplatní všechny tokeny uživatele.</remarks>
|
|||
|
|
[HttpPost("refresh")]
|
|||
|
|
[ProducesResponseType<AuthResponse>(StatusCodes.Status200OK)]
|
|||
|
|
[ProducesResponseType<ProblemDetails>(StatusCodes.Status401Unauthorized)]
|
|||
|
|
public Task<AuthResponse> Refresh(RefreshTokenRequest request, CancellationToken ct) => auth.RefreshAsync(request, ct);
|
|||
|
|
|
|||
|
|
/// <summary>Odhlášení tohoto zařízení – zneplatní aktuální access token a předaný refresh token.</summary>
|
|||
|
|
[Authorize]
|
|||
|
|
[HttpPost("logout")]
|
|||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|||
|
|
public async Task<IActionResult> Logout(RefreshTokenRequest request, [FromServices] ICurrentUser currentUser, CancellationToken ct)
|
|||
|
|
{
|
|||
|
|
await auth.LogoutAsync(currentUser, request, ct);
|
|||
|
|
return NoContent();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>Odhlášení ze všech zařízení – zneplatní všechny tokeny uživatele.</summary>
|
|||
|
|
[Authorize]
|
|||
|
|
[HttpPost("logout-all")]
|
|||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|||
|
|
public async Task<IActionResult> LogoutAll([FromServices] ICurrentUser currentUser, CancellationToken ct)
|
|||
|
|
{
|
|||
|
|
await auth.LogoutAllAsync(currentUser, ct);
|
|||
|
|
return NoContent();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>Odešle e-mail s odkazem pro nastavení nového hesla. Odpověď je stejná, ať účet existuje, nebo ne.</summary>
|
|||
|
|
[HttpPost("forgot-password")]
|
|||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|||
|
|
public async Task<IActionResult> ForgotPassword(ForgotPasswordRequest request, CancellationToken ct)
|
|||
|
|
{
|
|||
|
|
await auth.ForgotPasswordAsync(request, ct);
|
|||
|
|
return NoContent();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>Nastaví nové heslo tokenem z e-mailu; zneplatní všechna dosavadní přihlášení.</summary>
|
|||
|
|
[HttpPost("reset-password")]
|
|||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|||
|
|
[ProducesResponseType<ProblemDetails>(StatusCodes.Status400BadRequest)]
|
|||
|
|
public async Task<IActionResult> ResetPassword(ResetPasswordRequest request, CancellationToken ct)
|
|||
|
|
{
|
|||
|
|
await auth.ResetPasswordAsync(request, ct);
|
|||
|
|
return NoContent();
|
|||
|
|
}
|
|||
|
|
}
|