mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Отрефакторен компонент User\Component
Добавлен метод getActiveSession Добавлена логика для функции "Разлогинить всех" в форме смены пароля
This commit is contained in:
@@ -3,17 +3,24 @@ namespace api\components\User;
|
||||
|
||||
use api\models\AccountIdentity;
|
||||
use common\models\AccountSession;
|
||||
use Emarref\Jwt\Algorithm\AlgorithmInterface;
|
||||
use Emarref\Jwt\Algorithm\Hs256;
|
||||
use Emarref\Jwt\Claim;
|
||||
use Emarref\Jwt\Encryption\Factory as EncryptionFactory;
|
||||
use Emarref\Jwt\Encryption\Factory;
|
||||
use Emarref\Jwt\Exception\VerificationException;
|
||||
use Emarref\Jwt\Jwt;
|
||||
use Emarref\Jwt\Token;
|
||||
use Emarref\Jwt\Verification\Context as VerificationContext;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\web\IdentityInterface;
|
||||
use yii\web\User as YiiUserComponent;
|
||||
|
||||
/**
|
||||
* @property AccountSession|null $activeSession
|
||||
*/
|
||||
class Component extends YiiUserComponent {
|
||||
|
||||
public $secret;
|
||||
@@ -43,8 +50,7 @@ class Component extends YiiUserComponent {
|
||||
|
||||
$id = $identity->getId();
|
||||
$ip = Yii::$app->request->userIP;
|
||||
|
||||
$jwt = $this->getJWT($identity);
|
||||
$token = $this->createToken($identity);
|
||||
if ($rememberMe) {
|
||||
$session = new AccountSession();
|
||||
$session->account_id = $id;
|
||||
@@ -53,10 +59,14 @@ class Component extends YiiUserComponent {
|
||||
if (!$session->save()) {
|
||||
throw new ErrorException('Cannot save account session model');
|
||||
}
|
||||
|
||||
$token->addClaim(new SessionIdClaim($session->id));
|
||||
} else {
|
||||
$session = null;
|
||||
}
|
||||
|
||||
$jwt = $this->serializeToken($token);
|
||||
|
||||
Yii::info("User '{$id}' logged in from {$ip}.", __METHOD__);
|
||||
|
||||
$result = new LoginResult($identity, $jwt, $session);
|
||||
@@ -70,7 +80,8 @@ class Component extends YiiUserComponent {
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$identity = new AccountIdentity($account->attributes);
|
||||
$jwt = $this->getJWT($identity);
|
||||
$token = $this->createToken($identity);
|
||||
$jwt = $this->serializeToken($token);
|
||||
|
||||
$result = new RenewResult($identity, $jwt);
|
||||
|
||||
@@ -89,26 +100,79 @@ class Component extends YiiUserComponent {
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getJWT(IdentityInterface $identity) {
|
||||
/**
|
||||
* @param string $jwtString
|
||||
* @return Token распаршенный токен
|
||||
* @throws VerificationException если один из Claims не пройдёт проверку
|
||||
*/
|
||||
public function parseToken(string $jwtString) : Token {
|
||||
$hostInfo = Yii::$app->request->hostInfo;
|
||||
|
||||
$jwt = new Jwt();
|
||||
$token = $jwt->deserialize($jwtString);
|
||||
$context = new VerificationContext(Factory::create($this->getAlgorithm()));
|
||||
$context->setAudience($hostInfo);
|
||||
$context->setIssuer($hostInfo);
|
||||
$jwt->verify($token, $context);
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Метод находит AccountSession модель, относительно которой был выдан текущий JWT токен.
|
||||
* В случае, если на пути поиска встретится ошибка, будет возвращено значение null. Возможные кейсы:
|
||||
* - Юзер не авторизован
|
||||
* - Почему-то нет заголовка с токеном
|
||||
* - Во время проверки токена возникла ошибка, что привело к исключению
|
||||
* - В токене не найдено ключа сессии. Такое возможно, если юзер выбрал "не запоминать меня" или просто старые
|
||||
* токены, без поддержки сохранения используемой сессии
|
||||
*
|
||||
* @return AccountSession|null
|
||||
*/
|
||||
public function getActiveSession() {
|
||||
if ($this->getIsGuest()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$authHeader = Yii::$app->request->getHeaders()->get('Authorization');
|
||||
if ($authHeader === null || !preg_match('/^Bearer\s+(.*?)$/', $authHeader, $matches)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$token = $matches[1];
|
||||
try {
|
||||
$token = $this->parseToken($token);
|
||||
} catch (VerificationException $e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$sessionId = $token->getPayload()->findClaimByName(SessionIdClaim::NAME);
|
||||
if ($sessionId === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return AccountSession::findOne($sessionId->getValue());
|
||||
}
|
||||
|
||||
public function getAlgorithm() : AlgorithmInterface {
|
||||
return new Hs256($this->secret);
|
||||
}
|
||||
|
||||
protected function serializeToken(Token $token) : string {
|
||||
return (new Jwt())->serialize($token, EncryptionFactory::create($this->getAlgorithm()));
|
||||
}
|
||||
|
||||
protected function createToken(IdentityInterface $identity) : Token {
|
||||
$token = new Token();
|
||||
foreach($this->getClaims($identity) as $claim) {
|
||||
$token->addClaim($claim);
|
||||
}
|
||||
|
||||
return $jwt->serialize($token, EncryptionFactory::create($this->getAlgorithm()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Hs256
|
||||
*/
|
||||
public function getAlgorithm() {
|
||||
return new Hs256($this->secret);
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param IdentityInterface $identity
|
||||
*
|
||||
* @return Claim\AbstractClaim[]
|
||||
*/
|
||||
protected function getClaims(IdentityInterface $identity) {
|
||||
|
17
api/components/User/SessionIdClaim.php
Normal file
17
api/components/User/SessionIdClaim.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace api\components\User;
|
||||
|
||||
use Emarref\Jwt\Claim\AbstractClaim;
|
||||
|
||||
class SessionIdClaim extends AbstractClaim {
|
||||
|
||||
const NAME = 'sid';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getName() {
|
||||
return self::NAME;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user