Skip to content

Commit e1c853f

Browse files
committed
update
1 parent b65b8fb commit e1c853f

6 files changed

Lines changed: 68 additions & 65 deletions

File tree

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
"thinkphp"
88
],
99
"description": "ThinkPHP Jwt Component",
10-
"minimum-stability": "stable",
10+
"minimum-stability": "dev",
1111
"authors": [
1212
{
1313
"name": "xiaodi",
1414
"email": "liangjinbiao@live.com"
1515
}
1616
],
1717
"require": {
18-
"php": ">=7.1.0",
18+
"php": ">=7.4.0",
1919
"ext-json": "*",
2020
"ext-mbstring": "*",
2121
"lcobucci/jwt": "4.0.1",

config/config.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
'enable' => false,
88
],
99
'token' => [
10-
'unique_id_key' => 'uid',
1110
'signer_key' => 'tant',
1211
'not_before' => 0,
1312
'expires_at' => 3600,

src/JwtService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace xiaodi\JWTAuth;
66

7-
use xiaodi\JWTAuth\Service\Jwt;
7+
use xiaodi\JWTAuth\Service\JwtAuth;
88
use xiaodi\JWTAuth\Service\Manager;
99
use xiaodi\JWTAuth\Service\Token;
1010
use xiaodi\JWTAuth\Service\SSO;
@@ -14,7 +14,7 @@ class JwtService extends \think\Service
1414
{
1515
public function register()
1616
{
17-
$this->app->bind('jwt', Jwt::class);
17+
$this->app->bind('jwt', JwtAuth::class);
1818
$this->app->bind('jwt.manager', Manager::class);
1919
$this->app->bind('jwt.token', Token::class);
2020
$this->app->bind('jwt.sso', SSO::class);
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44

55
namespace xiaodi\JWTAuth\Service;
66

7-
use DateTime;
87
use DateTimeImmutable;
9-
use Exception;
108
use think\App;
119

12-
use Lcobucci\JWT\Token as JwtToken;
10+
use Lcobucci\JWT\Token;
1311
use xiaodi\JWTAuth\Exception\JWTException;
1412
use xiaodi\JWTAuth\Exception\TokenAlreadyEexpired;
1513

16-
class Jwt
14+
class JwtAuth
1715
{
1816
/**
1917
* 应用名称
@@ -22,13 +20,6 @@ class Jwt
2220
*/
2321
protected $store;
2422

25-
/**
26-
* Token
27-
*
28-
* @var Token
29-
*/
30-
protected $token;
31-
3223
protected $user;
3324

3425
public function __construct(App $app)
@@ -65,12 +56,14 @@ protected function getDefaultApp(): string
6556
* 生成 Token
6657
*
6758
* @param array $claims
68-
* @return JwtToken
59+
* @return Token
6960
*/
70-
public function token($identifier, array $claims = []): JwtToken
61+
public function token($identifier, array $claims = []): Token
7162
{
7263
$token = $this->app->get('jwt.token')->make($identifier, $claims);
7364

65+
$this->app->get('jwt.manager')->login($token);
66+
7467
return $token;
7568
}
7669

@@ -89,6 +82,7 @@ public function verify(?string $token): bool
8982

9083
if (!$service->validate($token)) {
9184
$now = new DateTimeImmutable();
85+
9286
$token = $service->getToken();
9387
if (!$service->isRefreshExpired($now)) {
9488
$config = $service->getConfig();
@@ -98,11 +92,15 @@ public function verify(?string $token): bool
9892
} else {
9993
throw new JWTException('效验失败', 401);
10094
}
95+
} else {
96+
$token = $this->app->get('jwt.token')->getToken();
10197
}
10298

10399
// 是否存在黑名单
104100
if (true === $this->app->get('jwt.manager')->wasBan($token)) {
105-
throw new TokenAlreadyEexpired('token was ban', $this->config->getReloginCode());
101+
$config = $this->app->get('jwt.token')->getConfig();
102+
103+
throw new TokenAlreadyEexpired('token was ban', $config->getReloginCode());
106104
}
107105

108106
return true;

src/Service/Manager.php

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace xiaodi\JWTAuth\Service;
66

7-
use Lcobucci\JWT\Token;
8-
use Lcobucci\JWT\Parser;
97
use think\App;
8+
use Lcobucci\JWT\Token;
109
use xiaodi\JWTAuth\Config\Manager as Config;
1110

1211
class Manager
@@ -65,23 +64,29 @@ protected function pushWhitelist(Token $token): void
6564
{
6665
$jti = $token->claims()->get('jti');
6766
$store = $token->claims()->get('store');
68-
$exp = $token->claims()->get('exp') - time();
67+
68+
$now = time();
69+
$exp = $token->claims()->get('exp');
70+
71+
$ttl = $exp->getTimestamp() - $now;
6972
$tag = $store . '-' . $this->config->getWhitelist();
7073

71-
$key = $this->formatKey($store, $this->config->getWhitelist(), $jti, (string)$token);
72-
$this->setCache($tag, $key, (string)$token, $exp);
74+
$key = $this->makeKey($store, $this->config->getWhitelist(), $jti, $token);
75+
$this->setCache($tag, $key, $token, $ttl);
7376
}
7477

7578
protected function pushBlacklist(Token $token): void
7679
{
7780
$jti = $token->claims()->get('jti');
7881
$store = $token->claims()->get('store');
7982

80-
$exp = $token->claims()->get('exp') - time();
83+
$now = time();
84+
$exp = $token->claims()->get('exp');
85+
$ttl = $exp->getTimestamp() - $now;
8186
$tag = $store . '-' . $this->config->getBlacklist();
82-
$key = $this->formatKey($store, $this->config->getBlacklist(), $jti, (string)$token);
87+
$key = $this->makeKey($store, $this->config->getBlacklist(), $jti, $token);
8388

84-
$this->setCache($tag, $key, (string)$token, $exp);
89+
$this->setCache($tag, $key, $token, $ttl);
8590
}
8691

8792
public function logout(Token $token): void
@@ -91,15 +96,14 @@ public function logout(Token $token): void
9196

9297
public function wasBan(Token $token): bool
9398
{
94-
$jti = $token->claims()->get('jti');
95-
$store = $token->claims()->get('store');
96-
97-
return $this->getBlacklist($store, $jti, $token) === $token->toString();
99+
return $this->getBlacklist($token) === $token->toString();
98100
}
99101

100-
protected function getBlacklist(string $store, string $jti, Token $token)
102+
protected function getBlacklist(Token $token)
101103
{
102-
return $this->getCache($store, $this->config->getBlacklist(), $jti, $token->toString());
104+
$jti = $token->claims()->get('jti');
105+
$store = $token->claims()->get('store');
106+
return $this->getCache($store, $this->config->getBlacklist(), $jti, $token);
103107
}
104108

105109
public function destroyStoreWhitelist($store): void
@@ -112,27 +116,44 @@ public function destroyStoreBlacklist($store): void
112116
$this->clearStoreBlacklist($store);
113117
}
114118

119+
protected function decodeFileCache($filename)
120+
{
121+
$content = @file_get_contents($filename);
122+
if (false !== $content) {
123+
$expire = (int) substr($content, 8, 12);
124+
125+
$content = substr($content, 32);
126+
return is_string($content) ? ['content' => $content, 'expire' => $expire] : null;
127+
}
128+
}
129+
115130
public function destroyToken($id, $store): void
116131
{
117132
$type = $this->config->getWhitelist();
118133
$tag = $store . '-' . $type;
119-
120-
$rule = implode(':', [$this->config->getPrefix(), $store, $type, $id]);
121134
$keys = $this->app->cache->getTagItems($tag);
122-
$parser = new Parser();
123135

124136
foreach ($keys as $key) {
125-
if (false !== strpos($key, $rule)) {
126-
$value = $this->app->cache->get($key);
137+
$handle = strtolower($this->app->config->get('cache.default'));
138+
if ($handle == 'file') {
139+
$token = unserialize($this->decodeFileCache($key)['content']);
140+
} else if ($handle == 'redis') {
141+
}
127142

128-
if ($value) {
129-
$token = $parser->parse($value);
130-
$this->pushBlacklist($token);
131-
}
143+
$token = $this->app->get('jwt.token')->parse($token);
144+
if ($token->claims()->has('jti') && $token->claims()->get('jti') == $id) {
145+
$this->pushBlacklist($token);
132146
}
133147
}
134148
}
135149

150+
private function makeKey($store, $type, $uid, Token $token): string
151+
{
152+
$key = implode(':', [$this->config->getPrefix(), $store, $type, $uid, md5($token->toString())]);
153+
154+
return $key;
155+
}
156+
136157
protected function clearStoreWhitelist($store): void
137158
{
138159
$this->clearTag($store . '-' . $this->config->getWhitelist());
@@ -148,29 +169,14 @@ private function clearTag($tag): void
148169
$this->app->cache->tag($tag)->clear();
149170
}
150171

151-
private function setCache($tag, $key, $value, $exp): void
172+
private function setCache($tag, $key, Token $token, $exp): void
152173
{
153-
$this->app->cache->tag($tag)->set($key, $value, $exp);
154-
}
155-
156-
private function formatKey($store, $type, $uid, $value): string
157-
{
158-
$key = implode(':', [$this->config->getPrefix(), $store, $type, $uid, md5($value)]);
159-
160-
return $key;
161-
}
162-
163-
private function clearCache($store, $type, $uid): void
164-
{
165-
$key = $this->formatKey($store, $type, $uid);
166-
167-
$this->app->cache->delete($key);
174+
$this->app->cache->tag($tag)->set($key, $token->toString(), $exp);
168175
}
169176

170177
private function getCache($store, $type, $jti, $token)
171178
{
172-
$key = implode(':', [$this->config->getPrefix(), $store, $type, $jti, md5($token)]);
173-
179+
$key = $this->makeKey($store, $type, $jti, $token);
174180
return $this->app->cache->get($key);
175181
}
176182
}

src/Service/Token.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818
use Lcobucci\Clock\SystemClock;
1919
use xiaodi\JWTAuth\Exception\JWTException;
2020

21-
/**
22-
*
23-
* @method JwtToken make()
24-
* @method bool verify()
25-
*/
2621
class Token
2722
{
2823
/**
@@ -146,6 +141,11 @@ public function validate(string $token)
146141
return $this->jwtConfiguration->validator()->validate($token, ...$constraints);
147142
}
148143

144+
public function login(JwtToken $token)
145+
{
146+
$this->app->get('jwt.manange')->login($token);
147+
}
148+
149149
public function logout(?string $token): void
150150
{
151151
$token = $token ?: $this->getRequestToken();

0 commit comments

Comments
 (0)