123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- namespace Symfony\Tests\Component\Security\Http\RememberMe;
- use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
- use Symfony\Component\Security\Core\Authentication\Token\Token;
- use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
- use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices;
- use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
- use Symfony\Component\Security\Core\Exception\CookieTheftException;
- class TokenBasedRememberMeServicesTest extends \PHPUnit_Framework_TestCase
- {
- public function testAutoLoginReturnsNullWhenNoCookie()
- {
- $service = $this->getService(null, array('name' => 'foo'));
- $this->assertNull($service->autoLogin(new Request()));
- }
- /**
- * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException
- * @expectedMessage The cookie is invalid.
- */
- public function testAutoLoginThrowsExceptionOnInvalidCookie()
- {
- $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo'));
- $request = new Request;
- $request->request->set('foo', 'true');
- $request->cookies->set('foo', 'foo');
- $service->autoLogin($request);
- }
- /**
- * @expectedException Symfony\Component\Security\Core\Exception\UsernameNotFoundException
- */
- public function testAutoLoginThrowsExceptionOnNonExistentUser()
- {
- $userProvider = $this->getProvider();
- $service = $this->getService($userProvider, array('name' => 'foo', 'always_remember_me' => true, 'lifetime' => 3600));
- $request = new Request;
- $request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time()+3600, 'foopass'));
- $userProvider
- ->expects($this->once())
- ->method('loadUserByUsername')
- ->will($this->throwException(new UsernameNotFoundException('user not found')))
- ;
- $service->autoLogin($request);
- }
- /**
- * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException
- * @expectedMessage The cookie's hash is invalid.
- */
- public function testAutoLoginDoesNotAcceptCookieWithInvalidHash()
- {
- $userProvider = $this->getProvider();
- $service = $this->getService($userProvider, array('name' => 'foo', 'always_remember_me' => true, 'lifetime' => 3600));
- $request = new Request;
- $request->cookies->set('foo', base64_encode('class:'.base64_encode('foouser').':123456789:fooHash'));
- $user = $this->getMock('Symfony\Component\Security\Core\User\AccountInterface');
- $user
- ->expects($this->once())
- ->method('getPassword')
- ->will($this->returnValue('foopass'))
- ;
- $userProvider
- ->expects($this->once())
- ->method('loadUserByUsername')
- ->with($this->equalTo('foouser'))
- ->will($this->returnValue($user))
- ;
- $service->autoLogin($request);
- }
- /**
- * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException
- * @expectedMessage The cookie has expired.
- */
- public function testAutoLoginDoesNotAcceptAnExpiredCookie()
- {
- $userProvider = $this->getProvider();
- $service = $this->getService($userProvider, array('name' => 'foo', 'always_remember_me' => true, 'lifetime' => 3600));
- $request = new Request;
- $request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time() - 1, 'foopass'));
- $user = $this->getMock('Symfony\Component\Security\Core\User\AccountInterface');
- $user
- ->expects($this->once())
- ->method('getPassword')
- ->will($this->returnValue('foopass'))
- ;
- $userProvider
- ->expects($this->once())
- ->method('loadUserByUsername')
- ->with($this->equalTo('foouser'))
- ->will($this->returnValue($user))
- ;
- $service->autoLogin($request);
- }
- public function testAutoLogin()
- {
- $user = $this->getMock('Symfony\Component\Security\Core\User\AccountInterface');
- $user
- ->expects($this->once())
- ->method('getRoles')
- ->will($this->returnValue(array('ROLE_FOO')))
- ;
- $user
- ->expects($this->once())
- ->method('getPassword')
- ->will($this->returnValue('foopass'))
- ;
- $userProvider = $this->getProvider();
- $userProvider
- ->expects($this->once())
- ->method('loadUserByUsername')
- ->with($this->equalTo('foouser'))
- ->will($this->returnValue($user))
- ;
- $service = $this->getService($userProvider, array('name' => 'foo', 'always_remember_me' => true, 'lifetime' => 3600));
- $request = new Request;
- $request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time()+3600, 'foopass'));
- $returnedToken = $service->autoLogin($request);
- $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', $returnedToken);
- $this->assertSame($user, $returnedToken->getUser());
- $this->assertEquals('fookey', $returnedToken->getKey());
- }
- public function testLogout()
- {
- $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
- $request = new Request();
- $response = new Response();
- $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
- $this->assertFalse($response->headers->hasCookie('foo'));
- $service->logout($request, $response, $token);
- $cookie = $response->headers->getCookie('foo');
- $this->assertTrue($cookie->isCleared());
- $this->assertNull($cookie->getPath());
- $this->assertNull($cookie->getDomain());
- }
- public function testLoginFail()
- {
- $service = $this->getService(null, array('name' => 'foo', 'path' => '/foo', 'domain' => 'foodomain.foo'));
- $request = new Request();
- $response = new Response();
- $this->assertFalse($response->headers->hasCookie('foo'));
- $service->loginFail($request, $response);
- $cookie = $response->headers->getCookie('foo');
- $this->assertTrue($cookie->isCleared());
- $this->assertEquals('/foo', $cookie->getPath());
- $this->assertEquals('foodomain.foo', $cookie->getDomain());
- }
- public function testLoginSuccessDoesNotRenewRememberMeToken()
- {
- $service = $this->getService(null, array('name' => 'foo', 'domain' => 'myfoodomain.foo', 'path' => '/foo/path', 'secure' => true, 'httponly' => true, 'lifetime' => 3600));
- $request = new Request;
- $response = new Response;
- $user = $this->getMock('Symfony\Component\Security\Core\User\AccountInterface');
- $user
- ->expects($this->once())
- ->method('getRoles')
- ->will($this->returnValue(array('ROLE_FOO')))
- ;
- $token = new RememberMeToken($user, 'fookey', 'foo');
- $this->assertFalse($response->headers->hasCookie('foo'));
- $service->loginSuccess($request, $response, $token);
- $this->assertFalse($response->headers->hasCookie('foo'));
- }
- public function testLoginSuccessIgnoresTokensWhichDoNotContainAnAccountInterfaceImplementation()
- {
- $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => true));
- $request = new Request;
- $response = new Response;
- $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
- $token
- ->expects($this->once())
- ->method('getUser')
- ->will($this->returnValue('foo'))
- ;
- $this->assertFalse($response->headers->hasCookie('foo'));
- $service->loginSuccess($request, $response, $token);
- $this->assertFalse($response->headers->hasCookie('foo'));
- }
- public function testLoginSuccess()
- {
- $service = $this->getService(null, array('name' => 'foo', 'domain' => 'myfoodomain.foo', 'path' => '/foo/path', 'secure' => true, 'httponly' => true, 'lifetime' => 3600, 'always_remember_me' => true));
- $request = new Request;
- $response = new Response;
- $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
- $user = $this->getMock('Symfony\Component\Security\Core\User\AccountInterface');
- $user
- ->expects($this->once())
- ->method('getPassword')
- ->will($this->returnValue('foopass'))
- ;
- $user
- ->expects($this->once())
- ->method('getUsername')
- ->will($this->returnValue('foouser'))
- ;
- $token
- ->expects($this->atLeastOnce())
- ->method('getUser')
- ->will($this->returnValue($user))
- ;
- $this->assertFalse($response->headers->hasCookie('foo'));
- $service->loginSuccess($request, $response, $token);
- $cookie = $response->headers->getCookie('foo');
- $this->assertFalse($cookie->isCleared());
- $this->assertTrue($cookie->isSecure());
- $this->assertTrue($cookie->isHttpOnly());
- $this->assertTrue($cookie->getExpire() > time() + 3590 && $cookie->getExpire() < time() + 3610);
- $this->assertEquals('myfoodomain.foo', $cookie->getDomain());
- $this->assertEquals('/foo/path', $cookie->getPath());
- }
- protected function getCookie($class, $username, $expires, $password)
- {
- $service = $this->getService();
- $r = new \ReflectionMethod($service, 'generateCookieValue');
- $r->setAccessible(true);
- return $r->invoke($service, $class, $username, $expires, $password);
- }
- protected function encodeCookie(array $parts)
- {
- $service = $this->getService();
- $r = new \ReflectionMethod($service, 'encodeCookie');
- $r->setAccessible(true);
- return $r->invoke($service, $parts);
- }
- protected function getService($userProvider = null, $options = array(), $logger = null)
- {
- if (null === $userProvider) {
- $userProvider = $this->getProvider();
- }
- $service = new TokenBasedRememberMeServices(array($userProvider), 'fookey', 'fookey', $options, $logger);
- return $service;
- }
- protected function getProvider()
- {
- $provider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
- $provider
- ->expects($this->any())
- ->method('supportsClass')
- ->will($this->returnValue(true))
- ;
- return $provider;
- }
- }
|