AuthenticatorInterface.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\Security\Http\Authenticator;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
  16. /**
  17. * The interface for all authenticators.
  18. *
  19. * @author Ryan Weaver <ryan@symfonycasts.com>
  20. * @author Amaury Leroux de Lens <amaury@lerouxdelens.com>
  21. * @author Wouter de Jong <wouter@wouterj.nl>
  22. *
  23. * @experimental in 5.2
  24. */
  25. interface AuthenticatorInterface
  26. {
  27. /**
  28. * Does the authenticator support the given Request?
  29. *
  30. * If this returns false, the authenticator will be skipped.
  31. *
  32. * Returning null means authenticate() can be called lazily when accessing the token storage.
  33. */
  34. public function supports(Request $request): ?bool;
  35. /**
  36. * Create a passport for the current request.
  37. *
  38. * The passport contains the user, credentials and any additional information
  39. * that has to be checked by the Symfony Security system. For example, a login
  40. * form authenticator will probably return a passport containing the user, the
  41. * presented password and the CSRF token value.
  42. *
  43. * You may throw any AuthenticationException in this method in case of error (e.g.
  44. * a UsernameNotFoundException when the user cannot be found).
  45. *
  46. * @throws AuthenticationException
  47. */
  48. public function authenticate(Request $request): PassportInterface;
  49. /**
  50. * Create an authenticated token for the given user.
  51. *
  52. * If you don't care about which token class is used or don't really
  53. * understand what a "token" is, you can skip this method by extending
  54. * the AbstractAuthenticator class from your authenticator.
  55. *
  56. * @see AbstractAuthenticator
  57. *
  58. * @param PassportInterface $passport The passport returned from authenticate()
  59. */
  60. public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface;
  61. /**
  62. * Called when authentication executed and was successful!
  63. *
  64. * This should return the Response sent back to the user, like a
  65. * RedirectResponse to the last page they visited.
  66. *
  67. * If you return null, the current request will continue, and the user
  68. * will be authenticated. This makes sense, for example, with an API.
  69. */
  70. public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response;
  71. /**
  72. * Called when authentication executed, but failed (e.g. wrong username password).
  73. *
  74. * This should return the Response sent back to the user, like a
  75. * RedirectResponse to the login page or a 403 response.
  76. *
  77. * If you return null, the request will continue, but the user will
  78. * not be authenticated. This is probably not what you want to do.
  79. */
  80. public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response;
  81. }