Skip to content

Commit

Permalink
Fix EventDispatcherInterface::dispatch() deprecation on Symfony 4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
chalasr committed Mar 30, 2019
1 parent 0d7062f commit ec25ff3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
24 changes: 20 additions & 4 deletions Security/Guard/JWTTokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 18 additions & 4 deletions Services/JWTManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down

0 comments on commit ec25ff3

Please sign in to comment.