-
-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from chalasr/failure_events
Allow to set a custom response in case of authentication failure or invalid/not found token
- Loading branch information
Showing
7 changed files
with
150 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Event; | ||
|
||
/** | ||
* JWTInvalidEvent | ||
* | ||
* @author Robin Chalas <robin.chalas@gmail.com> | ||
*/ | ||
class JWTInvalidEvent extends AuthenticationFailureEvent | ||
{ | ||
} |
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
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 |
---|---|---|
|
@@ -4,20 +4,24 @@ | |
|
||
use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\TokenExtractorInterface; | ||
use Lexik\Bundle\JWTAuthenticationBundle\Security\Authentication\Token\JWTUserToken; | ||
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTInvalidEvent; | ||
use Lexik\Bundle\JWTAuthenticationBundle\Events; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; | ||
use Symfony\Component\Security\Core\SecurityContextInterface; | ||
use Symfony\Component\Security\Http\Firewall\ListenerInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
/** | ||
* JWTListener | ||
* | ||
* @author Nicolas Cabot <n.cabot@lexik.fr> | ||
* @author Robin Chalas <robin.chalas@gmail.com> | ||
* @author Robin Chalas <robin.chalas@gmail.com> | ||
*/ | ||
class JWTListener implements ListenerInterface | ||
{ | ||
|
@@ -31,6 +35,11 @@ class JWTListener implements ListenerInterface | |
*/ | ||
protected $authenticationManager; | ||
|
||
/** | ||
* @var EventDispatcherInterface | ||
*/ | ||
protected $dispatcher; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
|
@@ -46,8 +55,7 @@ class JWTListener implements ListenerInterface | |
* @param AuthenticationManagerInterface $authenticationManager | ||
* @param array $config | ||
*/ | ||
public function __construct($tokenStorage, AuthenticationManagerInterface $authenticationManager, array $config = [] | ||
) | ||
public function __construct($tokenStorage, AuthenticationManagerInterface $authenticationManager, array $config = []) | ||
{ | ||
if (!$tokenStorage instanceof TokenStorageInterface && !$tokenStorage instanceof SecurityContextInterface) { | ||
throw new \InvalidArgumentException('Argument 1 should be an instance of Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface or Symfony\Component\Security\Core\SecurityContextInterface'); | ||
|
@@ -64,37 +72,38 @@ public function __construct($tokenStorage, AuthenticationManagerInterface $authe | |
*/ | ||
public function handle(GetResponseEvent $event) | ||
{ | ||
if (!($requestToken = $this->getRequestToken($event->getRequest()))) { | ||
return; | ||
} | ||
|
||
$token = new JWTUserToken(); | ||
$token->setRawToken($requestToken); | ||
$request = $event->getRequest(); | ||
|
||
try { | ||
|
||
$requestToken = $this->getRequestToken($request); | ||
|
||
$token = new JWTUserToken(); | ||
$token->setRawToken($requestToken); | ||
|
||
$authToken = $this->authenticationManager->authenticate($token); | ||
$this->tokenStorage->setToken($authToken); | ||
|
||
return; | ||
|
||
} catch (AuthenticationException $failed) { | ||
|
||
$statusCode = 401; | ||
|
||
if ($this->config['throw_exceptions']) { | ||
throw $failed; | ||
} | ||
|
||
$data = [ | ||
'code' => $statusCode, | ||
'code' => 401, | ||
'message' => $failed->getMessage(), | ||
]; | ||
|
||
$response = new JsonResponse($data, $statusCode); | ||
$response = new JsonResponse($data, $data['code']); | ||
$response->headers->set('WWW-Authenticate', 'Bearer'); | ||
|
||
$event->setResponse($response); | ||
$jwtInvalidEvent = new JWTInvalidEvent($request, $failed, $response); | ||
$this->dispatcher->dispatch(Events::JWT_INVALID, $jwtInvalidEvent); | ||
|
||
$event->setResponse($jwtInvalidEvent->getResponse()); | ||
} | ||
} | ||
|
||
|
@@ -106,6 +115,14 @@ public function addTokenExtractor(TokenExtractorInterface $extractor) | |
$this->tokenExtractors[] = $extractor; | ||
} | ||
|
||
/** | ||
* @param EventDispatcherInterface $dispatcher | ||
*/ | ||
public function setDispatcher(EventDispatcherInterface $dispatcher) | ||
{ | ||
$this->dispatcher = $dispatcher; | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* | ||
|
@@ -120,6 +137,6 @@ protected function getRequestToken(Request $request) | |
} | ||
} | ||
|
||
return false; | ||
throw new AuthenticationCredentialsNotFoundException('No JWT token found'); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
slepp
|
||
} | ||
} |
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 is a breaking change in the behaviour of using IS_AUTHENTICATE_ANONYMOUSLY in the security configuration. Parts of the API that were open to the public are returning 401 invalid from here as they're not providing tokens to the API.