Реализован функционал Mojang API

Исправлена ошибка доступа к authserver из-за перехода на использование хостов, а не доменов
This commit is contained in:
ErickSkrauch
2016-09-21 11:13:43 +03:00
parent 78e5d3e103
commit df1859f0c1
17 changed files with 576 additions and 10 deletions

View File

@@ -70,10 +70,13 @@ return [
'modules' => [
'authserver' => [
'class' => api\modules\authserver\Module::class,
'baseDomain' => getenv('AUTHSERVER_HOST'),
'host' => $params['authserverHost'],
],
'session' => [
'class' => api\modules\session\Module::class,
],
'mojang' => [
'class' => api\modules\mojang\Module::class,
],
],
];

View File

@@ -1,4 +1,7 @@
<?php
/**
* @var array $params
*/
return [
'/accounts/change-email/initialize' => 'accounts/change-email-initialize',
'/accounts/change-email/submit-new-email' => 'accounts/change-email-submit-new-email',
@@ -13,4 +16,12 @@ return [
'/minecraft/session/hasJoined' => 'session/session/has-joined',
'/minecraft/session/legacy/hasJoined' => 'session/session/has-joined-legacy',
'/minecraft/session/profile/<uuid>' => 'session/session/profile',
'/mojang/profiles/<username>' => 'mojang/api/uuid-by-username',
'/mojang/profiles/<uuid>/names' => 'mojang/api/usernames-by-uuid',
'POST /mojang/profiles' => 'mojang/api/uuids-by-usernames',
"http://{$params['authserverHost']}/mojang/api/users/profiles/minecraft/<username>" => 'mojang/api/uuid-by-username',
"http://{$params['authserverHost']}/mojang/api/user/profiles/<uuid>/names" => 'mojang/api/usernames-by-uuid',
"POST http://{$params['authserverHost']}/mojang/api/profiles/minecraft" => 'mojang/api/uuids-by-usernames',
];

View File

@@ -9,6 +9,7 @@ use common\components\UserFriendlyRandomKey;
use common\models\Account;
use common\models\confirmations\RegistrationConfirmation;
use common\models\EmailActivation;
use common\models\UsernameHistory;
use common\validators\LanguageValidator;
use common\validators\PasswordValidate;
use Ely\Email\Renderer;
@@ -108,6 +109,14 @@ class RegistrationForm extends ApiForm {
throw new ErrorException('Unable save email-activation model.');
}
$usernamesHistory = new UsernameHistory();
$usernamesHistory->account_id = $account->id;
$usernamesHistory->username = $account->username;
$usernamesHistory->applied_in = $account->created_at;
if (!$usernamesHistory->save()) {
throw new ErrorException('Cannot save username history record');
}
$this->sendMail($emailActivation, $account);
$changeUsernameForm = new ChangeUsernameForm();

View File

@@ -15,11 +15,11 @@ class Module extends \yii\base\Module implements BootstrapInterface {
/**
* @var string базовый домен, запросы на который этот модуль должен обрабатывать
*/
public $baseDomain = 'https://authserver.ely.by';
public $host = 'authserver.ely.by';
public function init() {
parent::init();
if ($this->baseDomain === null) {
if ($this->host === null) {
throw new InvalidConfigException('base domain must be specified');
}
}
@@ -39,7 +39,7 @@ class Module extends \yii\base\Module implements BootstrapInterface {
*/
public function bootstrap($app) {
$app->getUrlManager()->addRules([
$this->baseDomain . '/' . $this->id . '/auth/<action>' => $this->id . '/authentication/<action>',
"http://$this->host/$this->id/auth/<action>" => "$this->id/authentication/<action>",
], false);
}
@@ -59,7 +59,7 @@ class Module extends \yii\base\Module implements BootstrapInterface {
* @throws NotFoundHttpException
*/
protected function checkHost() {
if (Yii::$app->request->getHostInfo() !== $this->baseDomain) {
if (parse_url(Yii::$app->request->getHostInfo(), PHP_URL_HOST) !== $this->host) {
throw new NotFoundHttpException();
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace api\modules\mojang;
class Module extends \yii\base\Module {
public $id = 'mojang';
public $defaultRoute = 'api';
}

View File

@@ -0,0 +1,145 @@
<?php
namespace api\modules\mojang\controllers;
use api\controllers\Controller;
use common\models\Account;
use common\models\UsernameHistory;
use Ramsey\Uuid\Uuid;
use Yii;
use yii\web\Response;
class ApiController extends Controller {
public function behaviors() {
$behaviors = parent::behaviors();
unset($behaviors['authenticator']);
return $behaviors;
}
public function actionUuidByUsername($username, $at = null) {
if ($at !== null) {
/** @var UsernameHistory|null $record */
$record = UsernameHistory::find()
->andWhere(['username' => $username])
->orderBy(['applied_in' => SORT_DESC])
->andWhere(['<=', 'applied_in', $at])
->one();
// Запрос выше находит просто последний случай использования, не учитывая то, что ник
// мог быть сменён с тех пор. Поэтому дополнительно проводим проверку, чтобы ник находился
// в каком-либо периоде (т.е. существовала последующая запись) или последний использовавший
// ник пользователь не сменил его на нечто иное
$account = null;
if ($record !== null) {
if ($record->findNext($at) !== null || $record->account->username === $record->username) {
$account = $record->account;
}
}
} else {
/** @var Account|null $record */
$account = Account::findOne(['username' => $username]);
}
if ($account === null) {
return $this->noContentResponse();
}
return [
'id' => str_replace('-', '', $account->uuid),
'name' => $account->username,
];
}
public function actionUsernamesByUuid($uuid) {
try {
$uuid = Uuid::fromString($uuid)->toString();
} catch(\InvalidArgumentException $e) {
return $this->illegalArgumentResponse('Invalid uuid format.');
}
$account = Account::findOne(['uuid' => $uuid]);
if ($account === null) {
return $this->noContentResponse();
}
/** @var UsernameHistory[] $usernameHistory */
$usernameHistory = $account->getUsernameHistory()
->orderBy(['applied_in' => SORT_ASC])
->all();
$data = [];
foreach($usernameHistory as $record) {
$data[] = [
'name' => $record->username,
'changedToAt' => $record->applied_in * 1000,
];
}
// У первого элемента не должно быть времени, когда он был применён
// Хотя мы в принципе эту инфу знаем. А вот Mojang, вероятно, нет
unset($data[0]['changedToAt']);
return $data;
}
public function actionUuidsByUsernames() {
$usernames = Yii::$app->request->post();
if (empty($usernames)) {
$usernames = json_decode(Yii::$app->request->getRawBody());
if (empty($usernames)) {
return $this->illegalArgumentResponse('Passed array of profile names is an invalid JSON string.');
}
}
$usernames = array_unique($usernames);
if (count($usernames) > 100) {
return $this->illegalArgumentResponse('Not more that 100 profile name per call is allowed.');
}
foreach($usernames as $username) {
if (empty($username) || is_array($username)) {
return $this->illegalArgumentResponse('profileName can not be null, empty or array key.');
}
}
/** @var Account[] $accounts */
$accounts = Account::find()
->andWhere(['username' => $usernames])
->orderBy(['username' => $usernames])
->limit(count($usernames))
->all();
$responseData = [];
foreach($accounts as $account) {
$responseData[] = [
'id' => str_replace('-', '', $account->uuid),
'name' => $account->username,
];
}
return $responseData;
}
private function noContentResponse() {
$response = Yii::$app->getResponse();
$response->setStatusCode(204);
$response->format = Response::FORMAT_RAW;
$response->content = '';
return $response;
}
private function illegalArgumentResponse(string $errorMessage) {
$response = Yii::$app->getResponse();
$response->setStatusCode(400);
$response->format = Response::FORMAT_JSON;
$response->data = [
'error' => 'IllegalArgumentException',
'errorMessage' => $errorMessage,
];
return $response;
}
}