vendor/nellapp/sdk-bundle/src/Sync/Security/CoreAuthenticator.php line 57

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the nellapp-core package.
  4.  *
  5.  * (c) Benjamin Georgeault
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Nellapp\Bundle\SDKBundle\Sync\Security;
  11. use Nellapp\Bundle\SDKBundle\Sync\Security\User\Core;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  18. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  19. use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
  20. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  21. /**
  22.  * Class CoreAuthenticator
  23.  *
  24.  * @author Benjamin Georgeault
  25.  * @method TokenInterface createToken(Passport $passport, string $firewallName)
  26.  */
  27. class CoreAuthenticator extends AbstractAuthenticator
  28. {
  29.     public function __construct(
  30.         private string $syncSecret,
  31.     ) {}
  32.     public function supports(Request $request): bool
  33.     {
  34.         return preg_match('/^\s*Core\s+[a-zA-Z0-9]+/i'$request->headers->get('Authorization'''));
  35.     }
  36.     public function authenticate(Request $request): Passport
  37.     {
  38.         if (null === $authorization $request->headers->get('authorization')) {
  39.             throw new AuthenticationException('Invalid request parameters');
  40.         }
  41.         $valid preg_match('/^Core (.+)/'$authorization$matches);
  42.         $credentials $this->getCredentials($request);
  43.         $validCredentials $this->checkCredentials($credentials);
  44.         if (!$valid || count($matches) !== || !$validCredentials) {
  45.             throw new AuthenticationException('Invalid request parameters');
  46.         }
  47.         return new SelfValidatingPassport(new UserBadge($matches[1], function(string $token) use ($credentials) {
  48.             return $this->getUser($credentials);
  49.         }));
  50.     }
  51.     public function getCredentials(Request $request): ?string
  52.     {
  53.         return trim(preg_replace('/^\s*Core\s+/'''$request->headers->get('Authorization')));
  54.     }
  55.     public function getUser($credentials): ?Core
  56.     {
  57.         if (null === $credentials) {
  58.             return null;
  59.         }
  60.         return new Core();
  61.     }
  62.     public function checkCredentials($credentials): bool
  63.     {
  64.         return $credentials === $this->syncSecret;
  65.     }
  66.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$firewallName): ?Response
  67.     {
  68.         return null;
  69.     }
  70.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  71.     {
  72.         return new JsonResponse(''Response::HTTP_UNAUTHORIZED);
  73.     }
  74. }