PersistentTokenBasedRememberMeServicesTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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\Exception\UsernameNotFoundException;
  14. use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
  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\PersistentTokenBasedRememberMeServices;
  19. use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
  20. use Symfony\Component\Security\Core\Exception\CookieTheftException;
  21. class PersistentTokenBasedRememberMeServicesTest 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 testAutoLoginThrowsExceptionOnNonExistentToken()
  38. {
  39. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => false, 'remember_me_parameter' => 'foo'));
  40. $request = new Request;
  41. $request->request->set('foo', 'true');
  42. $request->cookies->set('foo', $this->encodeCookie(array(
  43. $series = 'fooseries',
  44. $tokenValue = 'foovalue',
  45. )));
  46. $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
  47. $tokenProvider
  48. ->expects($this->once())
  49. ->method('loadTokenBySeries')
  50. ->will($this->throwException(new TokenNotFoundException('Token not found.')))
  51. ;
  52. $service->setTokenProvider($tokenProvider);
  53. $this->assertNull($service->autoLogin($request));
  54. $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
  55. }
  56. public function testAutoLoginReturnsNullOnNonExistentUser()
  57. {
  58. $userProvider = $this->getProvider();
  59. $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true, 'lifetime' => 3600, 'secure' => false, 'httponly' => false));
  60. $request = new Request;
  61. $request->cookies->set('foo', $this->encodeCookie(array('fooseries', 'foovalue')));
  62. $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
  63. $tokenProvider
  64. ->expects($this->once())
  65. ->method('loadTokenBySeries')
  66. ->will($this->returnValue(new PersistentToken('fooclass', 'fooname', 'fooseries', 'foovalue', new \DateTime())))
  67. ;
  68. $service->setTokenProvider($tokenProvider);
  69. $userProvider
  70. ->expects($this->once())
  71. ->method('loadUserByUsername')
  72. ->will($this->throwException(new UsernameNotFoundException('user not found')))
  73. ;
  74. $this->assertNull($service->autoLogin($request));
  75. $this->assertTrue($request->attributes->has(RememberMeServicesInterface::COOKIE_ATTR_NAME));
  76. }
  77. public function testAutoLoginThrowsExceptionOnStolenCookieAndRemovesItFromThePersistentBackend()
  78. {
  79. $userProvider = $this->getProvider();
  80. $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true));
  81. $request = new Request;
  82. $request->cookies->set('foo', $this->encodeCookie(array('fooseries', 'foovalue')));
  83. $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
  84. $service->setTokenProvider($tokenProvider);
  85. $tokenProvider
  86. ->expects($this->once())
  87. ->method('loadTokenBySeries')
  88. ->will($this->returnValue(new PersistentToken('fooclass', 'foouser', 'fooseries', 'anotherFooValue', new \DateTime())))
  89. ;
  90. $tokenProvider
  91. ->expects($this->once())
  92. ->method('deleteTokenBySeries')
  93. ->with($this->equalTo('fooseries'))
  94. ->will($this->returnValue(null))
  95. ;
  96. try {
  97. $service->autoLogin($request);
  98. $this->fail('Expected CookieTheftException was not thrown.');
  99. } catch (CookieTheftException $theft) { }
  100. $this->assertTrue($request->attributes->has(RememberMeServicesInterface::COOKIE_ATTR_NAME));
  101. }
  102. public function testAutoLoginDoesNotAcceptAnExpiredCookie()
  103. {
  104. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true, 'lifetime' => 3600));
  105. $request = new Request;
  106. $request->cookies->set('foo', $this->encodeCookie(array('fooseries', 'foovalue')));
  107. $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
  108. $tokenProvider
  109. ->expects($this->once())
  110. ->method('loadTokenBySeries')
  111. ->with($this->equalTo('fooseries'))
  112. ->will($this->returnValue(new PersistentToken('fooclass', 'username', 'fooseries', 'foovalue', new \DateTime('yesterday'))))
  113. ;
  114. $service->setTokenProvider($tokenProvider);
  115. $this->assertNull($service->autoLogin($request));
  116. $this->assertTrue($request->attributes->has(RememberMeServicesInterface::COOKIE_ATTR_NAME));
  117. }
  118. public function testAutoLogin()
  119. {
  120. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  121. $user
  122. ->expects($this->once())
  123. ->method('getRoles')
  124. ->will($this->returnValue(array('ROLE_FOO')))
  125. ;
  126. $userProvider = $this->getProvider();
  127. $userProvider
  128. ->expects($this->once())
  129. ->method('loadUserByUsername')
  130. ->with($this->equalTo('foouser'))
  131. ->will($this->returnValue($user))
  132. ;
  133. $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'secure' => false, 'httponly' => false, 'always_remember_me' => true, 'lifetime' => 3600));
  134. $request = new Request;
  135. $request->cookies->set('foo', $this->encodeCookie(array('fooseries', 'foovalue')));
  136. $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
  137. $tokenProvider
  138. ->expects($this->once())
  139. ->method('loadTokenBySeries')
  140. ->with($this->equalTo('fooseries'))
  141. ->will($this->returnValue(new PersistentToken('fooclass', 'foouser', 'fooseries', 'foovalue', new \DateTime())))
  142. ;
  143. $service->setTokenProvider($tokenProvider);
  144. $returnedToken = $service->autoLogin($request);
  145. $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', $returnedToken);
  146. $this->assertSame($user, $returnedToken->getUser());
  147. $this->assertEquals('fookey', $returnedToken->getKey());
  148. $this->assertTrue($request->attributes->has(RememberMeServicesInterface::COOKIE_ATTR_NAME));
  149. }
  150. public function testLogout()
  151. {
  152. $service = $this->getService(null, array('name' => 'foo', 'path' => '/foo', 'domain' => 'foodomain.foo'));
  153. $request = new Request();
  154. $request->cookies->set('foo', $this->encodeCookie(array('fooseries', 'foovalue')));
  155. $response = new Response();
  156. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  157. $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
  158. $tokenProvider
  159. ->expects($this->once())
  160. ->method('deleteTokenBySeries')
  161. ->with($this->equalTo('fooseries'))
  162. ->will($this->returnValue(null))
  163. ;
  164. $service->setTokenProvider($tokenProvider);
  165. $service->logout($request, $response, $token);
  166. $cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
  167. $this->assertTrue($cookie->isCleared());
  168. $this->assertEquals('/foo', $cookie->getPath());
  169. $this->assertEquals('foodomain.foo', $cookie->getDomain());
  170. }
  171. public function testLogoutSimplyIgnoresNonSetRequestCookie()
  172. {
  173. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
  174. $request = new Request;
  175. $response = new Response;
  176. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  177. $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
  178. $tokenProvider
  179. ->expects($this->never())
  180. ->method('deleteTokenBySeries')
  181. ;
  182. $service->setTokenProvider($tokenProvider);
  183. $service->logout($request, $response, $token);
  184. $cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
  185. $this->assertTrue($cookie->isCleared());
  186. $this->assertEquals('/', $cookie->getPath());
  187. $this->assertNull($cookie->getDomain());
  188. }
  189. public function testLogoutSimplyIgnoresInvalidCookie()
  190. {
  191. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
  192. $request = new Request;
  193. $request->cookies->set('foo', 'somefoovalue');
  194. $response = new Response;
  195. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  196. $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
  197. $tokenProvider
  198. ->expects($this->never())
  199. ->method('deleteTokenBySeries')
  200. ;
  201. $service->setTokenProvider($tokenProvider);
  202. $service->logout($request, $response, $token);
  203. $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
  204. }
  205. public function testLoginFail()
  206. {
  207. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
  208. $request = new Request();
  209. $this->assertFalse($request->attributes->has(RememberMeServicesInterface::COOKIE_ATTR_NAME));
  210. $service->loginFail($request);
  211. $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
  212. }
  213. public function testLoginSuccessSetsCookieWhenLoggedInWithNonRememberMeTokenInterfaceImplementation()
  214. {
  215. $service = $this->getService(null, array('name' => 'foo', 'domain' => 'myfoodomain.foo', 'path' => '/foo/path', 'secure' => true, 'httponly' => true, 'lifetime' => 3600, 'always_remember_me' => true));
  216. $request = new Request;
  217. $response = new Response;
  218. $account = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  219. $account
  220. ->expects($this->once())
  221. ->method('getUsername')
  222. ->will($this->returnValue('foo'))
  223. ;
  224. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  225. $token
  226. ->expects($this->any())
  227. ->method('getUser')
  228. ->will($this->returnValue($account))
  229. ;
  230. $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
  231. $tokenProvider
  232. ->expects($this->once())
  233. ->method('createNewToken')
  234. ;
  235. $service->setTokenProvider($tokenProvider);
  236. $cookies = $response->headers->getCookies();
  237. $this->assertEquals(0, count($cookies));
  238. $service->loginSuccess($request, $response, $token);
  239. $cookies = $response->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  240. $cookie = $cookies['myfoodomain.foo']['/foo/path']['foo'];
  241. $this->assertFalse($cookie->isCleared());
  242. $this->assertTrue($cookie->isSecure());
  243. $this->assertTrue($cookie->isHttpOnly());
  244. $this->assertTrue($cookie->getExpiresTime() > time() + 3590 && $cookie->getExpiresTime() < time() + 3610);
  245. $this->assertEquals('myfoodomain.foo', $cookie->getDomain());
  246. $this->assertEquals('/foo/path', $cookie->getPath());
  247. }
  248. protected function encodeCookie(array $parts)
  249. {
  250. $service = $this->getService();
  251. $r = new \ReflectionMethod($service, 'encodeCookie');
  252. $r->setAccessible(true);
  253. return $r->invoke($service, $parts);
  254. }
  255. protected function getService($userProvider = null, $options = array(), $logger = null)
  256. {
  257. if (null === $userProvider) {
  258. $userProvider = $this->getProvider();
  259. }
  260. return new PersistentTokenBasedRememberMeServices(array($userProvider), 'fookey', 'fookey', $options, $logger);
  261. }
  262. protected function getProvider()
  263. {
  264. $provider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
  265. $provider
  266. ->expects($this->any())
  267. ->method('supportsClass')
  268. ->will($this->returnValue(true))
  269. ;
  270. return $provider;
  271. }
  272. }