Skip to content

Commit

Permalink
Merge pull request #7 from Messhias/feature/throw-blacklisted-excepti…
Browse files Browse the repository at this point in the history
…on-be-optional

Created new configurations to optional TokenBlacklistedException
  • Loading branch information
eschricker authored Sep 21, 2021
2 parents 7e4efa1 + 3d07008 commit 48015c4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
14 changes: 12 additions & 2 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,19 @@
| Specify the provider that is used to store tokens in the blacklist.
|
*/

'storage' => PHPOpenSourceSaver\JWTAuth\Providers\Storage\Illuminate::class,

/*
|--------------------------------------------------------------------------
| Show blacklisted token option
|--------------------------------------------------------------------------
|
| Specify if you want to show black listed token exception on the laravel logs.
|
*/

'storage' => PHPOpenSourceSaver\JWTAuth\Providers\Storage\Illuminate::class,

'show_black_list_exception' => env('JWT_SHOW_BLACKLIST_EXCEPTION', true),
],

];
36 changes: 35 additions & 1 deletion src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Manager
*/
protected $persistentClaims = [];

protected $showBlackListException = true;

/**
* Constructor.
*
Expand Down Expand Up @@ -106,7 +108,14 @@ public function decode(Token $token, $checkBlacklist = true)
->make();

if ($checkBlacklist && $this->blacklistEnabled && $this->blacklist->has($payload)) {
throw new TokenBlacklistedException('The token has been blacklisted');
if (
$checkBlacklist &&
$this->blacklistEnabled &&
$this->blacklist->has($payload) &&
$this->getBlackListExceptionEnabled()
) {
throw new TokenBlacklistedException('The token has been blacklisted');
}
}

return $payload;
Expand Down Expand Up @@ -229,6 +238,31 @@ public function setBlacklistEnabled($enabled)
return $this;
}

/**
* Configuration to set up if show the TokenBlacklistedException
* can be throwable or not.
*
* @param bool $showBlackListException
*
* @removed this
*/
public function setBlackListExceptionEnabled($showBlackListException = true)
{
$this->showBlackListException = $showBlackListException;

return $this;
}

/**
* Get if the blacklist instance is enabled.
*
* @return bool
*/
public function getBlackListExceptionEnabled()
{
return $this->showBlackListException;
}

/**
* Set the claims to be persisted when refreshing a token.
*
Expand Down
3 changes: 2 additions & 1 deletion src/Providers/AbstractServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ protected function registerManager()
);

return $instance->setBlacklistEnabled((bool) $this->config('blacklist_enabled'))
->setPersistentClaims($this->config('persistent_claims'));
->setPersistentClaims($this->config('persistent_claims'))
->setBlackListExceptionEnabled((bool) $this->config('show_black_list_exception', true));
});
}

Expand Down
24 changes: 24 additions & 0 deletions tests/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,28 @@ public function it_should_get_the_blacklist()
{
$this->assertInstanceOf(Blacklist::class, $this->manager->getBlacklist());
}

/** @test */
public function test_if_show_blacklisted_exception_configuration_is_enabled()
{
$this->manager->setBlackListExceptionEnabled(true);

$this->assertIsBool($this->manager->gettBlackListExceptionEnabled());
}

/** @test */
public function test_if_black_listed_exception_is_set_to_true()
{
$this->manager->setBlackListExceptionEnabled(true);

$this->assertTrue($this->manager->gettBlackListExceptionEnabled());
}

/** @test */
public function test_if_black_listed_exception_is_set_to_false()
{
$this->manager->setBlackListExceptionEnabled(false);

$this->assertFalse($this->manager->gettBlackListExceptionEnabled());
}
}

0 comments on commit 48015c4

Please sign in to comment.