Centralize access token verification

This commit is contained in:
ErickSkrauch committed 2026-11-07 18:15:04 +01:00
1 parent 94975261c8
commit df40f8fc30
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())) {