55namespace xiaodi \JWTAuth ;
66
77use Lcobucci \JWT \Token ;
8+ use Lcobucci \JWT \Parser ;
9+ use think \App ;
810
911class Manager
1012{
11- private $ blacklist ;
13+ protected $ store ;
14+ protected $ cache ;
1215
13- public function __construct (Blacklist $ blacklist )
16+ public function __construct (App $ app )
1417 {
15- $ this ->blacklist = $ blacklist ;
18+ $ this ->app = $ app ;
19+ $ this ->cache = $ this ->getDefaultCache ();
20+ }
21+
22+ /**
23+ * 获取 缓存驱动.
24+ *
25+ * @return void
26+ */
27+ protected function getDefaultCache ()
28+ {
29+ return $ this ->app ->cache ;
1630 }
1731
1832 /**
@@ -24,8 +38,96 @@ public function __construct(Blacklist $blacklist)
2438 */
2539 public function login (Token $ token )
2640 {
27- // TODO 但凡获取新token后 都把以前的注销(黑名单)
28- // $jti = $token->getClaim('jti');
41+ $ jti = $ token ->getClaim ('jti ' );
42+ $ store = $ token ->getClaim ('store ' );
43+
44+ if ($ jwt = $ this ->getUidToken ($ jti , $ store )) {
45+ $ oldToken = (new Parser )->parse ($ jwt );
46+ $ this ->addBlackList ($ oldToken );
47+ }
48+
49+ $ this ->addWhitelist ($ token );
50+ }
51+
52+ /**
53+ * 加入白名单.
54+ *
55+ * @param Token $token
56+ *
57+ * @return void
58+ */
59+ public function addWhitelist (Token $ token )
60+ {
61+ $ jti = $ token ->getClaim ('jti ' );
62+ $ store = $ token ->getClaim ('store ' );
63+ $ key = $ this ->getUidWhiteKey ($ jti , $ store );
64+ $ exp = $ token ->getClaim ('exp ' ) - time ();
65+
66+ $ this ->cache ->set ($ key , (string ) $ token , $ exp );
67+ $ this ->addWhiteStore ($ store , $ key );
68+ }
69+
70+ /**
71+ * 加入缓存用户已登录的应用
72+ *
73+ * @param [type] $store
74+ * @param [type] $value
75+ * @return void
76+ */
77+ protected function addWhiteStore ($ store , $ value )
78+ {
79+ $ key = 'jwt ' . ': ' . 'whitelist ' . ': ' . $ store ;
80+ $ this ->cache ->push ($ key , $ value );
81+ }
82+
83+ /**
84+ * 加入黑名单
85+ *
86+ * @return void
87+ */
88+ public function addBlackList (Token $ token )
89+ {
90+ $ jti = $ token ->getClaim ('jti ' );
91+ $ store = $ token ->getClaim ('store ' );
92+ $ key = $ this ->getUidBlackKey ($ jti , $ store );
93+
94+ $ exp = $ token ->getClaim ('exp ' ) - time ();
95+ $ key .= ': ' . md5 ((string )$ token );
96+ $ this ->cache ->set ($ key , (string ) $ token , $ exp );
97+ }
98+
99+ /**
100+ * 获取用户最新token
101+ *
102+ * @param [type] $jti
103+ * @return void
104+ */
105+ public function getUidToken ($ jti , $ store )
106+ {
107+ $ key = $ this ->getUidWhiteKey ($ jti , $ store );
108+ return $ this ->cache ->get ($ key );
109+ }
110+
111+ /**
112+ * 获取jti 白名单 key
113+ *
114+ * @param string $jti
115+ * @return string
116+ */
117+ public function getUidWhiteKey ($ jti , $ store )
118+ {
119+ return 'jwt ' . ': ' . 'whitelist ' . ': ' . $ store . ': ' . $ jti ;
120+ }
121+
122+ /**
123+ * 获取jti 黑名单 key
124+ *
125+ * @param [type] $jti
126+ * @return void
127+ */
128+ public function getUidBlackKey ($ jti , $ store )
129+ {
130+ return 'jwt ' . ': ' . 'blacklist ' . ': ' . $ store . ': ' . $ jti ;
29131 }
30132
31133 /**
@@ -37,7 +139,7 @@ public function login(Token $token)
37139 */
38140 public function logout (Token $ token )
39141 {
40- $ this ->blacklist -> add ($ token );
142+ $ this ->addBlackList ($ token );
41143 }
42144
43145 /**
@@ -49,7 +151,6 @@ public function logout(Token $token)
49151 */
50152 public function refresh (Token $ token )
51153 {
52- // 注销此Token
53154 $ this ->logout ($ token );
54155 }
55156
@@ -62,6 +163,37 @@ public function refresh(Token $token)
62163 */
63164 public function hasBlacklist (Token $ token )
64165 {
65- return $ this ->blacklist ->has ($ token );
166+ $ jti = $ token ->getClaim ('jti ' );
167+ $ store = $ token ->getClaim ('store ' );
168+ $ key = $ this ->getUidBlackKey ($ jti , $ store );
169+ $ key .= ': ' . md5 ((string )$ token );
170+ return $ this ->cache ->has ($ key );
171+ }
172+
173+ /**
174+ * 删除应用所有白名单内的Token
175+ *
176+ * @param [type] $store
177+ * @return void
178+ */
179+ public function resetStoreWhiteToken ($ store )
180+ {
181+ $ key = 'jwt ' . ': ' . 'whitelist ' . ': ' . $ store ;
182+
183+ $ keys = $ this ->cache ->get ($ key );
184+
185+ $ parse = new Parser ();
186+ if ($ keys ) {
187+ foreach ($ keys as $ item ) {
188+ $ token = $ this ->cache ->get ($ item );
189+ if ($ token ) {
190+ $ this ->cache ->delete ($ item );
191+ $ token = $ parse ->parse ($ token );
192+ $ store = $ token ->getClaim ('store ' );
193+ $ this ->addBlackList ($ token , $ store );
194+ }
195+ }
196+ $ this ->cache ->delete ($ key );
197+ }
66198 }
67199}
0 commit comments