2017-09-19 22:36:16 +05:30
|
|
|
<?php
|
2019-08-01 14:47:12 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
namespace api\components\User;
|
|
|
|
|
2019-12-09 22:01:54 +05:30
|
|
|
use api\components\Tokens\TokenReader;
|
2019-08-02 05:59:20 +05:30
|
|
|
use Carbon\Carbon;
|
2024-12-02 15:40:55 +05:30
|
|
|
use Carbon\FactoryImmutable;
|
2017-09-19 22:36:16 +05:30
|
|
|
use common\models\Account;
|
2019-12-10 04:08:09 +05:30
|
|
|
use common\models\OauthClient;
|
|
|
|
use common\models\OauthSession;
|
2024-12-02 15:40:55 +05:30
|
|
|
use DateTimeImmutable;
|
2017-09-19 22:36:16 +05:30
|
|
|
use Exception;
|
2024-12-02 15:40:55 +05:30
|
|
|
use Lcobucci\JWT\UnencryptedToken;
|
|
|
|
use Lcobucci\JWT\Validation\Constraint\LooseValidAt;
|
|
|
|
use Lcobucci\JWT\Validation\Validator;
|
2017-09-19 22:36:16 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\base\NotSupportedException;
|
|
|
|
use yii\web\UnauthorizedHttpException;
|
|
|
|
|
|
|
|
class JwtIdentity implements IdentityInterface {
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
private ?TokenReader $reader = null;
|
2019-12-09 22:01:54 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
private function __construct(
|
|
|
|
private readonly UnencryptedToken $token,
|
|
|
|
) {
|
2018-04-18 02:17:25 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public static function findIdentityByAccessToken($token, $type = null): IdentityInterface {
|
2017-09-19 22:36:16 +05:30
|
|
|
try {
|
2024-12-02 15:40:55 +05:30
|
|
|
$parsedToken = Yii::$app->tokens->parse($token);
|
2017-09-19 22:36:16 +05:30
|
|
|
} catch (Exception $e) {
|
|
|
|
Yii::error($e);
|
|
|
|
throw new UnauthorizedHttpException('Incorrect token');
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
if (!Yii::$app->tokens->verify($parsedToken)) {
|
2019-08-01 14:47:12 +05:30
|
|
|
throw new UnauthorizedHttpException('Incorrect token');
|
|
|
|
}
|
2017-09-19 22:36:16 +05:30
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
$now = Carbon::now();
|
2024-12-02 15:40:55 +05:30
|
|
|
if ($parsedToken->isExpired($now)) {
|
2019-08-01 14:47:12 +05:30
|
|
|
throw new UnauthorizedHttpException('Token expired');
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
if (!(new Validator())->validate($parsedToken, new LooseValidAt(FactoryImmutable::getDefaultInstance()))) {
|
2019-08-01 14:47:12 +05:30
|
|
|
throw new UnauthorizedHttpException('Incorrect token');
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
$tokenReader = new TokenReader($parsedToken);
|
2019-12-10 04:08:09 +05:30
|
|
|
$accountId = $tokenReader->getAccountId();
|
2019-12-11 01:21:11 +05:30
|
|
|
if ($accountId !== null) {
|
2024-12-02 15:40:55 +05:30
|
|
|
/** @var DateTimeImmutable $iat */
|
|
|
|
$iat = $parsedToken->claims()->get('iat');
|
2019-12-11 01:21:11 +05:30
|
|
|
if ($tokenReader->getMinecraftClientToken() !== null
|
|
|
|
&& self::isRevoked($accountId, OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER, $iat)
|
|
|
|
) {
|
|
|
|
throw new UnauthorizedHttpException('Token has been revoked');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($tokenReader->getClientId() !== null
|
|
|
|
&& self::isRevoked($accountId, $tokenReader->getClientId(), $iat)
|
|
|
|
) {
|
|
|
|
throw new UnauthorizedHttpException('Token has been revoked');
|
|
|
|
}
|
2019-12-10 04:08:09 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
return new self($parsedToken);
|
2019-08-01 14:47:12 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function getToken(): UnencryptedToken {
|
2019-08-02 05:59:20 +05:30
|
|
|
return $this->token;
|
|
|
|
}
|
|
|
|
|
2019-08-01 14:47:12 +05:30
|
|
|
public function getAccount(): ?Account {
|
2019-12-09 22:01:54 +05:30
|
|
|
return Account::findOne(['id' => $this->getReader()->getAccountId()]);
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function getAssignedPermissions(): array {
|
2019-12-09 22:01:54 +05:30
|
|
|
return $this->getReader()->getScopes() ?? [];
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function getId(): string {
|
2024-12-02 15:40:55 +05:30
|
|
|
return $this->token->toString();
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
/** @codeCoverageIgnoreStart */
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getAuthKey() {
|
|
|
|
throw new NotSupportedException('This method used for cookie auth, except we using Bearer auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateAuthKey($authKey) {
|
|
|
|
throw new NotSupportedException('This method used for cookie auth, except we using Bearer auth');
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
/**
|
|
|
|
* @throws NotSupportedException
|
|
|
|
*/
|
2017-09-19 22:36:16 +05:30
|
|
|
public static function findIdentity($id) {
|
|
|
|
throw new NotSupportedException('This method used for cookie auth, except we using Bearer auth');
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
private static function isRevoked(int $accountId, string $clientId, DateTimeImmutable $iat): bool {
|
2019-12-11 01:21:11 +05:30
|
|
|
$session = OauthSession::findOne(['account_id' => $accountId, 'client_id' => $clientId]);
|
2024-12-02 15:40:55 +05:30
|
|
|
return $session !== null && $session->revoked_at !== null && $session->revoked_at > $iat->getTimestamp();
|
2019-12-11 01:21:11 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
/** @codeCoverageIgnoreEnd */
|
2019-12-09 22:01:54 +05:30
|
|
|
private function getReader(): TokenReader {
|
|
|
|
if ($this->reader === null) {
|
|
|
|
$this->reader = new TokenReader($this->token);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->reader;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|