forked from markitosgv/JWTRefreshTokenBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes markitosgv#287 - Add LogoutEventListener
This commit introcudes a `LogoutEventListener` which invalidates the given `refresh_token` and unsets the cookie, if enabled. If there is no `refresh_token`, an error is returned and the cookie is not unset. The same happens if the supplied `refresh_token` is invalid. Because the `LogoutEventListener` always sets a response, it would inhibit normal logout behavior and therefore should only run on a specifically configured firewall. Therefore a new configuration option is introduced, called `logout_firewall`, which contains the name of the firewall that triggers the logout event we want to hook into (default: `api`).
- Loading branch information
Showing
5 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the GesdinetJWTRefreshTokenBundle package. | ||
* | ||
* (c) Gesdinet <http://www.gesdinet.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Gesdinet\JWTRefreshTokenBundle\EventListener; | ||
|
||
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface; | ||
use Gesdinet\JWTRefreshTokenBundle\Request\Extractor\ExtractorInterface; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\Security\Http\Event\LogoutEvent; | ||
|
||
class LogoutEventListener | ||
{ | ||
/** | ||
* @var RefreshTokenManagerInterface | ||
*/ | ||
private $refreshTokenManager; | ||
|
||
/** | ||
* @var ExtractorInterface | ||
*/ | ||
private $refreshTokenExtractor; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $tokenParameterName; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $cookieSettings; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $logout_firewall_context; | ||
|
||
public function __construct( | ||
RefreshTokenManagerInterface $refreshTokenManager, | ||
ExtractorInterface $refreshTokenExtractor, | ||
string $tokenParameterName, | ||
array $cookieSettings, | ||
string $logout_firewall_context, | ||
) { | ||
$this->refreshTokenManager = $refreshTokenManager; | ||
$this->refreshTokenExtractor = $refreshTokenExtractor; | ||
$this->tokenParameterName = $tokenParameterName; | ||
$this->cookieSettings = array_merge([ | ||
'enabled' => false, | ||
'same_site' => 'lax', | ||
'path' => '/', | ||
'domain' => null, | ||
'http_only' => true, | ||
'secure' => true, | ||
'remove_token_from_body' => true, | ||
], $cookieSettings); | ||
$this->logout_firewall_context = $logout_firewall_context; | ||
} | ||
|
||
public function onLogout(LogoutEvent $event): void | ||
{ | ||
$request = $event->getRequest(); | ||
$current_firewall_context = $request->attributes->get('_firewall_context'); | ||
|
||
if ($current_firewall_context !== $this->logout_firewall_context) { | ||
return; | ||
} | ||
|
||
$tokenString = $this->refreshTokenExtractor->getRefreshToken($request, $this->tokenParameterName); | ||
if ($tokenString === null) { | ||
$event->setResponse( | ||
new JsonResponse( | ||
[ | ||
'code' => 422, | ||
'message' => 'No refresh_token found.', | ||
], | ||
JsonResponse::HTTP_UNPROCESSABLE_ENTITY | ||
) | ||
); | ||
} else { | ||
$refreshToken = $this->refreshTokenManager->get($tokenString); | ||
if ($refreshToken === null) { | ||
$event->setResponse( | ||
new JsonResponse( | ||
[ | ||
'code' => 422, | ||
'message' => 'Invalid refresh_token supplied.', | ||
], | ||
JsonResponse::HTTP_UNPROCESSABLE_ENTITY | ||
) | ||
); | ||
} else { | ||
$this->refreshTokenManager->delete($refreshToken); | ||
$event->setResponse( | ||
new JsonResponse( | ||
[ | ||
'code' => 200, | ||
'message' => 'The refresh_token has been invalidated.', | ||
], | ||
JsonResponse::HTTP_OK | ||
) | ||
); | ||
} | ||
} | ||
|
||
if ($this->cookieSettings['enabled']) { | ||
$response = $event->getResponse(); | ||
$response->headers->clearCookie( | ||
$this->tokenParameterName, | ||
$this->cookieSettings['path'], | ||
$this->cookieSettings['domain'], | ||
$this->cookieSettings['secure'], | ||
$this->cookieSettings['http_only'], | ||
$this->cookieSettings['same_site'] | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters