Skip to content

Commit 6c11338

Browse files
author
Bui Sy Nguyen
committed
Improve OAuth2 class
1 parent ef91314 commit 6c11338

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

fproject/authclient/OAuth2.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function getPublicKey()
163163
public function verifyAndDecodeToken($token, $checkRevoked=true)
164164
{
165165
$payload = JWT::decode($token, $this->getPublicKey(), [self::CRYPTO_ALG]);
166-
if($checkRevoked && $this->checkRevokedSub($payload))
166+
if($checkRevoked && $this->checkRevokedToken($token, $payload))
167167
throw new TokenRevokedException('Token is revoked.');
168168
return $payload;
169169
}
@@ -184,35 +184,40 @@ public function getUserInfo($accessToken=null)
184184

185185
/**
186186
* Check if token is revoked
187+
* @param string $token the JWT token
187188
* @param \stdClass $payload the token's payload
188189
* @return bool true if the token is revoked
189190
*/
190-
public function checkRevokedSub($payload)
191+
public function checkRevokedToken($token, $payload)
191192
{
192-
if(!empty($payload) && property_exists($payload, 'sub') && Yii::$app->cache)
193+
if(!empty($payload) && Yii::$app->cache)
193194
{
194-
$cacheKey = "Revoked_JWT_".sha1($payload->sub);
195-
return Yii::$app->cache->get($cacheKey) !== false;
195+
return Yii::$app->cache->get($this->getRevokedTokenCacheKey($token)) !== false;
196196
}
197197
return false;
198198
}
199199

200200
/**
201201
* Save revoked token to cache
202+
* @param string $token the JWT token
202203
* @param \stdClass $payload the token's payload
203204
*/
204-
public function saveRevokedToken($payload)
205+
public function saveRevokedToken($token, $payload)
205206
{
206-
if(!empty($payload) && property_exists($payload, 'sub') && property_exists($payload,'exp') && Yii::$app->cache)
207+
if(!empty($payload) && property_exists($payload,'exp') && Yii::$app->cache)
207208
{
208-
$cacheKey = "Revoked_JWT_".sha1($payload->sub);
209209
$duration = (int)$payload->exp + JWT::$leeway - time();
210210

211211
if($duration > 0)
212-
Yii::$app->cache->set($cacheKey, true, $duration);
212+
Yii::$app->cache->set($this->getRevokedTokenCacheKey($token), true, $duration);
213213
}
214214
}
215215

216+
private function getRevokedTokenCacheKey($token)
217+
{
218+
return "Revoked_JWT_".sha1($token);
219+
}
220+
216221
/**
217222
* Logout the current user by identity
218223
* @param bool $globalLogout

0 commit comments

Comments
 (0)