feat: 适配 hyperf 3.1

This commit is contained in:
李东云
2023-12-05 10:14:06 +08:00
parent 74e2566220
commit 32a7accc87

View File

@@ -21,14 +21,15 @@ use Hyperf\Server\Exception\InvalidArgumentException;
use Singularity\HDK\Auth\Resource\User;
use Singularity\HDK\Core\Constants\CommonErrorCode;
use Singularity\HDK\Core\Exceptions\Unauthorized;
use Singularity\HDK\Core\Exceptions\ValidateException;
use function Hyperf\Config\config;
class SessionAuthentication implements AuthenticationInterface
{
use JustDont;
private string $lastInvalidateTimeKey;
public function __construct(
private SessionInterface $session,
private RequestInterface $request,
@@ -38,7 +39,7 @@ class SessionAuthentication implements AuthenticationInterface
$last_invalidate_time_key = config('common.token.session.forbidden_key', 'user:last_invalidate_time');
$this->lastInvalidateTimeKey = $redis_path . $last_invalidate_time_key;
}
/**
* @inheritDoc
*/
@@ -46,10 +47,10 @@ class SessionAuthentication implements AuthenticationInterface
{
$this->session->set('userInfo', $user->toArray());
$this->session->set('createdAt', microtime(true));
return $this->session->getId();
}
/**
* 解码,并返回验证后的值
*/
@@ -58,17 +59,17 @@ class SessionAuthentication implements AuthenticationInterface
// if (!$this->session->isValidId($token ?? '')) {
// throw new ValidateException(CommonErrorCode::AUTH_SESSION_ERROR, 'token', $token);
// }
$user = $this->session->get('userInfo');
if (empty($user)) {
throw new Unauthorized(CommonErrorCode::AUTH_SESSION_ERROR);
}
$user = new User($user);
if (empty($user['uid'])) {
throw new Unauthorized(CommonErrorCode::AUTH_SESSION_UID_ERROR);
}
// 判断用户 session 是否应该失效
$last_invalidate_time = $this->redis->hGet(
$this->lastInvalidateTimeKey,
@@ -80,10 +81,10 @@ class SessionAuthentication implements AuthenticationInterface
if ($this->session->get('createdAt') < $last_invalidate_time) {
throw new Unauthorized(CommonErrorCode::AUTH_SESSION_CREATED_AT_ERROR);
}
return $user;
}
/**
* @inheritDoc
*/
@@ -100,7 +101,7 @@ class SessionAuthentication implements AuthenticationInterface
);
}
$this->session->invalidate();
return new Cookie(
'is_login',
'',
@@ -111,7 +112,7 @@ class SessionAuthentication implements AuthenticationInterface
sameSite: Cookie::SAMESITE_LAX
);
}
/**
* 从请求中解析出 token.
*
@@ -130,7 +131,7 @@ class SessionAuthentication implements AuthenticationInterface
? null
: $token;
}
/**
* @inheritDoc
*/
@@ -141,7 +142,7 @@ class SessionAuthentication implements AuthenticationInterface
): User|string|int|null {
// 查询是否已通过中间件鉴权
$user = $this->session->get('userInfo');
// 未匹配到任何用户信息
if (empty($user)) {
if ($returnNull) {
@@ -149,7 +150,7 @@ class SessionAuthentication implements AuthenticationInterface
}
throw new Unauthorized(CommonErrorCode::AUTH_SESSION_ERROR);
}
// 已匹配到时返回指定字段或整个用户信息
if ($column !== null) {
if (!isset($user[$column])) {
@@ -158,12 +159,12 @@ class SessionAuthentication implements AuthenticationInterface
}
throw new InvalidArgumentException("属性 $column 不存在");
}
return $user[$column];
}
return new User($user);
}
/**
* @inheritDoc
*/
@@ -176,7 +177,7 @@ class SessionAuthentication implements AuthenticationInterface
);
return $result !== false;
}
/**
* @inheritDoc
*/