add
This commit is contained in:
92
app/common/library/token/Driver.php
Normal file
92
app/common/library/token/Driver.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\library\token;
|
||||
|
||||
use think\facade\Config;
|
||||
|
||||
/**
|
||||
* Token 驱动抽象类
|
||||
*/
|
||||
abstract class Driver
|
||||
{
|
||||
/**
|
||||
* 具体驱动的句柄 Mysql|Redis
|
||||
* @var object
|
||||
*/
|
||||
protected object $handler;
|
||||
|
||||
/**
|
||||
* @var array 配置数据
|
||||
*/
|
||||
protected array $options = [];
|
||||
|
||||
/**
|
||||
* 设置 token
|
||||
* @param string $token Token
|
||||
* @param string $type Type
|
||||
* @param int $userId 用户ID
|
||||
* @param ?int $expire 过期时间
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function set(string $token, string $type, int $userId, int $expire = null): bool;
|
||||
|
||||
/**
|
||||
* 获取 token 的数据
|
||||
* @param string $token Token
|
||||
* @return array
|
||||
*/
|
||||
abstract public function get(string $token): array;
|
||||
|
||||
/**
|
||||
* 检查token是否有效
|
||||
* @param string $token
|
||||
* @param string $type
|
||||
* @param int $userId
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function check(string $token, string $type, int $userId): bool;
|
||||
|
||||
/**
|
||||
* 删除一个token
|
||||
* @param string $token
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function delete(string $token): bool;
|
||||
|
||||
/**
|
||||
* 清理一个用户的所有token
|
||||
* @param string $type
|
||||
* @param int $userId
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function clear(string $type, int $userId): bool;
|
||||
|
||||
/**
|
||||
* 返回句柄对象
|
||||
* @access public
|
||||
* @return object|null
|
||||
*/
|
||||
public function handler(): ?object
|
||||
{
|
||||
return $this->handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $token
|
||||
* @return string
|
||||
*/
|
||||
protected function getEncryptedToken(string $token): string
|
||||
{
|
||||
$config = Config::get('buildadmin.token');
|
||||
return hash_hmac($config['algo'], $token, $config['key']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $expireTime
|
||||
* @return int
|
||||
*/
|
||||
protected function getExpiredIn(int $expireTime): int
|
||||
{
|
||||
return $expireTime ? max(0, $expireTime - time()) : 365 * 86400;
|
||||
}
|
||||
}
|
||||
16
app/common/library/token/TokenExpirationException.php
Normal file
16
app/common/library/token/TokenExpirationException.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\library\token;
|
||||
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
* Token过期异常
|
||||
*/
|
||||
class TokenExpirationException extends Exception
|
||||
{
|
||||
public function __construct(protected $message = '', protected $code = 409, protected $data = [])
|
||||
{
|
||||
parent::__construct($message, $code);
|
||||
}
|
||||
}
|
||||
109
app/common/library/token/driver/Mysql.php
Normal file
109
app/common/library/token/driver/Mysql.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\library\token\driver;
|
||||
|
||||
use Throwable;
|
||||
use think\facade\Db;
|
||||
use think\facade\Cache;
|
||||
use app\common\library\token\Driver;
|
||||
|
||||
/**
|
||||
* @see Driver
|
||||
*/
|
||||
class Mysql extends Driver
|
||||
{
|
||||
/**
|
||||
* 默认配置
|
||||
* @var array
|
||||
*/
|
||||
protected array $options = [];
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @access public
|
||||
* @param array $options 参数
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
if (!empty($options)) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
|
||||
if ($this->options['name']) {
|
||||
$this->handler = Db::connect($this->options['name'])->name($this->options['table']);
|
||||
} else {
|
||||
$this->handler = Db::name($this->options['table']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function set(string $token, string $type, int $userId, int $expire = null): bool
|
||||
{
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
$expireTime = $expire !== 0 ? time() + $expire : 0;
|
||||
$token = $this->getEncryptedToken($token);
|
||||
$this->handler->insert([
|
||||
'token' => $token,
|
||||
'type' => $type,
|
||||
'user_id' => $userId,
|
||||
'create_time' => time(),
|
||||
'expire_time' => $expireTime,
|
||||
]);
|
||||
|
||||
// 每隔48小时清理一次过期Token
|
||||
$time = time();
|
||||
$lastCacheCleanupTime = Cache::get('last_cache_cleanup_time');
|
||||
if (!$lastCacheCleanupTime || $lastCacheCleanupTime < $time - 172800) {
|
||||
Cache::set('last_cache_cleanup_time', $time);
|
||||
$this->handler->where('expire_time', '<', time())->where('expire_time', '>', 0)->delete();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function get(string $token): array
|
||||
{
|
||||
$data = $this->handler->where('token', $this->getEncryptedToken($token))->find();
|
||||
if (!$data) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$data['token'] = $token; // 返回未加密的token给客户端使用
|
||||
$data['expires_in'] = $this->getExpiredIn($data['expire_time'] ?? 0); // 返回剩余有效时间
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function check(string $token, string $type, int $userId): bool
|
||||
{
|
||||
$data = $this->get($token);
|
||||
if (!$data || ($data['expire_time'] && $data['expire_time'] <= time())) return false;
|
||||
return $data['type'] == $type && $data['user_id'] == $userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function delete(string $token): bool
|
||||
{
|
||||
$this->handler->where('token', $this->getEncryptedToken($token))->delete();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function clear(string $type, int $userId): bool
|
||||
{
|
||||
$this->handler->where('type', $type)->where('user_id', $userId)->delete();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
146
app/common/library/token/driver/Redis.php
Normal file
146
app/common/library/token/driver/Redis.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\library\token\driver;
|
||||
|
||||
use Throwable;
|
||||
use BadFunctionCallException;
|
||||
use app\common\library\token\Driver;
|
||||
|
||||
/**
|
||||
* @see Driver
|
||||
*/
|
||||
class Redis extends Driver
|
||||
{
|
||||
/**
|
||||
* 默认配置
|
||||
* @var array
|
||||
*/
|
||||
protected array $options = [];
|
||||
|
||||
/**
|
||||
* Token 过期后缓存继续保留的时间(s)
|
||||
*/
|
||||
protected int $expiredHold = 60 * 60 * 24 * 2;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @access public
|
||||
* @param array $options 参数
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
if (!extension_loaded('redis')) {
|
||||
throw new BadFunctionCallException('未安装redis扩展');
|
||||
}
|
||||
if (!empty($options)) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
$this->handler = new \Redis();
|
||||
if ($this->options['persistent']) {
|
||||
$this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
|
||||
} else {
|
||||
$this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']);
|
||||
}
|
||||
|
||||
if ('' != $this->options['password']) {
|
||||
$this->handler->auth($this->options['password']);
|
||||
}
|
||||
|
||||
if (false !== $this->options['select']) {
|
||||
$this->handler->select($this->options['select']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function set(string $token, string $type, int $userId, int $expire = null): bool
|
||||
{
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
$expireTime = $expire !== 0 ? time() + $expire : 0;
|
||||
$token = $this->getEncryptedToken($token);
|
||||
$tokenInfo = [
|
||||
'token' => $token,
|
||||
'type' => $type,
|
||||
'user_id' => $userId,
|
||||
'create_time' => time(),
|
||||
'expire_time' => $expireTime,
|
||||
];
|
||||
$tokenInfo = json_encode($tokenInfo, JSON_UNESCAPED_UNICODE);
|
||||
if ($expire) {
|
||||
$expire += $this->expiredHold;
|
||||
$result = $this->handler->setex($token, $expire, $tokenInfo);
|
||||
} else {
|
||||
$result = $this->handler->set($token, $tokenInfo);
|
||||
}
|
||||
$this->handler->sAdd($this->getUserKey($type, $userId), $token);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function get(string $token): array
|
||||
{
|
||||
$key = $this->getEncryptedToken($token);
|
||||
$data = $this->handler->get($key);
|
||||
if (is_null($data) || false === $data) {
|
||||
return [];
|
||||
}
|
||||
$data = json_decode($data, true);
|
||||
|
||||
$data['token'] = $token; // 返回未加密的token给客户端使用
|
||||
$data['expires_in'] = $this->getExpiredIn($data['expire_time'] ?? 0); // 过期时间
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function check(string $token, string $type, int $userId): bool
|
||||
{
|
||||
$data = $this->get($token);
|
||||
if (!$data || ($data['expire_time'] && $data['expire_time'] <= time())) return false;
|
||||
return $data['type'] == $type && $data['user_id'] == $userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function delete(string $token): bool
|
||||
{
|
||||
$data = $this->get($token);
|
||||
if ($data) {
|
||||
$key = $this->getEncryptedToken($token);
|
||||
$this->handler->del($key);
|
||||
$this->handler->sRem($this->getUserKey($data['type'], $data['user_id']), $key);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function clear(string $type, int $userId): bool
|
||||
{
|
||||
$userKey = $this->getUserKey($type, $userId);
|
||||
$keys = $this->handler->sMembers($userKey);
|
||||
$this->handler->del($userKey);
|
||||
$this->handler->del($keys);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员的key
|
||||
* @param $type
|
||||
* @param $userId
|
||||
* @return string
|
||||
*/
|
||||
protected function getUserKey($type, $userId): string
|
||||
{
|
||||
return $this->options['prefix'] . $type . '-' . $userId;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user