mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 05:11:10 +00:00
Implements account registration
This commit is contained in:
1 parent
d971b53512
commit
bd97c2e8c1
13 files changed
+736
No files matched your search
@@ -0,0 +1,21 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Contracts.Responses.Authentication;
|
||||
using Mediator;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.UsersManagement.Register;
|
||||
|
||||
/// <summary>
|
||||
/// Command for registering a new account.
|
||||
/// </summary>
|
||||
/// <param name="Username">The username for the new account.</param>
|
||||
/// <param name="Password">The password for the new account.</param>
|
||||
/// <param name="PasswordConfirm">Confirmation of the password.</param>
|
||||
/// <param name="Use2fa">Indicates whether to enable two-factor authentication for the new account.</param>
|
||||
public record RegisterCommand(
|
||||
string? Username,
|
||||
string? Password,
|
||||
string? PasswordConfirm,
|
||||
bool Use2fa
|
||||
) : IRequest<ErrorOr<RegistrationResponse>>;
|
||||
@@ -0,0 +1,88 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Users;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Security;
|
||||
using Lumina.Contracts.Responses.Authentication;
|
||||
using Mediator;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.UsersManagement.Register;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for the command to register a new user account.
|
||||
/// </summary>
|
||||
public class RegisterCommandHandler : IRequestHandler<RegisterCommand, ErrorOr<RegistrationResponse>>
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IHashService _hashService;
|
||||
private readonly ICryptographyService _cryptographyService;
|
||||
private readonly ITotpTokenGenerator _totpTokenGenerator;
|
||||
private readonly IQRCodeGenerator _qRCodeGenerator;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RegisterCommandHandler"/> class.
|
||||
/// </summary>
|
||||
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
|
||||
/// <param name="hashService">Injected service for password hashing functionality.</param>
|
||||
/// <param name="cryptographyService">Injected service for cryptographic functionality.</param>
|
||||
/// <param name="totpTokenGenerator">Injected service for generating and validating TOTP tokens.</param>
|
||||
/// <param name="qRCodeGenerator">Injected service for generating QR codes.</param>
|
||||
public RegisterCommandHandler(
|
||||
IUnitOfWork unitOfWork,
|
||||
IHashService hashService,
|
||||
ICryptographyService cryptographyService,
|
||||
ITotpTokenGenerator totpTokenGenerator,
|
||||
IQRCodeGenerator qRCodeGenerator)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_hashService = hashService;
|
||||
_cryptographyService = cryptographyService;
|
||||
_totpTokenGenerator = totpTokenGenerator;
|
||||
_qRCodeGenerator = qRCodeGenerator;
|
||||
}
|
||||
|
||||
public async ValueTask<ErrorOr<RegistrationResponse>> Handle(RegisterCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// check if any users already exists (admin account is only set once!)
|
||||
IUserRepository userRepository = _unitOfWork.GetRepository<IUserRepository>();
|
||||
ErrorOr<UserEntity?> resultSelectUser = await userRepository.GetByUsernameAsync(request.Username!, cancellationToken);
|
||||
if (resultSelectUser.IsError)
|
||||
return resultSelectUser.Errors;
|
||||
else if (resultSelectUser.Value is not null)
|
||||
return Errors.Authentication.UsernameAlreadyExists;
|
||||
string? totpSecret = null;
|
||||
UserEntity user = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Username = request.Username!,
|
||||
Password = Uri.EscapeDataString(_hashService.HashString(request.Password!)),
|
||||
Created = DateTime.UtcNow,
|
||||
Libraries = []
|
||||
};
|
||||
// if the user enabled two factor auth, include a QR with the totp secret
|
||||
if (request.Use2fa)
|
||||
{
|
||||
// generate a TOTP secret
|
||||
byte[] secret = _totpTokenGenerator.GenerateSecret();
|
||||
// convert the secret into a QR code for the user to scan
|
||||
totpSecret = _qRCodeGenerator.GenerateQrCodeDataUri(request.Username!, secret);
|
||||
// store the TOTP secret in the repository, encrypted
|
||||
user.TotpSecret = _cryptographyService.Encrypt(Convert.ToBase64String(secret));
|
||||
}
|
||||
// insert the user
|
||||
ErrorOr<Created> resultInsertUser = await userRepository.InsertAsync(user, cancellationToken);
|
||||
if (resultInsertUser.IsError)
|
||||
return resultInsertUser.Errors;
|
||||
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
// TODO: insert the default admin profile preferences when they are implemented
|
||||
// if 2FA was enabled, the TOTP secret needs to be delivered to the client unhashed, so it can be displayed
|
||||
return new RegistrationResponse(user.Id, user.Username, totpSecret);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentValidation;
|
||||
using Lumina.Application.Common.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.UsersManagement.Register;
|
||||
|
||||
/// <summary>
|
||||
/// Validates the needed validation rules for <see cref="RegisterCommand"/>.
|
||||
/// </summary>
|
||||
public class RegisterCommandValidator : AbstractValidator<RegisterCommand>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RegisterCommandValidator"/> class.
|
||||
/// </summary>
|
||||
public RegisterCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.Username).NotEmpty().WithMessage(Errors.Authentication.UsernameCannotBeEmpty.Description);
|
||||
RuleFor(x => x.Password).NotEmpty().WithMessage(Errors.Authentication.PasswordCannotBeEmpty.Description)
|
||||
.Matches("^(?=.*[A-Z])(?=.*[0-9])(?=.*[^a-zA-Z0-9]).{8,}$").WithMessage(Errors.Authentication.InvalidPassword.Description);
|
||||
RuleFor(x => x.PasswordConfirm).NotEmpty().WithMessage(Errors.Authentication.PasswordConfirmCannotBeEmpty.Description);
|
||||
RuleFor(x => x.Password).Equal(x => x.PasswordConfirm).WithMessage(Errors.Authentication.PasswordsNotMatch.Description);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user