mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Логика уничтожения активных сессий вынесена в компонент User
Теперь при смене пароля и включении двухфакторной аутентификации также очищаются и сессии Minecraft
This commit is contained in:
@@ -27,6 +27,11 @@ use yii\web\User as YiiUserComponent;
|
||||
*/
|
||||
class Component extends YiiUserComponent {
|
||||
|
||||
const TERMINATE_MINECRAFT_SESSIONS = 1;
|
||||
const TERMINATE_SITE_SESSIONS = 2;
|
||||
const DO_NOT_TERMINATE_CURRENT_SESSION = 4;
|
||||
const TERMINATE_ALL = self::TERMINATE_MINECRAFT_SESSIONS | self::TERMINATE_SITE_SESSIONS;
|
||||
|
||||
public $enableSession = false;
|
||||
|
||||
public $loginUrl = null;
|
||||
@@ -184,6 +189,24 @@ class Component extends YiiUserComponent {
|
||||
return AccountSession::findOne($sessionId->getValue());
|
||||
}
|
||||
|
||||
public function terminateSessions(int $mode = self::TERMINATE_ALL | self::DO_NOT_TERMINATE_CURRENT_SESSION): void {
|
||||
$identity = $this->getIdentity();
|
||||
$activeSession = ($mode & self::DO_NOT_TERMINATE_CURRENT_SESSION) ? $this->getActiveSession() : null;
|
||||
if ($mode & self::TERMINATE_SITE_SESSIONS) {
|
||||
foreach ($identity->sessions as $session) {
|
||||
if ($activeSession === null || $activeSession->id !== $session->id) {
|
||||
$session->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($mode & self::TERMINATE_MINECRAFT_SESSIONS) {
|
||||
foreach ($identity->minecraftAccessKeys as $minecraftAccessKey) {
|
||||
$minecraftAccessKey->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getAlgorithm() : AlgorithmInterface {
|
||||
return new Hs256($this->secret);
|
||||
}
|
||||
|
@@ -66,15 +66,7 @@ class ChangePasswordForm extends ApiForm {
|
||||
$account->setPassword($this->newPassword);
|
||||
|
||||
if ($this->logoutAll) {
|
||||
/** @var \api\components\User\Component $userComponent */
|
||||
$userComponent = Yii::$app->user;
|
||||
$sessions = $account->sessions;
|
||||
$activeSession = $userComponent->getActiveSession();
|
||||
foreach ($sessions as $session) {
|
||||
if (!$activeSession || $activeSession->id !== $session->id) {
|
||||
$session->delete();
|
||||
}
|
||||
}
|
||||
Yii::$app->user->terminateSessions();
|
||||
}
|
||||
|
||||
if (!$account->save()) {
|
||||
|
@@ -14,6 +14,7 @@ use common\components\Qr\ElyDecorator;
|
||||
use common\helpers\Error as E;
|
||||
use common\models\Account;
|
||||
use OTPHP\TOTP;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
|
||||
class TwoFactorAuthForm extends ApiForm {
|
||||
@@ -73,12 +74,18 @@ class TwoFactorAuthForm extends ApiForm {
|
||||
return false;
|
||||
}
|
||||
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
|
||||
$account = $this->account;
|
||||
$account->is_otp_enabled = true;
|
||||
if (!$account->save()) {
|
||||
throw new ErrorException('Cannot enable otp for account');
|
||||
}
|
||||
|
||||
Yii::$app->user->terminateSessions();
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -142,6 +149,7 @@ class TwoFactorAuthForm extends ApiForm {
|
||||
* строка составляет 160% от исходной. Поэтому, генерируя исходный приватный ключ, мы должны обеспечить
|
||||
* ему такую длину, чтобы 160% его длины было равно запрошенному значению
|
||||
*
|
||||
* @param int $length
|
||||
* @throws ErrorException
|
||||
*/
|
||||
protected function setOtpSecret(int $length = 24): void {
|
||||
|
Reference in New Issue
Block a user