TokenBasedRememberMeServicesTest.php 10 KB

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