diff --git a/DotBased.ASP.Auth/AuthDataCache.cs b/DotBased.ASP.Auth/AuthDataCache.cs index c01a47b..be49f50 100644 --- a/DotBased.ASP.Auth/AuthDataCache.cs +++ b/DotBased.ASP.Auth/AuthDataCache.cs @@ -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 _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> RequestAuthStateAsync(string id) + public async Task> RequestAuthStateAsync(IAuthDataRepository dataRepository, string id) { if (_authenticationStateCollection.TryGetValue(id, out var node)) { @@ -32,7 +30,7 @@ public class AuthDataCache return Result.Ok(node.Object); } - var dbResult = await DataRepository.GetAuthenticationStateAsync(id); + var dbResult = await dataRepository.GetAuthenticationStateAsync(id); if (!dbResult.Success || dbResult.Value == null) { _authenticationStateCollection.Remove(id); diff --git a/DotBased.ASP.Auth/Domains/Auth/AuthenticationStateModel.cs b/DotBased.ASP.Auth/Domains/Auth/AuthenticationStateModel.cs index 9a163b4..78a5c79 100644 --- a/DotBased.ASP.Auth/Domains/Auth/AuthenticationStateModel.cs +++ b/DotBased.ASP.Auth/Domains/Auth/AuthenticationStateModel.cs @@ -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) { diff --git a/DotBased.ASP.Auth/Scheme/BasedAuthenticationHandler.cs b/DotBased.ASP.Auth/Scheme/BasedAuthenticationHandler.cs index 722644e..2f7678a 100644 --- a/DotBased.ASP.Auth/Scheme/BasedAuthenticationHandler.cs +++ b/DotBased.ASP.Auth/Scheme/BasedAuthenticationHandler.cs @@ -24,6 +24,7 @@ public class BasedAuthenticationHandler : AuthenticationHandler HandleAuthenticateAsync() { + /*var principal = new ClaimsPrincipal();*/ var principal = new ClaimsPrincipal(new ClaimsIdentity(new List() { new Claim(ClaimTypes.Role, "Admin"), new Claim(ClaimTypes.Name, "Anon") }, AuthenticationScheme)); var ticket = new AuthenticationTicket(principal, AuthenticationScheme); return Task.FromResult(AuthenticateResult.Success(ticket)); diff --git a/DotBased.ASP.Auth/Services/AuthService.cs b/DotBased.ASP.Auth/Services/AuthService.cs index 1bb19c1..74e8c1a 100644 --- a/DotBased.ASP.Auth/Services/AuthService.cs +++ b/DotBased.ASP.Auth/Services/AuthService.cs @@ -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> LoginAsync(LoginModel login) { - if (login.UserName.IsNullOrWhiteSpace()) - return Result.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.Failed(""); + UserModel? user = null; + Result 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.Failed("Username & Email is empty, cannot login!"); + + if (user == null || !usrResult.Success) + return new Result(usrResult); + + if (user.PasswordHash != login.Password) //TODO: Hash password and compare + return Result.Failed("Login failed, invalid password."); + var state = new AuthenticationStateModel(user); + await _authDataRepository.CreateAuthenticationStateAsync(state); + return Result.Ok(state); } public async Task Logout(string state) diff --git a/DotBased/Result.cs b/DotBased/Result.cs index cf9ba5b..49e8ba6 100755 --- a/DotBased/Result.cs +++ b/DotBased/Result.cs @@ -1,31 +1,64 @@ namespace DotBased; /// -/// 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. /// -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(bool success, string message, TValue? value, Exception? exception) : Result(success, message, exception) +public class Result : 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 Ok(TValue value) => new Result(true, string.Empty, value, null); + public static Result Ok(TValue value) => new(true, string.Empty, value, null); public new static Result Failed(string message, Exception? exception = null) => - new Result(false, message, default, exception); + new(false, message, default, exception); } -public class ListResult(bool success, string message, int totalCount, IEnumerable? items, Exception? exception) : Result(success, message, exception) +public class ListResult : Result { - public readonly IReadOnlyList Items = items != null ? new List(items) : new List(); + public ListResult(bool success, string message, int totalCount, IEnumerable? items, Exception? exception) : base(success, message, exception) + { + Items = items != null ? new List(items) : new List(); + TotalCount = totalCount; + } + + public ListResult(Result bObj) : base(bObj) + { + Items = new List(); + } + + public readonly IReadOnlyList Items; /// /// The amount of items that this result contains. /// @@ -34,11 +67,11 @@ public class ListResult(bool success, string message, int totalCount, IEn /// /// The total amount of item that is available. /// - public int TotalCount { get; } = totalCount; + public int TotalCount { get; } public static ListResult Ok(IEnumerable items, int totalCount = -1) => - new ListResult(true, string.Empty, totalCount, items, null); + new(true, string.Empty, totalCount, items, null); public new static ListResult Failed(string message, Exception? exception = null) => - new ListResult(false, message, -1,null, exception); + new(false, message, -1, null, exception); } \ No newline at end of file