Implements authentication system

This commit is contained in:
BinaryAlley committed 2024-11-11 22:57:33 +02:00
1 parent 38829d64ff
commit a6dcf65d1c
103 files changed
+2983 -454

No files matched your search

@@ -29,6 +29,11 @@ public class UserEntity : IStorageEntity
/// </summary>
public required string Password { get; set; }
/// <summary>
/// Gets or sets the temporary hashed password used in password recovery.
/// </summary>
public string? TempPassword { get; set; }
/// <summary>
/// Gets or sets the optional Time-based One-Time Password (TOTP) secret for two-factor authentication.
/// </summary>
@@ -44,6 +49,11 @@ public class UserEntity : IStorageEntity
/// </summary>
public DateTime? VerificationTokenCreated { get; set; }
/// <summary>
/// Gets or sets the date and time when the temporary password used in password recovery was created.
/// </summary>
public DateTime? TempPasswordCreated { get; set; }
/// <summary>
/// Gets the collection of libraries owned by this user.
/// </summary>
@@ -14,7 +14,8 @@ namespace Lumina.Application.Common.DataAccess.Repositories.Users;
/// </summary>
public interface IUserRepository : IRepository<UserEntity>,
IGetAllRepositoryAction<UserEntity>,
IInsertRepositoryAction<UserEntity>
IInsertRepositoryAction<UserEntity>,
IUpdateRepositoryAction<UserEntity>
{
/// <summary>
/// Gets a username identified by <paramref name="username"/> from the repository, if it exists.
@@ -12,12 +12,20 @@ public static partial class Errors
public static class Authentication
{
public static Error UsernameAlreadyExists => Error.Conflict(description: nameof(UsernameAlreadyExists));
public static Error UsernameDoesNotExist => Error.NotFound(description: nameof(UsernameDoesNotExist));
public static Error UsernameCannotBeEmpty => Error.Validation(description: nameof(UsernameCannotBeEmpty));
public static Error TotpCannotBeEmpty => Error.Validation(description: nameof(TotpCannotBeEmpty));
public static Error PasswordCannotBeEmpty => Error.Validation(description: nameof(PasswordCannotBeEmpty));
public static Error NewPasswordCannotBeEmpty => Error.Validation(description: nameof(NewPasswordCannotBeEmpty));
public static Error CurrentPasswordCannotBeEmpty => Error.Validation(description: nameof(CurrentPasswordCannotBeEmpty));
public static Error PasswordConfirmCannotBeEmpty => Error.Validation(description: nameof(PasswordConfirmCannotBeEmpty));
public static Error NewPasswordConfirmCannotBeEmpty => Error.Validation(description: nameof(NewPasswordConfirmCannotBeEmpty));
public static Error InvalidPassword => Error.Validation(description: nameof(InvalidPassword));
public static Error PasswordsNotMatch => Error.Validation(description: nameof(PasswordsNotMatch));
public static Error InvalidTotpCode => Error.Validation(description: nameof(InvalidTotpCode));
public static Error InvalidUsernameOrPassword => Error.Failure(description: nameof(InvalidUsernameOrPassword));
public static Error InvalidCurrentPassword => Error.Failure(description: nameof(InvalidCurrentPassword));
public static Error TempPasswordExpired => Error.Failure(description: nameof(TempPasswordExpired));
public static Error PasswordResetAlreadyRequested => Error.Failure(description: nameof(PasswordResetAlreadyRequested));
}
}
@@ -0,0 +1,16 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
#endregion
namespace Lumina.Application.Common.Errors;
/// <summary>
/// Application authorization related error types.
/// </summary>
public static partial class Errors
{
public static class Authorization
{
public static Error AdminAccountAlreadyCreated => Error.Unauthorized(description: nameof(AdminAccountAlreadyCreated));
}
}
@@ -0,0 +1,15 @@
namespace Lumina.Application.Common.Infrastructure.Authentication;
/// <summary>
/// Interface for generating JWT tokens.
/// </summary>
public interface IJwtTokenGenerator
{
/// <summary>
/// Generates a new JWT token.
/// </summary>
/// <param name="id">The id of the user for which to generate the token.</param>
/// <param name="username">The username for which to generate the token.</param>
/// <returns>The generated JWT token.</returns>
string GenerateToken(string id, string username);
}
@@ -0,0 +1,15 @@
namespace Lumina.Application.Common.Infrastructure.Authentication;
/// <summary>
/// Interface for the service for generating QR codes for TOTP.
/// </summary>
public interface IQRCodeGenerator
{
/// <summary>
/// Generates a QR code Data URI for the specified TOTP secret.
/// </summary>
/// <param name="username">User's username or unique identifier.</param>
/// <param name="secret">The TOTP secret for which the QR code should be generated.</param>
/// <returns>Data URI for the QR code.</returns>
string GenerateQrCodeDataUri(string username, byte[] secret);
}
@@ -0,0 +1,13 @@
namespace Lumina.Application.Common.Infrastructure.Authentication;
/// <summary>
/// Interface for the service for generating secure tokens for accounts confirmation or recovery.
/// </summary>
public interface ITokenGenerator
{
/// <summary>
/// Generates a secure token.
/// </summary>
/// <returns>A secure token.</returns>
string GenerateToken();
}
@@ -0,0 +1,21 @@
namespace Lumina.Application.Common.Infrastructure.Authentication;
/// <summary>
/// Interface for the service for generating and validating TOTP tokens.
/// </summary>
public interface ITotpTokenGenerator
{
/// <summary>
/// Generates a new TOTP secret for a user.
/// </summary>
/// <returns>The generated TOTP secret.</returns>
byte[] GenerateSecret();
/// <summary>
/// Validates a TOTP token against the stored secret.
/// </summary>
/// <param name="secret">The user's TOTP secret.</param>
/// <param name="token">The token to validate.</param>
/// <returns><see langword="true"/> if valid, <see langword="false"/> otherwise.</returns>
bool ValidateToken(byte[] secret, string token);
}
@@ -2,7 +2,7 @@
using System;
#endregion
namespace Lumina.Application.Common.Infrastructure;
namespace Lumina.Application.Common.Infrastructure.Security;
/// <summary>
/// Interface for the service for cryptographic concerns.
@@ -1,4 +1,4 @@
namespace Lumina.Application.Common.Infrastructure;
namespace Lumina.Application.Common.Infrastructure.Security;
/// <summary>
/// Interface for the service for hashing passwords.
@@ -0,0 +1,16 @@
#region ========================================================================= USING =====================================================================================
using System;
#endregion
namespace Lumina.Application.Common.Infrastructure.Time;
/// <summary>
/// Interface for time related concerns.
/// </summary>
public interface IDateTimeProvider
{
/// <summary>
/// The current UTC time.
/// </summary>
DateTime UtcNow { get; }
}
@@ -0,0 +1,27 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.UsersManagement.Authentication.Commands.ChangePassword;
using Lumina.Contracts.Requests.Authentication;
#endregion
namespace Lumina.Application.Common.Mapping.Authentication;
/// <summary>
/// Extension methods for converting <see cref="ChangePasswordRequest"/>.
/// </summary>
public static class ChangePasswordRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="ChangePasswordCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted command.</returns>
public static ChangePasswordCommand ToCommand(this ChangePasswordRequest request)
{
return new ChangePasswordCommand(
request.Username,
request.CurrentPassword,
request.NewPassword,
request.NewPasswordConfirm
);
}
}
@@ -0,0 +1,25 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.UsersManagement.Authentication.Commands.RecoverPassword;
using Lumina.Contracts.Requests.Authentication;
#endregion
namespace Lumina.Application.Common.Mapping.Authentication;
/// <summary>
/// Extension methods for converting <see cref="RecoverPasswordRequest"/>.
/// </summary>
public static class RecoverPasswordRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="RecoverPasswordCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted command.</returns>
public static RecoverPasswordCommand ToCommand(this RecoverPasswordRequest request)
{
return new RecoverPasswordCommand(
request.Username,
request.TotpCode
);
}
}
@@ -1,6 +1,6 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Maintenance.ApplicationSetup.Commands;
using Lumina.Application.Core.UsersManagement.Register;
using Lumina.Application.Core.UsersManagement.Authentication.Commands.RegisterUser;
using Lumina.Contracts.Requests.Authentication;
#endregion
@@ -27,13 +27,13 @@ public static class RegistrationRequestMapping
}
/// <summary>
/// Converts <paramref name="request"/> to <see cref="RegisterCommand"/>.
/// Converts <paramref name="request"/> to <see cref="RegisterUserCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted command.</returns>
public static RegisterCommand ToCommand(this RegistrationRequest request)
public static RegisterUserCommand ToCommand(this RegistrationRequest request)
{
return new RegisterCommand(
return new RegisterUserCommand(
request.Username,
request.Password,
request.PasswordConfirm,