From 8c9123beaa7919f6466ecbc66e0ca26ced91c5fe Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Wed, 19 Mar 2025 13:54:32 +0100 Subject: [PATCH] Fixed tests --- app/controllers/api/account.php | 10 +++++++--- app/controllers/api/users.php | 4 +++- src/Appwrite/Auth/Validator/PasswordHistory.php | 14 +++++--------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index d029eff4f0..51a1c4f101 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -383,7 +383,7 @@ App::post('/v1/account') 'emailVerification' => false, 'status' => true, 'password' => $hash, - 'passwordHistory' => $passwordHistory > 0 ? [$password] : [], + 'passwordHistory' => $passwordHistory > 0 ? [$hash] : [], 'passwordUpdate' => DateTime::now(), 'hash' => $proof->getHash()->getName(), 'hashOptions' => $proof->getHash()->getOptions(), @@ -2894,9 +2894,11 @@ App::patch('/v1/account/password') $newPassword = $proofForPassword->hash($password); $historyLimit = $project->getAttribute('auths', [])['passwordHistory'] ?? 0; + $hash = ProofsPassword::createHash($user->getAttribute('hash'), $user->getAttribute('hashOptions')); $history = $user->getAttribute('passwordHistory', []); + if ($historyLimit > 0) { - $validator = new PasswordHistory($history, $user->getAttribute('hash'), $user->getAttribute('hashOptions')); + $validator = new PasswordHistory($history, $hash); if (!$validator->isValid($password)) { throw new Exception(Exception::USER_PASSWORD_RECENTLY_USED); } @@ -3441,10 +3443,12 @@ App::put('/v1/account/recovery') $newPassword = $proofForPassword->hash($password); + $hash = ProofsPassword::createHash($profile->getAttribute('hash'), $profile->getAttribute('hashOptions')); $historyLimit = $project->getAttribute('auths', [])['passwordHistory'] ?? 0; $history = $profile->getAttribute('passwordHistory', []); + if ($historyLimit > 0) { - $validator = new PasswordHistory($history, $profile->getAttribute('hash'), $profile->getAttribute('hashOptions')); + $validator = new PasswordHistory($history, $hash); if (!$validator->isValid($password)) { throw new Exception(Exception::USER_PASSWORD_RECENTLY_USED); } diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index d4e8c9cb48..65a35d616a 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -1319,10 +1319,12 @@ App::patch('/v1/users/:userId/password') $newPassword = $hasher->hash($password); + $hash = ProofsPassword::createHash($user->getAttribute('hash'), $user->getAttribute('hashOptions')); $historyLimit = $project->getAttribute('auths', [])['passwordHistory'] ?? 0; $history = $user->getAttribute('passwordHistory', []); + if ($historyLimit > 0) { - $validator = new PasswordHistory($history, $user->getAttribute('hash'), $user->getAttribute('hashOptions')); + $validator = new PasswordHistory($history, $hash); if (!$validator->isValid($password)) { throw new Exception(Exception::USER_PASSWORD_RECENTLY_USED); } diff --git a/src/Appwrite/Auth/Validator/PasswordHistory.php b/src/Appwrite/Auth/Validator/PasswordHistory.php index 7677deafc0..9b40b6a794 100644 --- a/src/Appwrite/Auth/Validator/PasswordHistory.php +++ b/src/Appwrite/Auth/Validator/PasswordHistory.php @@ -2,7 +2,7 @@ namespace Appwrite\Auth\Validator; -use Utopia\Auth\Proofs\Password as ProofsPassword; +use Utopia\Auth\Hash; /** * Password. @@ -12,16 +12,14 @@ use Utopia\Auth\Proofs\Password as ProofsPassword; class PasswordHistory extends Password { protected array $history; - protected string $algo; - protected array $algoOptions; + protected Hash $hash; - public function __construct(array $history, string $algo, array $algoOptions = []) + public function __construct(array $history, Hash $hash) { parent::__construct(); $this->history = $history; - $this->algo = $algo; - $this->algoOptions = $algoOptions; + $this->hash = $hash; } /** @@ -45,10 +43,8 @@ class PasswordHistory extends Password */ public function isValid($value): bool { - $proofForPassword = ProofsPassword::createHash($this->algo, $this->algoOptions); - foreach ($this->history as $hash) { - if (!empty($hash) && $proofForPassword->verify($value, $hash)) { + if (!empty($hash) && $this->hash->verify($value, $hash)) { return false; } }