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;
|
2019-12-11 01:21:11 +05:30
|
|
|
use api\rbac\Permissions as P;
|
2016-08-29 04:47:45 +05:30
|
|
|
use common\helpers\Error as E;
|
|
|
|
use common\models\Account;
|
2019-12-11 01:21:11 +05:30
|
|
|
use common\models\OauthClient;
|
|
|
|
use common\models\OauthSession;
|
2024-11-24 14:55:22 +05:30
|
|
|
use Ramsey\Uuid\Uuid;
|
2019-12-11 01:21:11 +05:30
|
|
|
use Webmozart\Assert\Assert;
|
2019-12-04 23:40:15 +05:30
|
|
|
use Yii;
|
2024-12-02 15:40:55 +05:30
|
|
|
use yii\db\Exception;
|
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;
|
|
|
|
|
2021-03-06 15:07:58 +05:30
|
|
|
/**
|
|
|
|
* @var string|bool
|
|
|
|
*/
|
|
|
|
public $requestUser;
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
public function rules(): array {
|
2016-08-21 04:51:39 +05:30
|
|
|
return [
|
2024-11-24 14:55:22 +05:30
|
|
|
[['username', 'password'], RequiredValidator::class],
|
2018-01-02 22:52:56 +05:30
|
|
|
[['clientToken'], ClientTokenValidator::class],
|
2021-03-06 15:07:58 +05:30
|
|
|
[['requestUser'], 'boolean'],
|
2016-08-21 04:51:39 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return AuthenticateData
|
2024-12-02 15:40:55 +05:30
|
|
|
* @throws ForbiddenOperationException
|
|
|
|
* @throws Exception
|
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
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
// The previous authorization server implementation used the nickname field instead of username,
|
|
|
|
// so we keep such behavior
|
2024-12-02 15:40:55 +05:30
|
|
|
$attribute = !str_contains($this->username, '@') ? 'nickname' : 'email';
|
2020-06-12 02:57:02 +05:30
|
|
|
|
2021-03-09 02:51:10 +05:30
|
|
|
$password = $this->password;
|
|
|
|
$totp = null;
|
|
|
|
if (preg_match('/.{8,}:(\d{6})$/', $password, $matches) === 1) {
|
|
|
|
$totp = $matches[1];
|
|
|
|
$password = mb_substr($password, 0, -7); // :123456 - 7 chars
|
|
|
|
}
|
|
|
|
|
|
|
|
login:
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
$loginForm = new LoginForm();
|
2016-08-21 04:51:39 +05:30
|
|
|
$loginForm->login = $this->username;
|
2021-03-09 02:51:10 +05:30
|
|
|
$loginForm->password = $password;
|
|
|
|
$loginForm->totp = $totp;
|
|
|
|
|
|
|
|
$isValid = $loginForm->validate();
|
|
|
|
// Handle case when user's password matches the template for totp via password
|
|
|
|
if (!$isValid && $totp !== null && $loginForm->getFirstError('password') === E::PASSWORD_INCORRECT) {
|
|
|
|
$password = "{$password}:{$totp}";
|
|
|
|
$totp = null;
|
|
|
|
|
|
|
|
goto login;
|
|
|
|
}
|
2018-04-18 02:17:25 +05:30
|
|
|
|
2021-03-09 02:51:10 +05:30
|
|
|
if (!$isValid || $loginForm->getAccount()->status === Account::STATUS_DELETED) {
|
|
|
|
$errors = $loginForm->getFirstErrors();
|
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.");
|
2021-03-09 02:51:10 +05:30
|
|
|
} elseif (isset($errors['totp'])) {
|
|
|
|
if ($errors['totp'] === E::TOTP_REQUIRED) {
|
|
|
|
Authserver::error("User with login = '{$this->username}' protected by two factor auth.");
|
|
|
|
throw new ForbiddenOperationException('Account protected with two factor auth.');
|
|
|
|
}
|
2016-08-21 04:51:39 +05:30
|
|
|
|
2021-03-09 02:51:10 +05:30
|
|
|
Authserver::error("User with login = '{$this->username}' passed wrong totp token");
|
|
|
|
}
|
2016-08-21 04:51:39 +05:30
|
|
|
|
|
|
|
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();
|
2024-11-24 14:55:22 +05:30
|
|
|
$clientToken = $this->clientToken ?: Uuid::uuid4()->toString();
|
|
|
|
$token = Yii::$app->tokensFactory->createForMinecraftAccount($account, $clientToken);
|
2024-12-02 15:40:55 +05:30
|
|
|
$dataModel = new AuthenticateData($account, $token->toString(), $clientToken, (bool)$this->requestUser);
|
2019-12-11 01:21:11 +05:30
|
|
|
/** @var OauthSession|null $minecraftOauthSession */
|
2020-09-30 23:00:04 +05:30
|
|
|
$minecraftOauthSession = $account->getOauthSessions()
|
2019-12-11 01:21:11 +05:30
|
|
|
->andWhere(['client_id' => OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER])
|
2020-09-30 23:00:04 +05:30
|
|
|
->one();
|
|
|
|
if ($minecraftOauthSession === null) {
|
2019-12-11 01:21:11 +05:30
|
|
|
$minecraftOauthSession = new OauthSession();
|
|
|
|
$minecraftOauthSession->account_id = $account->id;
|
|
|
|
$minecraftOauthSession->client_id = OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER;
|
|
|
|
$minecraftOauthSession->scopes = [P::MINECRAFT_SERVER_SESSION];
|
|
|
|
}
|
2016-08-29 04:47:45 +05:30
|
|
|
|
2020-09-30 23:00:04 +05:30
|
|
|
$minecraftOauthSession->last_used_at = time();
|
|
|
|
Assert::true($minecraftOauthSession->save());
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|