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;
|
|
|
|
|
|
|
|
|
|
use api\models\authentication\LoginForm;
|
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;
|
2016-08-29 04:47:45 +05:30
|
|
|
|
use api\modules\authserver\Module as Authserver;
|
2018-01-02 22:52:56 +05:30
|
|
|
|
use api\modules\authserver\validators\ClientTokenValidator;
|
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;
|
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 AuthenticationForm 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 $username;
|
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 $password;
|
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 [
|
|
|
|
|
[['username', 'password', 'clientToken'], RequiredValidator::class],
|
2018-01-02 22:52:56 +05:30
|
|
|
|
[['clientToken'], ClientTokenValidator::class],
|
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 authenticate(): AuthenticateData {
|
|
|
|
|
// This validating method will throw an exception in case when validation will not pass successfully
|
2016-08-21 04:51:39 +05:30
|
|
|
|
$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
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
|
$loginForm = new LoginForm();
|
2016-08-21 04:51:39 +05:30
|
|
|
|
$loginForm->login = $this->username;
|
|
|
|
|
$loginForm->password = $this->password;
|
|
|
|
|
if (!$loginForm->validate()) {
|
|
|
|
|
$errors = $loginForm->getFirstErrors();
|
2017-09-06 22:47:52 +05:30
|
|
|
|
if (isset($errors['totp'])) {
|
2017-02-23 22:45:03 +05:30
|
|
|
|
Authserver::error("User with login = '{$this->username}' protected by two factor auth.");
|
|
|
|
|
throw new ForbiddenOperationException('Account protected with two factor auth.');
|
2018-04-18 02:17:25 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.');
|
|
|
|
|
}
|
2018-04-18 02:17:25 +05:30
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-07-15 04:29:56 +05:30
|
|
|
|
// The previous authorization server implementation used the nickname field instead of username,
|
|
|
|
|
// so we keep such behavior
|
2016-08-21 04:51:39 +05:30
|
|
|
|
$attribute = $loginForm->getLoginAttribute();
|
|
|
|
|
if ($attribute === 'username') {
|
|
|
|
|
$attribute = 'nickname';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: эта логика дублируется с логикой в SignoutForm
|
|
|
|
|
|
|
|
|
|
throw new ForbiddenOperationException("Invalid credentials. Invalid {$attribute} or password.");
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
|
/** @var Account $account */
|
2016-08-21 04:51:39 +05:30
|
|
|
|
$account = $loginForm->getAccount();
|
2019-12-04 23:40:15 +05:30
|
|
|
|
$token = Yii::$app->tokensFactory->createForMinecraftAccount($account, $this->clientToken);
|
|
|
|
|
$dataModel = new AuthenticateData($account, (string)$token, $this->clientToken);
|
2019-12-10 04:08:09 +05:30
|
|
|
|
// TODO: issue session in the oauth_sessions
|
2016-08-29 04:47:45 +05:30
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
}
|