Skip to content

Commit 4cc0376

Browse files
committed
update
1 parent 0097119 commit 4cc0376

3 files changed

Lines changed: 57 additions & 45 deletions

File tree

src/Service/Jwt.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,9 @@ public function logout(?string $token = null)
121121
{
122122
return $this->app->get('jwt.token')->logout($token);
123123
}
124+
125+
public function destroyToken($jti, $store)
126+
{
127+
return $this->app->get('jwt.manager')->destroyToken($jti, $store);
128+
}
124129
}

src/Service/Manager.php

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace xiaodi\JWTAuth\Service;
66

77
use Lcobucci\JWT\Token;
8+
use Lcobucci\JWT\Parser;
89
use think\App;
910
use xiaodi\JWTAuth\Config\Manager as Config;
1011

@@ -37,57 +38,61 @@ protected function resloveConfig()
3738

3839
public function login(Token $token): void
3940
{
40-
$jti = $token->getClaim('jti');
41-
$store = $token->getClaim('store');
42-
43-
$exp = $token->getClaim('exp') - time();
44-
4541
if ($this->app->get('jwt.sso')->getEnable()) {
46-
$this->handleSSO($store, $jti, (string) $token, $exp);
42+
$this->handleSSO($token);
4743
}
4844

49-
$this->pushWhitelist($store, $jti, (string) $token, $exp);
45+
$this->pushWhitelist($token);
5046
}
5147

52-
protected function handleSSO($store, $jti, $token, $exp)
48+
protected function handleSSO(Token $token): void
5349
{
54-
$key = $this->formatWhiteKey($store, $jti);
55-
if ($this->app->cache->has($key)) {
56-
$this->clearCache($store, $this->config->getWhitelist(), $jti);
57-
$this->pushBlacklist($store, $jti, (string) $token, $exp);
58-
}
59-
}
50+
$jti = $token->getClaim('jti');
51+
$store = $token->getClaim('store');
52+
$exp = $token->getClaim('exp') - time();
6053

61-
protected function pushWhitelist($store, $jti, string $value, $exp): void
62-
{
63-
$this->setCache($store, $this->config->getWhitelist(), $jti, $value, $exp);
54+
$this->destroyToken($jti, $store);
6455
}
6556

66-
protected function pushBlacklist($store, $jti, string $value, $exp): void
57+
protected function pushWhitelist(Token $token): void
6758
{
68-
$this->setCache($store, $this->config->getBlacklist(), $jti, $value, $exp);
59+
$jti = $token->getClaim('jti');
60+
$store = $token->getClaim('store');
61+
$exp = $token->getClaim('exp') - time();
62+
$tag = $store .'-' . $this->config->getWhitelist();
63+
64+
$key = $this->formatKey($store, $this->config->getWhitelist(), $jti, (string)$token);
65+
$this->setCache($tag, $key, (string)$token, $exp);
6966
}
7067

71-
public function logout(Token $token): void
68+
protected function pushBlacklist(Token $token): void
7269
{
7370
$jti = $token->getClaim('jti');
7471
$store = $token->getClaim('store');
7572

7673
$exp = $token->getClaim('exp') - time();
77-
$this->pushBlacklist($store, $jti, (string) $token, $exp);
74+
$tag = $store .'-' . $this->config->getBlacklist();
75+
$key = $this->formatKey($store, $this->config->getBlacklist(), $jti, (string)$token);
76+
77+
$this->setCache($tag, $key, (string)$token, $exp);
78+
}
79+
80+
public function logout(Token $token): void
81+
{
82+
$this->pushBlacklist($token);
7883
}
7984

8085
public function wasBan(Token $token): bool
8186
{
8287
$jti = $token->getClaim('jti');
8388
$store = $token->getClaim('store');
8489

85-
return $this->getBlacklist($store, $jti) ? true : false;
90+
return $this->getBlacklist($store, $jti, (string)$token) === (string) $token ? true : false;
8691
}
8792

