src/Security/SimpleAuthenticator.php line 21

  1. <?php
  2. namespace App\Security;
  3. use Symfony\Component\HttpFoundation\RedirectResponse;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  9. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  10. use Symfony\Component\Security\Http\Attribute\IsGranted;
  11. use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  15. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  16. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  17. #[IsGranted('IS_AUTHENTICATED_FULLY')]
  18. class SimpleAuthenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterface
  19. {
  20.     use TargetPathTrait;
  21.     public const LOGIN_ROUTE 'app_login';
  22.     public function __construct(
  23.         private UrlGeneratorInterface $urlGenerator,
  24.         private string $adminUsername,
  25.         private string $adminPassword
  26.     ) {
  27.     }
  28.     public function supports(Request $request): ?bool
  29.     {
  30.         return $request->isMethod('POST') && $request->getPathInfo() === '/login';
  31.     }
  32.     public function authenticate(Request $request): Passport
  33.     {
  34.         $username $request->request->get('username''');
  35.         $password $request->request->get('password''');
  36.         if (empty($username) || empty($password)) {
  37.             throw new CustomUserMessageAuthenticationException('Please provide both username and password.');
  38.         }
  39.         return new Passport(
  40.             new UserBadge($username, function ($username) {
  41.                 if ($username !== $this->adminUsername) {
  42.                     throw new CustomUserMessageAuthenticationException('Invalid credentials.');
  43.                 }
  44.                 return new SimpleUser($username);
  45.             }),
  46.             new CustomCredentials(function ($password) {
  47.                 return $password === $this->adminPassword;
  48.             },$password)
  49.         );
  50.     }
  51.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  52.     {
  53.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  54.             return new RedirectResponse($targetPath);
  55.         }
  56.         return new RedirectResponse($this->urlGenerator->generate('app_home'));
  57.     }
  58.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  59.     {
  60.         $request->getSession()->set('_security.last_error'$exception);
  61.         return new RedirectResponse($this->urlGenerator->generate(self::LOGIN_ROUTE));
  62.     }
  63.     public function start(Request $requestAuthenticationException $authException null): ?Response
  64.     {
  65.         return new RedirectResponse($this->urlGenerator->generate(self::LOGIN_ROUTE));
  66.     }