TokenBasedRememberMeServicesTest.php 11 KB

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