src/Security/ClientAuthenticator.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Connection;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use PHPUnit\Util\Exception;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  16. class ClientAuthenticator extends AbstractAuthenticator
  17. {
  18.     /**
  19.      * @var EntityManagerInterface $em
  20.      */
  21.     private EntityManagerInterface $em;
  22.     /**
  23.      * TokenAuthenticator constructor.
  24.      *
  25.      * @param EntityManagerInterface $em
  26.      */
  27.     public function __construct(EntityManagerInterface $em)
  28.     {
  29.         $this->em $em;
  30.     }
  31.     public function supports(Request $request): ?bool
  32.     {
  33.         $supports $request->getSession()->has('clientId');
  34.         if ($request->getMethod() === Request::METHOD_POST) {
  35.             $supports $supports || $request->request->has('clientId');
  36.         }
  37.         if ($request->getMethod() === Request::METHOD_GET) {
  38.             $supports $supports || $request->query->has('clientId');
  39.         }
  40.         return $supports;
  41.     }
  42.     public function authenticate(Request $request): Passport
  43.     {
  44.         $token $request->getSession()->get('clientId');
  45.         if ($request->getMethod() === Request::METHOD_POST) {
  46.             $token $token ?? $request->request->get('clientId');
  47.         }
  48.         if ($request->getMethod() === Request::METHOD_GET) {
  49.             $token $token ?? $request->query->get('clientId');
  50.         }
  51.         if (null === $token) {
  52.             throw new Exception('No token provided');
  53.         }
  54.         return new SelfValidatingPassport(
  55.             new UserBadge(
  56.                 $token,
  57.                 function ($token) {
  58.                     return $this->em->getRepository(Connection::class)->findOneBy(['clientId' => $token]);
  59.                 }
  60.             ),
  61.             []
  62.         );
  63.     }
  64.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  65.     {
  66.         return null;
  67.     }
  68.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  69.     {
  70.         $data = [
  71.             'message' => strtr($exception->getMessageKey(), $exception->getMessageData())
  72.         ];
  73.         return new JsonResponse($dataResponse::HTTP_UNAUTHORIZED);
  74.     }
  75. }