mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-01-18 10:04:20 +01:00
Result classes can derive when new instance is created.
This commit is contained in:
parent
0fed89e140
commit
17f69824eb
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -1,31 +1,64 @@
|
|||
namespace DotBased;
|
||||
|
||||
/// <summary>
|
||||
/// Simple result class for returning a result state or a message and a exception.
|
||||
/// Simple result class for returning a result state or a message and an exception.
|
||||
/// </summary>
|
||||
public class Result(bool success, string message, Exception? exception)
|
||||
public class Result
|
||||
{
|
||||
public bool Success { get; set; } = success;
|
||||
public string Message { get; set; } = message;
|
||||
public Exception? Exception { get; set; } = exception;
|
||||
public Result(bool success, string message, Exception? exception)
|
||||
{
|
||||
Success = success;
|
||||
Message = message;
|
||||
Exception = exception;
|
||||
}
|
||||
|
||||
public static Result Ok() => new Result(true, string.Empty, null);
|
||||
public static Result Failed(string message, Exception? exception = null) => new Result(false, message, exception);
|
||||
public Result(Result bObj)
|
||||
{
|
||||
Success = bObj.Success;
|
||||
Message = bObj.Message;
|
||||
Exception = bObj.Exception;
|
||||
}
|
||||
|
||||
public bool Success { get; set; }
|
||||
public string Message { get; set; }
|
||||
public Exception? Exception { get; set; }
|
||||
|
||||
public static Result Ok() => new(true, string.Empty, null);
|
||||
public static Result Failed(string message, Exception? exception = null) => new(false, message, exception);
|
||||
}
|
||||
|
||||
public class Result<TValue>(bool success, string message, TValue? value, Exception? exception) : Result(success, message, exception)
|
||||
public class Result<TValue> : Result
|
||||
{
|
||||
public TValue? Value { get; set; } = value;
|
||||
public Result(bool success, string message, TValue? value, Exception? exception) : base(success, message, exception)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
public Result(Result bObj) : base(bObj)
|
||||
{
|
||||
|
||||
}
|
||||
public TValue? Value { get; set; }
|
||||
|
||||
public static Result<TValue> Ok(TValue value) => new Result<TValue>(true, string.Empty, value, null);
|
||||
public static Result<TValue> Ok(TValue value) => new(true, string.Empty, value, null);
|
||||
|
||||
public new static Result<TValue> Failed(string message, Exception? exception = null) =>
|
||||
new Result<TValue>(false, message, default, exception);
|
||||
new(false, message, default, exception);
|
||||
}
|
||||
|
||||
public class ListResult<TItem>(bool success, string message, int totalCount, IEnumerable<TItem>? items, Exception? exception) : Result(success, message, exception)
|
||||
public class ListResult<TItem> : Result
|
||||
{
|
||||
public readonly IReadOnlyList<TItem> Items = items != null ? new List<TItem>(items) : new List<TItem>();
|
||||
public ListResult(bool success, string message, int totalCount, IEnumerable<TItem>? items, Exception? exception) : base(success, message, exception)
|
||||
{
|
||||
Items = items != null ? new List<TItem>(items) : new List<TItem>();
|
||||
TotalCount = totalCount;
|
||||
}
|
||||
|
||||
public ListResult(Result bObj) : base(bObj)
|
||||
{
|
||||
Items = new List<TItem>();
|
||||
}
|
||||
|
||||
public readonly IReadOnlyList<TItem> Items;
|
||||
/// <summary>
|
||||
/// The amount of items that this result contains.
|
||||
/// </summary>
|
||||
|
@ -34,11 +67,11 @@ public class ListResult<TItem>(bool success, string message, int totalCount, IEn
|
|||
/// <summary>
|
||||
/// The total amount of item that is available.
|
||||
/// </summary>
|
||||
public int TotalCount { get; } = totalCount;
|
||||
public int TotalCount { get; }
|
||||
|
||||
public static ListResult<TItem> Ok(IEnumerable<TItem> items, int totalCount = -1) =>
|
||||
new ListResult<TItem>(true, string.Empty, totalCount, items, null);
|
||||
new(true, string.Empty, totalCount, items, null);
|
||||
|
||||
public new static ListResult<TItem> Failed(string message, Exception? exception = null) =>
|
||||
new ListResult<TItem>(false, message, -1,null, exception);
|
||||
new(false, message, -1, null, exception);
|
||||
}
|
Loading…
Reference in New Issue
Block a user