88-
protected function getBlacklist($store, $jti)
93+
protected function getBlacklist(string $store, string $jti, string $token)
8994
{
90-
return $this->getCache($store, $jti, $this->config->getBlacklist());
95+
return $this->getCache($store, $this->config->getBlacklist(), $jti, $token);
9196
}
9297

9398
public function destroyStoreWhitelist($store): void
@@ -102,7 +107,22 @@ public function destroyStoreBlacklist($store): void
102107

103108
public function destroyToken($id, $store): void
104109
{
105-
$this->clearCache($store, $this->config->getWhitelist(), $id);
110+
$type = $this->config->getWhitelist();
111+
$tag = $store .'-' . $type;
112+
113+
$rule = implode(':', [$this->config->getPrefix(), $store, $type, $id]);
114+
$keys = $this->app->cache->getTagItems($tag);
115+
116+
$parser = new Parser();
117+
118+
foreach($keys as $key) {
119+
if (false !== strpos($key, $rule)) {
120+
$value = $this->app->cache->get($key);
121+
$token = $parser->parse($value);
122+
123+
$this->pushBlacklist($token);
124+
}
125+
}
106126
}
107127

108128
protected function clearStoreWhitelist($store): void
@@ -120,26 +140,14 @@ private function clearTag($tag): void
120140
$this->app->cache->tag($tag)->clear();
121141
}
122142

123-
private function setCache($store, $type, $uid, $value, $exp): void
124-
{
125-
$key = $this->formatKey($store, $type, $uid);
126-
127-
$this->app->cache->tag($store . '-' . $type)->set($key, $value, $exp);
128-
}
129-
130-
protected function formatWhitelist($store, $uid): string
131-
{
132-
return $this->formatKey($store, $this->config->getWhitelist(), $uid);
133-
}
134-
135-
protected function formatBlacklist($store, $uid): string
143+
private function setCache($tag, $key, $value, $exp): void
136144
{
137-
return $this->formatKey($store, $this->config->getBlacklist(), $uid);
145+
$this->app->cache->tag($tag)->set($key, $value, $exp);
138146
}
139147

140-
private function formatKey($store, $type, $uid): string
148+
private function formatKey($store, $type, $uid, $value): string
141149
{
142-
$key = implode(':', [$this->config->getPrefix(), $store, $type, $uid]);
150+
$key = implode(':', [$this->config->getPrefix(), $store, $type, $uid, md5($value)]);
143151

144152
return $key;
145153
}
@@ -151,9 +159,9 @@ private function clearCache($store, $type, $uid): void
151159
$this->app->cache->delete($key);
152160
}
153161

154-
private function getCache($store, $uid, $type)
162+
private function getCache($store, $type, $jti, $token)
155163
{
156-
$key = implode(':', [$this->config->getPrefix(), $store, $type, $uid]);
164+
$key = implode(':', [$this->config->getPrefix(), $store, $type, $jti, md5($token)]);
157165

158166
return $this->app->cache->get($key);
159167
}

src/Service/Token.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,12 @@ public function verify(string $token): ?bool
181181
if ($this->config->getAutomaticRenewal()) {
182182
$this->token = $this->automaticRenewalToken($this->token);
183183
} else {
184-
throw new TokenAlreadyEexpired('Token 已过期,请重新刷新', $this->config->getReloginCode());
184+
throw new TokenAlreadyEexpired('Token 已过期,请重新刷新', $this->config->getRefreshCode());
185185
}
186186
} else {
187187
throw new TokenAlreadyEexpired('Token 刷新时间已过,请重新登录', $this->config->getReloginCode());
188188
}
189189
} else {
190-
dump($this->app->get('jwt.manager')->wasBan($this->token));
191190
// 是否存在黑名单
192191
if (true === $this->app->get('jwt.manager')->wasBan($this->token)) {
193192
throw new TokenAlreadyEexpired('Token 已被禁用,请重新登录', $this->config->getReloginCode());

0 commit comments

Comments
 (0)