From 247b04d72e962995af864e811b020ac2304d6f34 Mon Sep 17 00:00:00 2001 From: "a.klapper" Date: Wed, 20 Sep 2023 13:14:13 +0200 Subject: [PATCH 1/6] Make tokenManagerSessionKey configurable --- src/components/TokenManager.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/TokenManager.php b/src/components/TokenManager.php index 63c59e2..9121303 100644 --- a/src/components/TokenManager.php +++ b/src/components/TokenManager.php @@ -36,7 +36,7 @@ class TokenManager extends BaseTokenManager implements TokenManagerStorageInterf /** * session value identifier (key) */ - protected const TOKEN_MANAGER_SESSION_KEY = __CLASS__; + public string $tokenManagerSessionKey = __CLASS__; /** * Static storage fallback if user session is disabled @@ -118,9 +118,9 @@ public function getToken(): UnencryptedToken public function persistTokenInStorage(): void { if ($this->getUser()->enableSession) { - $this->getSession()->set(static::TOKEN_MANAGER_SESSION_KEY, $this->_token); + $this->getSession()->set($this->tokenManagerSessionKey, $this->_token); } else { - static::$_storage['token'] = $this->_token; + static::$_storage['token'] = $this->_token; } } @@ -134,7 +134,7 @@ public function loadTokenFromStorage(): bool { /** @var UnencryptedToken|null $token */ if ($this->getUser()->enableSession) { - $token = $this->getSession()->get(static::TOKEN_MANAGER_SESSION_KEY); + $token = $this->getSession()->get($this->tokenManagerSessionKey); } else { $token = static::$_storage['token'] ?? null; } From 402b2f7706aa488e9f239af90f56b6c172742079 Mon Sep 17 00:00:00 2001 From: "a.klapper" Date: Wed, 20 Sep 2023 13:15:52 +0200 Subject: [PATCH 2/6] Add support for Console Applications --- src/components/ConsoleTokenManager.php | 138 +++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 src/components/ConsoleTokenManager.php diff --git a/src/components/ConsoleTokenManager.php b/src/components/ConsoleTokenManager.php new file mode 100644 index 0000000..f2871d4 --- /dev/null +++ b/src/components/ConsoleTokenManager.php @@ -0,0 +1,138 @@ +isStorageEnabled()) { + $this->persistTokenInStorage(); + } + } + + /** + * @inheritdoc + * + * @throws LoadTokenException if storage is enabled and token load failed + */ + public function getRoles(): array + { + if ($this->isStorageEnabled() && $this->loadTokenFromStorage()) { + return parent::getRoles(); + } + return []; + } + + /** + * @inheritdoc + * @throws LoadTokenException + */ + public function getClaim(string $name, $default = null): mixed + { + if ($this->isStorageEnabled() && $this->loadTokenFromStorage()) { + return parent::getClaim($name, $default); + } + return $default; + } + + /** + * @inheritdoc + * @throws LoadTokenException + */ + public function getToken(): UnencryptedToken + { + if ($this->isStorageEnabled() && !$this->loadTokenFromStorage()) { + throw new LoadTokenException('Error while loading token data'); + } + return parent::getToken(); + } + + /** + * Persist set token in storage + * + * @return void + */ + public function persistTokenInStorage(): void + { + static::$_storage['token'] = $this->_token; + } + + /** + * Load saved token from (session) storage + * + * @return bool + * @throws LoadTokenException + */ + public function loadTokenFromStorage(): bool + { + /** @var UnencryptedToken|null $token */ + $token = static::$_storage['token'] ?? null; + if ($token instanceof UnencryptedToken) { + $this->setToken($token); + return true; + } else { + if (!$this->suppressExceptions) { + throw new LoadTokenException(); + } + } + return false; + } + + /** + * Storage always enabled in console applications + * + * @return bool + */ + public function isStorageEnabled(): bool + { + return true; + } +} From 0a189e72aabaaf227c33c038bae54c0e57f93f0a Mon Sep 17 00:00:00 2001 From: "a.klapper" Date: Wed, 20 Sep 2023 13:30:31 +0200 Subject: [PATCH 3/6] Removed unused key in console mode --- src/components/ConsoleTokenManager.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/components/ConsoleTokenManager.php b/src/components/ConsoleTokenManager.php index f2871d4..55c947a 100644 --- a/src/components/ConsoleTokenManager.php +++ b/src/components/ConsoleTokenManager.php @@ -26,11 +26,6 @@ class ConsoleTokenManager extends BaseTokenManager implements TokenManagerStorag */ public bool $suppressExceptions = true; - /** - * session value identifier (key) - */ - public string $tokenManagerSessionKey = __CLASS__; - /** * Static storage fallback if user session is disabled * From b91010324bef24c43009b77e206d698ec87fba99 Mon Sep 17 00:00:00 2001 From: "a.klapper" Date: Wed, 20 Sep 2023 13:42:02 +0200 Subject: [PATCH 4/6] Removed unused init method --- src/components/ConsoleTokenManager.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/components/ConsoleTokenManager.php b/src/components/ConsoleTokenManager.php index 55c947a..beebd25 100644 --- a/src/components/ConsoleTokenManager.php +++ b/src/components/ConsoleTokenManager.php @@ -33,14 +33,6 @@ class ConsoleTokenManager extends BaseTokenManager implements TokenManagerStorag */ private static array $_storage = []; - /** - * @return void - */ - public function init() - { - parent::init(); - } - /** * @inheritdoc */ From e097e8e19a275bfa6fdd4d0d8e53dcb3edf1a5a3 Mon Sep 17 00:00:00 2001 From: "a.klapper" Date: Wed, 20 Sep 2023 14:53:17 +0200 Subject: [PATCH 5/6] Deal with console applications in main TokenManager Removed console token manager --- src/components/ConsoleTokenManager.php | 125 ------------------------- src/components/TokenManager.php | 11 ++- 2 files changed, 8 insertions(+), 128 deletions(-) delete mode 100644 src/components/ConsoleTokenManager.php diff --git a/src/components/ConsoleTokenManager.php b/src/components/ConsoleTokenManager.php deleted file mode 100644 index beebd25..0000000 --- a/src/components/ConsoleTokenManager.php +++ /dev/null @@ -1,125 +0,0 @@ -isStorageEnabled()) { - $this->persistTokenInStorage(); - } - } - - /** - * @inheritdoc - * - * @throws LoadTokenException if storage is enabled and token load failed - */ - public function getRoles(): array - { - if ($this->isStorageEnabled() && $this->loadTokenFromStorage()) { - return parent::getRoles(); - } - return []; - } - - /** - * @inheritdoc - * @throws LoadTokenException - */ - public function getClaim(string $name, $default = null): mixed - { - if ($this->isStorageEnabled() && $this->loadTokenFromStorage()) { - return parent::getClaim($name, $default); - } - return $default; - } - - /** - * @inheritdoc - * @throws LoadTokenException - */ - public function getToken(): UnencryptedToken - { - if ($this->isStorageEnabled() && !$this->loadTokenFromStorage()) { - throw new LoadTokenException('Error while loading token data'); - } - return parent::getToken(); - } - - /** - * Persist set token in storage - * - * @return void - */ - public function persistTokenInStorage(): void - { - static::$_storage['token'] = $this->_token; - } - - /** - * Load saved token from (session) storage - * - * @return bool - * @throws LoadTokenException - */ - public function loadTokenFromStorage(): bool - { - /** @var UnencryptedToken|null $token */ - $token = static::$_storage['token'] ?? null; - if ($token instanceof UnencryptedToken) { - $this->setToken($token); - return true; - } else { - if (!$this->suppressExceptions) { - throw new LoadTokenException(); - } - } - return false; - } - - /** - * Storage always enabled in console applications - * - * @return bool - */ - public function isStorageEnabled(): bool - { - return true; - } -} diff --git a/src/components/TokenManager.php b/src/components/TokenManager.php index 9121303..4bd42fa 100644 --- a/src/components/TokenManager.php +++ b/src/components/TokenManager.php @@ -9,6 +9,7 @@ use yii\di\Instance; use yii\web\Session; use yii\web\User; +use Yii; /** * @property UnencryptedToken $token @@ -117,7 +118,7 @@ public function getToken(): UnencryptedToken */ public function persistTokenInStorage(): void { - if ($this->getUser()->enableSession) { + if ($this->isStorageEnabled()) { $this->getSession()->set($this->tokenManagerSessionKey, $this->_token); } else { static::$_storage['token'] = $this->_token; @@ -133,7 +134,7 @@ public function persistTokenInStorage(): void public function loadTokenFromStorage(): bool { /** @var UnencryptedToken|null $token */ - if ($this->getUser()->enableSession) { + if ($this->isStorageEnabled()) { $token = $this->getSession()->get($this->tokenManagerSessionKey); } else { $token = static::$_storage['token'] ?? null; @@ -171,7 +172,11 @@ protected function getUser(): User { */ public function isStorageEnabled(): bool { - if ($this->getUser()->enableSession) { + if(Yii::$app instanceof \yii\console\Application){ + return false; + } + + if ( $this->getUser()->enableSession) { return $this->getSession()->getIsActive(); } // use temporary static property for cache From bb5439e6646da9cd362b44fdb5c9a3df6848e18e Mon Sep 17 00:00:00 2001 From: "a.klapper" Date: Wed, 20 Sep 2023 15:52:59 +0200 Subject: [PATCH 6/6] fix source --- src/components/TokenManager.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/components/TokenManager.php b/src/components/TokenManager.php index 4bd42fa..be25e1d 100644 --- a/src/components/TokenManager.php +++ b/src/components/TokenManager.php @@ -51,8 +51,8 @@ class TokenManager extends BaseTokenManager implements TokenManagerStorageInterf private Session $_session; /** - * @throws InvalidConfigException * @return void + * @throws InvalidConfigException */ public function init() { @@ -128,8 +128,8 @@ public function persistTokenInStorage(): void /** * Load saved token from (session) storage * - * @throws LoadTokenException * @return bool + * @throws LoadTokenException */ public function loadTokenFromStorage(): bool { @@ -154,14 +154,16 @@ public function loadTokenFromStorage(): bool /** * @return Session */ - protected function getSession(): Session { + protected function getSession(): Session + { return $this->_session; } /** * @return User */ - protected function getUser(): User { + protected function getUser(): User + { return $this->_user; } @@ -172,14 +174,14 @@ protected function getUser(): User { */ public function isStorageEnabled(): bool { - if(Yii::$app instanceof \yii\console\Application){ + if (Yii::$app instanceof \yii\console\Application) { return false; } - if ( $this->getUser()->enableSession) { + if ($this->getUser()->enableSession) { return $this->getSession()->getIsActive(); } // use temporary static property for cache - return true; + return false; } }