update
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class RememberTokenModel extends Model
|
||||
{
|
||||
public const LIFETIME_SECONDS = 30 * 24 * 60 * 60;
|
||||
|
||||
protected $table = 'remember_tokens';
|
||||
protected $primaryKey = 'id';
|
||||
protected $returnType = 'array';
|
||||
protected $useTimestamps = true;
|
||||
protected $allowedFields = ['selector', 'validator_hash', 'expires_at'];
|
||||
|
||||
public function issueToken(): string
|
||||
{
|
||||
$selector = bin2hex(random_bytes(16));
|
||||
$validator = bin2hex(random_bytes(32));
|
||||
|
||||
$this->insert([
|
||||
'selector' => $selector,
|
||||
'validator_hash' => hash('sha256', $validator),
|
||||
'expires_at' => date('Y-m-d H:i:s', time() + self::LIFETIME_SECONDS),
|
||||
]);
|
||||
|
||||
return $selector . ':' . $validator;
|
||||
}
|
||||
|
||||
public function verifyToken(string $token): bool
|
||||
{
|
||||
[$selector, $validator] = $this->splitToken($token);
|
||||
if ($selector === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$row = $this->where('selector', $selector)->first();
|
||||
if (!$row || strtotime($row['expires_at']) <= time()) {
|
||||
if ($row) {
|
||||
$this->delete($row['id']);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return hash_equals($row['validator_hash'], hash('sha256', $validator));
|
||||
}
|
||||
|
||||
public function deleteToken(string $token): void
|
||||
{
|
||||
[$selector] = $this->splitToken($token);
|
||||
if ($selector !== null) {
|
||||
$this->where('selector', $selector)->delete();
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteExpired(): void
|
||||
{
|
||||
$this->where('expires_at <=', date('Y-m-d H:i:s'))->delete();
|
||||
}
|
||||
|
||||
private function splitToken(string $token): array
|
||||
{
|
||||
$parts = explode(':', $token, 2);
|
||||
if (count($parts) !== 2 || !ctype_xdigit($parts[0]) || strlen($parts[0]) !== 32
|
||||
|| !ctype_xdigit($parts[1]) || strlen($parts[1]) !== 64) {
|
||||
return [null, null];
|
||||
}
|
||||
|
||||
return $parts;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user