Skip to content

Commit

Permalink
what if no token? supress?
Browse files Browse the repository at this point in the history
  • Loading branch information
eluhr committed Jan 13, 2023
1 parent 28e0c82 commit eed634e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
33 changes: 21 additions & 12 deletions src/components/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -13,6 +13,13 @@
class TokenManager extends BaseTokenManager implements TokenManagerStorageInterface
{

/**
* Suppress all exceptions
*
* @var bool
*/
public bool $suppressExceptions = true;

/**
* session value identifier (key)
*/
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/TokenManagerStorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit eed634e

Please sign in to comment.