2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Models\Entities;
|
|
|
|
use openvk\Web\Models\Repositories\Users;
|
|
|
|
use openvk\Web\Models\RowModel;
|
|
|
|
use openvk\Web\Util\DateTime;
|
|
|
|
|
|
|
|
class PasswordReset extends RowModel
|
|
|
|
{
|
|
|
|
protected $tableName = "password_resets";
|
|
|
|
|
|
|
|
function getUser(): User
|
|
|
|
{
|
|
|
|
return (new Users)->get($this->getRecord()->profile);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getKey(): string
|
|
|
|
{
|
|
|
|
return $this->getRecord()->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getToken(): string
|
|
|
|
{
|
|
|
|
return $this->getKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCreationTime(): DateTime
|
|
|
|
{
|
|
|
|
return new DateTime($this->getRecord()->timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* User can request password reset only if he does not have any "new" password resets.
|
|
|
|
* Password reset becomes "old" after 5 minutes and one second.
|
|
|
|
*/
|
|
|
|
function isNew(): bool
|
|
|
|
{
|
2022-01-31 14:45:53 +03:00
|
|
|
return $this->getRecord()->timestamp > (time() - (5 * MINUTE));
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:45:53 +03:00
|
|
|
/**
|
|
|
|
* Token is valid only for 3 days.
|
|
|
|
*/
|
2020-06-07 19:04:43 +03:00
|
|
|
function isStillValid(): bool
|
|
|
|
{
|
2022-01-31 14:45:53 +03:00
|
|
|
return $this->getRecord()->timestamp > (time() - (3 * DAY));
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function verify(string $token): bool
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->isStillValid() ? sodium_memcmp($this->getKey(), $token) : false;
|
|
|
|
} catch(\SodiumException $ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 00:13:36 +03:00
|
|
|
function save(?bool $log = false): void
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
$this->stateChanges("key", base64_encode(openssl_random_pseudo_bytes(46)));
|
|
|
|
$this->stateChanges("timestamp", time());
|
|
|
|
|
2023-10-31 00:13:36 +03:00
|
|
|
parent::save($log);
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
}
|