stuff
This commit is contained in:
parent
14fd359ea8
commit
a4ef2b4a20
26 changed files with 331 additions and 78 deletions
|
@ -4,23 +4,20 @@ using JetBrains.Annotations;
|
|||
|
||||
namespace Femto.Modules.Auth.Models;
|
||||
|
||||
internal class UserPassword
|
||||
internal class Password
|
||||
{
|
||||
private const int Iterations = 3;
|
||||
private const int MemorySize = 67108864;
|
||||
|
||||
public Guid Id { get; private set; }
|
||||
public byte[] Hash { get; private set; }
|
||||
|
||||
private byte[] Hash { get; set; }
|
||||
|
||||
private byte[] Salt { get; set; }
|
||||
public byte[] Salt { get; private set; }
|
||||
|
||||
[UsedImplicitly]
|
||||
private UserPassword() {}
|
||||
private Password() {}
|
||||
|
||||
public UserPassword(string password)
|
||||
public Password(string password)
|
||||
{
|
||||
this.Id = Guid.NewGuid();
|
||||
this.Salt = ComputeSalt();
|
||||
this.Hash = ComputePasswordHash(password, Salt);
|
||||
}
|
|
@ -12,7 +12,7 @@ internal class UserIdentity : Entity
|
|||
|
||||
public string Username { get; private set; }
|
||||
|
||||
public UserPassword Password { get; private set; }
|
||||
public Password? Password { get; private set; }
|
||||
|
||||
public ICollection<UserSession> Sessions { get; private set; } = [];
|
||||
|
||||
|
@ -34,7 +34,7 @@ internal class UserIdentity : Entity
|
|||
|
||||
public void SetPassword(string password)
|
||||
{
|
||||
this.Password = new UserPassword(password);
|
||||
this.Password = new Password(password);
|
||||
}
|
||||
|
||||
public bool HasPassword(string requestPassword)
|
||||
|
@ -47,6 +47,16 @@ internal class UserIdentity : Entity
|
|||
return this.Password.Check(requestPassword);
|
||||
}
|
||||
|
||||
public UserSession PossiblyRefreshSession(string sessionId)
|
||||
{
|
||||
var session = this.Sessions.Single(s => s.Id == sessionId);
|
||||
|
||||
if (session.ExpiresSoon)
|
||||
return this.StartNewSession();
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
public UserSession StartNewSession()
|
||||
{
|
||||
var session = UserSession.Create();
|
||||
|
|
|
@ -2,10 +2,13 @@ namespace Femto.Modules.Auth.Models;
|
|||
|
||||
public class UserSession
|
||||
{
|
||||
private static TimeSpan SessionTimeout = TimeSpan.FromMinutes(30);
|
||||
private static TimeSpan SessionTimeout { get; } = TimeSpan.FromMinutes(30);
|
||||
private static TimeSpan ExpiryBuffer { get; } = TimeSpan.FromMinutes(5);
|
||||
public string Id { get; private set; }
|
||||
public DateTimeOffset Expires { get; private set; }
|
||||
|
||||
public bool ExpiresSoon => Expires < DateTimeOffset.UtcNow + ExpiryBuffer;
|
||||
|
||||
private UserSession() {}
|
||||
|
||||
public static UserSession Create()
|
||||
|
@ -13,7 +16,7 @@ public class UserSession
|
|||
return new()
|
||||
{
|
||||
Id = Convert.ToBase64String(System.Security.Cryptography.RandomNumberGenerator.GetBytes(32)),
|
||||
Expires = DateTimeOffset.Now + SessionTimeout
|
||||
Expires = DateTimeOffset.UtcNow + SessionTimeout
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue