GuardAuthenticatorInterface.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Guard;
  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\Core\User\UserInterface;
  16. use Symfony\Component\Security\Core\User\UserProviderInterface;
  17. use Symfony\Component\Security\Guard\Token\GuardTokenInterface;
  18. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  19. /**
  20. * The interface for all "guard" authenticators.
  21. *
  22. * The methods on this interface are called throughout the guard authentication
  23. * process to give you the power to control most parts of the process from
  24. * one location.
  25. *
  26. * @author Ryan Weaver <ryan@knpuniversity.com>
  27. */
  28. interface GuardAuthenticatorInterface extends AuthenticationEntryPointInterface
  29. {
  30. /**
  31. * Get the authentication credentials from the request and return them
  32. * as any type (e.g. an associate array). If you return null, authentication
  33. * will be skipped.
  34. *
  35. * Whatever value you return here will be passed to getUser() and checkCredentials()
  36. *
  37. * For example, for a form login, you might:
  38. *
  39. * if ($request->request->has('_username')) {
  40. * return array(
  41. * 'username' => $request->request->get('_username'),
  42. * 'password' => $request->request->get('_password'),
  43. * );
  44. * } else {
  45. * return;
  46. * }
  47. *
  48. * Or for an API token that's on a header, you might use:
  49. *
  50. * return array('api_key' => $request->headers->get('X-API-TOKEN'));
  51. *
  52. * @param Request $request
  53. *
  54. * @return mixed|null
  55. */
  56. public function getCredentials(Request $request);
  57. /**
  58. * Return a UserInterface object based on the credentials.
  59. *
  60. * The *credentials* are the return value from getCredentials()
  61. *
  62. * You may throw an AuthenticationException if you wish. If you return
  63. * null, then a UsernameNotFoundException is thrown for you.
  64. *
  65. * @param mixed $credentials
  66. * @param UserProviderInterface $userProvider
  67. *
  68. * @throws AuthenticationException
  69. *
  70. * @return UserInterface|null
  71. */
  72. public function getUser($credentials, UserProviderInterface $userProvider);
  73. /**
  74. * Returns true if the credentials are valid.
  75. *
  76. * If any value other than true is returned, authentication will
  77. * fail. You may also throw an AuthenticationException if you wish
  78. * to cause authentication to fail.
  79. *
  80. * The *credentials* are the return value from getCredentials()
  81. *
  82. * @param mixed $credentials
  83. * @param UserInterface $user
  84. *
  85. * @return bool
  86. *
  87. * @throws AuthenticationException
  88. */
  89. public function checkCredentials($credentials, UserInterface $user);
  90. /**
  91. * Create an authenticated token for the given user.
  92. *
  93. * If you don't care about which token class is used or don't really
  94. * understand what a "token" is, you can skip this method by extending
  95. * the AbstractGuardAuthenticator class from your authenticator.
  96. *
  97. * @see AbstractGuardAuthenticator
  98. *
  99. * @param UserInterface $user
  100. * @param string $providerKey The provider (i.e. firewall) key
  101. *
  102. * @return GuardTokenInterface
  103. */
  104. public function createAuthenticatedToken(UserInterface $user, $providerKey);
  105. /**
  106. * Called when authentication executed, but failed (e.g. wrong username password).
  107. *
  108. * This should return the Response sent back to the user, like a
  109. * RedirectResponse to the login page or a 403 response.
  110. *
  111. * If you return null, the request will continue, but the user will
  112. * not be authenticated. This is probably not what you want to do.
  113. *
  114. * @param Request $request
  115. * @param AuthenticationException $exception
  116. *
  117. * @return Response|null
  118. */
  119. public function onAuthenticationFailure(Request $request, AuthenticationException $exception);
  120. /**
  121. * Called when authentication executed and was successful!
  122. *
  123. * This should return the Response sent back to the user, like a
  124. * RedirectResponse to the last page they visited.
  125. *
  126. * If you return null, the current request will continue, and the user
  127. * will be authenticated. This makes sense, for example, with an API.
  128. *
  129. * @param Request $request
  130. * @param TokenInterface $token
  131. * @param string $providerKey The provider (i.e. firewall) key
  132. *
  133. * @return Response|null
  134. */
  135. public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey);
  136. /**
  137. * Does this method support remember me cookies?
  138. *
  139. * Remember me cookie will be set if *all* of the following are met:
  140. * A) This method returns true
  141. * B) The remember_me key under your firewall is configured
  142. * C) The "remember me" functionality is activated. This is usually
  143. * done by having a _remember_me checkbox in your form, but
  144. * can be configured by the "always_remember_me" and "remember_me_parameter"
  145. * parameters under the "remember_me" firewall key
  146. * D) The onAuthenticationSuccess method returns a Response object
  147. *
  148. * @return bool
  149. */
  150. public function supportsRememberMe();
  151. }