63 lines
3.0 KiB
C#
63 lines
3.0 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)
|
||
|
|
return ServiceResult<AuthResponse>.Failure(string.Join(", ", created.Errors.Select(e => e.Description)));
|
||
|
|
|
||
|
|
var role = await _users.AddToRoleAsync(user, "Admin").ConfigureAwait(false);
|
||
|
|
if (!role.Succeeded)
|
||
|
|
return ServiceResult<AuthResponse>.Failure(string.Join(", ", role.Errors.Select(e => e.Description)));
|
||
|
|
|
||
|
|
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 for {Email}", request.Email);
|
||
|
|
return await AuthHelper.CreateAuthResponseAsync(user, _jwt, _permissions, _users).ConfigureAwait(false);
|
||
|
|
}, ct).ConfigureAwait(false);
|
||
|
|
}
|
||
|
|
}
|