52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
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);
|