@@ -157,27 +157,48 @@ public function getPublicKey()
157157 /**
158158 * Verify and decode a JWT token
159159 * @param string $token the encoded JWT token
160+ * @param bool $checkRevoked
160161 * @return \stdClass the payload data of JWT token
161162 */
162- public function verifyAndDecodeToken ($ token )
163+ public function verifyAndDecodeToken ($ token, $ checkRevoked = true )
163164 {
164165 $ payload = JWT ::decode ($ token , $ this ->getPublicKey (), [self ::CRYPTO_ALG ]);
165- if (!empty ($ payload ) && property_exists ($ payload ,'sub ' ))
166- if ($ this ->checkRevokedSub ($ payload ->sub ))
167- throw new TokenRevokedException ('Token is revoked. ' );
166+ if ($ checkRevoked && $ this ->checkRevokedSub ($ payload ))
167+ throw new TokenRevokedException ('Token is revoked. ' );
168168 return $ payload ;
169169 }
170170
171- public function checkRevokedSub ($ sub )
171+ /**
172+ * Check if token is revoked
173+ * @param \stdClass $payload the token's payload
174+ * @return bool true if the token is revoked
175+ */
176+ public function checkRevokedSub ($ payload )
172177 {
173- if (Yii::$ app ->cache )
178+ if (! empty ( $ payload ) && property_exists ( $ payload , ' sub ' ) && Yii::$ app ->cache )
174179 {
175- $ cacheKey = "Revoked_JWT_ " .$ sub ;
180+ $ cacheKey = "Revoked_JWT_ " .sha1 ( $ payload -> sub ) ;
176181 return Yii::$ app ->cache ->get ($ cacheKey ) !== false ;
177182 }
178183 return false ;
179184 }
180185
186+ /**
187+ * Save revoked token to cache
188+ * @param \stdClass $payload the token's payload
189+ */
190+ public function saveRevokedToken ($ payload )
191+ {
192+ if (!empty ($ payload ) && property_exists ($ payload , 'sub ' ) && property_exists ($ payload ,'exp ' ) && Yii::$ app ->cache )
193+ {
194+ $ cacheKey = "Revoked_JWT_ " .sha1 ($ payload ->sub );
195+ $ duration = time () + JWT ::$ leeway - $ payload ->exp ;
196+
197+ if ($ duration > 0 )
198+ Yii::$ app ->cache ->set ($ cacheKey , true , $ duration );
199+ }
200+ }
201+
181202 /**
182203 * Logout the current user by identity
183204 * @param bool $globalLogout
0 commit comments