TokenBasedRememberMeServicesTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Tests\Component\Security\Http\RememberMe;
  11. use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
  13. use Symfony\Component\Security\Core\Authentication\Token\Token;
  14. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  15. use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices;
  19. use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
  20. use Symfony\Component\Security\Core\Exception\CookieTheftException;
  21. class TokenBasedRememberMeServicesTest extends \PHPUnit_Framework_TestCase
  22. {
  23. public function testAutoLoginReturnsNullWhenNoCookie()
  24. {
  25. $service = $this->getService(null, array('name' => 'foo'));
  26. $this->assertNull($service->autoLogin(new Request()));
  27. }
  28. public function testAutoLoginThrowsExceptionOnInvalidCookie()
  29. {
  30. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => false, 'remember_me_parameter' => 'foo'));
  31. $request = new Request;
  32. $request->request->set('foo', 'true');
  33. $request->cookies->set('foo', 'foo');
  34. $this->assertNull($service->autoLogin($request));
  35. $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
  36. }
  37. public function testAutoLoginThrowsExceptionOnNonExistentUser()
  38. {
  39. $userProvider = $this->getProvider();
  40. $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true, 'lifetime' => 3600));
  41. $request = new Request;
  42. $request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time()+3600, 'foopass'));
  43. $userProvider
  44. ->expects($this->once())
  45. ->method('loadUserByUsername')
  46. ->will($this->throwException(new UsernameNotFoundException('user not found')))
  47. ;
  48. $this->assertNull($service->autoLogin($request));
  49. $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
  50. }
  51. public function testAutoLoginDoesNotAcceptCookieWithInvalidHash()
  52. {
  53. $userProvider = $this->getProvider();
  54. $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true, 'lifetime' => 3600));
  55. $request = new Request;
  56. $request->cookies->set('foo', base64_encode('class:'.base64_encode('foouser').':123456789:fooHash'));
  57. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  58. $user
  59. ->expects($this->once())
  60. ->method('getPassword')
  61. ->will($this->returnValue('foopass'))
  62. ;
  63. $userProvider
  64. ->expects($this->once())
  65. ->method('loadUserByUsername')
  66. ->with($this->equalTo('foouser'))
  67. ->will($this->returnValue($user))
  68. ;
  69. $this->assertNull($service->autoLogin($request));
  70. $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
  71. }
  72. public function testAutoLoginDoesNotAcceptAnExpiredCookie()
  73. {
  74. $userProvider = $this->getProvider();
  75. $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true, 'lifetime' => 3600));
  76. $request = new Request;
  77. $request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time() - 1, 'foopass'));
  78. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  79. $user
  80. ->expects($this->once())
  81. ->method('getPassword')
  82. ->will($this->returnValue('foopass'))
  83. ;
  84. $userProvider
  85. ->expects($this->once())
  86. ->method('loadUserByUsername')
  87. ->with($this->equalTo('foouser'))
  88. ->will($this->returnValue($user))
  89. ;
  90. $this->assertNull($service->autoLogin($request));
  91. $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
  92. }
  93. public function testAutoLogin()
  94. {
  95. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  96. $user
  97. ->expects($this->once())
  98. ->method('getRoles')
  99. ->will($this->returnValue(array('ROLE_FOO')))
  100. ;
  101. $user
  102. ->expects($this->once())
  103. ->method('getPassword')
  104. ->will($this->returnValue('foopass'))
  105. ;
  106. $userProvider = $this->getProvider();
  107. $userProvider
  108. ->expects($this->once())
  109. ->method('loadUserByUsername')
  110. ->with($this->equalTo('foouser'))
  111. ->will($this->returnValue($user))
  112. ;
  113. $service = $this->getService($userProvider, array('name' => 'foo', 'always_remember_me' => true, 'lifetime' => 3600));
  114. $request = new Request;
  115. $request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time()+3600, 'foopass'));
  116. $returnedToken = $service->autoLogin($request);
  117. $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', $returnedToken);
  118. $this->assertSame($user, $returnedToken->getUser());
  119. $this->assertEquals('fookey', $returnedToken->getKey());
  120. }
  121. public function testLogout()
  122. {
  123. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
  124. $request = new Request();
  125. $response = new Response();
  126. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  127. $service->logout($request, $response, $token);
  128. $cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
  129. $this->assertTrue($cookie->isCleared());
  130. $this->assertNull($cookie->getPath());
  131. $this->assertNull($cookie->getDomain());
  132. }
  133. public function testLoginFail()
  134. {
  135. $service = $this->getService(null, array('name' => 'foo', 'path' => '/foo', 'domain' => 'foodomain.foo'));
  136. $request = new Request();
  137. $response = new Response();
  138. $service->loginFail($request, $response);
  139. $cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
  140. $this->assertTrue($cookie->isCleared());
  141. $this->assertEquals('/foo', $cookie->getPath());
  142. $this->assertEquals('foodomain.foo', $cookie->getDomain());
  143. }
  144. public function testLoginSuccessIgnoresTokensWhichDoNotContainAnUserInterfaceImplementation()
  145. {
  146. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => true));
  147. $request = new Request;
  148. $response = new Response;
  149. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  150. $token
  151. ->expects($this->once())
  152. ->method('getUser')
  153. ->will($this->returnValue('foo'))
  154. ;
  155. $this->assertFalse($response->headers->hasCookie('foo'));
  156. $service->loginSuccess($request, $response, $token);
  157. $this->assertFalse($response->headers->hasCookie('foo'));
  158. }
  159. public function testLoginSuccess()
  160. {
  161. $service = $this->getService(null, array('name' => 'foo', 'domain' => 'myfoodomain.foo', 'path' => '/foo/path', 'secure' => true, 'httponly' => true, 'lifetime' => 3600, 'always_remember_me' => true));
  162. $request = new Request;
  163. $response = new Response;
  164. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  165. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  166. $user
  167. ->expects($this->once())
  168. ->method('getPassword')
  169. ->will($this->returnValue('foopass'))
  170. ;
  171. $user
  172. ->expects($this->once())
  173. ->method('getUsername')
  174. ->will($this->returnValue('foouser'))
  175. ;
  176. $token
  177. ->expects($this->atLeastOnce())
  178. ->method('getUser')
  179. ->will($this->returnValue($user))
  180. ;
  181. $this->assertFalse($response->headers->hasCookie('foo'));
  182. $service->loginSuccess($request, $response, $token);
  183. $cookie = $response->headers->getCookie('foo');
  184. $this->assertFalse($cookie->isCleared());
  185. $this->assertTrue($cookie->isSecure());
  186. $this->assertTrue($cookie->isHttpOnly());
  187. $this->assertTrue($cookie->getExpiresTime() > time() + 3590 && $cookie->getExpiresTime() < time() + 3610);
  188. $this->assertEquals('myfoodomain.foo', $cookie->getDomain());
  189. $this->assertEquals('/foo/path', $cookie->getPath());
  190. }
  191. protected function getCookie($class, $username, $expires, $password)
  192. {
  193. $service = $this->getService();
  194. $r = new \ReflectionMethod($service, 'generateCookieValue');
  195. $r->setAccessible(true);
  196. return $r->invoke($service, $class, $username, $expires, $password);
  197. }
  198. protected function encodeCookie(array $parts)
  199. {
  200. $service = $this->getService();
  201. $r = new \ReflectionMethod($service, 'encodeCookie');
  202. $r->setAccessible(true);
  203. return $r->invoke($service, $parts);
  204. }
  205. protected function getService($userProvider = null, $options = array(), $logger = null)
  206. {
  207. if (null === $userProvider) {
  208. $userProvider = $this->getProvider();
  209. }
  210. $service = new TokenBasedRememberMeServices(array($userProvider), 'fookey', 'fookey', $options, $logger);
  211. return $service;
  212. }
  213. protected function getProvider()
  214. {
  215. $provider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
  216. $provider
  217. ->expects($this->any())
  218. ->method('supportsClass')
  219. ->will($this->returnValue(true))
  220. ;
  221. return $provider;
  222. }
  223. }