PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Security/Http/Tests/RememberMe/TokenBasedRememberMeServicesTest.php

https://gitlab.com/Isaki/le331.fr
PHP | 285 lines | 224 code | 48 blank | 13 comment | 2 complexity | cfac80b39f3077e9d07f09c5e2242463 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Http\Tests\RememberMe;
  11. use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
  12. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  16. use Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices;
  17. class TokenBasedRememberMeServicesTest extends \PHPUnit_Framework_TestCase
  18. {
  19. public function testAutoLoginReturnsNullWhenNoCookie()
  20. {
  21. $service = $this->getService(null, array('name' => 'foo'));
  22. $this->assertNull($service->autoLogin(new Request()));
  23. }
  24. public function testAutoLoginThrowsExceptionOnInvalidCookie()
  25. {
  26. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => false, 'remember_me_parameter' => 'foo'));
  27. $request = new Request();
  28. $request->request->set('foo', 'true');
  29. $request->cookies->set('foo', 'foo');
  30. $this->assertNull($service->autoLogin($request));
  31. $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
  32. }
  33. public function testAutoLoginThrowsExceptionOnNonExistentUser()
  34. {
  35. $userProvider = $this->getProvider();
  36. $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true, 'lifetime' => 3600));
  37. $request = new Request();
  38. $request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time() + 3600, 'foopass'));
  39. $userProvider
  40. ->expects($this->once())
  41. ->method('loadUserByUsername')
  42. ->will($this->throwException(new UsernameNotFoundException('user not found')))
  43. ;
  44. $this->assertNull($service->autoLogin($request));
  45. $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
  46. }
  47. public function testAutoLoginDoesNotAcceptCookieWithInvalidHash()
  48. {
  49. $userProvider = $this->getProvider();
  50. $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true, 'lifetime' => 3600));
  51. $request = new Request();
  52. $request->cookies->set('foo', base64_encode('class:'.base64_encode('foouser').':123456789:fooHash'));
  53. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  54. $user
  55. ->expects($this->once())
  56. ->method('getPassword')
  57. ->will($this->returnValue('foopass'))
  58. ;
  59. $userProvider
  60. ->expects($this->once())
  61. ->method('loadUserByUsername')
  62. ->with($this->equalTo('foouser'))
  63. ->will($this->returnValue($user))
  64. ;
  65. $this->assertNull($service->autoLogin($request));
  66. $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
  67. }
  68. public function testAutoLoginDoesNotAcceptAnExpiredCookie()
  69. {
  70. $userProvider = $this->getProvider();
  71. $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true, 'lifetime' => 3600));
  72. $request = new Request();
  73. $request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time() - 1, 'foopass'));
  74. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  75. $user
  76. ->expects($this->once())
  77. ->method('getPassword')
  78. ->will($this->returnValue('foopass'))
  79. ;
  80. $userProvider
  81. ->expects($this->once())
  82. ->method('loadUserByUsername')
  83. ->with($this->equalTo('foouser'))
  84. ->will($this->returnValue($user))
  85. ;
  86. $this->assertNull($service->autoLogin($request));
  87. $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
  88. }
  89. /**
  90. * @dataProvider provideUsernamesForAutoLogin
  91. *
  92. * @param string $username
  93. */
  94. public function testAutoLogin($username)
  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($username))
  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', $username, 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 provideUsernamesForAutoLogin()
  123. {
  124. return array(
  125. array('foouser', 'Simple username'),
  126. array('foo'.TokenBasedRememberMeServices::COOKIE_DELIMITER.'user', 'Username might contain the delimiter'),
  127. );
  128. }
  129. public function testLogout()
  130. {
  131. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null, 'secure' => true, 'httponly' => false));
  132. $request = new Request();
  133. $response = new Response();
  134. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  135. $service->logout($request, $response, $token);
  136. $cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
  137. $this->assertTrue($cookie->isCleared());
  138. $this->assertEquals('/', $cookie->getPath());
  139. $this->assertNull($cookie->getDomain());
  140. $this->assertTrue($cookie->isSecure());
  141. $this->assertFalse($cookie->isHttpOnly());
  142. }
  143. public function testLoginFail()
  144. {
  145. $service = $this->getService(null, array('name' => 'foo', 'path' => '/foo', 'domain' => 'foodomain.foo'));
  146. $request = new Request();
  147. $response = new Response();
  148. $service->loginFail($request, $response);
  149. $cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
  150. $this->assertTrue($cookie->isCleared());
  151. $this->assertEquals('/foo', $cookie->getPath());
  152. $this->assertEquals('foodomain.foo', $cookie->getDomain());
  153. }
  154. public function testLoginSuccessIgnoresTokensWhichDoNotContainAnUserInterfaceImplementation()
  155. {
  156. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => true, 'path' => null, 'domain' => null));
  157. $request = new Request();
  158. $response = new Response();
  159. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  160. $token
  161. ->expects($this->once())
  162. ->method('getUser')
  163. ->will($this->returnValue('foo'))
  164. ;
  165. $cookies = $response->headers->getCookies();
  166. $this->assertCount(0, $cookies);
  167. $service->loginSuccess($request, $response, $token);
  168. $cookies = $response->headers->getCookies();
  169. $this->assertCount(0, $cookies);
  170. }
  171. public function testLoginSuccess()
  172. {
  173. $service = $this->getService(null, array('name' => 'foo', 'domain' => 'myfoodomain.foo', 'path' => '/foo/path', 'secure' => true, 'httponly' => true, 'lifetime' => 3600, 'always_remember_me' => true));
  174. $request = new Request();
  175. $response = new Response();
  176. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  177. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  178. $user
  179. ->expects($this->once())
  180. ->method('getPassword')
  181. ->will($this->returnValue('foopass'))
  182. ;
  183. $user
  184. ->expects($this->once())
  185. ->method('getUsername')
  186. ->will($this->returnValue('foouser'))
  187. ;
  188. $token
  189. ->expects($this->atLeastOnce())
  190. ->method('getUser')
  191. ->will($this->returnValue($user))
  192. ;
  193. $cookies = $response->headers->getCookies();
  194. $this->assertCount(0, $cookies);
  195. $service->loginSuccess($request, $response, $token);
  196. $cookies = $response->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  197. $cookie = $cookies['myfoodomain.foo']['/foo/path']['foo'];
  198. $this->assertFalse($cookie->isCleared());
  199. $this->assertTrue($cookie->isSecure());
  200. $this->assertTrue($cookie->isHttpOnly());
  201. $this->assertTrue($cookie->getExpiresTime() > time() + 3590 && $cookie->getExpiresTime() < time() + 3610);
  202. $this->assertEquals('myfoodomain.foo', $cookie->getDomain());
  203. $this->assertEquals('/foo/path', $cookie->getPath());
  204. }
  205. protected function getCookie($class, $username, $expires, $password)
  206. {
  207. $service = $this->getService();
  208. $r = new \ReflectionMethod($service, 'generateCookieValue');
  209. $r->setAccessible(true);
  210. return $r->invoke($service, $class, $username, $expires, $password);
  211. }
  212. protected function encodeCookie(array $parts)
  213. {
  214. $service = $this->getService();
  215. $r = new \ReflectionMethod($service, 'encodeCookie');
  216. $r->setAccessible(true);
  217. return $r->invoke($service, $parts);
  218. }
  219. protected function getService($userProvider = null, $options = array(), $logger = null)
  220. {
  221. if (null === $userProvider) {
  222. $userProvider = $this->getProvider();
  223. }
  224. $service = new TokenBasedRememberMeServices(array($userProvider), 'fookey', 'fookey', $options, $logger);
  225. return $service;
  226. }
  227. protected function getProvider()
  228. {
  229. $provider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
  230. $provider
  231. ->expects($this->any())
  232. ->method('supportsClass')
  233. ->will($this->returnValue(true))
  234. ;
  235. return $provider;
  236. }
  237. }