Skip to content

Commit c384b99

Browse files
author
Bui Sy Nguyen
committed
Improve code
1 parent 9173811 commit c384b99

File tree

6 files changed

+69
-21
lines changed

6 files changed

+69
-21
lines changed

fproject/authclient/AuthLogoutAction.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
namespace fproject\authclient;
2121
use Yii;
22-
use yii\authclient\Collection;
2322

2423
class AuthLogoutAction extends AuthLogoutActionBase
2524
{

fproject/authclient/AuthRevokeAction.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
namespace fproject\authclient;
2121
use Yii;
22-
use yii\authclient\Collection;
2322

2423
class AuthRevokeAction extends AuthLogoutActionBase
2524
{

fproject/authclient/OAuth2.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
use Firebase\JWT\JWK;
2323
use Firebase\JWT\JWT;
24+
use fproject\web\User;
2425
use fproject\web\UserIdentity;
26+
use yii\authclient\Collection;
2527
use yii\helpers\Json;
2628
use 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
}

fproject/authclient/OAuthToken.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
namespace fproject\authclient;
2121

22-
use Firebase\JWT\JWK;
2322
use Firebase\JWT\JWT;
2423
use Yii;
2524

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Bui
5+
* Date: 10/2/2015
6+
* Time: 1:21 AM
7+
*/
8+
9+
namespace fproject\authclient;
10+
11+
12+
class TokenRevokedException extends \UnexpectedValueException
13+
{
14+
15+
}

fproject/web/UserIdentity.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use fproject\authclient\OAuth2;
2323
use fproject\authclient\OAuthTokenPayload;
2424
use Yii;
25-
use yii\authclient\Collection;
2625
use yii\web\IdentityInterface;
2726

2827
/**
@@ -134,22 +133,13 @@ public static function findIdentity($id)
134133
*/
135134
public static function findIdentityByAccessToken($token, $type = null)
136135
{
137-
/** @var User $user */
138-
$user = Yii::$app->user;
139-
if(isset($user->authClientConfig) && isset($user->authClientConfig['collection']) && isset($user->authClientConfig['id']))
136+
if(OAuth2::getInstance())
140137
{
141-
/** @var Collection $collection */
142-
$collection = Yii::$app->get($user->authClientConfig['collection']);
143-
if($collection->hasClient($user->authClientConfig['id']))
138+
$rawPayload = OAuth2::getInstance()->verifyAndDecodeToken($token);
139+
if(!empty($rawPayload))
144140
{
145-
/** @var OAuth2 $client */
146-
$client = $collection->getClient($user->authClientConfig['id']);
147-
$rawPayload = $client->verifyAndDecodeToken($token);
148-
if(!empty($rawPayload))
149-
{
150-
$payload = new OAuthTokenPayload($rawPayload);
151-
return new UserIdentity((array)$payload);
152-
}
141+
$payload = new OAuthTokenPayload($rawPayload);
142+
return new UserIdentity((array)$payload);
153143
}
154144
}
155145
return null;

0 commit comments

Comments
 (0)