2121
2222use Firebase \JWT \JWK ;
2323use Firebase \JWT \JWT ;
24+ use fproject \web \User ;
2425use fproject \web \UserIdentity ;
26+ use yii \authclient \Collection ;
2527use yii \helpers \Json ;
2628use Yii ;
2729
@@ -139,13 +141,15 @@ public function getPublicKey()
139141 $ jwk = Yii::$ app ->cache ->get ($ cacheKey );
140142 }
141143
142- if (! empty ($ jwk ))
144+ if (empty ($ jwk ))
143145 {
144146 $ jwk = $ this ->sendRequest ('GET ' , $ this ->jwkUrl );
145147 if (!empty ($ jwk ) && Yii::$ app ->cache )
146148 Yii::$ app ->cache ->set ($ cacheKey , $ jwk , self ::PUBLIC_KEY_EXPIRE_DURATION );
147- $ this ->_publicKey = JWK ::parseKeySet ($ jwk );
148149 }
150+
151+ if (!empty ($ jwk ))
152+ $ this ->_publicKey = JWK ::parseKeySet ($ jwk );
149153 }
150154 return $ this ->_publicKey ;
151155 }
@@ -157,7 +161,21 @@ public function getPublicKey()
157161 */
158162 public function verifyAndDecodeToken ($ token )
159163 {
160- return JWT ::decode ($ token , $ this ->getPublicKey (), [self ::CRYPTO_ALG ]);
164+ $ 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. ' );
168+ return $ payload ;
169+ }
170+
171+ public function checkRevokedSub ($ sub )
172+ {
173+ if (Yii::$ app ->cache )
174+ {
175+ $ cacheKey = "Revoked_JWT_ " .$ sub ;
176+ return Yii::$ app ->cache ->get ($ cacheKey ) !== false ;
177+ }
178+ return false ;
161179 }
162180
163181 /**
@@ -185,4 +203,32 @@ public function logout($globalLogout=true)
185203 }
186204 return true ;
187205 }
206+
207+ /** @var OAuth2 $_instance Singleton instance */
208+ private static $ _instance ;
209+
210+ /**
211+ * Get singleton instance using Yii's auth client configuration
212+ * @return null|OAuth2
213+ * @throws \yii\base\InvalidConfigException
214+ */
215+ public static function getInstance ()
216+ {
217+ if (!isset (self ::$ _instance ))
218+ {
219+ /** @var User $user */
220+ $ user = Yii::$ app ->user ;
221+ if (isset ($ user ->authClientConfig ) && isset ($ user ->authClientConfig ['collection ' ]) && isset ($ user ->authClientConfig ['id ' ]))
222+ {
223+ /** @var Collection $collection */
224+ $ collection = Yii::$ app ->get ($ user ->authClientConfig ['collection ' ]);
225+ if ($ collection ->hasClient ($ user ->authClientConfig ['id ' ]))
226+ {
227+ self ::$ _instance = $ collection ->getClient ($ user ->authClientConfig ['id ' ]);
228+ }
229+ }
230+ }
231+
232+ return self ::$ _instance ;
233+ }
188234}
0 commit comments