@@ -33,6 +33,12 @@ class Token
3333 */
3434 protected $ claims ;
3535
36+ /**
37+ *
38+ * @var JwtToken
39+ */
40+ protected $ token ;
41+
3642 public function __construct (App $ app )
3743 {
3844 $ this ->app = $ app ;
@@ -141,27 +147,37 @@ protected function parseToken(string $token): JwtToken
141147 return $ token ;
142148 }
143149
150+ /**
151+ * 验证成功的Token
152+ *
153+ * @return JWTToken
154+ */
155+ public function getToken (): ?JwtToken
156+ {
157+ return $ this ->token ;
158+ }
159+
144160 public function verify (string $ token ): ?bool
145161 {
146- $ token = $ this ->parseToken ($ token );
162+ $ this -> token = $ this ->parseToken ($ token );
147163
148- if (false === $ token ->verify ($ this ->config ->getSigner (), $ this ->config ->makeSignerKey ())) {
164+ if (false === $ this -> token ->verify ($ this ->config ->getSigner (), $ this ->config ->makeSignerKey ())) {
149165 throw new JWTException ('此 Token 与 密钥不匹配 ' , $ this ->config ->getReloginCode ());
150166 }
151167
152168 // Token 是否已可用
153169 $ now = time ();
154- $ exp = $ token ->getClaim ('nbf ' );
170+ $ exp = $ this -> token ->getClaim ('nbf ' );
155171 if ($ now < $ exp ) {
156172 throw new JWTException ('此 Token 暂未可用 ' , 500 );
157173 }
158174
159175 // 是否已过期
160- if (true === $ token ->isExpired ()) {
161- if ($ now <= $ token ->getClaim ('refreshAt ' )) {
176+ if (true === $ this -> token ->isExpired ()) {
177+ if ($ now <= $ this -> token ->getClaim ('refreshAt ' )) {
162178 // 是否开启自动续签
163179 if ($ this ->config ->getAutomaticRenewal ()) {
164- $ token = $ this ->automaticRenewalToken ($ token );
180+ $ this -> token = $ this ->automaticRenewalToken ($ this -> token );
165181 } else {
166182 throw new TokenAlreadyEexpired ('Token 已过期,请重新刷新 ' , $ this ->config ->getReloginCode ());
167183 }
@@ -172,12 +188,12 @@ public function verify(string $token): ?bool
172188
173189 $ data = new ValidationData ();
174190
175- $ jwt_id = $ token ->getHeader ('jti ' );
191+ $ jwt_id = $ this -> token ->getHeader ('jti ' );
176192 $ data ->setIssuer ($ this ->config ->getIss ());
177193 $ data ->setAudience ($ this ->config ->getAud ());
178194 $ data ->setId ($ jwt_id );
179195
180- if (!$ token ->validate ($ data )) {
196+ if (!$ this -> token ->validate ($ data )) {
181197 throw new JWTException ('此 Token 效验不通过 ' , $ this ->config ->getReloginCode ());
182198 }
183199
0 commit comments