55namespace xiaodi \JWTAuth \Service ;
66
77use Lcobucci \JWT \Token ;
8+ use Lcobucci \JWT \Parser ;
89use think \App ;
910use 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 }
0 commit comments