mirror of
https://github.com/delight-im/PHP-Auth.git
synced 2025-08-02 22:27:30 +02:00
Extract usages of hashing for passwords to new class 'PasswordHash'
This commit is contained in:
@@ -366,7 +366,7 @@ final class Auth extends UserManager {
|
||||
}
|
||||
|
||||
if (!empty($expectedHash)) {
|
||||
$validated = \password_verify($password, $expectedHash);
|
||||
$validated = PasswordHash::verify($password, $expectedHash);
|
||||
|
||||
if (!$validated) {
|
||||
$this->throttle([ 'reconfirmPassword', $this->getIpAddress() ], 3, (60 * 60), 4, false);
|
||||
@@ -1229,9 +1229,9 @@ final class Auth extends UserManager {
|
||||
|
||||
$password = self::validatePassword($password);
|
||||
|
||||
if (\password_verify($password, $userData['password'])) {
|
||||
if (PasswordHash::verify($password, $userData['password'])) {
|
||||
// if the password needs to be re-hashed to keep up with improving password cracking techniques
|
||||
if (\password_needs_rehash($userData['password'], \PASSWORD_DEFAULT)) {
|
||||
if (PasswordHash::needsRehash($userData['password'])) {
|
||||
// create a new hash from the password and update it in the database
|
||||
$this->updatePasswordInternal($userData['id'], $password);
|
||||
}
|
||||
|
46
src/PasswordHash.php
Normal file
46
src/PasswordHash.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
final class PasswordHash {
|
||||
|
||||
const HASH_ALGORITHM_IDENTIFIER = \PASSWORD_DEFAULT;
|
||||
|
||||
/**
|
||||
* Creates a computationally expensive hash from a password
|
||||
*
|
||||
* @param string $passwordText
|
||||
* @return string|bool
|
||||
*/
|
||||
public static function from($passwordText) {
|
||||
return \password_hash($passwordText, self::HASH_ALGORITHM_IDENTIFIER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies whether a password matches a computationally expensive hash
|
||||
*
|
||||
* @param string $passwordText
|
||||
* @param string $expectedHash
|
||||
* @return bool
|
||||
*/
|
||||
public static function verify($passwordText, $expectedHash) {
|
||||
return \password_verify($passwordText, $expectedHash);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a computationally expensive hash needs to be updated to match a desired algorithm and set of options
|
||||
*
|
||||
* @param string $existingHash
|
||||
* @return bool
|
||||
*/
|
||||
public static function needsRehash($existingHash) {
|
||||
return \password_needs_rehash($existingHash, self::HASH_ALGORITHM_IDENTIFIER);
|
||||
}
|
||||
|
||||
}
|
@@ -160,7 +160,7 @@ abstract class UserManager {
|
||||
}
|
||||
}
|
||||
|
||||
$password = \password_hash($password, \PASSWORD_DEFAULT);
|
||||
$password = PasswordHash::from($password);
|
||||
$verified = \is_callable($callback) ? 0 : 1;
|
||||
|
||||
try {
|
||||
@@ -201,7 +201,7 @@ abstract class UserManager {
|
||||
* @throws AuthError if an internal problem occurred (do *not* catch)
|
||||
*/
|
||||
protected function updatePasswordInternal($userId, $newPassword) {
|
||||
$newPassword = \password_hash($newPassword, \PASSWORD_DEFAULT);
|
||||
$newPassword = PasswordHash::from($newPassword);
|
||||
|
||||
try {
|
||||
$affected = $this->db->update(
|
||||
|
Reference in New Issue
Block a user