From eed634ec574e2ec709c39b35d998afe90fcc5f0d Mon Sep 17 00:00:00 2001 From: Elias Luhr Date: Fri, 13 Jan 2023 14:48:59 +0100 Subject: [PATCH] what if no token? supress? --- src/components/TokenManager.php | 33 ++++++++++++------- .../TokenManagerStorageInterface.php | 4 +-- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/components/TokenManager.php b/src/components/TokenManager.php index cb71078..d7825e1 100644 --- a/src/components/TokenManager.php +++ b/src/components/TokenManager.php @@ -2,9 +2,9 @@ namespace dmstr\tokenManager\components; -use Lcobucci\JWT\UnencryptedToken; use dmstr\tokenManager\exceptions\LoadTokenException; use dmstr\tokenManager\interfaces\TokenManagerStorageInterface; +use Lcobucci\JWT\UnencryptedToken; use Yii; /** @@ -13,6 +13,13 @@ class TokenManager extends BaseTokenManager implements TokenManagerStorageInterface { + /** + * Suppress all exceptions + * + * @var bool + */ + public bool $suppressExceptions = true; + /** * session value identifier (key) */ @@ -37,20 +44,18 @@ public function setToken(UnencryptedToken $token): void */ public function getRoles(): array { - if ($this->isStorageEnabled()) { - $this->loadTokenFromStorage(); + if ($this->isStorageEnabled() && $this->loadTokenFromStorage()) { + return parent::getRoles(); } - - return parent::getRoles(); + return []; } public function getClaim(string $name, $default = null): mixed { - if ($this->isStorageEnabled()) { - $this->loadTokenFromStorage(); + if ($this->isStorageEnabled() && $this->loadTokenFromStorage()) { + return parent::getClaim($name, $default); } - - return parent::getClaim($name, $default); + return $default; } /** @@ -67,17 +72,21 @@ public function persistTokenInStorage(): void * Load saved token from (session) storage * * @throws LoadTokenException - * @return void + * @return bool */ - public function loadTokenFromStorage(): void + public function loadTokenFromStorage(): bool { /** @var UnencryptedToken|null $token */ $token = Yii::$app->getSession()->get(static::TOKEN_MANAGER_SESSION_KEY); if ($token instanceof UnencryptedToken) { $this->setToken($token); + return true; } else { - throw new LoadTokenException(); + if (!$this->suppressExceptions) { + throw new LoadTokenException(); + } } + return false; } /** diff --git a/src/interfaces/TokenManagerStorageInterface.php b/src/interfaces/TokenManagerStorageInterface.php index 8f73dd0..18ba5fc 100644 --- a/src/interfaces/TokenManagerStorageInterface.php +++ b/src/interfaces/TokenManagerStorageInterface.php @@ -14,9 +14,9 @@ public function persistTokenInStorage(): void; /** * Load saved token from (session) storage * - * @return void + * @return bool */ - public function loadTokenFromStorage(): void; + public function loadTokenFromStorage(): bool; /** * Check whether the storage is enabled / disabled