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 api\exceptions\ThisShouldNotHappenException;
|
|
|
|
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 common\rbac\Roles as R;
|
2016-11-04 14:53:04 +05:30
|
|
|
use DateInterval;
|
|
|
|
use DateTime;
|
2016-06-05 19:31:35 +05:30
|
|
|
use Emarref\Jwt\Algorithm\AlgorithmInterface;
|
2016-05-30 05:14:17 +05:30
|
|
|
use Emarref\Jwt\Algorithm\Hs256;
|
2019-07-25 18:59:08 +05:30
|
|
|
use Emarref\Jwt\Algorithm\Rs256;
|
2016-05-30 05:14:17 +05:30
|
|
|
use Emarref\Jwt\Claim;
|
2019-07-26 19:34:57 +05:30
|
|
|
use Emarref\Jwt\Encryption\Asymmetric as AsymmetricEncryption;
|
2019-07-26 13:41:09 +05:30
|
|
|
use Emarref\Jwt\Encryption\EncryptionInterface;
|
2016-05-30 05:14:17 +05:30
|
|
|
use Emarref\Jwt\Encryption\Factory as EncryptionFactory;
|
2016-06-05 19:31:35 +05:30
|
|
|
use Emarref\Jwt\Exception\VerificationException;
|
2019-07-25 18:59:08 +05:30
|
|
|
use Emarref\Jwt\HeaderParameter\Custom;
|
2016-05-30 05:14:17 +05:30
|
|
|
use Emarref\Jwt\Token;
|
2016-06-05 19:31:35 +05:30
|
|
|
use Emarref\Jwt\Verification\Context as VerificationContext;
|
2017-09-19 22:36:16 +05:30
|
|
|
use Exception;
|
2019-07-26 19:34:57 +05:30
|
|
|
use InvalidArgumentException;
|
2019-07-25 18:59:08 +05:30
|
|
|
use Webmozart\Assert\Assert;
|
2016-05-30 05:14:17 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\base\InvalidConfigException;
|
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
|
|
|
|
2017-12-02 23:34:48 +05:30
|
|
|
public const JWT_SUBJECT_PREFIX = 'ely|';
|
2017-02-23 04:31:32 +05:30
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
private const LATEST_JWT_VERSION = 1;
|
|
|
|
|
2016-08-04 03:37:21 +05:30
|
|
|
public $enableSession = false;
|
|
|
|
|
|
|
|
public $loginUrl = null;
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public $identityClass = Identity::class;
|
2016-08-04 03:37:21 +05:30
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
public $secret;
|
|
|
|
|
2019-07-26 13:41:09 +05:30
|
|
|
public $publicKeyPath;
|
2019-07-25 18:59:08 +05:30
|
|
|
|
2019-07-26 13:41:09 +05:30
|
|
|
public $privateKeyPath;
|
2019-07-25 18:59:08 +05:30
|
|
|
|
2016-11-04 14:53:04 +05:30
|
|
|
public $expirationTimeout = 'PT1H';
|
|
|
|
|
|
|
|
public $sessionTimeout = 'P7D';
|
2016-05-30 05:14:17 +05:30
|
|
|
|
2019-07-26 13:41:09 +05:30
|
|
|
private $publicKey;
|
|
|
|
|
|
|
|
private $privateKey;
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
/**
|
|
|
|
* @var Token[]
|
|
|
|
*/
|
|
|
|
private static $parsedTokensCache = [];
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
public function init() {
|
|
|
|
parent::init();
|
2019-07-25 18:59:08 +05:30
|
|
|
Assert::notEmpty($this->secret, 'secret must be specified');
|
2019-07-26 13:41:09 +05:30
|
|
|
Assert::notEmpty($this->publicKeyPath, 'public key path must be specified');
|
|
|
|
Assert::notEmpty($this->privateKeyPath, 'private key path must be specified');
|
|
|
|
}
|
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
|
|
|
/** @var \api\components\User\IdentityInterface|string $identityClass */
|
|
|
|
$identityClass = $this->identityClass;
|
|
|
|
try {
|
|
|
|
return $identityClass::findIdentityByAccessToken($accessToken);
|
2017-12-02 23:34:48 +05:30
|
|
|
} catch (UnauthorizedHttpException $e) {
|
|
|
|
// 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-26 19:34:57 +05:30
|
|
|
public function createJwtAuthenticationToken(Account $account, AccountSession $session = null): Token {
|
2017-09-19 22:36:16 +05:30
|
|
|
$token = $this->createToken($account);
|
2019-07-26 19:34:57 +05:30
|
|
|
if ($session !== null) {
|
2017-09-19 22:36:16 +05:30
|
|
|
$token->addClaim(new Claim\JwtId($session->id));
|
2016-05-30 05:14:17 +05:30
|
|
|
} else {
|
2019-07-15 04:29:56 +05:30
|
|
|
// If we don't remember a session, the token should live longer
|
|
|
|
// so that the session doesn't end while working with the account
|
2016-11-04 14:53:04 +05:30
|
|
|
$token->addClaim(new Claim\Expiration((new DateTime())->add(new DateInterval($this->sessionTimeout))));
|
2016-05-30 05:14:17 +05:30
|
|
|
}
|
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
return $token;
|
2016-05-30 05:14:17 +05:30
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function renewJwtAuthenticationToken(AccountSession $session): AuthenticationResult {
|
2016-05-31 03:33:30 +05:30
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
$account = $session->account;
|
|
|
|
$token = $this->createToken($account);
|
|
|
|
$token->addClaim(new Claim\JwtId($session->id));
|
|
|
|
$jwt = $this->serializeToken($token);
|
2016-05-31 03:33:30 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
$result = new AuthenticationResult($account, $jwt, $session);
|
2016-05-31 03:33:30 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
$session->setIp(Yii::$app->request->userIP);
|
|
|
|
$session->last_refreshed_at = time();
|
|
|
|
if (!$session->save()) {
|
|
|
|
throw new ThisShouldNotHappenException('Cannot update session info');
|
2016-05-31 03:33:30 +05:30
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
$transaction->commit();
|
|
|
|
|
2016-05-31 03:33:30 +05:30
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
public function serializeToken(Token $token): string {
|
|
|
|
$encryption = $this->getEncryptionForVersion(self::LATEST_JWT_VERSION);
|
|
|
|
$this->prepareEncryptionForEncoding($encryption);
|
|
|
|
|
|
|
|
return (new Jwt())->serialize($token, $encryption);
|
|
|
|
}
|
|
|
|
|
2016-06-05 19:31:35 +05:30
|
|
|
/**
|
|
|
|
* @param string $jwtString
|
2019-07-15 04:29:56 +05:30
|
|
|
* @return Token
|
|
|
|
* @throws VerificationException in case when some Claim not pass the validation
|
2016-06-05 19:31:35 +05:30
|
|
|
*/
|
2017-09-19 22:36:16 +05:30
|
|
|
public function parseToken(string $jwtString): Token {
|
|
|
|
$token = &self::$parsedTokensCache[$jwtString];
|
|
|
|
if ($token === null) {
|
|
|
|
$jwt = new Jwt();
|
2018-01-01 19:42:15 +05:30
|
|
|
try {
|
|
|
|
$notVerifiedToken = $jwt->deserialize($jwtString);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new VerificationException('Incorrect token encoding', 0, $e);
|
|
|
|
}
|
2017-09-19 22:36:16 +05:30
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
$versionHeader = $notVerifiedToken->getHeader()->findParameterByName('v');
|
|
|
|
$version = $versionHeader ? $versionHeader->getValue() : 0;
|
|
|
|
$encryption = $this->getEncryptionForVersion($version);
|
|
|
|
$this->prepareEncryptionForDecoding($encryption);
|
2019-07-25 18:59:08 +05:30
|
|
|
|
|
|
|
$context = new VerificationContext($encryption);
|
2017-09-19 22:36:16 +05:30
|
|
|
$context->setSubject(self::JWT_SUBJECT_PREFIX);
|
|
|
|
$jwt->verify($notVerifiedToken, $context);
|
2016-06-05 19:31:35 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
$token = $notVerifiedToken;
|
|
|
|
}
|
2016-05-30 05:14:17 +05:30
|
|
|
|
2016-06-05 19:31:35 +05:30
|
|
|
return $token;
|
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 {
|
2016-12-11 20:28:08 +05:30
|
|
|
$token = $this->parseToken($bearer);
|
2016-06-05 19:31:35 +05:30
|
|
|
} catch (VerificationException $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
$sessionId = $token->getPayload()->findClaimByName(Claim\JwtId::NAME);
|
2016-06-05 19:31:35 +05:30
|
|
|
if ($sessionId === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AccountSession::findOne($sessionId->getValue());
|
|
|
|
}
|
|
|
|
|
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 getPublicKey() {
|
|
|
|
if (empty($this->publicKey)) {
|
|
|
|
if (!($this->publicKey = file_get_contents($this->publicKeyPath))) {
|
|
|
|
throw new InvalidConfigException('invalid public key path');
|
|
|
|
}
|
2019-07-26 13:41:09 +05:30
|
|
|
}
|
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
return $this->publicKey;
|
2019-07-26 13:41:09 +05:30
|
|
|
}
|
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
private function getPrivateKey() {
|
|
|
|
if (empty($this->privateKey)) {
|
|
|
|
if (!($this->privateKey = file_get_contents($this->privateKeyPath))) {
|
|
|
|
throw new InvalidConfigException('invalid private key path');
|
|
|
|
}
|
|
|
|
}
|
2019-07-25 18:59:08 +05:30
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
return $this->privateKey;
|
2016-06-05 19:31:35 +05:30
|
|
|
}
|
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
private function createToken(Account $account): Token {
|
2016-06-05 19:31:35 +05:30
|
|
|
$token = new Token();
|
2019-07-25 18:59:08 +05:30
|
|
|
$token->addHeader(new Custom('v', 1));
|
2018-04-18 02:17:25 +05:30
|
|
|
foreach ($this->getClaims($account) as $claim) {
|
2016-06-05 19:31:35 +05:30
|
|
|
$token->addClaim($claim);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
/**
|
2017-09-19 22:36:16 +05:30
|
|
|
* @param Account $account
|
2016-05-30 05:14:17 +05:30
|
|
|
* @return Claim\AbstractClaim[]
|
|
|
|
*/
|
2019-07-26 19:34:57 +05:30
|
|
|
private function getClaims(Account $account): array {
|
2016-11-04 14:53:04 +05:30
|
|
|
$currentTime = new DateTime();
|
2016-05-30 05:14:17 +05:30
|
|
|
|
|
|
|
return [
|
2017-09-19 22:36:16 +05:30
|
|
|
new ScopesClaim([R::ACCOUNTS_WEB_USER]),
|
2016-05-30 05:14:17 +05:30
|
|
|
new Claim\IssuedAt($currentTime),
|
2016-11-04 14:53:04 +05:30
|
|
|
new Claim\Expiration($currentTime->add(new DateInterval($this->expirationTimeout))),
|
2017-09-19 22:36:16 +05:30
|
|
|
new Claim\Subject(self::JWT_SUBJECT_PREFIX . $account->id),
|
2016-05-30 05:14:17 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
private function getEncryptionForVersion(int $version): EncryptionInterface {
|
|
|
|
return EncryptionFactory::create($this->getAlgorithm($version ?? 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getAlgorithm(int $version): AlgorithmInterface {
|
|
|
|
switch ($version) {
|
|
|
|
case 0:
|
|
|
|
return new Hs256($this->secret);
|
|
|
|
case 1:
|
|
|
|
return new Rs256();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new InvalidArgumentException('Unsupported token version');
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
private function prepareEncryptionForEncoding(EncryptionInterface $encryption): void {
|
|
|
|
if ($encryption instanceof AsymmetricEncryption) {
|
|
|
|
$encryption->setPrivateKey($this->getPrivateKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function prepareEncryptionForDecoding(EncryptionInterface $encryption) {
|
|
|
|
if ($encryption instanceof AsymmetricEncryption) {
|
|
|
|
$encryption->setPublicKey($this->getPublicKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
}
|