src/Security/SparklingAuthenticator.php line 120

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Authentication\Person;
  4. use App\Repository\Authentication\TenantPersonTokenRepository;
  5. use App\Service\TenantPerson\TenantPersonService;
  6. use Psr\Cache\InvalidArgumentException;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. use Symfony\Component\Security\Core\User\UserProviderInterface;
  14. use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
  15. class SparklingAuthenticator extends AbstractGuardAuthenticator
  16. {
  17.     private $tenantPersonTokenRepository;
  18.     private $tenantPersonService;
  19.     public function __construct(
  20.         TenantPersonTokenRepository $tenantPersonTokenRepository,
  21.         TenantPersonService $tenantPersonService
  22.     ) {
  23.         $this->tenantPersonTokenRepository $tenantPersonTokenRepository;
  24.         $this->tenantPersonService $tenantPersonService;
  25.     }
  26.     /**
  27.      * Called on every request to decide if this authenticator should be
  28.      * used for the request. Returning `false` will cause this authenticator
  29.      * to be skipped.
  30.      */
  31.     public function supports(Request $request): bool
  32.     {
  33.         return $request->headers->has('X-AUTH-PROTOKEN');
  34.     }
  35.     /**
  36.      * Called on every request. Return whatever credentials you want to
  37.      * be passed to getUser() as $credentials.
  38.      */
  39.     public function getCredentials(Request $request): ?string
  40.     {
  41.         return $request->headers->get('X-AUTH-PROTOKEN');
  42.     }
  43.     /**
  44.      * @param mixed $credentials
  45.      *
  46.      * @return Person|UserInterface|null
  47.      *
  48.      * @throws \Exception
  49.      * @throws InvalidArgumentException
  50.      */
  51.     public function getUser($credentialsUserProviderInterface $userProvider)
  52.     {
  53.         if (null === $credentials) {
  54.             // The token header was empty, authentication fails with HTTP Status
  55.             // Code 401 "Unauthorized"
  56.             return null;
  57.         }
  58.         $tenantPersonToken $this->tenantPersonTokenRepository->findOneNotExpiredByToken($credentials);
  59.         if (!$tenantPersonToken) {
  60.             return null;
  61.         }
  62.         $tenantPerson $tenantPersonToken->getTenantPerson();
  63.         if (!$tenantPerson) {
  64.             return null;
  65.         }
  66.         $this->tenantPersonService->init($tenantPerson);
  67.         // If this returns a user, checkCredentials() is called next:
  68.         return $tenantPerson->getPerson();
  69.     }
  70.     /**
  71.      * @param mixed $credentials
  72.      */
  73.     public function checkCredentials($credentialsUserInterface $user): bool
  74.     {
  75.         // Check credentials - e.g. make sure the password is valid.
  76.         // In case of an API token, no credential check is needed.
  77.         // Return `true` to cause authentication success
  78.         return true;
  79.     }
  80.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  81.     {
  82.         throw new UnauthorizedHttpException('X-AUTH-PROTOKEN realm=Procall''Authentication Required.');
  83.     }
  84.     /**
  85.      * @param string $providerKey
  86.      *
  87.      * @return null
  88.      *
  89.      * @throws \Exception
  90.      */
  91.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$providerKey)
  92.     {
  93.         // on success, let the request continue
  94.         return null;
  95.     }
  96.     /**
  97.      * Called when authentication is needed, but it's not sent.
  98.      */
  99.     public function start(Request $requestAuthenticationException $authException null): Response
  100.     {
  101.         throw new UnauthorizedHttpException('X-AUTH-PROTOKEN realm=Procall''Authentication Required.');
  102.     }
  103.     public function supportsRememberMe(): bool
  104.     {
  105.         return false;
  106.     }
  107. }