mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-23 20:00:27 +08:00
52 lines
955 B
PHP
52 lines
955 B
PHP
<?php
|
|
|
|
namespace App\Library;
|
|
|
|
use Phalcon\Crypt;
|
|
use Phalcon\Di;
|
|
use Phalcon\Text;
|
|
|
|
class CsrfToken
|
|
{
|
|
|
|
/**
|
|
* @var Crypt
|
|
*/
|
|
protected $crypt;
|
|
|
|
protected $lifetime = 60 * 60;
|
|
|
|
protected $delimiter = '@@';
|
|
|
|
protected $fixed = 'KG';
|
|
|
|
public function __construct()
|
|
{
|
|
$this->crypt = Di::getDefault()->get('crypt');
|
|
}
|
|
|
|
public function getToken()
|
|
{
|
|
$text = implode($this->delimiter, [time(), $this->fixed, Text::random(8)]);
|
|
|
|
return $this->crypt->encryptBase64($text);
|
|
}
|
|
|
|
public function checkToken($token)
|
|
{
|
|
$text = $this->crypt->decryptBase64($token);
|
|
|
|
list($time, $fixed, $random) = explode($this->delimiter, $text);
|
|
|
|
if ($time != intval($time) || $fixed != $this->fixed || strlen($random) != 8) {
|
|
return false;
|
|
}
|
|
|
|
if (time() - $time > $this->lifetime) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
} |