src/Security/ApiTokenAuthenticator.php line 68

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Security;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use Symfony\Component\Security\Core\User\UserProviderInterface;
  11. use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
  12. use EADPlataforma\Entity\User;
  13. use EADPlataforma\Services\ConfigurationService;
  14. use EADPlataforma\Services\GeneralService;
  15. use EADPlataforma\Enum\ErrorEnum;
  16. class ApiTokenAuthenticator extends AbstractGuardAuthenticator {
  17.     /**
  18.      * @var EntityManagerInterface
  19.      */
  20.     private $em;
  21.     /**
  22.      * @var ConfigurationService
  23.      */
  24.     private $configurationService;
  25.     /**
  26.      * @param ConfigurationService $configurationService
  27.      */
  28.     public function __construct(ConfigurationService $configurationServiceGeneralService $generalService) {
  29.         $this->configurationService $configurationService;
  30.         $this->em $generalService->getService('SchoolEntityManager');
  31.     }
  32.     /**
  33.      * Called on every request to decide if this authenticator should be
  34.      * used for the request. Returning false will cause this authenticator
  35.      * to be skipped.
  36.      */
  37.     public function supports(Request $request) {
  38.         return true;
  39.     }
  40.     /**
  41.      * Called on every request. Return whatever credentials you want to
  42.      * be passed to getUser() as $credentials.
  43.      */
  44.     public function getCredentials(Request $request) {
  45.         return [
  46.             "token" => $request->headers->get('X-AUTH-TOKEN'),
  47.         ];
  48.     }
  49.     public function getUser($credentialsUserProviderInterface $userProvider) {
  50.         $apiToken $credentials['token'];
  51.         if (null === $apiToken) {
  52.             throw new AuthenticationException("Token not found");
  53.         }
  54.         $apiTokenClient $this->configurationService->get('ead_api_token');
  55.         
  56.         if($apiToken != $apiTokenClient){
  57.             throw new AuthenticationException("Invalid Token");
  58.         }
  59.         $user $this->em->getRepository(User::class)->find(1);
  60.         
  61.         if(!$user){
  62.             throw new AuthenticationException("User Session not found");
  63.         }
  64.         // if a User object, checkCredentials() is called
  65.         return $user;
  66.     }
  67.     public function checkCredentials($credentialsUserInterface $user) {
  68.         // check credentials - e.g. make sure the password is valid
  69.         // no credential check is needed in this case
  70.         // return true to cause authentication success
  71.         return true;
  72.     }
  73.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$providerKey) {
  74.         // on success, let the request continue
  75.         return null;
  76.     }
  77.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception) {
  78.         $data = [
  79.             "http_status" => 401,
  80.             "message" => "Token not found"
  81.         ];
  82.         return new JsonResponse($dataResponse::HTTP_UNAUTHORIZED);
  83.     }
  84.     /**
  85.      * Called when authentication is needed, but it's not sent
  86.      */
  87.     public function start(Request $requestAuthenticationException $authException null) {
  88.         $data = [
  89.             "data" => [ "message" => "Authentication Required" ],
  90.             "error" => ErrorEnum::AUTH_INVALID
  91.         ];
  92.         return new JsonResponse($dataResponse::HTTP_UNAUTHORIZED);
  93.     }
  94.     public function supportsRememberMe() {
  95.         return false;
  96.     }
  97. }