diff --git a/Controllers/PasswordVerification.cs b/Controllers/PasswordVerification.cs index 1bee7b0..4016532 100644 --- a/Controllers/PasswordVerification.cs +++ b/Controllers/PasswordVerification.cs @@ -3,20 +3,39 @@ using System.Security.Cryptography; using System.Text; using TextSender_API.Models; +using TextSender_API.Repositories; namespace TextSender_API.Controllers; public class PasswordVerification { #region Fiends + private UsersRepository? _userRepo; + private SaltRepository? _saltRepo; private int _keySize = 64; private int _iterations = 350000; #endregion #region Constructors + public PasswordVerification() + { + } + + public PasswordVerification(UsersRepository userRepo, SaltRepository saltRepo) + { + this._userRepo = userRepo; + this._saltRepo = saltRepo; + } #endregion #region Methods + public bool VerifyPassword(User user, string password) + { + var salt = this._saltRepo.Retrieve(user.Id); + var hashedPassword = this.HashPassword(user, salt, password); + + return hashedPassword.Equals(user.Password); + } public Salt CreateSalt(User user) { var salt = new Salt(); @@ -25,10 +44,15 @@ public class PasswordVerification return salt; } - public string HashPassword(User user, Salt salt) + public string HashPassword(User user, Salt salt, string password = "") { var hashAlgorithm = HashAlgorithmName.SHA512; + if (!string.IsNullOrEmpty(password)) + { + user.Password = password; + } + var hash = Rfc2898DeriveBytes.Pbkdf2( Encoding.UTF8.GetBytes(user.Password!), salt.Key!, diff --git a/Controllers/UsersController.cs b/Controllers/UsersController.cs index af82d06..9c203c9 100644 --- a/Controllers/UsersController.cs +++ b/Controllers/UsersController.cs @@ -27,6 +27,34 @@ public class UsersController : ControllerBase #endregion #region Methods + [HttpPost("login")] + public IActionResult Login([FromBody] User userRequest) + { + try + { + var connString = this._config.GetConnectionString("MongoDBURI"); + var userRepo = new UsersRepository(connString!); + var saltRepo = new SaltRepository(connString!); + var pwdVerify = new PasswordVerification(userRepo, saltRepo); + var result = userRepo.Exists(userRequest); + + if (result.Item1 && pwdVerify.VerifyPassword(result.Item2, userRequest.Password!)) + { + Ok(result.Item2); + } + else + { + return NotFound(); + } + } + catch (Exception ex) + { + this._logger.LogError($"An error occurred: {ex.Message}"); + } + + return Ok(); + } + [HttpPost("register"), DisableRequestSizeLimit] public IActionResult Upload([FromBody] User userRequest) { diff --git a/Repositories/UsersRepository.cs b/Repositories/UsersRepository.cs index fa10199..5088769 100644 --- a/Repositories/UsersRepository.cs +++ b/Repositories/UsersRepository.cs @@ -30,6 +30,20 @@ public class UsersRepository #endregion #region Metods + public Tuple Exists(User user) + { + var allUsers = this.RetrieveAllUsers(); + var result = allUsers.FirstOrDefault(ea => ea.Username!.Equals(user.Username)); + + if (result != null) + { + return new Tuple(true, result!); + } + else + { + return new Tuple(false, new User()); + } + } public void CreateUser(User user) { this._bookCollection.InsertOne(user); @@ -92,6 +106,11 @@ public class SaltRepository return this._bookCollection.Find(_ => true).ToList(); } + public Salt Retrieve(string userId) + { + return this._bookCollection.Find(s => s.UserId.Equals(userId)).FirstOrDefault(); + } + public void Update(Salt salt) { var filter = Builders.Filter.Eq(u => u.Id, salt.Id);