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;
|
2016-08-21 04:51:39 +05:30
|
|
|
use common\models\MinecraftAccessKey;
|
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;
|
|
|
|
|
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],
|
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();
|
2019-12-05 03:22:27 +05:30
|
|
|
$account = null;
|
2019-12-04 23:40:15 +05:30
|
|
|
if (mb_strlen($this->accessToken) === 36) {
|
|
|
|
/** @var MinecraftAccessKey $token */
|
|
|
|
$token = MinecraftAccessKey::findOne([
|
|
|
|
'access_token' => $this->accessToken,
|
|
|
|
'client_token' => $this->clientToken,
|
|
|
|
]);
|
2019-12-05 03:22:27 +05:30
|
|
|
if ($token !== null) {
|
|
|
|
$account = $token->account;
|
|
|
|
}
|
2019-12-04 23:40:15 +05:30
|
|
|
} else {
|
|
|
|
$token = Yii::$app->tokens->parse($this->accessToken);
|
2019-12-09 22:01:54 +05:30
|
|
|
$tokenReader = new TokenReader($token);
|
|
|
|
if ($tokenReader->getMinecraftClientToken() !== $this->clientToken) {
|
2019-12-04 23:40:15 +05:30
|
|
|
throw new ForbiddenOperationException('Invalid token.');
|
|
|
|
}
|
|
|
|
|
2019-12-09 22:01:54 +05:30
|
|
|
$account = Account::findOne(['id' => $tokenReader->getAccountId()]);
|
2016-08-21 04:51:39 +05:30
|
|
|
}
|
|
|
|
|
2019-12-05 03:22:27 +05:30
|
|
|
if ($account === null) {
|
|
|
|
throw new ForbiddenOperationException('Invalid token.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($account->status === Account::STATUS_BANNED) {
|
2016-09-01 13:01:43 +05:30
|
|
|
throw new ForbiddenOperationException('This account has been suspended.');
|
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
$hasMinecraftOauthSession = $account->getOauthSessions()
|
|
|
|
->andWhere(['client_id' => OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER])
|
|
|
|
->exists();
|
|
|
|
if ($hasMinecraftOauthSession === false) {
|
|
|
|
$minecraftOauthSession = new OauthSession();
|
|
|
|
$minecraftOauthSession->account_id = $account->id;
|
|
|
|
$minecraftOauthSession->client_id = OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER;
|
|
|
|
$minecraftOauthSession->scopes = [P::MINECRAFT_SERVER_SESSION];
|
|
|
|
Assert::true($minecraftOauthSession->save());
|
|
|
|
}
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
return new AuthenticateData($account, (string)$token, $this->clientToken);
|
2016-08-21 04:51:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|