2026-07-20 10:30:01 +00:00
|
|
|
using System.Security.Claims;
|
|
|
|
|
using Heimdall.Application.Common;
|
|
|
|
|
using Heimdall.Application.DTOs;
|
|
|
|
|
using Heimdall.Application.Services;
|
|
|
|
|
using Heimdall.Infrastructure.Security;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.RateLimiting;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Api.Controllers;
|
|
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/auth")]
|
|
|
|
|
[EnableRateLimiting("Auth")]
|
|
|
|
|
public class AuthController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IAuthService _authService;
|
|
|
|
|
private readonly IWebHostEnvironment _env;
|
|
|
|
|
|
|
|
|
|
public AuthController(IAuthService authService, IWebHostEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
_authService = authService;
|
|
|
|
|
_env = env;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Register a new user.</summary>
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpPost("register")]
|
|
|
|
|
[ProducesResponseType(typeof(AuthResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<IActionResult> Register([FromBody] RegisterRequest request, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
var result = await _authService.RegisterAsync(request, ct);
|
|
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok(result.Data)
|
|
|
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Authenticate a user.</summary>
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpPost("login")]
|
|
|
|
|
[ProducesResponseType(typeof(AuthResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
|
|
|
|
public async Task<IActionResult> Login([FromBody] LoginRequest request, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
var result = await _authService.LoginAsync(request, ct);
|
|
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok(result.Data)
|
|
|
|
|
: StatusCode(result.StatusCode ?? 401, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Verify a 2FA code.</summary>
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpPost("2fa/verify")]
|
|
|
|
|
[ProducesResponseType(typeof(AuthResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
|
|
|
|
public async Task<IActionResult> Verify2fa([FromBody] Verify2faRequest request, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
var result = await _authService.Verify2faAsync(request, ct);
|
|
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok(result.Data)
|
|
|
|
|
: StatusCode(result.StatusCode ?? 401, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Refresh an authentication token.</summary>
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpPost("refresh")]
|
|
|
|
|
[ProducesResponseType(typeof(AuthResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
|
|
|
|
public async Task<IActionResult> Refresh([FromBody] RefreshTokenRequest request, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
var result = await _authService.RefreshTokenAsync(request.RefreshToken, ct);
|
|
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok(result.Data)
|
|
|
|
|
: StatusCode(result.StatusCode ?? 401, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Change the current user's password.</summary>
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpPost("change-password")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
|
|
|
|
public async Task<IActionResult> ChangePassword(
|
|
|
|
|
[FromBody] ChangePasswordRequest request, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
var userIdClaim = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
|
|
|
if (userIdClaim is null || !Guid.TryParse(userIdClaim, out var userId))
|
2026-07-21 08:47:24 +00:00
|
|
|
return Unauthorized(new { error = "Invalid user identity" });
|
2026-07-20 10:30:01 +00:00
|
|
|
|
|
|
|
|
var result = await _authService.ChangePasswordAsync(
|
|
|
|
|
userId, request.CurrentPassword, request.NewPassword, ct);
|
|
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok()
|
|
|
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Test/Dev-only: register a user with Admin role (bypasses chicken-and-egg).</summary>
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpPost("register-admin")]
|
|
|
|
|
[ProducesResponseType(typeof(AuthResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
|
public async Task<IActionResult> RegisterAdmin([FromBody] RegisterRequest request, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
if (!_env.IsEnvironment("Testing") && !_env.IsDevelopment())
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
var result = await _authService.RegisterAdminAsync(request, ct);
|
|
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok(result.Data)
|
|
|
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Logout — revoke the current refresh token.</summary>
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpPost("logout")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
public async Task<IActionResult> Logout([FromBody] LogoutRequest request, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
var userIdClaim = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
|
|
|
if (userIdClaim is null || !Guid.TryParse(userIdClaim, out var userId))
|
2026-07-21 08:47:24 +00:00
|
|
|
return Unauthorized(new { error = "Invalid user identity" });
|
2026-07-20 10:30:01 +00:00
|
|
|
|
|
|
|
|
await _authService.LogoutAsync(userId, request.RefreshToken, ct);
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
}
|