Fix BC layer

This commit is contained in:
Robin Chalas 2017-10-05 09:59:11 +02:00
parent a7a6f8a678
commit 78eecba780
12 changed files with 122 additions and 170 deletions

View File

@ -296,15 +296,15 @@ Security
`DigestAuthenticationListener` and `DigestAuthenticationEntryPoint` will be `DigestAuthenticationListener` and `DigestAuthenticationEntryPoint` will be
removed in 4.0. Use another authentication system like `http_basic` instead. removed in 4.0. Use another authentication system like `http_basic` instead.
* The `GuardAuthenticatorInterface` has been deprecated and will be removed in 4.0.
Use `AuthenticatorInterface` instead.
SecurityBundle SecurityBundle
-------------- --------------
* Using voters that do not implement the `VoterInterface`is now deprecated in * Using voters that do not implement the `VoterInterface`is now deprecated in
the `AccessDecisionManager` and this functionality will be removed in 4.0. the `AccessDecisionManager` and this functionality will be removed in 4.0.
* Using guard authenticator that implement the `GuardAuthenticatorInterface` is now
deprecated, this will be removed in 4.0. `AuthenticatorInterface` must be used now.
* `FirewallContext::getListeners()` now returns `\Traversable|array` * `FirewallContext::getListeners()` now returns `\Traversable|array`
* `InitAclCommand::__construct()` now takes an instance of * `InitAclCommand::__construct()` now takes an instance of

View File

@ -659,9 +659,6 @@ Security
* The `RoleInterface` has been removed. Extend the `Symfony\Component\Security\Core\Role\Role` * The `RoleInterface` has been removed. Extend the `Symfony\Component\Security\Core\Role\Role`
class instead. class instead.
* The `GuardAuthenticatorInterface` has been removed. Implement
`Symfony\Component\Security\Guard\AuthenticatorInterface` class instead.
* The `LogoutUrlGenerator::registerListener()` method expects a 6th `string $context = null` argument. * The `LogoutUrlGenerator::registerListener()` method expects a 6th `string $context = null` argument.
* The `AccessDecisionManager::setVoters()` method has been removed. Pass the * The `AccessDecisionManager::setVoters()` method has been removed. Pass the
@ -676,6 +673,9 @@ Security
`DigestAuthenticationListener` and `DigestAuthenticationEntryPoint` classes `DigestAuthenticationListener` and `DigestAuthenticationEntryPoint` classes
have been removed. Use another authentication system like `http_basic` instead. have been removed. Use another authentication system like `http_basic` instead.
* The `GuardAuthenticatorInterface` interface has been removed.
Use `AuthenticatorInterface` instead.
SecurityBundle SecurityBundle
-------------- --------------

View File

@ -15,6 +15,7 @@ CHANGELOG
requests. requests.
* deprecated HTTP digest authentication * deprecated HTTP digest authentication
* Added a new password encoder for the Argon2i hashing algorithm * Added a new password encoder for the Argon2i hashing algorithm
* deprecated `GuardAuthenticatorInterface` in favor of `AuthenticatorInterface`
3.3.0 3.3.0
----- -----

View File

