Merge branch 'mojang_api'

This commit is contained in:
ErickSkrauch 2016-10-02 01:22:11 +03:00
commit 97e8e0d48e
21 changed files with 646 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;
}
}

View File

@ -12,6 +12,9 @@ return [
'username' => getenv('MYSQL_USER'),
'password' => getenv('MYSQL_PASSWORD'),
'charset' => 'utf8',
'schemaMap' => [
'mysql' => common\db\mysql\Schema::class,
],
],
'mailer' => [
'class' => yii\swiftmailer\Mailer::class,

View File

@ -0,0 +1,39 @@
<?php
namespace common\db\mysql;
use yii\db\Expression;
use yii\db\mysql\QueryBuilder as MysqlQueryBuilder;
class QueryBuilder extends MysqlQueryBuilder {
public function buildOrderBy($columns) {
if (empty($columns)) {
return '';
}
$orders = [];
foreach($columns as $name => $direction) {
if ($direction instanceof Expression) {
$orders[] = $direction->expression;
} elseif (is_array($direction)) {
// This is new feature
if (empty($direction)) {
continue;
}
$fieldValues = [];
foreach($direction as $fieldValue) {
$fieldValues[] = $this->db->quoteValue($fieldValue);
}
$orders[] = 'FIELD(' . $this->db->quoteColumnName($name) . ',' . implode(',', $fieldValues) . ')';
// End of new feature
} else {
$orders[] = $this->db->quoteColumnName($name) . ($direction === SORT_DESC ? ' DESC' : '');
}
}
return 'ORDER BY ' . implode(', ', $orders);
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace common\db\mysql;
use yii\db\mysql\Schema as MysqlSchema;
class Schema extends MysqlSchema {
public function createQueryBuilder() {
return new QueryBuilder($this->db);
}
}

View File

@ -1,7 +1,6 @@
<?php
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
@ -42,4 +41,16 @@ class UsernameHistory extends ActiveRecord {
return $this->hasOne(Account::class, ['id' => 'account_id']);
}
/**
* @param int $afterTime
* @return UsernameHistory|null
*/
public function findNext(int $afterTime = null) /*: ?UsernameHistory*/ {
return self::find()
->andWhere(['account_id' => $this->account_id])
->andWhere(['>', 'applied_in', $afterTime ?: $this->applied_in])
->orderBy(['applied_in' => SORT_ASC])
->one();
}
}

View File

@ -0,0 +1,33 @@
<?php
use console\db\Migration;
class m160919_170008_improve_username_history extends Migration {
public function safeUp() {
$this->execute('
INSERT INTO {{%usernames_history}} (account_id, username, applied_in)
SELECT id as account_id, username, created_at as applied_at
FROM {{%accounts}}
');
$this->createIndex('applied_in', '{{%usernames_history}}', 'applied_in');
$this->createIndex('username', '{{%usernames_history}}', 'username');
}
public function safeDown() {
$this->dropIndex('applied_in', '{{%usernames_history}}');
$this->dropIndex('username', '{{%usernames_history}}');
$this->execute('
DELETE FROM {{%usernames_history}}
WHERE id IN (
SELECT t1.id
FROM (
SELECT id, MIN(applied_in)
FROM {{%usernames_history}}
GROUP BY account_id
) t1
)
');
}
}

View File

@ -13,16 +13,24 @@ server {
set $request_url $request_uri;
set $host_with_uri '${host}${request_uri}';
if ($host_with_uri ~* '^${AUTHSERVER_HOST}/auth') {
rewrite_log on;
error_log /var/log/nginx/error.log debug;
if ($host_with_uri ~ '^${AUTHSERVER_HOST}/auth') {
set $request_url '/api/authserver${request_uri}';
rewrite ^/auth /api/authserver$uri last;
}
if ($host_with_uri ~* '^${AUTHSERVER_HOST}/session') {
if ($host_with_uri ~ '^${AUTHSERVER_HOST}/session') {
set $request_url '/api/minecraft${request_uri}';
rewrite ^/session /api/minecraft$uri last;
}
if ($host_with_uri ~ '^${AUTHSERVER_HOST}/api/(user|profiles)') {
set $request_url '/api/mojang${request_uri}';
rewrite ^/api/(user|profiles) /api/mojang$uri last;
}
location / {
alias $frontend_path;
index index.html;

View File

@ -0,0 +1,27 @@
<?php
namespace tests\codeception\api\_pages;
use yii\codeception\BasePage;
/**
* @property \tests\codeception\api\FunctionalTester $actor
*/
class MojangApiRoute extends BasePage {
public function usernameToUuid($username, $at = null) {
$this->route = '/mojang/profiles/' . $username;
$params = $at === null ? [] : ['at' => $at];
$this->actor->sendGET($this->getUrl(), $params);
}
public function usernamesByUuid($uuid) {
$this->route = "/mojang/profiles/{$uuid}/names";
$this->actor->sendGET($this->getUrl());
}
public function uuidsByUsernames($uuids) {
$this->route = '/mojang/profiles';
$this->actor->sendPOST($this->getUrl(), $uuids);
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace tests\codeception\api\functional\authserver;
use tests\codeception\api\_pages\MojangApiRoute;
use tests\codeception\api\FunctionalTester;
class UsernameToUuidCest {
/**
* @var MojangApiRoute
*/
private $route;
public function _before(FunctionalTester $I) {
$this->route = new MojangApiRoute($I);
}
public function getUuidByUsername(FunctionalTester $I) {
$I->wantTo('get user uuid by his username');
$this->route->usernameToUuid('Admin');
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
'id' => 'df936908b2e1544d96f82977ec213022',
'name' => 'Admin',
]);
}
public function getUuidByUsernameAtMoment(FunctionalTester $I) {
$I->wantTo('get user uuid by his username at fixed moment');
$this->route->usernameToUuid('klik201', 1474404142);
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
'id' => 'd6b3e93564664cb886dbb5df91ae6541',
'name' => 'klik202',
]);
}
public function getUuidByUsernameAtWrongMoment(FunctionalTester $I) {
$I->wantTo('get 204 if passed once used, but changed username at moment, when it was changed');
$this->route->usernameToUuid('klik201', 1474404144);
$I->canSeeResponseCodeIs(204);
$I->canSeeResponseEquals('');
}
public function getUuidByUsernameWithoutMoment(FunctionalTester $I) {
$I->wantTo('get 204 if username not busy and not passed valid time mark, when it was busy');
$this->route->usernameToUuid('klik201');
$I->canSeeResponseCodeIs(204);
$I->canSeeResponseEquals('');
}
public function getUuidByWrongUsername(FunctionalTester $I) {
$I->wantTo('get user uuid by some wrong username');
$this->route->usernameToUuid('not-exists-user');
$I->canSeeResponseCodeIs(204);
$I->canSeeResponseEquals('');
}
public function nonPassedUsername(FunctionalTester $I) {
$I->wantTo('get 404 on not passed username');
$this->route->usernameToUuid('');
$I->canSeeResponseCodeIs(404);
}
}

View File

@ -0,0 +1,131 @@
<?php
namespace tests\codeception\api\functional\authserver;
use tests\codeception\api\_pages\MojangApiRoute;
use tests\codeception\api\FunctionalTester;
class UsernamesToUuidsCest {
/**
* @var MojangApiRoute
*/
private $route;
public function _before(FunctionalTester $I) {
$this->route = new MojangApiRoute($I);
}
public function geUuidByOneUsername(FunctionalTester $I) {
$I->wantTo('get uuid by one username');
$this->route->uuidsByUsernames(['Admin']);
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
[
'id' => 'df936908b2e1544d96f82977ec213022',
'name' => 'Admin',
],
]);
}
public function getUuidsByUsernames(FunctionalTester $I) {
$I->wantTo('get uuids by few usernames');
$this->route->uuidsByUsernames(['Admin', 'AccWithOldPassword', 'Notch']);
$this->validateFewValidUsernames($I);
}
public function getUuidsByUsernamesWithPostString(FunctionalTester $I) {
$I->wantTo('get uuids by few usernames');
$this->route->uuidsByUsernames(json_encode(['Admin', 'AccWithOldPassword', 'Notch']));
$this->validateFewValidUsernames($I);
}
public function getUuidsByPartialNonexistentUsernames(FunctionalTester $I) {
$I->wantTo('get uuids by few usernames and some nonexistent');
$this->route->uuidsByUsernames(['Admin', 'not-exists-user']);
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
[
'id' => 'df936908b2e1544d96f82977ec213022',
'name' => 'Admin',
],
]);
}
public function passAllNonexistentUsernames(FunctionalTester $I) {
$I->wantTo('get specific response when pass all nonexistent usernames');
$this->route->uuidsByUsernames(['not-exists-1', 'not-exists-2']);
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([]);
}
public function passTooManyUsernames(FunctionalTester $I) {
$I->wantTo('get specific response when pass too many usernames');
$usernames = [];
for($i = 0; $i < 150; $i++) {
$usernames[] = random_bytes(10);
}
$this->route->uuidsByUsernames($usernames);
$I->canSeeResponseCodeIs(400);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
'error' => 'IllegalArgumentException',
'errorMessage' => 'Not more that 100 profile name per call is allowed.',
]);
}
public function passEmptyUsername(FunctionalTester $I) {
$I->wantTo('get specific response when pass empty username');
$this->route->uuidsByUsernames(['Admin', '']);
$I->canSeeResponseCodeIs(400);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
'error' => 'IllegalArgumentException',
'errorMessage' => 'profileName can not be null, empty or array key.',
]);
}
public function passEmptyField(FunctionalTester $I) {
$I->wantTo('get response when pass empty array');
$this->route->uuidsByUsernames([]);
$I->canSeeResponseCodeIs(400);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
'error' => 'IllegalArgumentException',
'errorMessage' => 'Passed array of profile names is an invalid JSON string.',
]);
}
public function passWrongPostBody(FunctionalTester $I) {
$I->wantTo('get specific response when pass invalid json string');
$this->route->uuidsByUsernames('wrong-json');
$I->canSeeResponseCodeIs(400);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
'error' => 'IllegalArgumentException',
'errorMessage' => 'Passed array of profile names is an invalid JSON string.',
]);
}
private function validateFewValidUsernames(FunctionalTester $I) {
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
[
'id' => 'df936908b2e1544d96f82977ec213022',
'name' => 'Admin',
],
[
'id' => 'bdc239f08a22518d8b93f02d4827c3eb',
'name' => 'AccWithOldPassword',
],
[
'id' => '4aaf4f003b5b4d3692529e8ee0c86679',
'name' => 'Notch',
],
]);
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace tests\codeception\api\functional\authserver;
use Faker\Provider\Uuid;
use tests\codeception\api\_pages\MojangApiRoute;
use tests\codeception\api\FunctionalTester;
class UuidToUsernamesHistoryCest {
/**
* @var MojangApiRoute
*/
private $route;
public function _before(FunctionalTester $I) {
$this->route = new MojangApiRoute($I);
}
public function getUsernameByUuid(FunctionalTester $I) {
$I->wantTo('get usernames history by uuid for user, without history');
$this->route->usernamesByUuid('df936908b2e1544d96f82977ec213022');
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
[
'name' => 'Admin',
],
]);
}
public function getUsernameByUuidWithHistory(FunctionalTester $I) {
$I->wantTo('get usernames history by dashed uuid and expect history with time marks');
$this->route->usernamesByUuid('d6b3e935-6466-4cb8-86db-b5df91ae6541');
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
[
'name' => 'klik202',
],
[
'name' => 'klik201',
'changedToAt' => 1474404141000,
],
[
'name' => 'klik202',
'changedToAt' => 1474404143000,
],
]);
}
public function passWrongUuid(FunctionalTester $I) {
$I->wantTo('get user username by some wrong uuid');
$this->route->usernamesByUuid(Uuid::uuid());
$I->canSeeResponseCodeIs(204);
$I->canSeeResponseEquals('');
}
public function passWrongUuidFormat(FunctionalTester $I) {
$I->wantTo('call profile route with invalid uuid string');
$this->route->usernamesByUuid('bla-bla-bla');
$I->canSeeResponseCodeIs(400);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
'error' => 'IllegalArgumentException',
'errorMessage' => 'Invalid uuid format.',
]);
}
}

