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;
|
2026-08-11 21:35:52 +02:00
|
|
|
using Heimdall.Application.DTOs;
|
2026-07-22 19:43:13 +00:00
|
|
|
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.Enable2fa;
|
|
|
|
|
|
2026-07-28 14:37:52 +00:00
|
|
|
public class Enable2faHandler : IRequestHandler<Enable2faCommand, ServiceResult<Enable2faResponse>>
|
2026-07-22 19:43:13 +00:00
|
|
|
{
|
|
|
|
|
private readonly UserManager<AppUser> _userManager;
|
2026-08-11 21:35:52 +02:00
|
|
|
private readonly IApplicationDbContext _db;
|
2026-07-22 19:43:13 +00:00
|
|
|
private readonly ILogger<Enable2faHandler> _logger;
|
|
|
|
|
|
2026-08-11 21:35:52 +02:00
|
|
|
public Enable2faHandler(UserManager<AppUser> userManager, IApplicationDbContext db, ILogger<Enable2faHandler> logger)
|
2026-07-22 19:43:13 +00:00
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
2026-08-11 21:35:52 +02:00
|
|
|
_db = db;
|
2026-07-22 19:43:13 +00:00
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-28 14:37:52 +00:00
|
|
|
public async Task<ServiceResult<Enable2faResponse>> Handle(Enable2faCommand request, CancellationToken ct)
|
2026-07-22 19:43:13 +00:00
|
|
|
{
|
|
|
|
|
_logger.LogDebug("Enable2fa: UserId={UserId}", request.UserId);
|
|
|
|
|
|
2026-07-27 18:59:12 +00:00
|
|
|
var user = await _userManager.FindByIdAsync(request.UserId.ToString()).ConfigureAwait(false);
|
2026-07-22 19:43:13 +00:00
|
|
|
if (user is null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Enable2fa: User not found UserId={UserId}", request.UserId);
|
2026-07-28 14:37:52 +00:00
|
|
|
return ServiceResult<Enable2faResponse>.NotFound("User not found");
|
2026-07-22 19:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-04 10:26:07 +01:00
|
|
|
if (string.IsNullOrEmpty(request.CurrentPassword)
|
|
|
|
|
|| !await _userManager.CheckPasswordAsync(user, request.CurrentPassword).ConfigureAwait(false))
|
|
|
|
|
return ServiceResult<Enable2faResponse>.Failure("Current password is incorrect", 400);
|
|
|
|
|
|
|
|
|
|
if (await _userManager.GetTwoFactorEnabledAsync(user).ConfigureAwait(false))
|
|
|
|
|
return ServiceResult<Enable2faResponse>.Conflict("Two-factor authentication is already enabled");
|
2026-07-22 19:43:13 +00:00
|
|
|
|
2026-07-28 14:37:52 +00:00
|
|
|
// Generate authenticator key for the QR code setup (l'utilisateur doit scanner le code avec son app)
|
|
|
|
|
// Note: key is generated AFTER enabling 2FA so it's available immediately
|
2026-11-04 10:26:07 +01:00
|
|
|
var reset = await _userManager.ResetAuthenticatorKeyAsync(user).ConfigureAwait(false);
|
|
|
|
|
if (!reset.Succeeded)
|
|
|
|
|
return ServiceResult<Enable2faResponse>.Failure("Unable to initialize authenticator setup");
|
2026-07-28 14:37:52 +00:00
|
|
|
var key = await _userManager.GetAuthenticatorKeyAsync(user).ConfigureAwait(false);
|
|
|
|
|
if (string.IsNullOrEmpty(key))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Enable2fa: Failed to generate authenticator key UserId={UserId}", request.UserId);
|
|
|
|
|
return ServiceResult<Enable2faResponse>.Failure("Failed to generate 2FA key");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var qrCodeUri = $"otpauth://totp/Heimdall:{user.Email}?secret={key}&issuer=Heimdall&algorithm=SHA1&digits=6&period=30";
|
2026-11-04 10:26:07 +01:00
|
|
|
_logger.LogInformation("Authenticator setup initialized for user {UserId}; confirmation is still required", request.UserId);
|
|
|
|
|
return ServiceResult<Enable2faResponse>.Success(new Enable2faResponse(key, qrCodeUri));
|
2026-07-22 19:43:13 +00:00
|
|
|
}
|
|
|
|
|
}
|