mirror of
https://github.com/elyby/accounts.git
synced 2026-11-30 03:03:23 +05:30
Centralize access token verification
This commit is contained in:
13 files changed
+259
-167
No files matched your search
@@ -3,11 +3,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace api\modules\authserver\validators;
|
||||
|
||||
use api\components\Tokens\InvalidTokenException;
|
||||
use api\components\Tokens\TokenExpiredException;
|
||||
use api\components\Tokens\TokenReader;
|
||||
use api\modules\authserver\exceptions\ForbiddenOperationException;
|
||||
use Carbon\Carbon;
|
||||
use common\models\Account;
|
||||
use Exception;
|
||||
use Yii;
|
||||
use yii\validators\Validator;
|
||||
|
||||
@@ -26,17 +26,11 @@ class AccessTokenValidator extends Validator {
|
||||
*/
|
||||
protected function validateValue($value): ?array {
|
||||
try {
|
||||
$token = Yii::$app->tokens->parse($value);
|
||||
} catch (Exception) {
|
||||
throw new ForbiddenOperationException(self::INVALID_TOKEN);
|
||||
}
|
||||
|
||||
if (!Yii::$app->tokens->verify($token)) {
|
||||
throw new ForbiddenOperationException(self::INVALID_TOKEN);
|
||||
}
|
||||
|
||||
if ($this->verifyExpiration && $token->isExpired(Carbon::now())) {
|
||||
throw new ForbiddenOperationException(self::TOKEN_EXPIRED);
|
||||
$token = Yii::$app->tokens->verifyAccessToken($value, verifyExpiration: $this->verifyExpiration);
|
||||
} catch (TokenExpiredException $e) {
|
||||
throw new ForbiddenOperationException(self::TOKEN_EXPIRED, previous: $e);
|
||||
} catch (InvalidTokenException $e) {
|
||||
throw new ForbiddenOperationException(self::INVALID_TOKEN, previous: $e);
|
||||
}
|
||||
|
||||
if ($this->verifyAccount && !$this->validateAccount((new TokenReader($token))->getAccountId())) {
|
||||
|
||||
Reference in new issue
Block a user