2016-08-21 04:51:39 +05:30
|
|
|
<?php
|
2019-12-04 23:40:15 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-21 04:51:39 +05:30
|
|
|
namespace api\modules\authserver\models;
|
|
|
|
|
2019-12-09 22:01:54 +05:30
|
|
|
use api\components\Tokens\TokenReader;
|
2017-05-31 05:40:22 +05:30
|
|
|
use api\models\base\ApiForm;
|
2016-08-21 04:51:39 +05:30
|
|
|
use api\modules\authserver\exceptions\ForbiddenOperationException;
|
2019-12-04 23:40:15 +05:30
|
|
|
use api\modules\authserver\validators\AccessTokenValidator;
|
2016-08-21 04:51:39 +05:30
|
|
|
use api\modules\authserver\validators\RequiredValidator;
|
2019-12-15 21:03:15 +05:30
|
|
|
use api\rbac\Permissions as P;
|
2016-09-01 13:01:43 +05:30
|
|
|
use common\models\Account;
|
2019-12-11 01:21:11 +05:30
|
|
|
use common\models\OauthClient;
|
|
|
|
use common\models\OauthSession;
|
|
|
|
use Webmozart\Assert\Assert;
|
2019-12-04 23:40:15 +05:30
|
|
|
use Yii;
|
2016-08-21 04:51:39 +05:30
|
|
|
|
2017-05-31 05:40:22 +05:30
|
|
|
class RefreshTokenForm extends ApiForm {
|
2016-08-21 04:51:39 +05:30
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-21 04:51:39 +05:30
|
|
|
public $accessToken;
|
2018-04-18 02:17:25 +05:30
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-21 04:51:39 +05:30
|
|
|
public $clientToken;
|
|
|
|
|
2021-03-06 15:07:58 +05:30
|
|
|
/**
|
|
|
|
* @var string|bool
|
|
|
|
*/
|
|
|
|
public $requestUser;
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
public function rules(): array {
|
2016-08-21 04:51:39 +05:30
|
|
|
return [
|
2016-09-01 13:01:43 +05:30
|
|
|
[['accessToken', 'clientToken'], RequiredValidator::class],
|
2019-12-04 23:40:15 +05:30
|
|
|
[['accessToken'], AccessTokenValidator::class, 'verifyExpiration' => false],
|
2021-03-06 15:07:58 +05:30
|
|
|
[['requestUser'], 'boolean'],
|
2016-08-21 04:51:39 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return AuthenticateData
|
2019-12-04 23:40:15 +05:30
|
|
|
* @throws \api\modules\authserver\exceptions\IllegalArgumentException
|
|
|
|
* @throws \api\modules\authserver\exceptions\ForbiddenOperationException
|
2016-08-21 04:51:39 +05:30
|
|
|
*/
|
2019-12-04 23:40:15 +05:30
|
|
|
public function refresh(): AuthenticateData {
|
2016-08-21 04:51:39 +05:30
|
|
|
$this->validate();
|
2024-06-14 09:12:35 +05:30
|
|
|
$token = Yii::$app->tokens->parse($this->accessToken);
|
|
|
|
$tokenReader = new TokenReader($token);
|
|
|
|
if ($tokenReader->getMinecraftClientToken() !== $this->clientToken) {
|
|
|
|
throw new ForbiddenOperationException('Invalid token.');
|
2016-08-21 04:51:39 +05:30
|
|
|
}
|
|
|
|
|
2024-06-14 09:12:35 +05:30
|
|
|
$account = Account::findOne(['id' => $tokenReader->getAccountId()]);
|
2020-06-13 04:25:02 +05:30
|
|
|
if ($account === null) {
|
2019-12-05 03:22:27 +05:30
|
|
|
throw new ForbiddenOperationException('Invalid token.');
|
|
|
|
}
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
$token = Yii::$app->tokensFactory->createForMinecraftAccount($account, $this->clientToken);
|
2016-08-21 04:51:39 +05:30
|
|
|
|
2019-12-11 01:21:11 +05:30
|
|
|
// TODO: This behavior duplicates with the AuthenticationForm. Need to find a way to avoid duplication.
|
|
|
|
/** @var OauthSession|null $minecraftOauthSession */
|
2020-09-30 23:00:04 +05:30
|
|
|
$minecraftOauthSession = $account->getOauthSessions()
|
2019-12-11 01:21:11 +05:30
|
|
|
->andWhere(['client_id' => OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER])
|
2020-09-30 23:00:04 +05:30
|
|
|
->one();
|
|
|
|
if ($minecraftOauthSession === null) {
|
2019-12-11 01:21:11 +05:30
|
|
|
$minecraftOauthSession = new OauthSession();
|
|
|
|
$minecraftOauthSession->account_id = $account->id;
|
|
|
|
$minecraftOauthSession->client_id = OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER;
|
|
|
|
$minecraftOauthSession->scopes = [P::MINECRAFT_SERVER_SESSION];
|
|
|
|
}
|
|
|
|
|
2020-09-30 23:00:04 +05:30
|
|
|
$minecraftOauthSession->last_used_at = time();
|
|
|
|
Assert::true($minecraftOauthSession->save());
|
|
|
|
|
2021-03-06 15:07:58 +05:30
|
|
|
return new AuthenticateData($account, (string)$token, $this->clientToken, (bool)$this->requestUser);
|
2016-08-21 04:51:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|