|
|
@@ -18,14 +18,16 @@ class Auth implements AuthInterface
|
|
|
|
|
|
public const int SLEEP_WAY = 60;
|
|
|
|
|
|
- private array $config;
|
|
|
+ private static array $config;
|
|
|
|
|
|
public function __construct(private readonly UserTypeEnum $userType)
|
|
|
{
|
|
|
- $this->config = [
|
|
|
- 'jwt_secret' => env('JWT_SECRET', ''),
|
|
|
- 'expire_in' => env('JWT_EXPIRE_IN', 3600),
|
|
|
- ];
|
|
|
+ if (empty(self::$config)) {
|
|
|
+ self::$config = [
|
|
|
+ 'jwt_secret' => env('JWT_SECRET', ''),
|
|
|
+ 'expire_in' => (int) env('JWT_EXPIRE_IN', 3600),
|
|
|
+ ];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public function refreshToken(string $jwt): string
|
|
|
@@ -38,12 +40,14 @@ class Auth implements AuthInterface
|
|
|
$payload = [
|
|
|
'iss' => 'SixShop', // 签发者
|
|
|
'aud' => $this->userType->value, // 接收者
|
|
|
- 'sub' => encrypt_data($userId, $this->config['jwt_secret']), // 主题
|
|
|
- 'exp' => time() + $this->config['expire_in'], // 过期时间
|
|
|
+ 'sub' => encrypt_data($userId, self::$config['jwt_secret']), // 主题
|
|
|
+ 'exp' => time() + self::$config['expire_in'], // 过期时间
|
|
|
'iat' => time(), // 签发时间
|
|
|
'jti' => Uuid::uuid4()->toString(), // 唯一标识
|
|
|
];
|
|
|
- return JWT::encode($payload, $this->config['jwt_secret'], self::ALGORITHM);
|
|
|
+ $jwt = JWT::encode($payload, self::$config['jwt_secret'], self::ALGORITHM);
|
|
|
+ Event::trigger('token_generate', (object)$payload);
|
|
|
+ return $jwt;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -53,14 +57,14 @@ class Auth implements AuthInterface
|
|
|
{
|
|
|
JWT::$leeway = self::SLEEP_WAY;
|
|
|
try {
|
|
|
- $payload = JWT::decode($jwt, new Key($this->config['jwt_secret'], self::ALGORITHM));
|
|
|
+ $payload = JWT::decode($jwt, new Key(self::$config['jwt_secret'], self::ALGORITHM));
|
|
|
} catch (ExpiredException $e) {
|
|
|
// ... 忽略
|
|
|
$payload = $e->getPayload();
|
|
|
}
|
|
|
|
|
|
$res = match (UserTypeEnum::tryFrom($payload->aud)) {
|
|
|
- $this->userType => decrypt_data($payload->sub, $this->config['jwt_secret']),
|
|
|
+ $this->userType => decrypt_data($payload->sub, self::$config['jwt_secret']),
|
|
|
default => throw new \Exception('token 类型错误'),
|
|
|
};
|
|
|
Event::trigger('token_verify', $payload);
|
|
|
@@ -71,12 +75,10 @@ class Auth implements AuthInterface
|
|
|
{
|
|
|
JWT::$leeway = self::SLEEP_WAY;
|
|
|
try {
|
|
|
- $payload = JWT::decode($jwt, new Key($this->config['jwt_secret'], self::ALGORITHM));
|
|
|
+ $payload = JWT::decode($jwt, new Key(self::$config['jwt_secret'], self::ALGORITHM));
|
|
|
} catch (ExpiredException $e) {
|
|
|
// ... 忽略
|
|
|
$payload = $e->getPayload();
|
|
|
- } catch (\Exception) {
|
|
|
- return;
|
|
|
}
|
|
|
Event::trigger('token_revoke', $payload);
|
|
|
}
|
|
|
@@ -85,4 +87,9 @@ class Auth implements AuthInterface
|
|
|
{
|
|
|
return $this->userType;
|
|
|
}
|
|
|
+
|
|
|
+ public static function getConfig(): array
|
|
|
+ {
|
|
|
+ return self::$config;
|
|
|
+ }
|
|
|
}
|