mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Consolidate auth logic, update password service, and update namespaces
This commit is contained in:
@@ -1,35 +1,53 @@
|
||||
using DataAccessLayer.Entities;
|
||||
using DataAccessLayer.Repositories.UserAccount;
|
||||
using Repository.Core.Entities;
|
||||
using Repository.Core.Repositories.Auth;
|
||||
|
||||
namespace ServiceCore.Services
|
||||
{
|
||||
public class AuthService(IUserAccountRepository userRepo, IUserCredentialRepository credRepo) : IAuthService
|
||||
public class AuthService(
|
||||
IAuthRepository authRepo,
|
||||
IPasswordService passwordService
|
||||
) : IAuthService
|
||||
{
|
||||
public async Task<UserAccount> RegisterAsync(UserAccount userAccount, string password)
|
||||
public async Task<UserAccount?> RegisterAsync(UserAccount userAccount, string password)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
// Check if user already exists
|
||||
var user = await authRepo.GetUserByUsernameAsync(userAccount.Username);
|
||||
if (user is not null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// password hashing
|
||||
var hashed = passwordService.Hash(password);
|
||||
|
||||
// Register user with hashed password
|
||||
return await authRepo.RegisterUserAsync(
|
||||
userAccount.Username,
|
||||
userAccount.FirstName,
|
||||
userAccount.LastName,
|
||||
userAccount.Email,
|
||||
userAccount.DateOfBirth,
|
||||
hashed);
|
||||
}
|
||||
|
||||
public async Task<UserAccount?> LoginAsync(string username, string password)
|
||||
{
|
||||
// Attempt lookup by username
|
||||
var user = await userRepo.GetByUsernameAsync(username);
|
||||
|
||||
var user = await authRepo.GetUserByUsernameAsync(username);
|
||||
|
||||
// the user was not found
|
||||
if (user is null) return null;
|
||||
|
||||
// @todo handle expired passwords
|
||||
var activeCred = await credRepo.GetActiveCredentialByUserAccountIdAsync(user.UserAccountId);
|
||||
|
||||
if (activeCred is null) return null;
|
||||
if (!PasswordHasher.Verify(password, activeCred.Hash)) return null;
|
||||
|
||||
return user;
|
||||
var activeCred = await authRepo.GetActiveCredentialByUserAccountIdAsync(user.UserAccountId);
|
||||
|
||||
if (activeCred is null) return null;
|
||||
return !passwordService.Verify(password, activeCred.Hash) ? null : user;
|
||||
}
|
||||
|
||||
public async Task InvalidateAsync(Guid userAccountId)
|
||||
{
|
||||
await credRepo.InvalidateCredentialsByUserAccountIdAsync(userAccountId);
|
||||
await authRepo.InvalidateCredentialsByUserAccountIdAsync(userAccountId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using DataAccessLayer.Entities;
|
||||
using Repository.Core.Entities;
|
||||
|
||||
namespace ServiceCore.Services
|
||||
{
|
||||
@@ -7,4 +7,4 @@ namespace ServiceCore.Services
|
||||
Task<UserAccount> RegisterAsync(UserAccount userAccount, string password);
|
||||
Task<UserAccount?> LoginAsync(string username, string password);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace ServiceCore.Services;
|
||||
|
||||
public interface IPasswordService
|
||||
{
|
||||
public string Hash(string password);
|
||||
public bool Verify(string password, string stored);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using DataAccessLayer.Entities;
|
||||
using Repository.Core.Entities;
|
||||
|
||||
namespace ServiceCore.Services
|
||||
{
|
||||
@@ -9,4 +9,4 @@ namespace ServiceCore.Services
|
||||
|
||||
Task UpdateAsync(UserAccount userAccount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,14 @@ using Konscious.Security.Cryptography;
|
||||
|
||||
namespace ServiceCore.Services
|
||||
{
|
||||
public static class PasswordHasher
|
||||
public class PasswordService : IPasswordService
|
||||
{
|
||||
private const int SaltSize = 16; // 128-bit
|
||||
private const int HashSize = 32; // 256-bit
|
||||
private const int ArgonIterations = 4;
|
||||
private const int ArgonMemoryKb = 65536; // 64MB
|
||||
|
||||
public static string Hash(string password)
|
||||
public string Hash(string password)
|
||||
{
|
||||
var salt = RandomNumberGenerator.GetBytes(SaltSize);
|
||||
var argon2 = new Argon2id(Encoding.UTF8.GetBytes(password))
|
||||
@@ -26,7 +26,7 @@ namespace ServiceCore.Services
|
||||
return $"{Convert.ToBase64String(salt)}:{Convert.ToBase64String(hash)}";
|
||||
}
|
||||
|
||||
public static bool Verify(string password, string stored)
|
||||
public bool Verify(string password, string stored)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -53,4 +53,4 @@ namespace ServiceCore.Services
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using DataAccessLayer.Entities;
|
||||
using DataAccessLayer.Repositories.UserAccount;
|
||||
using Repository.Core.Entities;
|
||||
using Repository.Core.Repositories.UserAccount;
|
||||
|
||||
namespace ServiceCore.Services
|
||||
{
|
||||
@@ -14,7 +14,7 @@ namespace ServiceCore.Services
|
||||
{
|
||||
return await repository.GetByIdAsync(id);
|
||||
}
|
||||
|
||||
|
||||
public async Task UpdateAsync(UserAccount userAccount)
|
||||
{
|
||||
await repository.UpdateAsync(userAccount);
|
||||
|
||||
Reference in New Issue
Block a user