Fixed tests

This commit is contained in:
Eldad Fux
2025-03-19 13:54:32 +01:00
parent 3d967e695f
commit 8c9123beaa
3 changed files with 15 additions and 13 deletions
+7 -3
View File
@@ -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);
}
+3 -1
View File
@@ -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);
}
@@ -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;
}
}