Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #258 whereby logout fails if the JWT already being used has exp… #259

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
You can find and compare releases at the GitHub release page.

## [Unreleased]
- SetSecret regenerates config with new secret in the Lcobucci provider

### Added
- Fixes #259 - Can't logout with an expired token

### Removed

## [2.6.0] 2024-07-11

### Added
- New `getUserId` method

## [2.5.0] 2024-07-03

### Added
- Refresh iat claim when refreshing a token

## [2.4.0] 2024-05-27

### Added
- Support for lcobucci/jwt^5.0 (and dropped support for ^4.0)
- New `getUserId` method
- SetSecret regenerates config with new secret in the Lcobucci provider

## [2.3.0] 2024-05-09

Expand Down
6 changes: 5 additions & 1 deletion src/JWTGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@ public function login(JWTSubject $user)
*/
public function logout($forceForever = false)
{
$this->requireToken()->invalidate($forceForever);
try {
$this->requireToken()->invalidate($forceForever);
} catch (JWTException $e) {
// Proceed with the logout as normal if we can't invalidate the token
}

$this->fireLogoutEvent($this->user);

Expand Down
20 changes: 20 additions & 0 deletions tests/JWTGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Illuminate\Http\Request;
use Mockery\LegacyMockInterface;
use PHPOpenSourceSaver\JWTAuth\Exceptions\JWTException;
use PHPOpenSourceSaver\JWTAuth\Exceptions\TokenExpiredException;
use PHPOpenSourceSaver\JWTAuth\Exceptions\UserNotDefinedException;
use PHPOpenSourceSaver\JWTAuth\Factory;
use PHPOpenSourceSaver\JWTAuth\JWT;
Expand Down Expand Up @@ -353,6 +354,25 @@ public function testItShouldLogoutTheUserByInvalidatingTheToken()
$this->assertNull($this->guard->getUser());
}

public function testItShouldLogoutTheUserEvenWithExpiredToken()
{
$this->jwt->shouldReceive('setRequest')->andReturn($this->jwt);
$this->jwt->shouldReceive('getToken')->once()->andReturn(true);
$this->jwt->shouldReceive('invalidate')->andThrow(TokenExpiredException::class);
$this->jwt->shouldReceive('unsetToken')->once();

$this->eventDispatcher->shouldReceive('dispatch')
->never()
->with(\Mockery::type(Authenticated::class));

$this->eventDispatcher->shouldReceive('dispatch')
->once()
->with(\Mockery::type(Logout::class));

$this->guard->logout();
$this->assertNull($this->guard->getUser());
}

public function testItShouldRefreshTheToken()
{
$this->jwt->shouldReceive('setRequest')->andReturn($this->jwt);
Expand Down
Loading