Skip to content

Commit

Permalink
Improve password hashing security using password_hash and password_ve…
Browse files Browse the repository at this point in the history
…rify.
  • Loading branch information
maurobonfietti committed Jul 11, 2022
1 parent 10daf56 commit 6e92d77
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion database/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ INSERT INTO `users` (`name`, `email`, `password`) VALUES ('Carlos', 'bianchini@h
INSERT INTO `users` (`name`, `email`, `password`) VALUES ('Diego', '[email protected]', 'd5f4da62059760b35de35f8fbd8efb43eee26ac741ef8c6e51782a13ac7d50e927b653160c591616a9dc8a452c877a6b80c00aecba14504756a65f88439fcd1e');
INSERT INTO `users` (`name`, `email`, `password`) VALUES ('One User', '[email protected]', 'd5f4da62059760b35de35f8fbd8efb43eee26ac741ef8c6e51782a13ac7d50e927b653160c591616a9dc8a452c877a6b80c00aecba14504756a65f88439fcd1e');
INSERT INTO `users` (`name`, `email`, `password`) VALUES ('Diegol', '[email protected]', 'd5f4da62059760b35de35f8fbd8efb43eee26ac741ef8c6e51782a13ac7d50e927b653160c591616a9dc8a452c877a6b80c00aecba14504756a65f88439fcd1e');
INSERT INTO `users` (`name`, `email`, `password`) VALUES ('Test User', '[email protected]', 'd5f4da62059760b35de35f8fbd8efb43eee26ac741ef8c6e51782a13ac7d50e927b653160c591616a9dc8a452c877a6b80c00aecba14504756a65f88439fcd1e');
INSERT INTO `users` (`name`, `email`, `password`) VALUES ('Test User', '[email protected]', '$2y$10$S9.JvxDbDhESUZvZWmpyleWB4YTHEaCJ5nevlXMHNso8J4X4/Sgeq');

-- ----------------------------
-- Table structure for notes
Expand Down
6 changes: 4 additions & 2 deletions src/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,19 @@ public function loginUser(string $email, string $password): User
$query = '
SELECT *
FROM `users`
WHERE `email` = :email AND `password` = :password
WHERE `email` = :email
ORDER BY `id`
';
$statement = $this->database->prepare($query);
$statement->bindParam('email', $email);
$statement->bindParam('password', $password);
$statement->execute();
$user = $statement->fetchObject(User::class);
if (! $user) {
throw new \App\Exception\User('Login failed: Email or password incorrect.', 400);
}
if (! password_verify($password, $user->getPassword())) {
throw new \App\Exception\User('Login failed: Email or password incorrect.', 400);
}

return $user;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Service/User/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ private function validateUserData(array $input): User
$myuser = new User();
$myuser->updateName(self::validateUserName($user->name));
$myuser->updateEmail(self::validateEmail($user->email));
$myuser->updatePassword(hash('sha512', $user->password));
$hash = password_hash($user->password, PASSWORD_BCRYPT);
$myuser->updatePassword($hash);
$this->userRepository->checkUserByEmail($user->email);

return $myuser;
Expand Down
3 changes: 1 addition & 2 deletions src/Service/User/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public function login(array $input): string
if (! isset($data->password)) {
throw new User('The field "password" is required.', 400);
}
$password = hash('sha512', $data->password);
$user = $this->userRepository->loginUser($data->email, $password);
$user = $this->userRepository->loginUser($data->email, $data->password);
$token = [
'sub' => $user->getId(),
'email' => $user->getEmail(),
Expand Down

0 comments on commit 6e92d77

Please sign in to comment.