feat(care): 新增care app的权限认证

This commit is contained in:
ricky
2022-06-07 18:38:13 +08:00
parent 597f6d91fa
commit e8268ad9d3
5 changed files with 119 additions and 0 deletions

View File

@@ -34,6 +34,10 @@ return [
// 'expire_time' => null, // 始终为 session 的过期时间
'forbidden_key' => 'user:last_invalidate_time', // redis 中储存时的 key 名(此时间之前登录的用户都会被 T 掉)
],
'app' => [
'expire_time' => 30 * 24 * 60 * 60,
'prefix_key' => 'token:'
]
],
// redis 补充配置

View File

@@ -116,6 +116,9 @@ return [
],
],
],
'app' => [
'default' => 'Please login',
]
],
// 服务出错

View File

@@ -125,6 +125,9 @@ return [
],
],
],
'app' => [
'default' => '登录失效,请重新登录',
]
],
// 服务出错

View File

@@ -0,0 +1,103 @@
<?php
namespace Singularity\HDK\Account\Services\Auth;
use Singularity\HDK\Account\Resource\User;
use Hyperf\Redis\Redis;
use Singularity\HDK\Utils\Constants\CommonErrorCode;
use Singularity\HDK\Utils\Exceptions\ValidateException;
use Hyperf\HttpServer\Contract\RequestInterface;
class AppAuthentication implements AuthenticationInterface
{
private $prefix;
private $expire;
private $user;
public function __construct(
private RequestInterface $request,
private Redis $redis,
) {
$config = config('common.token.app');
$this->prefix = $config['prefix_key'];
$this->expire = $config['expire_time'];
}
/**
* @param User $user
* @return string
*/
public function generate(User $user): string
{
$token = md5(uniqid((string)mt_rand(), true));
$this->redis->set($this->prefix . $token, json_encode($user), $this->expire);
$this->user = $user;
return $token;
}
public function verified(string $token): mixed
{
if (empty($token)) {
throw new ValidateException(CommonErrorCode::AUTH_APP_ERROR, 'token', $token);
}
$redis_data = $this->redis->get($this->prefix . $token);
if (empty($redis_data)) {
throw new ValidateException(CommonErrorCode::AUTH_APP_ERROR, 'token', $token);
}
$this->redis->expire($this->prefix . $token, $this->expire);
return json_decode($redis_data, true);
}
/**
* @return string|null
*/
public function parseTokenFromHeaders(): ?string
{
$token = $this->request->input('pp_token');
return $token ?? '';
}
/**
* @param string|null $column
* @param bool $returnNull
* @return User|string|int|null
*/
public function getCurrentUser(?string $column = null, bool $returnNull = false): User|string|int|null
{
// TODO: Implement getCurrentUser() method.
}
/**
* @param bool $clearAll
* @return mixed
*/
public function invalid(bool $clearAll = false)
{
// TODO: Implement invalid() method.
}
/**
* @param string $uid
* @return bool
*/
public function invalidByUser(string $uid): bool
{
// TODO: Implement invalidByUser() method.
}
/**
* @param string $token
* @return bool
*/
public function invalidByToken(string $token): bool
{
// TODO: Implement invalidByToken() method.
}
}

View File

@@ -309,6 +309,12 @@ class CommonErrorCode extends AbstractConstants
* @Message("common_error.auth.forbidden.delete.wechat.only")
*/
public const FORBIDDEN_DELETE_ONLY_USERNAME_WITH_WECHAT = 2040411;
// 205 App 鉴权
/**
* @Message("common_error.auth.app.default")
*/
public const AUTH_APP_ERROR = 205000;
// ============== 3 依赖服务出错 ===============
// 303 缓存异常