GuardAuthenticatorHandlerTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  16. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  17. use Symfony\Component\Security\Http\SecurityEvents;
  18. class GuardAuthenticatorHandlerTest extends TestCase
  19. {
  20. private $tokenStorage;
  21. private $dispatcher;
  22. private $token;
  23. private $request;
  24. private $sessionStrategy;
  25. private $guardAuthenticator;
  26. public function testAuthenticateWithToken()
  27. {
  28. $this->tokenStorage->expects($this->once())
  29. ->method('setToken')
  30. ->with($this->token);
  31. $loginEvent = new InteractiveLoginEvent($this->request, $this->token);
  32. $this->dispatcher
  33. ->expects($this->once())
  34. ->method('dispatch')
  35. ->with($this->equalTo(SecurityEvents::INTERACTIVE_LOGIN), $this->equalTo($loginEvent))
  36. ;
  37. $handler = new GuardAuthenticatorHandler($this->tokenStorage, $this->dispatcher);
  38. $handler->authenticateWithToken($this->token, $this->request);
  39. }
  40. public function testHandleAuthenticationSuccess()
  41. {
  42. $providerKey = 'my_handleable_firewall';
  43. $response = new Response('Guard all the things!');
  44. $this->guardAuthenticator->expects($this->once())
  45. ->method('onAuthenticationSuccess')
  46. ->with($this->request, $this->token, $providerKey)
  47. ->will($this->returnValue($response));
  48. $handler = new GuardAuthenticatorHandler($this->tokenStorage, $this->dispatcher);
  49. $actualResponse = $handler->handleAuthenticationSuccess($this->token, $this->request, $this->guardAuthenticator, $providerKey);
  50. $this->assertSame($response, $actualResponse);
  51. }
  52. public function testHandleAuthenticationFailure()
  53. {
  54. // setToken() not called - getToken() will return null, so there's nothing to clear
  55. $this->tokenStorage->expects($this->never())
  56. ->method('setToken')
  57. ->with(null);
  58. $authException = new AuthenticationException('Bad password!');
  59. $response = new Response('Try again, but with the right password!');
  60. $this->guardAuthenticator->expects($this->once())
  61. ->method('onAuthenticationFailure')
  62. ->with($this->request, $authException)
  63. ->will($this->returnValue($response));
  64. $handler = new GuardAuthenticatorHandler($this->tokenStorage, $this->dispatcher);
  65. $actualResponse = $handler->handleAuthenticationFailure($authException, $this->request, $this->guardAuthenticator, 'firewall_provider_key');
  66. $this->assertSame($response, $actualResponse);
  67. }
  68. /**
  69. * @dataProvider getTokenClearingTests
  70. */
  71. public function testHandleAuthenticationClearsToken($tokenClass, $tokenProviderKey, $actualProviderKey)
  72. {
  73. $token = $this->getMockBuilder($tokenClass)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $token->expects($this->any())
  77. ->method('getProviderKey')
  78. ->will($this->returnValue($tokenProviderKey));
  79. $this->tokenStorage->expects($this->never())
  80. ->method('setToken')
  81. ->with(null);
  82. $authException = new AuthenticationException('Bad password!');
  83. $response = new Response('Try again, but with the right password!');
  84. $this->guardAuthenticator->expects($this->once())
  85. ->method('onAuthenticationFailure')
  86. ->with($this->request, $authException)
  87. ->will($this->returnValue($response));
  88. $handler = new GuardAuthenticatorHandler($this->tokenStorage, $this->dispatcher);
  89. $actualResponse = $handler->handleAuthenticationFailure($authException, $this->request, $this->guardAuthenticator, $actualProviderKey);
  90. $this->assertSame($response, $actualResponse);
  91. }
  92. public function getTokenClearingTests()
  93. {
  94. $tests = array();
  95. // correct token class and matching firewall => clear the token
  96. $tests[] = array('Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken', 'the_firewall_key', 'the_firewall_key');
  97. $tests[] = array('Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken', 'the_firewall_key', 'different_key');
  98. $tests[] = array('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', 'the_firewall_key', 'the_firewall_key');
  99. return $tests;
  100. }
  101. public function testNoFailureIfSessionStrategyNotPassed()
  102. {
  103. $this->configurePreviousSession();
  104. $this->tokenStorage->expects($this->once())
  105. ->method('setToken')
  106. ->with($this->token);
  107. $handler = new GuardAuthenticatorHandler($this->tokenStorage, $this->dispatcher);
  108. $handler->authenticateWithToken($this->token, $this->request);
  109. }
  110. public function testSessionStrategyIsCalled()
  111. {
  112. $this->configurePreviousSession();
  113. $this->sessionStrategy->expects($this->once())
  114. ->method('onAuthentication')
  115. ->with($this->request, $this->token);
  116. $handler = new GuardAuthenticatorHandler($this->tokenStorage, $this->dispatcher);
  117. $handler->setSessionAuthenticationStrategy($this->sessionStrategy);
  118. $handler->authenticateWithToken($this->token, $this->request);
  119. }
  120. public function testSessionStrategyIsNotCalledWhenStateless()
  121. {
  122. $this->configurePreviousSession();
  123. $this->sessionStrategy->expects($this->never())
  124. ->method('onAuthentication');
  125. $handler = new GuardAuthenticatorHandler($this->tokenStorage, $this->dispatcher, array('some_provider_key'));
  126. $handler->setSessionAuthenticationStrategy($this->sessionStrategy);
  127. $handler->authenticateWithToken($this->token, $this->request, 'some_provider_key');
  128. }
  129. protected function setUp()
  130. {
  131. $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
  132. $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
  133. $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  134. $this->request = new Request(array(), array(), array(), array(), array(), array());
  135. $this->sessionStrategy = $this->getMockBuilder('Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface')->getMock();
  136. $this->guardAuthenticator = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorInterface')->getMock();
  137. }
  138. protected function tearDown()
  139. {
  140. $this->tokenStorage = null;
  141. $this->dispatcher = null;
  142. $this->token = null;
  143. $this->request = null;
  144. $this->guardAuthenticator = null;
  145. }
  146. private function configurePreviousSession()
  147. {
  148. $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock();
  149. $session->expects($this->any())
  150. ->method('getName')
  151. ->willReturn('test_session_name');
  152. $this->request->setSession($session);
  153. $this->request->cookies->set('test_session_name', 'session_cookie_val');
  154. }
  155. }