diff --git a/Security/Guard/JWTTokenAuthenticator.php b/Security/Guard/JWTTokenAuthenticator.php index 43d87a38..2f51983c 100644 --- a/Security/Guard/JWTTokenAuthenticator.php +++ b/Security/Guard/JWTTokenAuthenticator.php @@ -20,6 +20,7 @@ use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface; use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\TokenExtractorInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; @@ -171,10 +172,16 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio if ($authException instanceof ExpiredTokenException) { $event = new JWTExpiredEvent($authException, $response); - $this->dispatcher->dispatch(Events::JWT_EXPIRED, $event); + $eventName = Events::JWT_EXPIRED; } else { $event = new JWTInvalidEvent($authException, $response); - $this->dispatcher->dispatch(Events::JWT_INVALID, $event); + $eventName = Events::JWT_INVALID; + } + + if (interface_exists(ContractsEventDispatcherInterface::class)) { + $this->dispatcher->dispatch($event, $eventName); + } else { + $this->dispatcher->dispatch($eventName, $event); } return $event->getResponse(); @@ -198,7 +205,11 @@ public function start(Request $request, AuthenticationException $authException = $exception = new MissingTokenException('JWT Token not found', 0, $authException); $event = new JWTNotFoundEvent($exception, new JWTAuthenticationFailureResponse($exception->getMessageKey())); - $this->dispatcher->dispatch(Events::JWT_NOT_FOUND, $event); + if (interface_exists(ContractsEventDispatcherInterface::class)) { + $this->dispatcher->dispatch($event, Events::JWT_NOT_FOUND); + } else { + $this->dispatcher->dispatch(Events::JWT_NOT_FOUND, $event); + } return $event->getResponse(); } @@ -226,7 +237,12 @@ public function createAuthenticatedToken(UserInterface $user, $providerKey) $authToken = new JWTUserToken($user->getRoles(), $user, $preAuthToken->getCredentials(), $providerKey); - $this->dispatcher->dispatch(Events::JWT_AUTHENTICATED, new JWTAuthenticatedEvent($preAuthToken->getPayload(), $authToken)); + if (interface_exists(ContractsEventDispatcherInterface::class)) { + $this->dispatcher->dispatch(new JWTAuthenticatedEvent($preAuthToken->getPayload(), $authToken), Events::JWT_AUTHENTICATED); + } else { + $this->dispatcher->dispatch(Events::JWT_AUTHENTICATED, new JWTAuthenticatedEvent($preAuthToken->getPayload(), $authToken)); + } + $this->preAuthenticationTokenStorage->setToken(null); return $authToken; diff --git a/Security/Http/Authentication/AuthenticationFailureHandler.php b/Security/Http/Authentication/AuthenticationFailureHandler.php index 0c8c6922..f8fd180d 100644 --- a/Security/Http/Authentication/AuthenticationFailureHandler.php +++ b/Security/Http/Authentication/AuthenticationFailureHandler.php @@ -6,6 +6,7 @@ use Lexik\Bundle\JWTAuthenticationBundle\Events; use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; @@ -37,7 +38,11 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio { $event = new AuthenticationFailureEvent($exception, new JWTAuthenticationFailureResponse()); - $this->dispatcher->dispatch(Events::AUTHENTICATION_FAILURE, $event); + if (interface_exists(ContractsEventDispatcherInterface::class)) { + $this->dispatcher->dispatch($event, Events::AUTHENTICATION_FAILURE); + } else { + $this->dispatcher->dispatch(Events::AUTHENTICATION_FAILURE, $event); + } return $event->getResponse(); } diff --git a/Security/Http/Authentication/AuthenticationSuccessHandler.php b/Security/Http/Authentication/AuthenticationSuccessHandler.php index b9b06d6c..489e6a86 100644 --- a/Security/Http/Authentication/AuthenticationSuccessHandler.php +++ b/Security/Http/Authentication/AuthenticationSuccessHandler.php @@ -7,6 +7,7 @@ use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationSuccessResponse; use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\User\UserInterface; @@ -45,7 +46,12 @@ public function handleAuthenticationSuccess(UserInterface $user, $jwt = null) $response = new JWTAuthenticationSuccessResponse($jwt); $event = new AuthenticationSuccessEvent(['token' => $jwt], $user, $response); - $this->dispatcher->dispatch(Events::AUTHENTICATION_SUCCESS, $event); + if (interface_exists(ContractsEventDispatcherInterface::class)) { + $this->dispatcher->dispatch($event, Events::AUTHENTICATION_SUCCESS); + } else { + $this->dispatcher->dispatch(Events::AUTHENTICATION_SUCCESS, $event); + } + $response->setData($event->getData()); return $response; diff --git a/Services/JWTManager.php b/Services/JWTManager.php index 47b010e5..6d56c425 100644 --- a/Services/JWTManager.php +++ b/Services/JWTManager.php @@ -9,6 +9,7 @@ use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTEncodedEvent; use Lexik\Bundle\JWTAuthenticationBundle\Events; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface; use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\User\UserInterface; @@ -63,7 +64,12 @@ public function create(UserInterface $user) $this->addUserIdentityToPayload($user, $payload); $jwtCreatedEvent = new JWTCreatedEvent($payload, $user); - $this->dispatcher->dispatch(Events::JWT_CREATED, $jwtCreatedEvent); + if (interface_exists(ContractsEventDispatcherInterface::class)) { + $this->dispatcher->dispatch($jwtCreatedEvent, Events::JWT_CREATED); + } else { + $this->dispatcher->dispatch(Events::JWT_CREATED, $jwtCreatedEvent); + + } if ($this->jwtEncoder instanceof HeaderAwareJWTEncoderInterface) { $jwtString = $this->jwtEncoder->encode($jwtCreatedEvent->getData(), $jwtCreatedEvent->getHeader()); @@ -72,7 +78,12 @@ public function create(UserInterface $user) } $jwtEncodedEvent = new JWTEncodedEvent($jwtString); - $this->dispatcher->dispatch(Events::JWT_ENCODED, $jwtEncodedEvent); + + if (interface_exists(ContractsEventDispatcherInterface::class, false)) { + $this->dispatcher->dispatch($jwtEncodedEvent, Events::JWT_ENCODED); + } else { + $this->dispatcher->dispatch(Events::JWT_ENCODED, $jwtEncodedEvent); + } return $jwtString; } @@ -87,8 +98,11 @@ public function decode(TokenInterface $token) } $event = new JWTDecodedEvent($payload); - - $this->dispatcher->dispatch(Events::JWT_DECODED, $event); + if (interface_exists(ContractsEventDispatcherInterface::class)) { + $this->dispatcher->dispatch($event, Events::JWT_DECODED); + } else { + $this->dispatcher->dispatch(Events::JWT_DECODED, $event); + } if (!$event->isValid()) { return false;