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;
|
2017-09-19 22:36:16 +05:30
|
|
|
use Exception;
|
2019-07-26 19:34:57 +05:30
|
|
|
use InvalidArgumentException;
|
2016-05-30 05:14:17 +05:30
|
|
|
use Yii;
|
2017-12-02 23:34:48 +05:30
|
|
|
use yii\web\UnauthorizedHttpException;
|
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
|
|
|
|
2017-09-30 03:14:05 +05:30
|
|
|
public function findIdentityByAccessToken($accessToken): ?IdentityInterface {
|
2017-12-02 23:34:48 +05:30
|
|
|
if (empty($accessToken)) {
|
2017-09-30 03:14:05 +05:30
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
try {
|
2019-08-01 14:47:12 +05:30
|
|
|
return IdentityFactory::findIdentityByAccessToken($accessToken);
|
2017-12-02 23:34:48 +05:30
|
|
|
} catch (UnauthorizedHttpException $e) {
|
2019-08-01 14:47:12 +05:30
|
|
|
// TODO: if this exception is catched there, how it forms "Token expired" exception?
|
2017-12-02 23:34:48 +05:30
|
|
|
// Do nothing. It's okay to catch this.
|
2017-09-19 22:36:16 +05:30
|
|
|
} catch (Exception $e) {
|
|
|
|
Yii::error($e);
|
2016-05-30 05:14:17 +05:30
|
|
|
}
|
2017-12-02 23:34:48 +05:30
|
|
|
|
|
|
|
return null;
|
2017-09-19 22:36:16 +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
|
|
|
|
* - There is no header with a token
|
|
|
|
* - Token validation isn't passed and some exception has been thrown
|
|
|
|
* - 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;
|
|
|
|
}
|
|
|
|
|
2016-12-11 20:28:08 +05:30
|
|
|
$bearer = $this->getBearerToken();
|
2017-09-19 22:36:16 +05:30
|
|
|
if ($bearer === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-06-05 19:31:35 +05:30
|
|
|
try {
|
2019-08-01 14:47:12 +05:30
|
|
|
$token = Yii::$app->tokens->parse($bearer);
|
|
|
|
} catch (InvalidArgumentException $e) {
|
2016-06-05 19:31:35 +05:30
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-01 14:47:12 +05:30
|
|
|
$sessionId = $token->getClaim('jti', false);
|
|
|
|
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)) {
|
|
|
|
foreach ($account->minecraftAccessKeys as $minecraftAccessKey) {
|
2017-02-23 04:31:32 +05:30
|
|
|
$minecraftAccessKey->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
private function getBearerToken(): ?string {
|
2016-12-11 20:28:08 +05:30
|
|
|
$authHeader = Yii::$app->request->getHeaders()->get('Authorization');
|
|
|
|
if ($authHeader === null || !preg_match('/^Bearer\s+(.*?)$/', $authHeader, $matches)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $matches[1];
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
}
|