src/Security/SenacsAuthenticator.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  16. class SenacsAuthenticator extends AbstractAuthenticator
  17. {
  18. public const LOGIN_ROUTE = 'app_login';
  19. private UrlGeneratorInterface $urlGenerator;
  20. public function __construct(UrlGeneratorInterface $urlGenerator)
  21. {
  22. $this->urlGenerator = $urlGenerator;
  23. }
  24. #[\Override]
  25. public function supports(Request $request): ?bool
  26. {
  27. // TODO: Implement supports() method.
  28. return true;
  29. }
  30. #[\Override]
  31. public function authenticate(Request $request): Passport
  32. {
  33. $username = $request->request->get('username', '');
  34. $request->getSession()->set(Security::LAST_USERNAME, $username);
  35. return new Passport(
  36. new UserBadge($username),
  37. new PasswordCredentials($request->request->get('password', '')),
  38. [
  39. new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')),
  40. ]
  41. );
  42. }
  43. #[\Override]
  44. public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
  45. {
  46. return new RedirectResponse($this->urlGenerator->generate('homepage'));
  47. }
  48. #[\Override]
  49. public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
  50. {
  51. return null;
  52. }
  53. // public function start(Request $request, AuthenticationException $authException = null): Response
  54. // {
  55. // /*
  56. // * If you would like this class to control what happens when an anonymous user accesses a
  57. // * protected page (e.g. redirect to /login), uncomment this method and make this class
  58. // * implement Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface.
  59. // *
  60. // * For more details, see https://symfony.com/doc/current/security/experimental_authenticators.html#configuring-the-authentication-entry-point
  61. // */
  62. // }
  63. }