1717
1818class Jwt
1919{
20- /**
21- * @var User
22- */
20+ use \xiaodi \JWTAuth \Traits \Jwt;
21+
22+ private $ config ;
23+
24+ private $ store ;
25+
2326 private $ user ;
2427
25- /**
26- * @var Token
27- */
2828 private $ token ;
2929
30- /**
31- * @var Manager
32- */
33- private $ manager ;
30+ public function __construct (App $ app , $ store = null )
31+ {
32+ $ this ->app = $ app ;
33+
34+ if ($ store === null ) {
35+ $ store = $ this ->getDefaultStore ();
36+ }
3437
35- use \xiaodi \JWTAuth \Traits \Jwt;
38+ $ this ->store = $ store ;
39+ $ this ->make ();
40+ }
3641
37- public function __construct ( App $ app , Manager $ manager , User $ user )
42+ public function store ( string $ name = '' )
3843 {
39- $ this ->app = $ app ;
40- $ this ->manager = $ manager ;
41- $ this ->user = $ user ;
44+ $ jwt = app ('jwt ' , ['store ' => $ name ], true );
45+ $ this ->app ->bind ('jwt ' , $ jwt );
46+ return $ jwt ;
47+ }
4248
43- $ config = $ this ->getConfig ();
44- foreach ($ config as $ key => $ v ) {
45- $ this ->$ key = $ v ;
49+ protected function make ()
50+ {
51+ $ this ->setStoreConfig ();
52+
53+ return $ this ;
54+ }
55+
56+ public function getStore ()
57+ {
58+ return $ this ->store ;
59+ }
60+
61+ /**
62+ * 获取默认 app
63+ *
64+ * @return void
65+ */
66+ public function getDefaultStore ()
67+ {
68+ $ store = $ this ->app ->config ->get ("jwt.default " , '' );
69+ if (!$ store ) {
70+ throw new JWTException ('默认应用 未配置. ' , 500 );
4671 }
72+
73+ return $ store ;
4774 }
4875
4976 /**
50- * 获取jwt配置.
77+ * 获取 app jwt 配置
5178 *
52- * @return array
79+ * @return void
5380 */
54- public function getConfig (): array
81+ public function getStoreConfig ()
5582 {
56- return $ this ->app ->config ->get ('jwt.default ' , []);
83+ $ config = $ this ->app ->config ->get ("jwt.apps. {$ this ->store }.token " , []);
84+ if (empty ($ config )) {
85+ throw new JWTException ("{$ this ->store } 应用 未配置完整. " , 500 );
86+ }
87+
88+ return $ config ;
89+ }
90+
91+ protected function setStoreConfig ()
92+ {
93+ $ config = $ this ->getStoreConfig ();
94+ foreach ($ config as $ key => $ v ) {
95+ $ this ->$ key = $ v ;
96+ }
5797 }
5898
5999 /**
@@ -85,7 +125,7 @@ public function token(array $claims): Token
85125
86126 $ token = $ builder ->getToken ($ this ->getSigner (), $ this ->makeSignerKey ());
87127
88- $ this ->manager ->login ($ token );
128+ $ this ->app [ ' jwt. manager' ] ->login ($ token );
89129
90130 return $ token ;
91131 }
@@ -100,7 +140,7 @@ public function token(array $claims): Token
100140 private function makeTokenId (array $ claims ): string
101141 {
102142 if (empty ($ claims [$ this ->getUniqidKey ()])) {
103- throw new JWTException ('用户唯一值· uniqidKey· 未配置 ' , 500 );
143+ throw new JWTException ('uniqidKey 未配置 ' , 500 );
104144 }
105145
106146 return (string ) $ claims [$ this ->getUniqidKey ()];
@@ -113,7 +153,12 @@ private function makeTokenId(array $claims): string
113153 */
114154 public function user (): Model
115155 {
116- return $ this ->user ->get ();
156+ return $ this ->app ['jwt.user ' ]->get ();
157+ }
158+
159+ public function getToken ()
160+ {
161+ return $ this ->token ;
117162 }
118163
119164 /**
@@ -137,7 +182,7 @@ public function refresh(Token $token = null): Token
137182 unset($ claims ['aud ' ]);
138183
139184 // 加入黑名单
140- $ this ->manager ->refresh ($ token );
185+ $ this ->app [ ' jwt. manager' ] ->refresh ($ token );
141186
142187 return $ this ->token ($ claims );
143188 }
@@ -189,7 +234,7 @@ public function logout(Token $token = null)
189234 {
190235 $ token = $ token ?: $ this ->getRequestToken ();
191236
192- $ this ->manager ->logout ($ token );
237+ $ this ->app [ ' jwt. manager' ] ->logout ($ token );
193238 }
194239
195240 /**
@@ -252,7 +297,7 @@ protected function automaticRenewalToken(Token $token)
252297 protected function validateToken (Token $ token )
253298 {
254299 // 是否在黑名单
255- if ($ this ->manager ->hasBlacklist ($ token )) {
300+ if ($ this ->app [ ' jwt. manager' ] ->hasBlacklist ($ token )) {
256301 throw new TokenAlreadyEexpired ('此 Token 已注销,请重新登录 ' , $ this ->getReloginCode ());
257302 }
258303
0 commit comments