|
6 | 6 |
|
7 | 7 | use Lcobucci\JWT\Token; |
8 | 8 | use think\App; |
9 | | -use think\Container; |
10 | 9 | use xiaodi\JWTAuth\Config\Manager as Config; |
11 | | -use xiaodi\JWTAuth\Exception\JWTException; |
12 | 10 |
|
13 | 11 | class Manager |
14 | 12 | { |
@@ -37,42 +35,106 @@ protected function resloveConfig() |
37 | 35 | $this->config = new Config($options); |
38 | 36 | } |
39 | 37 |
|
40 | | - public function login(Token $token) |
| 38 | + public function login(Token $token): void |
41 | 39 | { |
42 | 40 | $jti = $token->getClaim('jti'); |
43 | 41 | $store = $token->getClaim('store'); |
44 | 42 |
|
45 | 43 | $exp = $token->getClaim('exp') - time(); |
46 | 44 |
|
47 | 45 | if ($this->app->get('jwt.sso')->getEnable()) { |
48 | | - $this->pushBlacklist($store, $jti, (string) $token, $exp); |
| 46 | + $this->handleSSO($store, $jti, (string) $token, $exp); |
49 | 47 | } |
50 | | - |
| 48 | + |
51 | 49 | $this->pushWhitelist($store, $jti, (string) $token, $exp); |
52 | 50 | } |
53 | 51 |
|
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 |
55 | 106 | { |
56 | | - $this->setCache($store, 'whitelist', $jti, $value, $exp); |
| 107 | + $this->app->cache->tag($tag)->clear(); |
57 | 108 | } |
58 | 109 |
|
59 | | - protected function pushBlacklist($store, $jti, string $value, $exp) |
| 110 | + private function setCache($store, $type, $uid, $value, $exp): void |
60 | 111 | { |
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); |
62 | 115 | } |
63 | 116 |
|
64 | | - private function setCache($store, $type, $uid, $value, $exp) |
| 117 | + protected function formatWhitelist($store, $uid): string |
65 | 118 | { |
66 | | - $key = implode(':', ['jwt', $store, $type, $uid]); |
67 | | - $this->app->cache->set($key, $value, $exp); |
| 119 | + return $this->formatKey($store, $this->config->getWhitelist(), $uid); |
68 | 120 | } |
69 | 121 |
|
70 | | - public function logout() |
71 | | - {} |
| 122 | + protected function formatBlacklist($store, $uid): string |
| 123 | + { |
| 124 | + return $this->formatKey($store, $this->config->getBlacklist(), $uid); |
| 125 | + } |
72 | 126 |
|
73 | | - public function destroyStore($store) |
74 | | - {} |
| 127 | + private function formatKey($store, $type, $uid): string |
| 128 | + { |
| 129 | + $key = implode(':', [$this->config->getPrefix(), $store, $type, $uid]); |
75 | 130 |
|
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 | + } |
78 | 140 | } |
0 commit comments