2016-08-21 02:21:39 +03:00
|
|
|
<?php
|
2019-12-04 21:10:15 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-21 02:21:39 +03:00
|
|
|
namespace api\modules\authserver\models;
|
|
|
|
|
2025-01-17 21:37:35 +01:00
|
|
|
use api\components\Tokens\TokensFactory;
|
2017-05-31 03:10:22 +03:00
|
|
|
use api\models\base\ApiForm;
|
2016-08-21 02:21:39 +03:00
|
|
|
use api\modules\authserver\exceptions\ForbiddenOperationException;
|
2016-08-29 02:17:45 +03:00
|
|
|
use api\modules\authserver\Module as Authserver;
|
2018-01-02 20:22:56 +03:00
|
|
|
use api\modules\authserver\validators\ClientTokenValidator;
|
2016-08-21 02:21:39 +03:00
|
|
|
use api\modules\authserver\validators\RequiredValidator;
|
2019-12-10 22:51:11 +03:00
|
|
|
use api\rbac\Permissions as P;
|
2025-01-17 21:37:35 +01:00
|
|
|
use common\components\Authentication\Entities\Credentials;
|
|
|
|
use common\components\Authentication\Exceptions;
|
|
|
|
use common\components\Authentication\Exceptions\AuthenticationException;
|
|
|
|
use common\components\Authentication\LoginServiceInterface;
|
2016-08-29 02:17:45 +03:00
|
|
|
use common\models\Account;
|
2019-12-10 22:51:11 +03:00
|
|
|
use common\models\OauthClient;
|
|
|
|
use common\models\OauthSession;
|
2024-11-24 10:25:22 +01:00
|
|
|
use Ramsey\Uuid\Uuid;
|
2019-12-10 22:51:11 +03:00
|
|
|
use Webmozart\Assert\Assert;
|
2016-08-21 02:21:39 +03:00
|
|
|
|
2025-01-17 21:37:35 +01:00
|
|
|
final class AuthenticationForm extends ApiForm {
|
2016-08-21 02:21:39 +03:00
|
|
|
|
2025-01-17 21:37:35 +01:00
|
|
|
public mixed $username = null;
|
2018-04-17 23:47:25 +03:00
|
|
|
|
2025-01-17 21:37:35 +01:00
|
|
|
public mixed $password = null;
|
2018-04-17 23:47:25 +03:00
|
|
|
|
2025-01-17 21:37:35 +01:00
|
|
|
public mixed $clientToken = null;
|
2016-08-21 02:21:39 +03:00
|
|
|
|
2025-01-17 21:37:35 +01:00
|
|
|
public mixed $requestUser = null;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
private readonly LoginServiceInterface $loginService,
|
|
|
|
private readonly TokensFactory $tokensFactory,
|
|
|
|
array $config = [],
|
|
|
|
) {
|
|
|
|
parent::__construct($config);
|
|
|
|
}
|
2021-03-06 10:37:58 +01:00
|
|
|
|
2019-12-04 21:10:15 +03:00
|
|
|
public function rules(): array {
|
2016-08-21 02:21:39 +03:00
|
|
|
return [
|
2024-11-24 10:25:22 +01:00
|
|
|
[['username', 'password'], RequiredValidator::class],
|
2018-01-02 20:22:56 +03:00
|
|
|
[['clientToken'], ClientTokenValidator::class],
|
2021-03-06 10:37:58 +01:00
|
|
|
[['requestUser'], 'boolean'],
|
2016-08-21 02:21:39 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-12-02 15:10:55 +05:00
|
|
|
* @throws ForbiddenOperationException
|
2016-08-21 02:21:39 +03:00
|
|
|
*/
|
2019-12-04 21:10:15 +03:00
|
|
|
public function authenticate(): AuthenticateData {
|
|
|
|
// This validating method will throw an exception in case when validation will not pass successfully
|
2016-08-21 02:21:39 +03:00
|
|
|
$this->validate();
|
|
|
|
|
2016-08-29 02:17:45 +03:00
|
|
|
Authserver::info("Trying to authenticate user by login = '{$this->username}'.");
|
2016-08-21 02:21:39 +03:00
|
|
|
|
2025-01-17 21:37:35 +01:00
|
|
|
$password = (string)$this->password;
|
2021-03-08 22:21:10 +01:00
|
|
|
$totp = null;
|
|
|
|
if (preg_match('/.{8,}:(\d{6})$/', $password, $matches) === 1) {
|
|
|
|
$totp = $matches[1];
|
|
|
|
$password = mb_substr($password, 0, -7); // :123456 - 7 chars
|
|
|
|
}
|
|
|
|
|
|
|
|
login:
|
|
|
|
|
2025-01-17 21:37:35 +01:00
|
|
|
$credentials = new Credentials(
|
|
|
|
login: (string)$this->username,
|
|
|
|
password: $password,
|
|
|
|
totp: $totp,
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$result = $this->loginService->loginByCredentials($credentials);
|
|
|
|
} catch (Exceptions\InvalidPasswordException $e) {
|
|
|
|
if ($totp !== null) {
|
|
|
|
$password = $this->password;
|
|
|
|
goto login;
|
|
|
|
}
|
2021-03-08 22:21:10 +01:00
|
|
|
|
2025-01-17 21:37:35 +01:00
|
|
|
$this->convertAuthenticationException($e);
|
|
|
|
} catch (AuthenticationException $e) {
|
|
|
|
$this->convertAuthenticationException($e);
|
2021-03-08 22:21:10 +01:00
|
|
|
}
|
2018-04-17 23:47:25 +03:00
|
|
|
|
2025-01-17 21:37:35 +01:00
|
|
|
$account = $result->account;
|
|
|
|
if ($account->status === Account::STATUS_DELETED) {
|
|
|
|
throw new ForbiddenOperationException('Invalid credentials. Invalid username or password.');
|
2016-08-21 02:21:39 +03:00
|
|
|
}
|
|
|
|
|
2024-11-24 10:25:22 +01:00
|
|
|
$clientToken = $this->clientToken ?: Uuid::uuid4()->toString();
|
2025-01-17 21:37:35 +01:00
|
|
|
$token = $this->tokensFactory->createForMinecraftAccount($account, $clientToken);
|
2024-12-02 15:10:55 +05:00
|
|
|
$dataModel = new AuthenticateData($account, $token->toString(), $clientToken, (bool)$this->requestUser);
|
2019-12-10 22:51:11 +03:00
|
|
|
/** @var OauthSession|null $minecraftOauthSession */
|
2020-09-30 20:30:04 +03:00
|
|
|
$minecraftOauthSession = $account->getOauthSessions()
|
2019-12-10 22:51:11 +03:00
|
|
|
->andWhere(['client_id' => OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER])
|
2020-09-30 20:30:04 +03:00
|
|
|
->one();
|
|
|
|
if ($minecraftOauthSession === null) {
|
2019-12-10 22:51:11 +03:00
|
|
|
$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 02:17:45 +03:00
|
|
|
|
2020-09-30 20:30:04 +03:00
|
|
|
$minecraftOauthSession->last_used_at = time();
|
|
|
|
Assert::true($minecraftOauthSession->save());
|
|
|
|
|
2016-08-29 02:17:45 +03:00
|
|
|
Authserver::info("User with id = {$account->id}, username = '{$account->username}' and email = '{$account->email}' successfully logged in.");
|
|
|
|
|
|
|
|
return $dataModel;
|
|
|
|
}
|
2016-08-21 02:21:39 +03:00
|
|
|
|
2025-01-17 21:37:35 +01:00
|
|
|
/**
|
|
|
|
* @throws \api\modules\authserver\exceptions\ForbiddenOperationException
|
|
|
|
*/
|
|
|
|
private function convertAuthenticationException(AuthenticationException $e): never {
|
|
|
|
throw match ($e::class) {
|
|
|
|
Exceptions\AccountBannedException::class => new ForbiddenOperationException('This account has been suspended.'),
|
|
|
|
Exceptions\TotpRequiredException::class => new ForbiddenOperationException('Account protected with two factor auth.'),
|
|
|
|
default => new ForbiddenOperationException('Invalid credentials. Invalid username or password.'),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-21 02:21:39 +03:00
|
|
|
}
|