Files
Heimdall/src/Heimdall.Application/Auth/Commands/BootstrapAdmin/BootstrapAdminHandler.cs
T
arnaud.dartois 2bf3dbc95b
Heimdall Gitea / Detect CI scope (pull_request) Successful in 5s
Heimdall Gitea / Build and tests (pull_request) Successful in 20m8s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Failing after 6m10s
Heimdall Gitea / Build and scan server image (pull_request) Failing after 8m2s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Failing after 2s
fix(identity): harden user lifecycle and permissions
2026-11-04 10:26:07 +01:00

71 lines
3.3 KiB
C#

using Heimdall.Application.Common;
using Heimdall.Application.DTOs;
using Heimdall.Application.Services;
using Heimdall.Domain.Entities;
using MediatR;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
namespace Heimdall.Application.Auth.Commands.BootstrapAdmin;
public sealed class BootstrapAdminHandler : IRequestHandler<BootstrapAdminCommand, ServiceResult<AuthResponse>>
{
private readonly IAuthDataService _authData;
private readonly UserManager<AppUser> _users;
private readonly IJwtService _jwt;
private readonly IRolePermissionService _permissions;
private readonly IPasswordBreachChecker _breachChecker;
private readonly ILogger<BootstrapAdminHandler> _logger;
public BootstrapAdminHandler(IAuthDataService authData, UserManager<AppUser> users, IJwtService jwt,
IRolePermissionService permissions, IPasswordBreachChecker breachChecker,
ILogger<BootstrapAdminHandler> logger)
{
_authData = authData;
_users = users;
_jwt = jwt;
_permissions = permissions;
_breachChecker = breachChecker;
_logger = logger;
}
public async Task<ServiceResult<AuthResponse>> Handle(BootstrapAdminCommand request, CancellationToken ct)
{
if (await _breachChecker.IsPasswordBreachedAsync(request.Password, ct).ConfigureAwait(false))
return ServiceResult<AuthResponse>.Failure("This password has appeared in a data breach. Please choose a different password.");
return await _authData.RunFirstAdminBootstrapTransactionAsync(async token =>
{
if (await _authData.HasAnyUsersAsync(token).ConfigureAwait(false))
return ServiceResult<AuthResponse>.Conflict("Initial administrator setup has already been completed.");
var user = new AppUser
{
UserName = request.Email,
Email = request.Email,
DisplayName = request.DisplayName,
EmailConfirmed = true
};
var created = await _users.CreateAsync(user, request.Password).ConfigureAwait(false);
if (!created.Succeeded)
{
_logger.LogWarning("Identity rejected initial administrator creation with codes {Codes}", string.Join(',', created.Errors.Select(e => e.Code)));
return ServiceResult<AuthResponse>.Failure("Initial administrator creation failed");
}
var role = await _users.AddToRoleAsync(user, "Admin").ConfigureAwait(false);
if (!role.Succeeded)
{
await _users.DeleteAsync(user).ConfigureAwait(false);
_logger.LogWarning("Identity rejected initial administrator role assignment with codes {Codes}", string.Join(',', role.Errors.Select(e => e.Code)));
return ServiceResult<AuthResponse>.Failure("Initial administrator creation failed");
}
await _authData.SetSettingValueAsync("auth.public-registration.enabled", "false", "auth",
"Public registration is disabled; accounts are created by administrators.", token).ConfigureAwait(false);
_logger.LogInformation("Initial administrator account created");
return await AuthHelper.CreateAuthResponseAsync(user, _jwt, _permissions, _users, ct: token).ConfigureAwait(false);
}, ct).ConfigureAwait(false);
}
}