@ -23,17 +23,13 @@ use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
abstract class AbstractGuardAuthenticator implements AuthenticatorInterface abstract class AbstractGuardAuthenticator implements AuthenticatorInterface
{ {
/** /**
* Default implementation of the AuthenticatorInterface::supports method * {@inheritdoc}
* As we still have the deprecated GuardAuthenticatorInterface, this method must be implemented here
* Once GuardAuthenticatorInterface will be removed, this method should be removed too.
* *
* @param Request $request * @deprecated since version 3.4, to be removed in 4.0
*
* @return bool
*/ */
public function supports(Request $request) public function supports(Request $request)
{ {
@trigger_error('The Symfony\Component\Security\Guard\AbstractGuardAuthenticator::supports default implementation is used. This is provided for backward compatibility on GuardAuthenticationInterface that is deprecated since version 3.1 and will be removed in 4.0. Provide your own implementation of the supports method instead.', E_USER_DEPRECATED); @trigger_error(sprintf('The "%s()" method is deprecated since version 3.4 and will be removed in 4.0. Implement the "%s::supports()" method in class "%s" instead.', __METHOD__, AuthenticatorInterface::class, get_class($this)), E_USER_DEPRECATED);
return true; return true;
} }

View File

@ -12,13 +12,6 @@
namespace Symfony\Component\Security\Guard; namespace Symfony\Component\Security\Guard;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\Token\GuardTokenInterface;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
/** /**
* The interface for all "guard" authenticators. * The interface for all "guard" authenticators.
@ -30,14 +23,12 @@ use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface
* @author Ryan Weaver <ryan@knpuniversity.com> * @author Ryan Weaver <ryan@knpuniversity.com>
* @author Amaury Leroux de Lens <amaury@lerouxdelens.com> * @author Amaury Leroux de Lens <amaury@lerouxdelens.com>
*/ */
interface AuthenticatorInterface extends AuthenticationEntryPointInterface interface AuthenticatorInterface extends GuardAuthenticatorInterface
{ {
/** /**
* Does the authenticator support the given Request? * Does the authenticator support the given Request?
* *
* If this returns true, authentication will continue (e.g. getCredentials() will be called). * If this returns false, the authenticator will be skipped.
* If false, this authenticator is done. The next (if any) authenticators will be called and
* may authenticate the user, or leave the user as anonymous.
* *
* @param Request $request * @param Request $request
* *
@ -47,8 +38,7 @@ interface AuthenticatorInterface extends AuthenticationEntryPointInterface
/** /**
* Get the authentication credentials from the request and return them * Get the authentication credentials from the request and return them
* as any type (e.g. an associate array). If you return null, authentication * as any type (e.g. an associate array).
* will be skipped.
* *
* Whatever value you return here will be passed to getUser() and checkCredentials() * Whatever value you return here will be passed to getUser() and checkCredentials()
* *
@ -65,106 +55,9 @@ interface AuthenticatorInterface extends AuthenticationEntryPointInterface
* *
* @param Request $request * @param Request $request
* *
* @return mixed|null * @return mixed Any non-null value
*
* @throws \UnexpectedValueException If null is returned
*/ */
public function getCredentials(Request $request); public function getCredentials(Request $request);
/**
* Return a UserInterface object based on the credentials.
*
* The *credentials* are the return value from getCredentials()
*
* You may throw an AuthenticationException if you wish. If you return
* null, then a UsernameNotFoundException is thrown for you.
*
* @param mixed $credentials
* @param UserProviderInterface $userProvider
*
* @throws AuthenticationException
*
* @return UserInterface|null
*/
public function getUser($credentials, UserProviderInterface $userProvider);
/**
* Returns true if the credentials are valid.
*
* If any value other than true is returned, authentication will
* fail. You may also throw an AuthenticationException if you wish
* to cause authentication to fail.
*
* The *credentials* are the return value from getCredentials()
*
* @param mixed $credentials
* @param UserInterface $user
*
* @return bool
*
* @throws AuthenticationException
*/
public function checkCredentials($credentials, UserInterface $user);
/**
* Creates an authenticated token for the given user.
*
* If you don't care about which token class is used or don't really
* understand what a "token" is, you can skip this method by extending
* the AbstractGuardAuthenticator class from your authenticator.
*
* @see AbstractGuardAuthenticator
*
* @param UserInterface $user
* @param string $providerKey The provider (i.e. firewall) key
*
* @return GuardTokenInterface
*/
public function createAuthenticatedToken(UserInterface $user, $providerKey);
/**
* Called when authentication executed, but failed (e.g. wrong username password).
*
* This should return the Response sent back to the user, like a
* RedirectResponse to the login page or a 403 response.
*
* If you return null, the request will continue, but the user will
* not be authenticated. This is probably not what you want to do.
*
* @param Request $request
* @param AuthenticationException $exception
*
* @return Response|null
*/
public function onAuthenticationFailure(Request $request, AuthenticationException $exception);
/**
* Called when authentication executed and was successful!
*
* This should return the Response sent back to the user, like a
* RedirectResponse to the last page they visited.
*
* If you return null, the current request will continue, and the user
* will be authenticated. This makes sense, for example, with an API.
*
* @param Request $request
* @param TokenInterface $token
* @param string $providerKey The provider (i.e. firewall) key
*
* @return Response|null
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey);
/**
* Does this method support remember me cookies?
*
* Remember me cookie will be set if *all* of the following are met:
* A) This method returns true
* B) The remember_me key under your firewall is configured
* C) The "remember me" functionality is activated. This is usually
* done by having a _remember_me checkbox in your form, but
* can be configured by the "always_remember_me" and "remember_me_parameter"
* parameters under the "remember_me" firewall key
*
* @return bool
*/
public function supportsRememberMe();
} }

View File

@ -15,9 +15,9 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler; use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Component\Security\Guard\GuardAuthenticatorInterface;
use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken; use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Guard\GuardAuthenticatorInterface;
use Symfony\Component\Security\Guard\AuthenticatorInterface; use Symfony\Component\Security\Guard\AuthenticatorInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@ -94,7 +94,7 @@ class GuardAuthenticationListener implements ListenerInterface
} }
} }
private function executeGuardAuthenticator($uniqueGuardKey, AuthenticatorInterface $guardAuthenticator, GetResponseEvent $event) private function executeGuardAuthenticator($uniqueGuardKey, GuardAuthenticatorInterface $guardAuthenticator, GetResponseEvent $event)
{ {
$request = $event->getRequest(); $request = $event->getRequest();
try { try {
@ -102,38 +102,29 @@ class GuardAuthenticationListener implements ListenerInterface
$this->logger->debug('Calling getCredentials() on guard configurator.', array('firewall_key' => $this->providerKey, 'authenticator' => get_class($guardAuthenticator))); $this->logger->debug('Calling getCredentials() on guard configurator.', array('firewall_key' => $this->providerKey, 'authenticator' => get_class($guardAuthenticator)));
} }
// abort the execution of the authenticator if it doesn't support the request. // abort the execution of the authenticator if it doesn't support the request
if ($guardAuthenticator instanceof GuardAuthenticatorInterface) { if ($guardAuthenticator instanceof AuthenticatorInterface) {
// it's a GuardAuthenticatorInterface if (!$guardAuthenticator->supports($request)) {
// we support the previous behaviour to avoid BC break.
$credentialsCanBeNull = true;
@trigger_error('The Symfony\Component\Security\Guard\GuardAuthenticatorInterface interface is deprecated since version 3.1 and will be removed in 4.0. Use Symfony\Component\Security\Guard\Authenticator\GuardAuthenticatorInterface instead.', E_USER_DEPRECATED);
} else {
if (true !== $guardAuthenticator->supports($request)) {
return; return;
} }
// as there was a support for given request, // as there was a support for given request,
// authenticator is expected to give not-null credentials. // authenticator is expected to give not-null credentials.
$credentialsCanBeNull = false; $credentialsCanBeNull = false;
} else {
// deprecated since version 3.4, to be removed in 4.0
$credentialsCanBeNull = true;
} }
// allow the authenticator to fetch authentication info from the request // allow the authenticator to fetch authentication info from the request
$credentials = $guardAuthenticator->getCredentials($request); $credentials = $guardAuthenticator->getCredentials($request);
if (null === $credentials) { if (null === $credentials) {
// if GuardAuthenticatorInterface is used // deprecated since version 3.4, to be removed in 4.0
// allow null to skip authentication.
if ($credentialsCanBeNull) { if ($credentialsCanBeNull) {
return; return;
} }
// otherwise something went wrong and the authentication must fail throw new \UnexpectedValueException(sprintf('The return value of "%s::getCredentials()" must not be null. Return false from "%s::supports()" instead.', get_class($guardAuthenticator), get_class($guardAuthenticator)));
throw new \UnexpectedValueException(sprintf(
'You must return some credentials from %s:getCredentials().
To skip authentication, return false from %s::supports().',
get_class($guardAuthenticator),
get_class($guardAuthenticator)
));
} }
// create a token with the unique key, so that the provider knows which authenticator to use // create a token with the unique key, so that the provider knows which authenticator to use
@ -205,7 +196,7 @@ class GuardAuthenticationListener implements ListenerInterface
* @param TokenInterface $token * @param TokenInterface $token
* @param Response $response * @param Response $response
*/ */
private function triggerRememberMe(AuthenticatorInterface $guardAuthenticator, Request $request, TokenInterface $token, Response $response = null) private function triggerRememberMe(GuardAuthenticatorInterface $guardAuthenticator, Request $request, TokenInterface $token, Response $response = null)
{ {
if (null === $this->rememberMeServices) { if (null === $this->rememberMeServices) {
if (null !== $this->logger) { if (null !== $this->logger) {

View File

@ -29,6 +29,8 @@ use Symfony\Component\Security\Http\SecurityEvents;
* can be called directly (e.g. for manual authentication) or overridden. * can be called directly (e.g. for manual authentication) or overridden.
* *
* @author Ryan Weaver <ryan@knpuniversity.com> * @author Ryan Weaver <ryan@knpuniversity.com>
*
* @final since version 3.4
*/ */
class GuardAuthenticatorHandler class GuardAuthenticatorHandler
{ {
@ -68,7 +70,7 @@ class GuardAuthenticatorHandler
* *
* @return null|Response * @return null|Response
*/ */
public function handleAuthenticationSuccess(TokenInterface $token, Request $request, AuthenticatorInterface $guardAuthenticator, $providerKey) public function handleAuthenticationSuccess(TokenInterface $token, Request $request, GuardAuthenticatorInterface $guardAuthenticator, $providerKey)
{ {
$response = $guardAuthenticator->onAuthenticationSuccess($request, $token, $providerKey); $response = $guardAuthenticator->onAuthenticationSuccess($request, $token, $providerKey);
@ -95,7 +97,7 @@ class GuardAuthenticatorHandler
* *
* @return Response|null * @return Response|null
*/ */
public function authenticateUserAndHandleSuccess(UserInterface $user, Request $request, AuthenticatorInterface $authenticator, $providerKey) public function authenticateUserAndHandleSuccess(UserInterface $user, Request $request, GuardAuthenticatorInterface $authenticator, $providerKey)
{ {
// create an authenticated token for the User // create an authenticated token for the User
$token = $authenticator->createAuthenticatedToken($user, $providerKey); $token = $authenticator->createAuthenticatedToken($user, $providerKey);
@ -117,7 +119,7 @@ class GuardAuthenticatorHandler
* *
* @return null|Response * @return null|Response
*/ */
public function handleAuthenticationFailure(AuthenticationException $authenticationException, Request $request, AuthenticatorInterface $guardAuthenticator, $providerKey) public function handleAuthenticationFailure(AuthenticationException $authenticationException, Request $request, GuardAuthenticatorInterface $guardAuthenticator, $providerKey)
{ {
$token = $this->tokenStorage->getToken(); $token = $this->tokenStorage->getToken();
if ($token instanceof PostAuthenticationGuardToken && $providerKey === $token->getProviderKey()) { if ($token instanceof PostAuthenticationGuardToken && $providerKey === $token->getProviderKey()) {

View File

@ -18,6 +18,7 @@ use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface; use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\Token\GuardTokenInterface; use Symfony\Component\Security\Guard\Token\GuardTokenInterface;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
/** /**
* The interface for all "guard" authenticators. * The interface for all "guard" authenticators.
@ -28,9 +29,9 @@ use Symfony\Component\Security\Guard\Token\GuardTokenInterface;
* *
* @author Ryan Weaver <ryan@knpuniversity.com> * @author Ryan Weaver <ryan@knpuniversity.com>
* *
* @deprecated Symfony\Component\Security\Guard\AuthenticatorInterface must be used instead * @deprecated since version 3.4, to be removed in 4.0. Use AuthenticatorInterface instead
*/ */
interface GuardAuthenticatorInterface extends AuthenticatorInterface interface GuardAuthenticatorInterface extends AuthenticationEntryPointInterface
{ {
/** /**
* Get the authentication credentials from the request and return them * Get the authentication credentials from the request and return them

View File

@ -56,7 +56,7 @@ class GuardAuthenticationProvider implements AuthenticationProviderInterface
/** /**
* Finds the correct authenticator for the token and calls it. * Finds the correct authenticator for the token and calls it.
* *
* @param TokenInterface|GuardTokenInterface $token * @param GuardTokenInterface $token
* *
* @return TokenInterface * @return TokenInterface
*/ */
@ -101,7 +101,7 @@ class GuardAuthenticationProvider implements AuthenticationProviderInterface
// instances that will be checked if you have multiple firewalls. // instances that will be checked if you have multiple firewalls.
} }
private function authenticateViaGuard(AuthenticatorInterface $guardAuthenticator, PreAuthenticationGuardToken $token) private function authenticateViaGuard($guardAuthenticator, PreAuthenticationGuardToken $token)
{ {
// get the user from the GuardAuthenticator // get the user from the GuardAuthenticator
$user = $guardAuthenticator->getUser($token->getCredentials(), $this->userProvider); $user = $guardAuthenticator->getUser($token->getCredentials(), $this->userProvider);
@ -142,6 +142,6 @@ class GuardAuthenticationProvider implements AuthenticationProviderInterface
public function supports(TokenInterface $token) public function supports(TokenInterface $token)
{ {
return $token instanceof TokenInterface; return $token instanceof GuardTokenInterface;
} }
} }

View File

@ -14,7 +14,10 @@ namespace Symfony\Component\Security\Guard\Tests\Firewall;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Guard\AuthenticatorInterface;
use Symfony\Component\Security\Guard\Firewall\GuardAuthenticationListener; use Symfony\Component\Security\Guard\Firewall\GuardAuthenticationListener;
use Symfony\Component\Security\Guard\GuardAuthenticatorInterface;
use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken; use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken;
use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\AuthenticationException;
@ -33,8 +36,8 @@ class GuardAuthenticationListenerTest extends TestCase
public function testHandleSuccess() public function testHandleSuccess()
{ {
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Guard\AuthenticatorInterface')->getMock(); $authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticateToken = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $authenticateToken = $this->getMockBuilder(TokenInterface::class)->getMock();
$providerKey = 'my_firewall'; $providerKey = 'my_firewall';
$credentials = array('username' => 'weaverryan', 'password' => 'all_your_base'); $credentials = array('username' => 'weaverryan', 'password' => 'all_your_base');
@ -88,8 +91,8 @@ class GuardAuthenticationListenerTest extends TestCase
public function testHandleSuccessStopsAfterResponseIsSet() public function testHandleSuccessStopsAfterResponseIsSet()
{ {
$authenticator1 = $this->getMockBuilder('Symfony\Component\Security\Guard\AuthenticatorInterface')->getMock(); $authenticator1 = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticator2 = $this->getMockBuilder('Symfony\Component\Security\Guard\AuthenticatorInterface')->getMock(); $authenticator2 = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
// mock the first authenticator to fail, and set a Response // mock the first authenticator to fail, and set a Response
$authenticator1 $authenticator1
@ -104,7 +107,6 @@ class GuardAuthenticationListenerTest extends TestCase
->expects($this->once()) ->expects($this->once())
->method('handleAuthenticationFailure') ->method('handleAuthenticationFailure')
->willReturn(new Response()); ->willReturn(new Response());
// the second authenticator should *never* be called // the second authenticator should *never* be called
$authenticator2 $authenticator2
->expects($this->never()) ->expects($this->never())
@ -123,8 +125,8 @@ class GuardAuthenticationListenerTest extends TestCase
public function testHandleSuccessWithRememberMe() public function testHandleSuccessWithRememberMe()
{ {
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Guard\AuthenticatorInterface')->getMock(); $authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticateToken = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $authenticateToken = $this->getMockBuilder(TokenInterface::class)->getMock();
$providerKey = 'my_firewall_with_rememberme'; $providerKey = 'my_firewall_with_rememberme';
$authenticator $authenticator
@ -171,7 +173,7 @@ class GuardAuthenticationListenerTest extends TestCase
public function testHandleCatchesAuthenticationException() public function testHandleCatchesAuthenticationException()
{ {
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Guard\AuthenticatorInterface')->getMock(); $authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$providerKey = 'my_firewall2'; $providerKey = 'my_firewall2';
$authException = new AuthenticationException('Get outta here crazy user with a bad password!'); $authException = new AuthenticationException('Get outta here crazy user with a bad password!');
@ -210,7 +212,7 @@ class GuardAuthenticationListenerTest extends TestCase
*/ */
public function testLegacyInterfaceNullCredentials() public function testLegacyInterfaceNullCredentials()
{ {
$authenticatorA = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock(); $authenticatorA = $this->getMockBuilder(GuardAuthenticatorInterface::class)->getMock();
$providerKey = 'my_firewall3'; $providerKey = 'my_firewall3';
$authenticatorA $authenticatorA
@ -243,8 +245,8 @@ class GuardAuthenticationListenerTest extends TestCase
*/ */
public function testLegacyInterfaceKeepsWorking() public function testLegacyInterfaceKeepsWorking()
{ {
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock(); $authenticator = $this->getMockBuilder(GuardAuthenticatorInterface::class)->getMock();
$authenticateToken = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $authenticateToken = $this->getMockBuilder(TokenInterface::class)->getMock();
$providerKey = 'my_firewall'; $providerKey = 'my_firewall';
$credentials = array('username' => 'weaverryan', 'password' => 'all_your_base'); $credentials = array('username' => 'weaverryan', 'password' => 'all_your_base');
@ -332,7 +334,7 @@ class GuardAuthenticationListenerTest extends TestCase
public function testSupportsReturnFalseSkipAuth() public function testSupportsReturnFalseSkipAuth()
{ {
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Guard\AuthenticatorInterface')->getMock(); $authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$providerKey = 'my_firewall4'; $providerKey = 'my_firewall4';
$authenticator $authenticator
@ -359,9 +361,9 @@ class GuardAuthenticationListenerTest extends TestCase
/** /**
* @expectedException \UnexpectedValueException * @expectedException \UnexpectedValueException
*/ */
public function testSupportsReturnTrueRaiseMissingCredentialsException() public function testReturnNullFromGetCredentials()
{ {
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Guard\AuthenticatorInterface')->getMock(); $authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$providerKey = 'my_firewall4'; $providerKey = 'my_firewall4';
$authenticator $authenticator

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Security\Guard\Tests;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Guard\AuthenticatorInterface;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler; use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
@ -128,7 +129,7 @@ class GuardAuthenticatorHandlerTest extends TestCase
$this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
$this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
$this->request = new Request(array(), array(), array(), array(), array(), array()); $this->request = new Request(array(), array(), array(), array(), array(), array());
$this->guardAuthenticator = $this->getMockBuilder('Symfony\Component\Security\Guard\AuthenticatorInterface')->getMock(); $this->guardAuthenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
} }
protected function tearDown() protected function tearDown()

View File

@ -12,6 +12,9 @@
namespace Symfony\Component\Security\Guard\Tests\Provider; namespace Symfony\Component\Security\Guard\Tests\Provider;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Guard\AuthenticatorInterface;
use Symfony\Component\Security\Guard\Provider\GuardAuthenticationProvider; use Symfony\Component\Security\Guard\Provider\GuardAuthenticationProvider;
use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken; use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
@ -28,6 +31,68 @@ class GuardAuthenticationProviderTest extends TestCase
{ {
$providerKey = 'my_cool_firewall'; $providerKey = 'my_cool_firewall';
$authenticatorA = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticatorB = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticatorC = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticators = array($authenticatorA, $authenticatorB, $authenticatorC);
// called 2 times - for authenticator A and B (stops on B because of match)
$this->preAuthenticationToken->expects($this->exactly(2))
->method('getGuardProviderKey')
// it will return the "1" index, which will match authenticatorB
->will($this->returnValue('my_cool_firewall_1'));
$enteredCredentials = array(
'username' => '_weaverryan_test_user',
'password' => 'guard_auth_ftw',
);
$this->preAuthenticationToken->expects($this->atLeastOnce())
->method('getCredentials')
->will($this->returnValue($enteredCredentials));
// authenticators A and C are never called
$authenticatorA->expects($this->never())
->method('getUser');
$authenticatorC->expects($this->never())
->method('getUser');
$mockedUser = $this->getMockBuilder(UserInterface::class)->getMock();
$authenticatorB->expects($this->once())
->method('getUser')
->with($enteredCredentials, $this->userProvider)
->will($this->returnValue($mockedUser));
// checkCredentials is called
$authenticatorB->expects($this->once())
->method('checkCredentials')
->with($enteredCredentials, $mockedUser)
// authentication works!
->will($this->returnValue(true));
$authedToken = $this->getMockBuilder(TokenInterface::class)->getMock();
$authenticatorB->expects($this->once())
->method('createAuthenticatedToken')
->with($mockedUser, $providerKey)
->will($this->returnValue($authedToken));
// user checker should be called
$this->userChecker->expects($this->once())
->method('checkPreAuth')
->with($mockedUser);
$this->userChecker->expects($this->once())
->method('checkPostAuth')
->with($mockedUser);
$provider = new GuardAuthenticationProvider($authenticators, $this->userProvider, $providerKey, $this->userChecker);
$actualAuthedToken = $provider->authenticate($this->preAuthenticationToken);
$this->assertSame($authedToken, $actualAuthedToken);
}
/**
* @group legacy
*/
public function testLegacyAuthenticate()
{
$providerKey = 'my_cool_firewall';
$authenticatorA = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock(); $authenticatorA = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock();
$authenticatorB = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock(); $authenticatorB = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock();
$authenticatorC = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock(); $authenticatorC = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock();