From ba55b692b4063f90777446f49092c5ea53790522 Mon Sep 17 00:00:00 2001 From: mfnefd Date: Thu, 6 Jun 2024 17:50:42 +0400 Subject: [PATCH] fix user logic. password hasher is now on bcrypt --- BusinessLogic/BusinessLogic.csproj | 5 +++++ BusinessLogic/BusinessLogic/UserLogic.cs | 14 ++++++++------ BusinessLogic/Tools/PasswordHasher.cs | 9 ++------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/BusinessLogic/BusinessLogic.csproj b/BusinessLogic/BusinessLogic.csproj index 8e23e43..c105ec7 100644 --- a/BusinessLogic/BusinessLogic.csproj +++ b/BusinessLogic/BusinessLogic.csproj @@ -6,7 +6,12 @@ enable + + + + + diff --git a/BusinessLogic/BusinessLogic/UserLogic.cs b/BusinessLogic/BusinessLogic/UserLogic.cs index 73f8156..b028660 100644 --- a/BusinessLogic/BusinessLogic/UserLogic.cs +++ b/BusinessLogic/BusinessLogic/UserLogic.cs @@ -102,19 +102,21 @@ namespace BusinessLogic.BusinessLogic return UserConverter.ToView(user); } - public UserViewModel Login(UserBindingModel model) + public UserViewModel Login(string email, string password) { - ArgumentNullException.ThrowIfNull(model); - - var user = _userStorage.GetElement(new() { Email = model.Email }); + if (email is null) + { + throw new AccountException("Email is null"); + } + var user = _userStorage.GetElement(new() { Email = email }); if (user is null) { throw new ElementNotFoundException(); } // Проверяем пароль - _validatePassword(model.Password); - if (PasswordHasher.Verify(model.Password, user.PasswordHash)) + _validatePassword(password); + if (!PasswordHasher.Verify(password, user.PasswordHash)) { throw new AccountException("The passwords don't match."); } diff --git a/BusinessLogic/Tools/PasswordHasher.cs b/BusinessLogic/Tools/PasswordHasher.cs index f73edca..f52c90f 100644 --- a/BusinessLogic/Tools/PasswordHasher.cs +++ b/BusinessLogic/Tools/PasswordHasher.cs @@ -16,11 +16,7 @@ namespace BusinessLogic.Tools /// Хеш пароля public static string Hash(string password) { - using (SHA256 sha256 = SHA256.Create()) - { - byte[] bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(password)); - return Convert.ToBase64String(bytes); - } + return BCrypt.Net.BCrypt.HashPassword(password); } /// @@ -31,8 +27,7 @@ namespace BusinessLogic.Tools /// public static bool Verify(string password, string passHash) { - var hash = Hash(password); - return hash == passHash; + return BCrypt.Net.BCrypt.Verify(password, passHash); } } } \ No newline at end of file