* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Security\Guard\Tests\Firewall; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; use Symfony\Component\Security\Guard\AuthenticatorInterface; use Symfony\Component\Security\Guard\Firewall\GuardAuthenticationListener; use Symfony\Component\Security\Guard\GuardAuthenticatorInterface; use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken; use Symfony\Component\Security\Core\Exception\AuthenticationException; /** * @author Ryan Weaver * @author Amaury Leroux de Lens */ class GuardAuthenticationListenerTest extends TestCase { private $authenticationManager; private $guardAuthenticatorHandler; private $event; private $logger; private $request; private $rememberMeServices; public function testHandleSuccess() { $authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock(); $authenticateToken = $this->getMockBuilder(TokenInterface::class)->getMock(); $providerKey = 'my_firewall'; $credentials = array('username' => 'weaverryan', 'password' => 'all_your_base'); $authenticator ->expects($this->once()) ->method('supports') ->willReturn(true); $authenticator ->expects($this->once()) ->method('getCredentials') ->with($this->equalTo($this->request)) ->will($this->returnValue($credentials)); // a clone of the token that should be created internally $uniqueGuardKey = 'my_firewall_0'; $nonAuthedToken = new PreAuthenticationGuardToken($credentials, $uniqueGuardKey); $this->authenticationManager ->expects($this->once()) ->method('authenticate') ->with($this->equalTo($nonAuthedToken)) ->will($this->returnValue($authenticateToken)); $this->guardAuthenticatorHandler ->expects($this->once()) ->method('authenticateWithToken') ->with($authenticateToken, $this->request); $this->guardAuthenticatorHandler ->expects($this->once()) ->method('handleAuthenticationSuccess') ->with($authenticateToken, $this->request, $authenticator, $providerKey); $listener = new GuardAuthenticationListener( $this->guardAuthenticatorHandler, $this->authenticationManager, $providerKey, array($authenticator), $this->logger ); $listener->setRememberMeServices($this->rememberMeServices); // should never be called - our handleAuthenticationSuccess() does not return a Response $this->rememberMeServices ->expects($this->never()) ->method('loginSuccess'); $listener->handle($this->event); } public function testHandleSuccessStopsAfterResponseIsSet() { $authenticator1 = $this->getMockBuilder(AuthenticatorInterface::class)->getMock(); $authenticator2 = $this->getMockBuilder(AuthenticatorInterface::class)->getMock(); // mock the first authenticator to fail, and set a Response $authenticator1 ->expects($this->once()) ->method('supports') ->willReturn(true); $authenticator1 ->expects($this->once()) ->method('getCredentials') ->willThrowException(new AuthenticationException()); $this->guardAuthenticatorHandler ->expects($this->once()) ->method('handleAuthenticationFailure') ->willReturn(new Response()); // the second authenticator should *never* be called $authenticator2 ->expects($this->never()) ->method('getCredentials'); $listener = new GuardAuthenticationListener( $this->guardAuthenticatorHandler, $this->authenticationManager, 'my_firewall', array($authenticator1, $authenticator2), $this->logger ); $listener->handle($this->event); } public function testHandleSuccessWithRememberMe() { $authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock(); $authenticateToken = $this->getMockBuilder(TokenInterface::class)->getMock(); $providerKey = 'my_firewall_with_rememberme'; $authenticator ->expects($this->once()) ->method('supports') ->with($this->equalTo($this->request)) ->willReturn(true); $authenticator ->expects($this->once()) ->method('getCredentials') ->with($this->equalTo($this->request)) ->will($this->returnValue(array('username' => 'anything_not_empty'))); $this->authenticationManager ->expects($this->once()) ->method('authenticate') ->will($this->returnValue($authenticateToken)); $successResponse = new Response('Success!'); $this->guardAuthenticatorHandler ->expects($this->once()) ->method('handleAuthenticationSuccess') ->will($this->returnValue($successResponse)); $listener = new GuardAuthenticationListener( $this->guardAuthenticatorHandler, $this->authenticationManager, $providerKey, array($authenticator), $this->logger ); $listener->setRememberMeServices($this->rememberMeServices); $authenticator->expects($this->once()) ->method('supportsRememberMe') ->will($this->returnValue(true)); // should be called - we do have a success Response $this->rememberMeServices ->expects($this->once()) ->method('loginSuccess'); $listener->handle($this->event); } public function testHandleCatchesAuthenticationException() { $authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock(); $providerKey = 'my_firewall2'; $authException = new AuthenticationException('Get outta here crazy user with a bad password!'); $authenticator ->expects($this->once()) ->method('supports') ->willReturn(true); $authenticator ->expects($this->once()) ->method('getCredentials') ->will($this->throwException($authException)); // this is not called $this->authenticationManager ->expects($this->never()) ->method('authenticate'); $this->guardAuthenticatorHandler ->expects($this->once()) ->method('handleAuthenticationFailure') ->with($authException, $this->request, $authenticator, $providerKey); $listener = new GuardAuthenticationListener( $this->guardAuthenticatorHandler, $this->authenticationManager, $providerKey, array($authenticator), $this->logger ); $listener->handle($this->event); } /** * @group legacy */ public function testLegacyInterfaceNullCredentials() { $authenticatorA = $this->getMockBuilder(GuardAuthenticatorInterface::class)->getMock(); $providerKey = 'my_firewall3'; $authenticatorA ->expects($this->once()) ->method('getCredentials') ->will($this->returnValue(null)); // this is not called $this->authenticationManager ->expects($this->never()) ->method('authenticate'); $this->guardAuthenticatorHandler ->expects($this->never()) ->method('handleAuthenticationSuccess'); $listener = new GuardAuthenticationListener( $this->guardAuthenticatorHandler, $this->authenticationManager, $providerKey, array($authenticatorA), $this->logger ); $listener->handle($this->event); } /** * @group legacy */ public function testLegacyInterfaceKeepsWorking() { $authenticator = $this->getMockBuilder(GuardAuthenticatorInterface::class)->getMock(); $authenticateToken = $this->getMockBuilder(TokenInterface::class)->getMock(); $providerKey = 'my_firewall'; $credentials = array('username' => 'weaverryan', 'password' => 'all_your_base'); $authenticator ->expects($this->once()) ->method('getCredentials') ->with($this->equalTo($this->request)) ->will($this->returnValue($credentials)); // a clone of the token that should be created internally $uniqueGuardKey = 'my_firewall_0'; $nonAuthedToken = new PreAuthenticationGuardToken($credentials, $uniqueGuardKey); $this->authenticationManager ->expects($this->once()) ->method('authenticate') ->with($this->equalTo($nonAuthedToken)) ->will($this->returnValue($authenticateToken)); $this->guardAuthenticatorHandler ->expects($this->once()) ->method('authenticateWithToken') ->with($authenticateToken, $this->request); $this->guardAuthenticatorHandler ->expects($this->once()) ->method('handleAuthenticationSuccess') ->with($authenticateToken, $this->request, $authenticator, $providerKey); $listener = new GuardAuthenticationListener( $this->guardAuthenticatorHandler, $this->authenticationManager, $providerKey, array($authenticator), $this->logger ); $listener->setRememberMeServices($this->rememberMeServices); // should never be called - our handleAuthenticationSuccess() does not return a Response $this->rememberMeServices ->expects($this->never()) ->method('loginSuccess'); $listener->handle($this->event); } /** * @group legacy */ public function testReturnNullToSkipAuth() { $authenticatorA = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock(); $authenticatorB = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock(); $providerKey = 'my_firewall3'; $authenticatorA ->expects($this->once()) ->method('getCredentials') ->will($this->returnValue(null)); $authenticatorB ->expects($this->once()) ->method('getCredentials') ->will($this->returnValue(null)); // this is not called $this->authenticationManager ->expects($this->never()) ->method('authenticate'); $this->guardAuthenticatorHandler ->expects($this->never()) ->method('handleAuthenticationSuccess'); $listener = new GuardAuthenticationListener( $this->guardAuthenticatorHandler, $this->authenticationManager, $providerKey, array($authenticatorA, $authenticatorB), $this->logger ); $listener->handle($this->event); } public function testSupportsReturnFalseSkipAuth() { $authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock(); $providerKey = 'my_firewall4'; $authenticator ->expects($this->once()) ->method('supports') ->will($this->returnValue(false)); // this is not called $authenticator ->expects($this->never()) ->method('getCredentials'); $listener = new GuardAuthenticationListener( $this->guardAuthenticatorHandler, $this->authenticationManager, $providerKey, array($authenticator), $this->logger ); $listener->handle($this->event); } /** * @expectedException \UnexpectedValueException */ public function testReturnNullFromGetCredentials() { $authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock(); $providerKey = 'my_firewall4'; $authenticator ->expects($this->once()) ->method('supports') ->will($this->returnValue(true)); // this will raise exception $authenticator ->expects($this->once()) ->method('getCredentials') ->will($this->returnValue(null)); $listener = new GuardAuthenticationListener( $this->guardAuthenticatorHandler, $this->authenticationManager, $providerKey, array($authenticator), $this->logger ); $listener->handle($this->event); } /** * @group legacy * @expectedDeprecation Returning null from "%s::getCredentials()" is deprecated since Symfony 3.4 and will throw an \UnexpectedValueException in 4.0. Return false from "%s::supports()" instead. */ public function testReturnNullFromGetCredentialsTriggersForAbstractGuardAuthenticatorInstances() { $authenticator = $this->getMockBuilder(AbstractGuardAuthenticator::class)->getMock(); $providerKey = 'my_firewall4'; $authenticator ->expects($this->once()) ->method('supports') ->will($this->returnValue(true)); // this will raise exception $authenticator ->expects($this->once()) ->method('getCredentials') ->will($this->returnValue(null)); $listener = new GuardAuthenticationListener( $this->guardAuthenticatorHandler, $this->authenticationManager, $providerKey, array($authenticator), $this->logger ); $listener->handle($this->event); } protected function setUp() { $this->authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager') ->disableOriginalConstructor() ->getMock(); $this->guardAuthenticatorHandler = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorHandler') ->disableOriginalConstructor() ->getMock(); $this->request = new Request(array(), array(), array(), array(), array(), array()); $this->event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent') ->disableOriginalConstructor() ->setMethods(array('getRequest')) ->getMock(); $this->event ->expects($this->any()) ->method('getRequest') ->will($this->returnValue($this->request)); $this->logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock(); $this->rememberMeServices = $this->getMockBuilder('Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface')->getMock(); } protected function tearDown() { $this->authenticationManager = null; $this->guardAuthenticatorHandler = null; $this->event = null; $this->logger = null; $this->request = null; $this->rememberMeServices = null; } } __halt_compiler();----SIGNATURE:----cDWTWByeDmvRUjj3d2Jk7g9Th3kdxJ7th6ccxkd5KVGGRD/qXzlkY+mq8p8wsGSr0bUPc073VT46fVQ3VEXvXHE3waFjIk6wNoCM3v/O6rqseySbXNMb5A9IiwZu1v7oMQfAWX2Pg7toPrYmHGOccP2dFqtQBxAX+EuopW1hUzG0GgIixZ3jdsVbs3zWtgV46UJZodEp53w/+fbzpugwFlxaB6VQXJQ4mEXt8ucH/OA7brYizWoUbkd4YmKGr0qaHkBZdcexZSuXQk9JKG2xe23tJgOT+qTiaSls9BPO4WrlvZulMoya+lupASeB3YMKXJB+vJ3jYtyBL7sz4ojGVXnyZa8nL/ttZ5J6OsKPj/musbDPAiSaAmISWalZHxzRcFmHFnPrThHT/9ElvZ6N3V1DzPuVue4fzbsAUy/W4/lBc0PDxE0gHqxqWAkx/F5sT1taRvB8xT/soXEPspA0JTpVvk7YhCPmuni2NXryoeSIa9rT2rxbCXzy0kPTxSacEhW+JhGVm1BnBcLitA6aoICd+tkoL/ctp1Y42aBgNa0qg+NP9Zo9lpRGpdgip9CpbAYqH/thBG3hePdypzJwcLRA7nj0UlKAULuIB8kMfdPTqGrIalcvpmfoywfhm9GB45YCLLXGNMDJvQhtN4O78r5yOTVXpjIEOMJyGizvsFc=----ATTACHMENT:----MzQ1ODk0NzMyMjI0MTIwOSA1MDUxODIzNDkzODMyMzQ4IDY3NTI4Mzg0NTgyMTY0NDQ=