2016-09-03 04:24:22 +05:30
|
|
|
<?php
|
|
|
|
namespace api\modules\session\models;
|
|
|
|
|
|
|
|
use api\modules\session\exceptions\ForbiddenOperationException;
|
|
|
|
use api\modules\session\exceptions\IllegalArgumentException;
|
2016-09-05 20:25:38 +05:30
|
|
|
use api\modules\session\models\protocols\JoinInterface;
|
2016-09-03 04:24:22 +05:30
|
|
|
use api\modules\session\Module as Session;
|
|
|
|
use api\modules\session\validators\RequiredValidator;
|
2016-09-05 20:25:38 +05:30
|
|
|
use common\helpers\StringHelper;
|
2016-09-03 04:24:22 +05:30
|
|
|
use common\models\OauthScope as S;
|
|
|
|
use common\validators\UuidValidator;
|
|
|
|
use common\models\Account;
|
|
|
|
use common\models\MinecraftAccessKey;
|
|
|
|
use Yii;
|
|
|
|
use yii\base\ErrorException;
|
2016-09-05 20:25:38 +05:30
|
|
|
use yii\base\Model;
|
2016-09-03 04:24:22 +05:30
|
|
|
use yii\web\UnauthorizedHttpException;
|
|
|
|
|
2016-09-05 20:25:38 +05:30
|
|
|
class JoinForm extends Model {
|
2016-09-03 04:24:22 +05:30
|
|
|
|
2016-09-06 15:26:39 +05:30
|
|
|
public $accessToken;
|
|
|
|
public $selectedProfile;
|
|
|
|
public $serverId;
|
2016-09-03 04:24:22 +05:30
|
|
|
|
2016-09-05 20:25:38 +05:30
|
|
|
/**
|
|
|
|
* @var Account|null
|
|
|
|
*/
|
2016-09-03 04:24:22 +05:30
|
|
|
private $account;
|
|
|
|
|
2016-09-05 20:25:38 +05:30
|
|
|
/**
|
|
|
|
* @var JoinInterface
|
|
|
|
*/
|
|
|
|
private $protocol;
|
|
|
|
|
|
|
|
public function __construct(JoinInterface $protocol, array $config = []) {
|
|
|
|
$this->protocol = $protocol;
|
|
|
|
$this->accessToken = $protocol->getAccessToken();
|
|
|
|
$this->selectedProfile = $protocol->getSelectedProfile();
|
|
|
|
$this->serverId = $protocol->getServerId();
|
|
|
|
|
|
|
|
parent::__construct($config);
|
|
|
|
}
|
|
|
|
|
2016-09-03 04:24:22 +05:30
|
|
|
public function rules() {
|
|
|
|
return [
|
2016-09-05 20:25:38 +05:30
|
|
|
[['accessToken', 'serverId'], RequiredValidator::class],
|
2016-09-03 04:24:22 +05:30
|
|
|
[['accessToken', 'selectedProfile'], 'validateUuid'],
|
|
|
|
[['accessToken'], 'validateAccessToken'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function join() {
|
2016-09-05 20:25:38 +05:30
|
|
|
$serverId = $this->serverId;
|
|
|
|
$accessToken = $this->accessToken;
|
|
|
|
Session::info("User with access_token = '{$accessToken}' trying join to server with server_id = '{$serverId}'.");
|
2016-09-03 04:24:22 +05:30
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$account = $this->getAccount();
|
2016-09-05 20:25:38 +05:30
|
|
|
$sessionModel = new SessionModel($account->username, $serverId);
|
2016-09-03 04:24:22 +05:30
|
|
|
if (!$sessionModel->save()) {
|
|
|
|
throw new ErrorException('Cannot save join session model');
|
|
|
|
}
|
|
|
|
|
|
|
|
Session::info(
|
2016-09-05 20:25:38 +05:30
|
|
|
"User with access_token = '{$accessToken}' and nickname = '{$account->username}' successfully joined to " .
|
|
|
|
"server_id = '{$serverId}'."
|
2016-09-03 04:24:22 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-05 20:25:38 +05:30
|
|
|
public function validate($attributeNames = null, $clearErrors = true) {
|
|
|
|
if (!$this->protocol->validate()) {
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::validate($attributeNames, $clearErrors);
|
|
|
|
}
|
|
|
|
|
2016-09-03 04:24:22 +05:30
|
|
|
public function validateUuid($attribute) {
|
|
|
|
if ($this->hasErrors($attribute)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-05 20:25:38 +05:30
|
|
|
if ($attribute === 'selectedProfile' && !StringHelper::isUuid($this->selectedProfile)) {
|
|
|
|
// Это нормально. Там может быть ник игрока, если это Legacy авторизация
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-03 04:24:22 +05:30
|
|
|
$validator = new UuidValidator();
|
|
|
|
$validator->validateAttribute($this, $attribute);
|
|
|
|
|
|
|
|
if ($this->hasErrors($attribute)) {
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \api\modules\session\exceptions\SessionServerException
|
|
|
|
*/
|
|
|
|
public function validateAccessToken() {
|
|
|
|
$accessToken = $this->accessToken;
|
|
|
|
/** @var MinecraftAccessKey|null $accessModel */
|
|
|
|
$accessModel = MinecraftAccessKey::findOne($accessToken);
|
|
|
|
if ($accessModel === null) {
|
|
|
|
try {
|
|
|
|
$identity = Yii::$app->apiUser->loginByAccessToken($accessToken);
|
|
|
|
} catch (UnauthorizedHttpException $e) {
|
|
|
|
$identity = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($identity === null) {
|
|
|
|
Session::error("User with access_token = '{$accessToken}' failed join by wrong access_token.");
|
|
|
|
throw new ForbiddenOperationException('Invalid access_token.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Yii::$app->apiUser->can(S::MINECRAFT_SERVER_SESSION)) {
|
|
|
|
Session::error("User with access_token = '{$accessToken}' doesn't have enough scopes to make join.");
|
|
|
|
throw new ForbiddenOperationException('The token does not have required scope.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$accessModel = $identity->getAccessToken();
|
|
|
|
$account = $identity->getAccount();
|
|
|
|
} else {
|
|
|
|
$account = $accessModel->account;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var MinecraftAccessKey|\common\models\OauthAccessToken $accessModel */
|
|
|
|
if ($accessModel->isExpired()) {
|
|
|
|
Session::error("User with access_token = '{$accessToken}' failed join by expired access_token.");
|
|
|
|
throw new ForbiddenOperationException('Expired access_token.');
|
|
|
|
}
|
|
|
|
|
2016-09-05 20:25:38 +05:30
|
|
|
$selectedProfile = $this->selectedProfile;
|
|
|
|
$isUuid = StringHelper::isUuid($selectedProfile);
|
|
|
|
if ($isUuid && $account->uuid !== $selectedProfile) {
|
2016-09-03 04:24:22 +05:30
|
|
|
Session::error(
|
2016-09-05 20:25:38 +05:30
|
|
|
"User with access_token = '{$accessToken}' trying to join with identity = '{$selectedProfile}'," .
|
2016-09-03 04:24:22 +05:30
|
|
|
" but access_token issued to account with id = '{$account->uuid}'."
|
|
|
|
);
|
|
|
|
throw new ForbiddenOperationException('Wrong selected_profile.');
|
2016-09-05 20:25:38 +05:30
|
|
|
} elseif (!$isUuid && $account->username !== $selectedProfile) {
|
|
|
|
Session::error(
|
|
|
|
"User with access_token = '{$accessToken}' trying to join with identity = '{$selectedProfile}'," .
|
|
|
|
" but access_token issued to account with username = '{$account->username}'."
|
|
|
|
);
|
|
|
|
throw new ForbiddenOperationException('Invalid credentials');
|
2016-09-03 04:24:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
$this->account = $account;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Account|null
|
|
|
|
*/
|
|
|
|
protected function getAccount() {
|
|
|
|
return $this->account;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|