2016-08-21 04:51:39 +05:30
|
|
|
|
<?php
|
|
|
|
|
namespace api\modules\authserver\models;
|
|
|
|
|
|
|
|
|
|
use api\models\authentication\LoginForm;
|
|
|
|
|
use api\modules\authserver\exceptions\ForbiddenOperationException;
|
2016-08-29 04:47:45 +05:30
|
|
|
|
use api\modules\authserver\Module as Authserver;
|
2016-08-21 04:51:39 +05:30
|
|
|
|
use api\modules\authserver\validators\RequiredValidator;
|
2016-08-29 04:47:45 +05:30
|
|
|
|
use common\helpers\Error as E;
|
|
|
|
|
use common\models\Account;
|
2016-08-21 04:51:39 +05:30
|
|
|
|
use common\models\MinecraftAccessKey;
|
|
|
|
|
|
|
|
|
|
class AuthenticationForm extends Form {
|
|
|
|
|
|
|
|
|
|
public $username;
|
|
|
|
|
public $password;
|
|
|
|
|
public $clientToken;
|
|
|
|
|
|
|
|
|
|
public function rules() {
|
|
|
|
|
return [
|
|
|
|
|
[['username', 'password', 'clientToken'], RequiredValidator::class],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return AuthenticateData
|
|
|
|
|
* @throws \api\modules\authserver\exceptions\AuthserverException
|
|
|
|
|
*/
|
|
|
|
|
public function authenticate() {
|
|
|
|
|
$this->validate();
|
|
|
|
|
|
2016-08-29 04:47:45 +05:30
|
|
|
|
Authserver::info("Trying to authenticate user by login = '{$this->username}'.");
|
2016-08-21 04:51:39 +05:30
|
|
|
|
|
2016-08-29 04:47:45 +05:30
|
|
|
|
$loginForm = $this->createLoginForm();
|
2016-08-21 04:51:39 +05:30
|
|
|
|
$loginForm->login = $this->username;
|
|
|
|
|
$loginForm->password = $this->password;
|
|
|
|
|
if (!$loginForm->validate()) {
|
|
|
|
|
$errors = $loginForm->getFirstErrors();
|
|
|
|
|
if (isset($errors['login'])) {
|
2016-08-29 04:47:45 +05:30
|
|
|
|
if ($errors['login'] === E::ACCOUNT_BANNED) {
|
|
|
|
|
Authserver::error("User with login = '{$this->username}' is banned");
|
|
|
|
|
throw new ForbiddenOperationException('This account has been suspended.');
|
|
|
|
|
} else {
|
|
|
|
|
Authserver::error("Cannot find user by login = '{$this->username}'");
|
|
|
|
|
}
|
2016-08-21 04:51:39 +05:30
|
|
|
|
} elseif (isset($errors['password'])) {
|
2016-08-29 04:47:45 +05:30
|
|
|
|
Authserver::error("User with login = '{$this->username}' passed wrong password.");
|
2016-08-21 04:51:39 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// На старом сервере авторизации использовалось поле nickname, а не username, так что сохраняем эту логику
|
|
|
|
|
$attribute = $loginForm->getLoginAttribute();
|
|
|
|
|
if ($attribute === 'username') {
|
|
|
|
|
$attribute = 'nickname';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: эта логика дублируется с логикой в SignoutForm
|
|
|
|
|
|
|
|
|
|
throw new ForbiddenOperationException("Invalid credentials. Invalid {$attribute} or password.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$account = $loginForm->getAccount();
|
2016-08-29 04:47:45 +05:30
|
|
|
|
$accessTokenModel = $this->createMinecraftAccessToken($account);
|
|
|
|
|
$dataModel = new AuthenticateData($accessTokenModel);
|
|
|
|
|
|
|
|
|
|
Authserver::info("User with id = {$account->id}, username = '{$account->username}' and email = '{$account->email}' successfully logged in.");
|
|
|
|
|
|
|
|
|
|
return $dataModel;
|
|
|
|
|
}
|
2016-08-21 04:51:39 +05:30
|
|
|
|
|
2016-08-29 04:47:45 +05:30
|
|
|
|
protected function createMinecraftAccessToken(Account $account) : MinecraftAccessKey {
|
2016-08-21 04:51:39 +05:30
|
|
|
|
/** @var MinecraftAccessKey|null $accessTokenModel */
|
2016-08-29 04:47:45 +05:30
|
|
|
|
$accessTokenModel = MinecraftAccessKey::findOne([
|
|
|
|
|
'client_token' => $this->clientToken,
|
|
|
|
|
'account_id' => $account->id,
|
|
|
|
|
]);
|
|
|
|
|
|
2016-08-21 04:51:39 +05:30
|
|
|
|
if ($accessTokenModel === null) {
|
|
|
|
|
$accessTokenModel = new MinecraftAccessKey();
|
|
|
|
|
$accessTokenModel->client_token = $this->clientToken;
|
|
|
|
|
$accessTokenModel->account_id = $account->id;
|
|
|
|
|
$accessTokenModel->insert();
|
|
|
|
|
} else {
|
|
|
|
|
$accessTokenModel->refreshPrimaryKeyValue();
|
2016-10-15 20:05:03 +05:30
|
|
|
|
$accessTokenModel->update();
|
2016-08-21 04:51:39 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-08-29 04:47:45 +05:30
|
|
|
|
return $accessTokenModel;
|
|
|
|
|
}
|
2016-08-21 04:51:39 +05:30
|
|
|
|
|
2016-08-29 04:47:45 +05:30
|
|
|
|
protected function createLoginForm() : LoginForm {
|
|
|
|
|
return new LoginForm();
|
2016-08-21 04:51:39 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|