-
-
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.
Add Events::JWT_INVALID and Events::AUTHENTICATION_FAILURE listening …
…feature Allow users to create an AuthenticationFailureListener and set a custom Response to the Events::AUTHENTICATION_FAILURE Add Events::JWT_INVALID and allow users to listen on it and set a custom Response Fix services Replace JWTInvalidEvent|AuthenticationFailureEvent::setMessage() by setResponse(), Replace dispatcher constructor injection in favor of setter injection Handle case of no token found as JWTInvalid Update documentation for JWT_INVALID & AUTHENTICATION_FAILURE Events Fix mistake in doc remove duplicated code in JWTInvalidEvent by extending AuthenticationFailureEvent Rollback lite comit squashing
- 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
* AuthenticationFailureEvent | ||
* | ||
* @author Emmanuel Vella <[email protected]> | ||
* @author Robin Chalas <[email protected]> | ||
*/ | ||
class AuthenticationFailureEvent extends Event | ||
{ | ||
|
@@ -36,9 +37,9 @@ class AuthenticationFailureEvent extends Event | |
*/ | ||
public function __construct(Request $request, AuthenticationException $exception, Response $response) | ||
{ | ||
$this->request = $request; | ||
$this->request = $request; | ||
$this->exception = $exception; | ||
$this->response = $response; | ||
$this->response = $response; | ||
} | ||
|
||
/** | ||
|
@@ -64,4 +65,12 @@ public function getResponse() | |
{ | ||
return $this->response; | ||
} | ||
|
||
/** | ||
* @param Response $response | ||
*/ | ||
public function setResponse(Response $response) | ||
{ | ||
$this->response = $response; | ||
} | ||
} |
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 <[email protected]> | ||
*/ | ||
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 <[email protected]> | ||
* @author Robin Chalas <[email protected]> | ||
* @author Robin Chalas <[email protected]> | ||
*/ | ||
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 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