hopefully not a horribly foolish refactoring

This commit is contained in:
john 2025-05-11 23:26:09 +02:00
parent 59d660165f
commit 1ecaf64dea
82 changed files with 782 additions and 398 deletions

View file

@ -0,0 +1,5 @@
using Femto.Common.Domain;
namespace Femto.Modules.Authentication.Models.Events;
internal record UserWasCreatedEvent(UserIdentity User) : DomainEvent;

View file

@ -0,0 +1,52 @@
using System.Text;
using Femto.Common.Domain;
using Femto.Modules.Authentication.Contracts;
using Femto.Modules.Authentication.Models.Events;
using Geralt;
namespace Femto.Modules.Authentication.Models;
internal class UserIdentity : Entity
{
public Guid Id { get; private set; }
public string Username { get; private set; }
public UserPassword Password { get; private set; }
private UserIdentity()
{
}
public UserIdentity(string username)
{
this.Id = Guid.CreateVersion7();
this.Username = username;
this.AddDomainEvent(new UserWasCreatedEvent(this));
}
public UserIdentity WithPassword(string password)
{
this.SetPassword(password);
return this;
}
public void SetPassword(string password)
{
var hash = new byte[128];
try
{
Argon2id.ComputeHash(hash, Encoding.UTF8.GetBytes(password), 3, 67108864);
}
catch (Exception e)
{
throw new SetPasswordError("Failed to hash password", e);
}
this.Password = new UserPassword(this.Id, hash, new byte[128]);
}
}
public class SetPasswordError(string message, Exception inner) : DomainException(message, inner);

View file

@ -0,0 +1,19 @@
namespace Femto.Modules.Authentication.Models;
internal class UserPassword
{
public Guid Id { get; private set; }
public byte[] Hash { get; private set; }
public byte[] Salt { get; private set; }
private UserPassword() {}
public UserPassword(Guid id, byte[] hash, byte[] salt)
{
Id = id;
Hash = hash;
Salt = salt;
}
}