src/Security/SenacsAuthenticator.php line 43

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. if ($request->hasSession()) {
  35. $request->getSession()->set(Security::LAST_USERNAME, $username);
  36. }
  37. return new Passport(
  38. new UserBadge($username),
  39. new PasswordCredentials($request->request->get('password', '')),
  40. [
  41. new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')),
  42. ]
  43. );
  44. }
  45. #[\Override]
  46. public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
  47. {
  48. return new RedirectResponse($this->urlGenerator->generate('homepage'));
  49. }
  50. #[\Override]
  51. public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
  52. {
  53. return null;
  54. }
  55. // public function start(Request $request, AuthenticationException $authException = null): Response
  56. // {
  57. // /*
  58. // * If you would like this class to control what happens when an anonymous user accesses a
  59. // * protected page (e.g. redirect to /login), uncomment this method and make this class
  60. // * implement Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface.
  61. // *
  62. // * For more details, see https://symfony.com/doc/current/security/experimental_authenticators.html#configuring-the-authentication-entry-point
  63. // */
  64. // }
  65. }