Skip to content

Commit c0d64b6

Browse files
committed
update
1 parent 9cc143d commit c0d64b6

2 files changed

Lines changed: 85 additions & 18 deletions

File tree

src/Service/Jwt.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,9 @@ public function verify(string $token): bool
8181
{
8282
return $this->app->get('jwt.token')->verify($token);
8383
}
84+
85+
public function destroyStoreWhitelist($store)
86+
{
87+
return $this->app->get('jwt.manager')->destroyStoreWhitelist($store);
88+
}
8489
}

src/Service/Manager.php

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
use Lcobucci\JWT\Token;
88
use think\App;
9-
use think\Container;
109
use xiaodi\JWTAuth\Config\Manager as Config;
11-
use xiaodi\JWTAuth\Exception\JWTException;
1210

1311
class Manager
1412
{
@@ -37,42 +35,106 @@ protected function resloveConfig()
3735
$this->config = new Config($options);
3836
}
3937

40-
public function login(Token $token)
38+
public function login(Token $token): void
4139
{
4240
$jti = $token->getClaim('jti');
4341
$store = $token->getClaim('store');
4442

4543
$exp = $token->getClaim('exp') - time();
4644

4745
if ($this->app->get('jwt.sso')->getEnable()) {
48-
$this->pushBlacklist($store, $jti, (string) $token, $exp);
46+
$this->handleSSO($store, $jti, (string) $token, $exp);
4947
}
50-
48+
5149
$this->pushWhitelist($store, $jti, (string) $token, $exp);
5250
}
5351

54-
protected function pushWhitelist($store, $jti, string $value, $exp)
52+
protected function handleSSO($store, $jti, $token, $exp)
53+
{
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+
}
60+
61+
protected function pushWhitelist($store, $jti, string $value, $exp): void
62+
{
63+
$this->setCache($store, $this->config->getWhitelist(), $jti, $value, $exp);
64+
}
65+
66+
protected function pushBlacklist($store, $jti, string $value, $exp): void
67+
{
68+
$this->setCache($store, $this->config->getBlacklist(), $jti, $value, $exp);
69+
}
70+
71+
public function logout(Token $token): void
72+
{
73+
$jti = $token->getClaim('jti');
74+
$store = $token->getClaim('store');
75+
76+
$exp = $token->getClaim('exp') - time();
77+
$this->pushBlacklist($store, $jti, (string) $token, $exp);
78+
}
79+
80+
public function destroyStoreWhitelist($store): void
81+
{
82+
$this->clearStoreWhitelist($store);
83+
}
84+
85+
public function destroyStoreBlacklist($store): void
86+
{
87+
$this->clearStoreBlacklist($store);
88+
}
89+
90+
public function destroyToken($id, $store): void
91+
{
92+
$this->clearCache($store, $this->config->getWhitelist(), $id);
93+
}
94+
95+
protected function clearStoreWhitelist($store): void
96+
{
97+
$this->clearTag($store . '-' . $this->config->getWhitelist());
98+
}
99+
100+
protected function clearStoreBlacklist($store): void
101+
{
102+
$this->clearTag($store . '-' . $this->config->getBlacklist());
103+
}
104+
105+
private function clearTag($tag): void
55106
{
56-
$this->setCache($store, 'whitelist', $jti, $value, $exp);
107+
$this->app->cache->tag($tag)->clear();
57108
}
58109

59-
protected function pushBlacklist($store, $jti, string $value, $exp)
110+
private function setCache($store, $type, $uid, $value, $exp): void
60111
{
61-
$this->setCache($store, 'blacklist', $jti, $value, $exp);
112+
$key = $this->formatKey($store, $type, $uid);
113+
114+
$this->app->cache->tag($store . '-' . $type)->set($key, $value, $exp);
62115
}
63116

64-
private function setCache($store, $type, $uid, $value, $exp)
117+
protected function formatWhitelist($store, $uid): string
65118
{
66-
$key = implode(':', ['jwt', $store, $type, $uid]);
67-
$this->app->cache->set($key, $value, $exp);
119+
return $this->formatKey($store, $this->config->getWhitelist(), $uid);
68120
}
69121

70-
public function logout()
71-
{}
122+
protected function formatBlacklist($store, $uid): string
123+
{
124+
return $this->formatKey($store, $this->config->getBlacklist(), $uid);
125+
}
72126

73-
public function destroyStore($store)
74-
{}
127+
private function formatKey($store, $type, $uid): string
128+
{
129+
$key = implode(':', [$this->config->getPrefix(), $store, $type, $uid]);
75130

76-
public function destroyToken($id)
77-
{}
131+
return $key;
132+
}
133+
134+
private function clearCache($store, $type, $uid): void
135+
{
136+
$key = $this->formatKey($store, $type, $uid);
137+
138+
$this->app->cache->delete($key);
139+
}
78140
}

0 commit comments

Comments
 (0)