44
55namespace xiaodi \JWTAuth \Service ;
66
7- use Lcobucci \JWT \Token ;
8- use Lcobucci \JWT \Parser ;
97use think \App ;
8+ use Lcobucci \JWT \Token ;
109use xiaodi \JWTAuth \Config \Manager as Config ;
1110
1211class 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}
0 commit comments