PageRenderTime 81ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 1ms

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

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