src/Security/TokenAuthenticator.php line 16

Open in your IDE?
  1. <?php
  2. // src/Security/TokenAuthenticator.php
  3. namespace App\Security;
  4. use App\Entity\User;
  5. use Doctrine\ORM\EntityManagerInterface;
  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\Core\User\UserProviderInterface;
  13. use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
  14. class TokenAuthenticator extends AbstractGuardAuthenticator
  15. {
  16.     private $em;
  17.     public function __construct(EntityManagerInterface $em)
  18.     {
  19.         $this->em $em;
  20.     }
  21.     /**
  22.      * Called on every request to decide if this authenticator should be
  23.      * used for the request. Returning `false` will cause this authenticator
  24.      * to be skipped.
  25.      */
  26.     public function supports(Request $request)
  27.     {
  28.         return $request->headers->has('InQuize-AUTH-TOKEN');
  29.     }
  30.     /**
  31.      * Called on every request. Return whatever credentials you want to
  32.      * be passed to getUser() as $credentials.
  33.      */
  34.     public function getCredentials(Request $request)
  35.     {
  36.         return $request->headers->get('InQuize-AUTH-TOKEN');
  37.     }
  38.     public function getUser($credentialsUserProviderInterface $userProvider)
  39.     {
  40.         if (null === $credentials) {
  41.             // The token header was empty, authentication fails with HTTP Status
  42.             // Code 401 "Unauthorized"
  43.             return null;
  44.         }
  45.         // The "username" in this case is the apiToken, see the key `property`
  46.         // of `your_db_provider` in `security.yaml`.
  47.         // If this returns a user, checkCredentials() is called next:
  48.         return $userProvider->loadUserByUsername($credentials);
  49.     }
  50.     public function checkCredentials($credentialsUserInterface $user)
  51.     {
  52.         // Check credentials - e.g. make sure the password is valid.
  53.         // In case of an API token, no credential check is needed.
  54.         // Return `true` to cause authentication success
  55.         return true;
  56.     }
  57.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$providerKey)
  58.     {
  59.         // on success, let the request continue
  60.         return null;
  61.     }
  62.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception)
  63.     {
  64.         $data = [
  65.             // you may want to customize or obfuscate the message first
  66.             'message' => strtr($exception->getMessageKey(), $exception->getMessageData())
  67.             // or to translate this message
  68.             // $this->translator->trans($exception->getMessageKey(), $exception->getMessageData())
  69.         ];
  70.         return new JsonResponse($dataResponse::HTTP_UNAUTHORIZED);
  71.     }
  72.     /**
  73.      * Called when authentication is needed, but it's not sent
  74.      */
  75.     public function start(Request $requestAuthenticationException $authException null)
  76.     {
  77.         $data = [
  78.             // you might translate this message
  79.             'message' => 'Authentication Required'
  80.         ];
  81.         return new JsonResponse($dataResponse::HTTP_UNAUTHORIZED);
  82.     }
  83.     public function supportsRememberMe()
  84.     {
  85.         return false;
  86.     }
  87. }