2026-07-20 10:30:01 +00:00
|
|
|
using System.Security.Claims;
|
2026-07-22 19:54:55 +00:00
|
|
|
using Heimdall.Application.Auth.Commands.Register;
|
|
|
|
|
using Heimdall.Application.Auth.Commands.Login;
|
|
|
|
|
using Heimdall.Application.Auth.Commands.Verify2fa;
|
|
|
|
|
using Heimdall.Application.Auth.Commands.RefreshToken;
|
|
|
|
|
using Heimdall.Application.Auth.Commands.ChangePassword;
|
|
|
|
|
using Heimdall.Application.Auth.Commands.RegisterAdmin;
|
|
|
|
|
using Heimdall.Application.Auth.Commands.Enable2fa;
|
|
|
|
|
using Heimdall.Application.Auth.Commands.Disable2fa;
|
|
|
|
|
using Heimdall.Application.Auth.Commands.Logout;
|
2026-07-20 10:30:01 +00:00
|
|
|
using Heimdall.Application.Common;
|
|
|
|
|
using Heimdall.Application.DTOs;
|
|
|
|
|
using Heimdall.Infrastructure.Security;
|
2026-07-22 19:54:55 +00:00
|
|
|
using MediatR;
|
2026-07-20 10:30:01 +00:00
|
|
|
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
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
private readonly IMediator _mediator;
|
2026-07-20 10:30:01 +00:00
|
|
|
private readonly IWebHostEnvironment _env;
|
|
|
|
|
|
2026-07-22 19:54:55 +00:00
|
|
|
public AuthController(IMediator mediator, IWebHostEnvironment env)
|
2026-07-20 10:30:01 +00:00
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
_mediator = mediator;
|
2026-07-20 10:30:01 +00:00
|
|
|
_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)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var command = new RegisterCommand(request.Email, request.Password, request.DisplayName);
|
|
|
|
|
var result = await _mediator.Send(command, ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
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)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var command = new LoginCommand(request.Email, request.Password);
|
|
|
|
|
var result = await _mediator.Send(command, ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
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)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var command = new Verify2faCommand(request.Email, request.Code);
|
|
|
|
|
var result = await _mediator.Send(command, ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
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)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new RefreshTokenCommand(request.RefreshToken), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
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
|
|
|
|
2026-07-22 19:54:55 +00:00
|
|
|
var command = new ChangePasswordCommand(userId, request.CurrentPassword, request.NewPassword);
|
|
|
|
|
var result = await _mediator.Send(command, ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
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();
|
|
|
|
|
|
2026-07-22 19:54:55 +00:00
|
|
|
var command = new RegisterAdminCommand(request.Email, request.Password, request.DisplayName);
|
|
|
|
|
var result = await _mediator.Send(command, ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok(result.Data)
|
|
|
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 08:57:53 +00:00
|
|
|
/// <summary>Enable 2FA for the current user.</summary>
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpPost("2fa/enable")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<IActionResult> Enable2fa(CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
var userIdClaim = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
|
|
|
if (userIdClaim is null || !Guid.TryParse(userIdClaim, out var userId))
|
|
|
|
|
return Unauthorized(new { error = "Invalid user identity" });
|
|
|
|
|
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new Enable2faCommand(userId), ct);
|
2026-07-21 08:57:53 +00:00
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok()
|
|
|
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Disable 2FA for the current user.</summary>
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpPost("2fa/disable")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<IActionResult> Disable2fa(CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
var userIdClaim = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
|
|
|
if (userIdClaim is null || !Guid.TryParse(userIdClaim, out var userId))
|
|
|
|
|
return Unauthorized(new { error = "Invalid user identity" });
|
|
|
|
|
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new Disable2faCommand(userId), ct);
|
2026-07-21 08:57:53 +00:00
|
|
|
return result.IsSuccess
|
|
|
|
|
? Ok()
|
|
|
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 10:30:01 +00:00
|
|
|
/// <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
|
|
|
|
2026-07-22 19:54:55 +00:00
|
|
|
await _mediator.Send(new LogoutCommand(userId, request.RefreshToken), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
}
|