Skip to content

Commit 1ed351f

Browse files
author
Bui Sy Nguyen
committed
[Done] Implement AuthRevokeAction
1 parent c384b99 commit 1ed351f

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

fproject/authclient/AuthRevokeAction.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ class AuthRevokeAction extends AuthLogoutActionBase
2525
/**
2626
* @inheritdoc
2727
*/
28-
public function run()
28+
public function run($token)
2929
{
30-
30+
$authClient = OAuth2::getInstance();
31+
if($authClient)
32+
{
33+
$payload = $authClient->verifyAndDecodeToken($token, false);
34+
$authClient->saveRevokedToken($payload);
35+
}
3136
}
3237

3338
}

fproject/authclient/OAuth2.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

fproject/web/UserIdentity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static function findIdentityByAccessToken($token, $type = null)
136136
if(OAuth2::getInstance())
137137
{
138138
$rawPayload = OAuth2::getInstance()->verifyAndDecodeToken($token);
139-
if(!empty($rawPayload))
139+
if(!empty($rawPayload) && property_exists($rawPayload, 'sub'))
140140
{
141141
$payload = new OAuthTokenPayload($rawPayload);
142142
return new UserIdentity((array)$payload);

0 commit comments

Comments
 (0)