2016-05-30 05:14:17 +05:30
|
|
|
<?php
|
2019-07-26 19:34:57 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
namespace api\components\User;
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
use common\models\Account;
|
2016-05-30 05:14:17 +05:30
|
|
|
use common\models\AccountSession;
|
2019-12-10 04:08:09 +05:30
|
|
|
use common\models\OauthClient;
|
|
|
|
use Webmozart\Assert\Assert;
|
2016-05-30 05:14:17 +05:30
|
|
|
use yii\web\User as YiiUserComponent;
|
|
|
|
|
2016-06-05 19:31:35 +05:30
|
|
|
/**
|
|
|
|
* @property AccountSession|null $activeSession
|
2017-09-19 22:36:16 +05:30
|
|
|
* @property IdentityInterface|null $identity
|
2016-07-17 23:16:04 +05:30
|
|
|
*
|
2017-09-19 22:36:16 +05:30
|
|
|
* @method IdentityInterface|null loginByAccessToken($token, $type = null)
|
|
|
|
* @method IdentityInterface|null getIdentity($autoRenew = true)
|
2016-06-05 19:31:35 +05:30
|
|
|
*/
|
2016-05-30 05:14:17 +05:30
|
|
|
class Component extends YiiUserComponent {
|
|
|
|
|
2017-12-02 23:34:48 +05:30
|
|
|
public const KEEP_MINECRAFT_SESSIONS = 1;
|
|
|
|
public const KEEP_SITE_SESSIONS = 2;
|
|
|
|
public const KEEP_CURRENT_SESSION = 4;
|
2017-09-19 22:36:16 +05:30
|
|
|
|
2016-08-04 03:37:21 +05:30
|
|
|
public $enableSession = false;
|
|
|
|
|
|
|
|
public $loginUrl = null;
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
/**
|
2019-08-01 14:47:12 +05:30
|
|
|
* We don't use the standard web authorization mechanism via cookies.
|
|
|
|
* Therefore, only one static method findIdentityByAccessToken is used from
|
|
|
|
* the whole IdentityInterface interface, which is implemented in the factory.
|
|
|
|
* The method only used from loginByAccessToken from base class.
|
|
|
|
*
|
|
|
|
* @var string
|
2017-09-19 22:36:16 +05:30
|
|
|
*/
|
2019-08-01 14:47:12 +05:30
|
|
|
public $identityClass = IdentityFactory::class;
|
2019-07-25 18:59:08 +05:30
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* The method searches AccountSession model, which one has been used to create current JWT token.
|
|
|
|
* null will be returned in case when any of the following situations occurred:
|
|
|
|
* - The user isn't authorized
|
2019-08-02 05:59:20 +05:30
|
|
|
* - The user isn't authorized via JWT token
|
2019-07-15 04:29:56 +05:30
|
|
|
* - No session key found in the token. This is possible if the user chose not to remember me
|
|
|
|
* or just some old tokens, without the support of saving the used session
|
2016-06-05 19:31:35 +05:30
|
|
|
*
|
|
|
|
* @return AccountSession|null
|
2016-05-30 05:14:17 +05:30
|
|
|
*/
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getActiveSession(): ?AccountSession {
|
2016-06-05 19:31:35 +05:30
|
|
|
if ($this->getIsGuest()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
/** @var IdentityInterface $identity */
|
|
|
|
$identity = $this->getIdentity();
|
|
|
|
if (!$identity instanceof JwtIdentity) {
|
2017-09-19 22:36:16 +05:30
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
$sessionId = $identity->getToken()->getClaim('jti', false);
|
2019-08-01 14:47:12 +05:30
|
|
|
if ($sessionId === false) {
|
2016-06-05 19:31:35 +05:30
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-01 14:47:12 +05:30
|
|
|
return AccountSession::findOne($sessionId);
|
2016-06-05 19:31:35 +05:30
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function terminateSessions(Account $account, int $mode = 0): void {
|
|
|
|
$currentSession = null;
|
|
|
|
if ($mode & self::KEEP_CURRENT_SESSION) {
|
|
|
|
$currentSession = $this->getActiveSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($mode & self::KEEP_SITE_SESSIONS)) {
|
|
|
|
foreach ($account->sessions as $session) {
|
|
|
|
if ($currentSession === null || $currentSession->id !== $session->id) {
|
2017-02-23 04:31:32 +05:30
|
|
|
$session->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
if (!($mode & self::KEEP_MINECRAFT_SESSIONS)) {
|
2019-12-10 04:08:09 +05:30
|
|
|
/** @var \common\models\OauthSession|null $minecraftSession */
|
2019-12-11 01:21:11 +05:30
|
|
|
$minecraftSession = $account->getOauthSessions()
|
2019-12-10 04:08:09 +05:30
|
|
|
->andWhere(['client_id' => OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER])
|
|
|
|
->one();
|
|
|
|
if ($minecraftSession !== null) {
|
|
|
|
$minecraftSession->revoked_at = time();
|
|
|
|
Assert::true($minecraftSession->save());
|
|
|
|
}
|
2017-02-23 04:31:32 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
}
|