TokenBasedRememberMeServicesTest.php 10 KB

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