2026-08-11 21:35:52 +02:00
|
|
|
using Heimdall.Application.Auth.Services;
|
2026-07-22 19:43:13 +00:00
|
|
|
using Heimdall.Application.Common;
|
|
|
|
|
using Heimdall.Application.DTOs;
|
|
|
|
|
using Heimdall.Application.Services;
|
|
|
|
|
using Heimdall.Domain.Entities;
|
|
|
|
|
using MediatR;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2026-08-11 21:35:52 +02:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2026-07-22 19:43:13 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Application.Auth.Commands.Verify2fa;
|
|
|
|
|
|
|
|
|
|
public class Verify2faHandler : IRequestHandler<Verify2faCommand, ServiceResult<AuthResponse>>
|
|
|
|
|
{
|
|
|
|
|
private readonly UserManager<AppUser> _userManager;
|
|
|
|
|
private readonly IJwtService _jwtService;
|
|
|
|
|
private readonly IRolePermissionService _rolePermissionService;
|
2026-08-11 21:35:52 +02:00
|
|
|
private readonly IApplicationDbContext _db;
|
2026-07-22 19:43:13 +00:00
|
|
|
private readonly ILogger<Verify2faHandler> _logger;
|
|
|
|
|
|
|
|
|
|
public Verify2faHandler(
|
|
|
|
|
UserManager<AppUser> userManager,
|
|
|
|
|
IJwtService jwtService,
|
|
|
|
|
IRolePermissionService rolePermissionService,
|
2026-08-11 21:35:52 +02:00
|
|
|
IApplicationDbContext db,
|
2026-07-22 19:43:13 +00:00
|
|
|
ILogger<Verify2faHandler> logger)
|
|
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_jwtService = jwtService;
|
|
|
|
|
_rolePermissionService = rolePermissionService;
|
2026-08-11 21:35:52 +02:00
|
|
|
_db = db;
|
2026-07-22 19:43:13 +00:00
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<ServiceResult<AuthResponse>> Handle(Verify2faCommand request, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogDebug("Verify2fa: Email={Email}", request.Email);
|
|
|
|
|
|
2026-07-27 18:59:12 +00:00
|
|
|
var user = await _userManager.FindByEmailAsync(request.Email).ConfigureAwait(false);
|
2026-07-22 19:43:13 +00:00
|
|
|
if (user is null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Verify2fa: User not found Email={Email}", request.Email);
|
|
|
|
|
return ServiceResult<AuthResponse>.Unauthorized("Invalid request");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var isValid = await _userManager.VerifyTwoFactorTokenAsync(
|
|
|
|
|
user, TokenOptions.DefaultAuthenticatorProvider, request.Code);
|
|
|
|
|
|
|
|
|
|
if (!isValid)
|
|
|
|
|
{
|
2026-08-11 21:35:52 +02:00
|
|
|
// TOTP invalide → tenter un code de récupération (usage unique)
|
|
|
|
|
var redeemed = await TryRedeemRecoveryCodeAsync(user, request.Code.Trim(), ct).ConfigureAwait(false);
|
|
|
|
|
if (!redeemed)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Verify2fa: Invalid code Email={Email}", request.Email);
|
|
|
|
|
return ServiceResult<AuthResponse>.Unauthorized("Invalid 2FA code");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Verify2fa: Recovery code redeemed Email={Email}", request.Email);
|
2026-07-22 19:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Verify2fa: Success Email={Email}", request.Email);
|
2026-07-27 18:59:12 +00:00
|
|
|
return await AuthHelper.CreateAuthResponseAsync(user, _jwtService, _rolePermissionService, _userManager).ConfigureAwait(false);
|
2026-07-22 19:43:13 +00:00
|
|
|
}
|
2026-08-11 21:35:52 +02:00
|
|
|
|
|
|
|
|
private async Task<bool> TryRedeemRecoveryCodeAsync(AppUser user, string code, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(code))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var codeHash = RecoveryCodeService.Hash(code);
|
|
|
|
|
var match = await _db.UserRecoveryCodes
|
|
|
|
|
.FirstOrDefaultAsync(r => r.UserId == user.Id && r.CodeHash == codeHash && r.UsedAt == null, ct)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (match is null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
match.MarkUsed();
|
|
|
|
|
await _db.SaveChangesAsync(ct).ConfigureAwait(false);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-07-22 19:43:13 +00:00
|
|
|
}
|