diff --git a/src/WorkFlowCheck.API/Controllers/UserController.cs b/src/WorkFlowCheck.API/Controllers/UserController.cs index b7bb68e..b3fbe6c 100644 --- a/src/WorkFlowCheck.API/Controllers/UserController.cs +++ b/src/WorkFlowCheck.API/Controllers/UserController.cs @@ -204,6 +204,59 @@ namespace WorkFlowCheck.API.Controllers } return retVal; } + [HttpPost("Authenticate2F2")] + public async Task> Authenticate2F2([FromBody] User2FADTO user2FADTO) + { + var retVal = new ApiResponseDTO() + { + IsSuccess = true + }; + if (string.IsNullOrEmpty(user2FADTO.UserName) || + string.IsNullOrEmpty(user2FADTO.Password) || + string.IsNullOrEmpty(user2FADTO.Code) || + string.IsNullOrEmpty(user2FADTO.Token2FA)) + { + retVal.IsSuccess = false; + retVal.Errors.Add("Nem megfelelo felhasználói név vagy jelszó"); + return retVal; + } + try + { + var userDTO_Response = await _userService.Authenticate2F2(user2FADTO.UserName, + user2FADTO.Password, + user2FADTO.Code, + user2FADTO.Token2FA); + if (userDTO_Response != null) + { + if (string.IsNullOrEmpty(userDTO_Response.UserName) == false) + { + retVal.IsSuccess = true; + retVal.Data = userDTO_Response; + Log.Information($"Sikeres azonosítás! {user2FADTO.UserName}"); + } + else + { + var error = $"Nem megfelelo felhasználó vagy jelszó! {user2FADTO.UserName}"; + retVal.IsSuccess = false; + retVal.Errors.Add(error); + Log.Information(error); + } + } + else + { + var error = $"Nem megfelelo felhasználó vagy jelszó! {user2FADTO.UserName}"; + retVal.IsSuccess = false; + retVal.Errors.Add(error); + Log.Information(error); + } + } + catch (Exception ex) + { + + Log.Error(ex.Message); + } + return retVal; + } [HttpPost("AuthenticateNFC")] public async Task> AuthenticateNFC([FromBody] UserSimpleDTO userSimpleDTO) { diff --git a/src/WorkFlowCheck.BL/Services/Interfaces/IUserService.cs b/src/WorkFlowCheck.BL/Services/Interfaces/IUserService.cs index 1c76d95..707c5ce 100644 --- a/src/WorkFlowCheck.BL/Services/Interfaces/IUserService.cs +++ b/src/WorkFlowCheck.BL/Services/Interfaces/IUserService.cs @@ -15,7 +15,7 @@ namespace WorkFlowCheck.BL.Services.Interfaces Task UpdateUserAsync(UserDTO userDTO); Task Authenticate(string UserName, string Password); Task Authenticate2F1(string UserName, string Password); - Task Authenticate2F2(string UserName, string Password, string token2FA); + Task Authenticate2F2(string UserName, string Password, string Code, string token2FA); Task AuthenticateNFC(string NFCCode); diff --git a/src/WorkFlowCheck.BL/Services/UserService.cs b/src/WorkFlowCheck.BL/Services/UserService.cs index a017fda..2503cad 100644 --- a/src/WorkFlowCheck.BL/Services/UserService.cs +++ b/src/WorkFlowCheck.BL/Services/UserService.cs @@ -93,7 +93,7 @@ namespace WorkFlowCheck.BL.Services var user = await _dbContext.Users .Include(i => i.UserRoles) .ThenInclude(i => i.Role) - .Where(w => w.UserName == userName) + .Where(w => w.UserName == userName && w.Active != false) .FirstOrDefaultAsync(); if (user != null && PasswordHasher.VerifyPassword(user.PasswordHash, password)) { @@ -137,15 +137,15 @@ namespace WorkFlowCheck.BL.Services { if (!string.IsNullOrEmpty(user.Email)) { - var token2F = CodeGenerator2F.GenerateSixDigitCode(); + var code = CodeGenerator2F.GenerateSixDigitCode(); await _messageService.SendMailAsync(new List() { user.Email, }, "Bejelentkezés megerősítése", - Get2FConfirmationMessageBody(token2F)); + Get2FConfirmationMessageBody(code)); - retVal = CodeGenerator2F.GenerateToken2F(userName, password, token2F); + retVal = CodeGenerator2F.GenerateToken2F(userName, password, code); return retVal; } } @@ -156,12 +156,49 @@ namespace WorkFlowCheck.BL.Services } return retVal; } - public async Task Authenticate2F2(string UserName, string Password, string token2FA) + public async Task Authenticate2F2(string userName, string password, string code, string token2FA) { var retVal = new UserDTO() { RoleDTO = new List() }; + try + { + var user = await _dbContext.Users + .Include(i => i.UserRoles) + .ThenInclude(i => i.Role) + .Where(w => w.UserName == userName && w.Active != false) + .FirstOrDefaultAsync(); + if (user != null && PasswordHasher.VerifyPassword(user.PasswordHash, password)) + { + if (CodeGenerator2F.ValidateToken2F(token2FA, userName, password, code)) + { + var userDTO = new UserDTO() + { + Id = user.Id, + UserName = user.UserName, + RoleDTO = new List(), + }; + foreach (var item in user.UserRoles) + { + userDTO.RoleDTO.Add(new RoleDTO() + { + Id = item.Role.Id, + RoleName = item.Role.RoleName, + }); + } + user.JwtToken = GenerateJwtToken(userDTO); + await _dbContext.SaveChangesAsync(); - + retVal = _mapper.Map(user); + return retVal; + } + } + } + catch (Exception ex) + { + Log.Error(ex.Message); + } return retVal; + + } public async Task AuthenticateNFC(string NFCCode) { diff --git a/src/WorkFlowCheck.BL/Static/CodeGenerator2F.cs b/src/WorkFlowCheck.BL/Static/CodeGenerator2F.cs index 5da052b..55ede76 100644 --- a/src/WorkFlowCheck.BL/Static/CodeGenerator2F.cs +++ b/src/WorkFlowCheck.BL/Static/CodeGenerator2F.cs @@ -33,7 +33,7 @@ namespace WorkFlowCheck.BL.Static var descriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), - Expires = DateTime.UtcNow.AddSeconds(60), + Expires = DateTime.UtcNow.AddSeconds(60 * 60 * 2), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(KeyBytes), SecurityAlgorithms.HmacSha256Signature) }; diff --git a/src/WorkFlowCheck.Common/DTO/User2FADTO.cs b/src/WorkFlowCheck.Common/DTO/User2FADTO.cs index 3e6dc3e..374da91 100644 --- a/src/WorkFlowCheck.Common/DTO/User2FADTO.cs +++ b/src/WorkFlowCheck.Common/DTO/User2FADTO.cs @@ -10,6 +10,8 @@ namespace WorkFlowCheck.Common.DTO { public int Id { get; set; } public string UserName { get; set; } = null!; + public string Password { get; set; } = null!; + public string Code { get; set; } = null!; public string Token2FA { get; set; } = null!; } }