src/EventListener/Oauth2AuthExceptionListener.php line 54

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventListener;
  4. use Throwable;
  5. use App\Entity\User;
  6. use App\Exception\ApiException;
  7. use League\OAuth2\Server\RequestAccessTokenEvent;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpKernel\Exception\HttpException;
  10. use League\Bundle\OAuth2ServerBundle\Event\UserResolveEvent;
  11. use Symfony\Component\Security\Http\Event\LoginFailureEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Security\Core\User\UserProviderInterface;
  14. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  15. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  18. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  19. use FOS\RestBundle\View\View;
  20. // https://github.com/thephpleague/oauth2-server-bundle/blob/master/docs/basic-setup.md
  21. // openssl genrsa -out private.key 2048
  22. // openssl rsa -in private.key -pubout -out public.key
  23. // bin/console league:oauth2-server:create-client mobile
  24. // bin/console league:oauth2-server:update-client 1528bea9771237d8d0eb83e32f653a3a --add-grant-type=refresh_token
  25. // bin/console league:oauth2-server:update-client 1528bea9771237d8d0eb83e32f653a3a --add-grant-type=client_credentials
  26. // bin/console league:oauth2-server:update-client 1528bea9771237d8d0eb83e32f653a3a --add-grant-type=password
  27. final class Oauth2AuthExceptionListener implements EventSubscriberInterface
  28. {
  29.    
  30.     public function __construct()
  31.     {
  32.     }
  33.     public function onUserAuthFailed(RequestAccessTokenEvent $event): void
  34.     {
  35.         // do something
  36.     }
  37.     public function onClientAuthFailed(RequestAccessTokenEvent $event): void
  38.     {
  39.         // do something
  40.         throw new AccessDeniedException('Access Denied');
  41.     }
  42.     public function onLoginFailure(LoginFailureEvent $event)
  43.     {
  44.         //if($event->getException() instanceof Throwable) {
  45.         //    throw $event;
  46.        // }
  47.         //$event->getException()->getCode();
  48.         //throw new ApiException($event->getException()->getCode(), $event->getException()->getMessage(), $event->getException()); 
  49.         //dd($event); die ("ok!");
  50.         //throw new AccessDeniedException($event->getException()->getMessage(), $event->getException());
  51.         // https://stackoverflow.com/questions/53139957/return-jsonresponse-when-i-use-an-authtokenauthenticator-symfony-3
  52.         // Skip if request is not an API-request
  53.         $request $event->getRequest();
  54.         if (strpos($request->getPathInfo(), '/api/') !== && strpos($request->getPathInfo(), '/oauth/') !== 0) {
  55.             return;
  56.         }
  57.          // API / OAuth route only
  58.         $exception $event->getException();
  59.         $statusCode $this->getStatusCodeFromException($exception);
  60.         $error = [
  61.             'code' => $statusCode,
  62.             //'error' => $this->getErrorTypeFromException($exception),
  63.             // Warning! Passing the exception message without checks is insecure.
  64.             // This will potentially leak sensitive information.
  65.             // Do not use this in production!
  66.             'message' => $exception->getMessage(),
  67.         ];
  68.         $response = new JsonResponse($error$statusCode);
  69.         $event->setResponse($response);
  70.     }
  71.     
  72.     public static function getSubscribedEvents()
  73.     {
  74.         return [
  75.             LoginFailureEvent::class => 'onLoginFailure',
  76.         ];
  77.     }
  78.     private function getStatusCodeFromException(\Throwable $exception): int
  79.     {
  80.         if ($exception instanceof HttpException) {
  81.             return $exception->getStatusCode();
  82.         }
  83.         if ($exception instanceof HttpExceptionInterface) {
  84.             return $exception->getStatusCode();
  85.         }
  86.         
  87.         return 500;
  88.     }
  89.     private function getErrorTypeFromException(\Throwable $exception): string
  90.     {
  91.         $parts explode('\\'get_class($exception));
  92.         return end($parts);
  93.     }
  94. }