Add password verification !

This commit is contained in:
Fransolet Thomas 2022-10-06 15:14:08 +02:00
parent 9328318468
commit 35e2d8296b
3 changed files with 14 additions and 10 deletions

View File

@ -22,20 +22,25 @@ namespace Manager.Framework.Business
_logger = logger; _logger = logger;
} }
public bool Authenticate(string email, string password) public void TestPassword(string email, string userPassword, string password)
{ {
if (string.IsNullOrWhiteSpace(email)) if (string.IsNullOrWhiteSpace(email))
{ {
_logger.LogError($"Authenticate error: No e-mail provided"); _logger.LogError($"Authenticate error: No e-mail provided");
throw new UnauthorizedAccessException("Authentication error"); throw new UnauthorizedAccessException("Authentication error");
} }
if (string.IsNullOrEmpty(password)) if (string.IsNullOrEmpty(password))
{ {
_logger.LogError($"Authenticate error: No password provided"); _logger.LogError($"Authenticate error: No password provided");
throw new UnauthorizedAccessException("Authentication error"); throw new UnauthorizedAccessException("Authentication error");
} }
return true; if (!PasswordUtils.Compare(userPassword, password, PasswordsPepper))
{
_logger.LogError($"Authenticate error: passwords doesn't match");
throw new UnauthorizedAccessException("Authentication error");
}
} }
/// <summary> /// <summary>

View File

@ -53,14 +53,14 @@ namespace ManagerService.Service.Controllers
email = "test@email.be"; email = "test@email.be";
password = "W/7aj4NB60i3YFKJq50pbw=="; // password = "kljqsdkljqsd"; password = "W/7aj4NB60i3YFKJq50pbw=="; // password = "kljqsdkljqsd";
#endif #endif
var token = _tokensService.Authenticate(email.ToLower(), password);
// Set user token ? // Set user token ?
var user = _UserDatabaseService.GetByEmail(email.ToLower()); var user = _UserDatabaseService.GetByEmail(email.ToLower());
if (user == null) if (user == null)
throw new KeyNotFoundException("User not found"); throw new KeyNotFoundException("User not found");
var token = _tokensService.Authenticate(user, password);
MqttClientService.SetServices(_DeviceDatabaseService, _ConfigurationDatabaseService); MqttClientService.SetServices(_DeviceDatabaseService, _ConfigurationDatabaseService);
return new OkObjectResult(token); return new OkObjectResult(token);

View File

@ -51,17 +51,16 @@ namespace ManagerService.Service.Services
/// <param name="email">Email</param> /// <param name="email">Email</param>
/// <param name="password">Password</param> /// <param name="password">Password</param>
/// <returns>Token DTO in case of success</returns> /// <returns>Token DTO in case of success</returns>
public TokenDTO Authenticate(string email, string password) public TokenDTO Authenticate(User user, string password)
{ {
try try
{ {
var claims = new List<System.Security.Claims.Claim>(); var claims = new List<System.Security.Claims.Claim>();
var expiration = DateTime.UtcNow.AddMinutes(_tokenSettings.AccessTokenExpiration); var expiration = DateTime.UtcNow.AddMinutes(_tokenSettings.AccessTokenExpiration);
// Todo nothing good here.. _profileLogic.TestPassword(user.Email, user.Password, password);
var profile = _profileLogic.Authenticate(email, password);
claims.Add(new Claim(ClaimTypes.Email, email)); claims.Add(new Claim(ClaimTypes.Email, user.Email));
// TODO: add refresh token support // TODO: add refresh token support
@ -85,12 +84,12 @@ namespace ManagerService.Service.Services
} }
catch (UnauthorizedAccessException ex) catch (UnauthorizedAccessException ex)
{ {
_logger?.LogError(ex, $"Authenticate error for user '{email}': unauthorized access"); _logger?.LogError(ex, $"Authenticate error for user '{user.Email}': unauthorized access");
throw; throw;
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger?.LogError(ex, $"Authenticate error for user '{email}': {ex.Message}"); _logger?.LogError(ex, $"Authenticate error for user '{user.Email}': {ex.Message}");
throw; throw;
} }
} }