validate(); Authserver::info("Trying to authenticate user by login = '{$this->username}'."); $loginForm = new LoginForm(); $loginForm->login = $this->username; $loginForm->password = $this->password; if (!$loginForm->validate()) { $errors = $loginForm->getFirstErrors(); if (isset($errors['totp'])) { Authserver::error("User with login = '{$this->username}' protected by two factor auth."); throw new ForbiddenOperationException('Account protected with two factor auth.'); } if (isset($errors['login'])) { if ($errors['login'] === E::ACCOUNT_BANNED) { Authserver::error("User with login = '{$this->username}' is banned"); throw new ForbiddenOperationException('This account has been suspended.'); } Authserver::error("Cannot find user by login = '{$this->username}'"); } elseif (isset($errors['password'])) { Authserver::error("User with login = '{$this->username}' passed wrong password."); } // The previous authorization server implementation used the nickname field instead of username, // so we keep such behavior $attribute = $loginForm->getLoginAttribute(); if ($attribute === 'username') { $attribute = 'nickname'; } // TODO: эта логика дублируется с логикой в SignoutForm throw new ForbiddenOperationException("Invalid credentials. Invalid {$attribute} or password."); } /** @var Account $account */ $account = $loginForm->getAccount(); $token = Yii::$app->tokensFactory->createForMinecraftAccount($account, $this->clientToken); $dataModel = new AuthenticateData($account, (string)$token, $this->clientToken); /** @var OauthSession|null $minecraftOauthSession */ $hasMinecraftOauthSession = $account->getOauthSessions() ->andWhere(['client_id' => OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER]) ->exists(); if ($hasMinecraftOauthSession === false) { $minecraftOauthSession = new OauthSession(); $minecraftOauthSession->account_id = $account->id; $minecraftOauthSession->client_id = OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER; $minecraftOauthSession->scopes = [P::MINECRAFT_SERVER_SESSION]; Assert::true($minecraftOauthSession->save()); } Authserver::info("User with id = {$account->id}, username = '{$account->username}' and email = '{$account->email}' successfully logged in."); return $dataModel; } }