Result classes can derive when new instance is created.

This commit is contained in:
max
2024-10-14 03:20:44 +02:00
parent 0fed89e140
commit 17f69824eb
5 changed files with 93 additions and 29 deletions

View File

@@ -5,20 +5,18 @@ namespace DotBased.ASP.Auth;
public class AuthDataCache
{
public AuthDataCache(IAuthDataRepository dataRepository, BasedAuthConfiguration configuration)
public AuthDataCache(BasedAuthConfiguration configuration)
{
DataRepository = dataRepository;
_configuration = configuration;
}
public readonly IAuthDataRepository DataRepository;
private readonly BasedAuthConfiguration _configuration;
private readonly CacheNodeCollection<AuthenticationStateModel> _authenticationStateCollection = [];
public Result PurgeSessionFromCache(string id) => _authenticationStateCollection.Remove(id) ? Result.Ok() : Result.Failed("Failed to purge session state from cache!");
public Result PurgeSessionFromCache(string id) => _authenticationStateCollection.Remove(id) ? Result.Ok() : Result.Failed("Failed to purge session state from cache! Or the session was not cached...");
public async Task<Result<AuthenticationStateModel>> RequestAuthStateAsync(string id)
public async Task<Result<AuthenticationStateModel>> RequestAuthStateAsync(IAuthDataRepository dataRepository, string id)
{
if (_authenticationStateCollection.TryGetValue(id, out var node))
{
@@ -32,7 +30,7 @@ public class AuthDataCache
return Result<AuthenticationStateModel>.Ok(node.Object);
}
var dbResult = await DataRepository.GetAuthenticationStateAsync(id);
var dbResult = await dataRepository.GetAuthenticationStateAsync(id);
if (!dbResult.Success || dbResult.Value == null)
{
_authenticationStateCollection.Remove(id);

View File

@@ -1,8 +1,17 @@
using DotBased.ASP.Auth.Domains.Identity;
namespace DotBased.ASP.Auth.Domains.Auth;
public class AuthenticationStateModel
{
public string Id { get; set; } = string.Empty;
public AuthenticationStateModel(UserModel user)
{
UserId = user.Id;
}
public string Id { get; set; } = Guid.NewGuid().ToString();
public string UserId { get; set; }
public DateTime CreationDate { get; set; } = DateTime.Now;
public override bool Equals(object? obj)
{

View File

@@ -24,6 +24,7 @@ public class BasedAuthenticationHandler : AuthenticationHandler<BasedAuthenticat
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
/*var principal = new ClaimsPrincipal();*/
var principal = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>() { new Claim(ClaimTypes.Role, "Admin"), new Claim(ClaimTypes.Name, "Anon") }, AuthenticationScheme));
var ticket = new AuthenticationTicket(principal, AuthenticationScheme);
return Task.FromResult(AuthenticateResult.Success(ticket));

View File

@@ -1,5 +1,6 @@
using DotBased.ASP.Auth.Domains;
using DotBased.ASP.Auth.Domains.Auth;
using DotBased.ASP.Auth.Domains.Identity;
using DotBased.Extensions;
using DotBased.Logging;
@@ -7,22 +8,44 @@ namespace DotBased.ASP.Auth.Services;
public class AuthService
{
public AuthService(AuthDataCache dataCache)
public AuthService(IAuthDataRepository authDataRepository, AuthDataCache dataCache)
{
_authDataRepository = authDataRepository;
_dataCache = dataCache;
_logger = LogService.RegisterLogger(typeof(AuthService));
}
private readonly IAuthDataRepository _authDataRepository;
private readonly AuthDataCache _dataCache;
private readonly ILogger _logger;
public async Task<Result<AuthenticationStateModel>> LoginAsync(LoginModel login)
{
if (login.UserName.IsNullOrWhiteSpace())
return Result<AuthenticationStateModel>.Failed("Username argument is empty!");
//var userResult = await _dataProvider.GetUserAsync(string.Empty, login.Email, login.UserName);
//TODO: validate user password and create a session state
return Result<AuthenticationStateModel>.Failed("");
UserModel? user = null;
Result<UserModel> usrResult;
if (!login.UserName.IsNullOrWhiteSpace())
{
usrResult = await _authDataRepository.GetUserAsync(string.Empty, string.Empty, login.UserName);
if (usrResult is { Success: true, Value: not null })
user = usrResult.Value;
}
else if (!login.Email.IsNullOrWhiteSpace())
{
usrResult = await _authDataRepository.GetUserAsync(string.Empty, login.Email, string.Empty);
if (usrResult is { Success: true, Value: not null })
user = usrResult.Value;
}
else
return Result<AuthenticationStateModel>.Failed("Username & Email is empty, cannot login!");
if (user == null || !usrResult.Success)
return new Result<AuthenticationStateModel>(usrResult);
if (user.PasswordHash != login.Password) //TODO: Hash password and compare
return Result<AuthenticationStateModel>.Failed("Login failed, invalid password.");
var state = new AuthenticationStateModel(user);
await _authDataRepository.CreateAuthenticationStateAsync(state);
return Result<AuthenticationStateModel>.Ok(state);
}
public async Task<Result> Logout(string state)