src/Security/Guard/TokenAuthenticator.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Guard;
  3. use Symfony\Component\HttpFoundation\JsonResponse;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  8. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  9. use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
  10. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  11. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  12. class TokenAuthenticator extends AbstractAuthenticator
  13. {
  14.     /**
  15.      * Called on every request to decide if this authenticator should be
  16.      * used for the request. Returning `false` will cause this authenticator
  17.      * to be skipped.
  18.      * @param Request $request
  19.      * @return bool
  20.      */
  21.     public function supports(Request $request): ?bool
  22.     {
  23.         return $request->headers->has('X-AUTH-TOKEN');
  24.     }
  25.     public function authenticate(Request $request): SelfValidatingPassport
  26.     {
  27.         $apiToken $request->headers->get('X-AUTH-TOKEN');
  28.         $apiKey $_ENV['PUBLIC_API_KEY'] ?? null;
  29.         if (null === $apiToken || null === $apiKey || $apiToken !== $apiKey) {
  30.             // The token header was empty, authentication fails with HTTP Status
  31.             // Code 401 "Unauthorized"
  32.             throw new CustomUserMessageAuthenticationException('No API token provided');
  33.         }
  34.         return new SelfValidatingPassport(new UserBadge($apiToken));
  35.     }
  36.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  37.     {
  38.         // on success, let the request continue
  39.         return null;
  40.     }
  41.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): JsonResponse
  42.     {
  43.         $data = [
  44.             // you may want to customize or obfuscate the message first
  45.             'message' => strtr($exception->getMessageKey(), $exception->getMessageData())
  46.             // or to translate this message
  47.             // $this->translator->trans($exception->getMessageKey(), $exception->getMessageData())
  48.         ];
  49.         return new JsonResponse($dataResponse::HTTP_UNAUTHORIZED);
  50.     }
  51.     /**
  52.      * Called when authentication is needed, but it's not sent
  53.      */
  54.     public function start(Request $requestAuthenticationException $authException null): JsonResponse
  55.     {
  56.         $data = [
  57.             // you might translate this message
  58.             'message' => 'Authentication Required'
  59.         ];
  60.         return new JsonResponse($dataResponse::HTTP_UNAUTHORIZED);
  61.     }
  62.     public function supportsRememberMe(): bool
  63.     {
  64.         return false;
  65.     }
  66. }