View File

@ -6,6 +6,7 @@ use api\models\authentication\RegistrationForm;
use Codeception\Specify;
use common\models\Account;
use common\models\EmailActivation;
use common\models\UsernameHistory;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\AccountFixture;
use Yii;
@ -118,6 +119,11 @@ class RegistrationFormTest extends DbTestCase {
'account_id' => $account->id,
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
])->exists())->true();
expect('username history record exists in database', UsernameHistory::find()->andWhere([
'username' => $account->username,
'account_id' => $account->id,
'applied_in' => $account->created_at,
])->exists())->true();
expect_file('message file exists', $this->getMessageFile())->exists();
}

View File

@ -133,4 +133,17 @@ return [
'created_at' => 1472682343,
'updated_at' => 1472682343,
],
'account-with-usernames-history' => [
'id' => 11,
'uuid' => 'd6b3e935-6466-4cb8-86db-b5df91ae6541',
'username' => 'klik202',
'email' => 'klik202@mail.ru',
'password_hash' => '$2y$13$2rYkap5T6jG8z/mMK8a3Ou6aZxJcmAaTha6FEuujvHEmybSHRzW5e', # password_0
'password_hash_strategy' => \common\models\Account::PASS_HASH_STRATEGY_YII2,
'lang' => 'ru',
'status' => \common\models\Account::STATUS_ACTIVE,
'rules_agreement_version' => \common\LATEST_RULES_VERSION,
'created_at' => 1474404139,
'updated_at' => 1474404149,
],
];

View File

@ -1,4 +1,27 @@
<?php
return [
[
'id' => 1,
'username' => 'Admin',
'account_id' => 1,
'applied_in' => 1451775316,
],
[
'id' => 2,
'username' => 'klik202',
'account_id' => 11,
'applied_in' => 1474404139,
],
[
'id' => 3,
'username' => 'klik201',
'account_id' => 11,
'applied_in' => 1474404141,
],
[
'id' => 4,
'username' => 'klik202',
'account_id' => 11,
'applied_in' => 1474404143,
],
];

View File

@ -0,0 +1,16 @@
<?php
namespace codeception\common\unit\db\mysql;
use common\db\mysql\QueryBuilder;
use tests\codeception\common\unit\TestCase;
use Yii;
class QueryBuilderTest extends TestCase {
public function testBuildOrderByField() {
$queryBuilder = new QueryBuilder(Yii::$app->db);
$result = $queryBuilder->buildOrderBy(['dummy' => ['first', 'second']]);
$this->assertEquals("ORDER BY FIELD(`dummy`,'first','second')", $result);
}
}

View File

@ -11,7 +11,7 @@ return [
],
'modules' => [
'authserver' => [
'baseDomain' => 'http://localhost',
'host' => 'localhost',
],
],
'params' => [