src/Security/ApiKeyAuthenticator.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  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\Passport;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  13. class ApiKeyAuthenticator extends AbstractAuthenticator
  14. {
  15.     public function start(): Response
  16.     {
  17.         $data = [
  18.             // you might translate this message
  19.             'code'    => Response::HTTP_UNAUTHORIZED,
  20.             'message' => Response::$statusTexts[Response::HTTP_UNAUTHORIZED],
  21.             'result'  => [
  22.                 'error' => 'Authentication Required'
  23.             ]
  24.         ];
  25.         return new JsonResponse($dataResponse::HTTP_UNAUTHORIZED);
  26.     }
  27.     public function supports(Request $request): ?bool
  28.     {
  29.         return $request->headers->has('X-API-KEY');
  30.     }
  31.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  32.     {
  33.         $data = [
  34.             // you may want to customize or obfuscate the message first
  35.             'code'    => Response::HTTP_UNAUTHORIZED,
  36.             'message' => Response::$statusTexts[Response::HTTP_UNAUTHORIZED],
  37.             'result'  => [
  38.                 'error' => $exception->getMessage()
  39.             ]
  40.             // or to translate this message
  41.             // $this->translator->trans($exception->getMessageKey(), $exception->getMessageData())
  42.         ];
  43.         return new JsonResponse($dataResponse::HTTP_UNAUTHORIZED);
  44.     }
  45.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  46.     {
  47.         // on success, let the request continue
  48.         return null;
  49.     }
  50.     public function supportsRememberMe(): void
  51.     {
  52.         // TODO: Implement supportsRememberMe() method.
  53.     }
  54.     public function authenticate(Request $request): Passport
  55.     {
  56.         $apiToken $request->headers->get('X-API-KEY');
  57.         if (null === $apiToken) {
  58.             throw new CustomUserMessageAuthenticationException('No X-API-KEY provided');
  59.         }
  60.         return new SelfValidatingPassport(new UserBadge($apiToken));
  61.     }
  62. }