Navigation Menu

Skip to content

Commit

Permalink
[Security] Allow "0" as a password
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Jun 20, 2012
1 parent e78a7ba commit 680b83c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 29 deletions.
Expand Up @@ -59,7 +59,7 @@ protected function checkAuthentication(UserInterface $user, UsernamePasswordToke
throw new BadCredentialsException('The credentials were changed from another session.');
}
} else {
if (!$presentedPassword = $token->getCredentials()) {
if ("" === ($presentedPassword = $token->getCredentials())) {
throw new BadCredentialsException('The presented password cannot be empty.');
}

Expand Down
Expand Up @@ -35,13 +35,13 @@ public function testRetrieveUserWhenProviderDoesNotReturnAnUserInterface()
*/
public function testRetrieveUserWhenUsernameIsNotFound()
{
$userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
$userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
$userProvider->expects($this->once())
->method('loadUserByUsername')
->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\UsernameNotFoundException', null, array(), '', false)))
->will($this->throwException($this->getMock('Symfony\\Component\\Security\\Core\\Exception\\UsernameNotFoundException', null, array(), '', false)))
;

$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'), 'key', $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface'));
$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
$method = new \ReflectionMethod($provider, 'retrieveUser');
$method->setAccessible(true);

Expand All @@ -53,13 +53,13 @@ public function testRetrieveUserWhenUsernameIsNotFound()
*/
public function testRetrieveUserWhenAnExceptionOccurs()
{
$userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
$userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
$userProvider->expects($this->once())
->method('loadUserByUsername')
->will($this->throwException($this->getMock('RuntimeException', null, array(), '', false)))
;

$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'), 'key', $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface'));
$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
$method = new \ReflectionMethod($provider, 'retrieveUser');
$method->setAccessible(true);

Expand All @@ -68,19 +68,19 @@ public function testRetrieveUserWhenAnExceptionOccurs()

public function testRetrieveUserReturnsUserFromTokenOnReauthentication()
{
$userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
$userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
$userProvider->expects($this->never())
->method('loadUserByUsername')
;

$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
$token = $this->getSupportedToken();
$token->expects($this->once())
->method('getUser')
->will($this->returnValue($user))
;

$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'), 'key', $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface'));
$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
$reflection = new \ReflectionMethod($provider, 'retrieveUser');
$reflection->setAccessible(true);
$result = $reflection->invoke($provider, null, $token);
Expand All @@ -90,15 +90,15 @@ public function testRetrieveUserReturnsUserFromTokenOnReauthentication()

public function testRetrieveUser()
{
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');

$userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
$userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
$userProvider->expects($this->once())
->method('loadUserByUsername')
->will($this->returnValue($user))
;

$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'), 'key', $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface'));
$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
$method = new \ReflectionMethod($provider, 'retrieveUser');
$method->setAccessible(true);

Expand All @@ -110,25 +110,63 @@ public function testRetrieveUser()
*/
public function testCheckAuthenticationWhenCredentialsAreEmpty()
{
$provider = $this->getProvider();
$encoder = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface');
$encoder
->expects($this->never())
->method('isPasswordValid')
;

$provider = $this->getProvider(false, false, $encoder);
$method = new \ReflectionMethod($provider, 'checkAuthentication');
$method->setAccessible(true);

$token = $this->getSupportedToken();
$token->expects($this->once())
->method('getCredentials')
->will($this->returnValue(''))
$token
->expects($this->once())
->method('getCredentials')
->will($this->returnValue(''))
;

$method->invoke(
$provider,
$this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'),
$token
);
}

public function testCheckAuthenticationWhenCredentialsAre0()
{
$encoder = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface');
$encoder
->expects($this->once())
->method('isPasswordValid')
->will($this->returnValue(true))
;

$provider = $this->getProvider(false, false, $encoder);
$method = new \ReflectionMethod($provider, 'checkAuthentication');
$method->setAccessible(true);

$token = $this->getSupportedToken();
$token
->expects($this->once())
->method('getCredentials')
->will($this->returnValue('0'))
;

$method->invoke($provider, $this->getMock('Symfony\Component\Security\Core\User\UserInterface'), $token);
$method->invoke(
$provider,
$this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'),
$token
);
}

/**
* @expectedException Symfony\Component\Security\Core\Exception\BadCredentialsException
*/
public function testCheckAuthenticationWhenCredentialsAreNotValid()
{
$encoder = $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
$encoder = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface');
$encoder->expects($this->once())
->method('isPasswordValid')
->will($this->returnValue(false))
Expand All @@ -144,15 +182,15 @@ public function testCheckAuthenticationWhenCredentialsAreNotValid()
->will($this->returnValue('foo'))
;

$method->invoke($provider, $this->getMock('Symfony\Component\Security\Core\User\UserInterface'), $token);
$method->invoke($provider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'), $token);
}

/**
* @expectedException Symfony\Component\Security\Core\Exception\BadCredentialsException
*/
public function testCheckAuthenticationDoesNotReauthenticateWhenPasswordHasChanged()
{
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
$user->expects($this->once())
->method('getPassword')
->will($this->returnValue('foo'))
Expand All @@ -163,7 +201,7 @@ public function testCheckAuthenticationDoesNotReauthenticateWhenPasswordHasChang
->method('getUser')
->will($this->returnValue($user));

$dbUser = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$dbUser = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
$dbUser->expects($this->once())
->method('getPassword')
->will($this->returnValue('newFoo'))
Expand All @@ -177,7 +215,7 @@ public function testCheckAuthenticationDoesNotReauthenticateWhenPasswordHasChang

public function testCheckAuthenticationWhenTokenNeedsReauthenticationWorksWithoutOriginalCredentials()
{
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
$user->expects($this->once())
->method('getPassword')
->will($this->returnValue('foo'))
Expand All @@ -188,7 +226,7 @@ public function testCheckAuthenticationWhenTokenNeedsReauthenticationWorksWithou
->method('getUser')
->will($this->returnValue($user));

$dbUser = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$dbUser = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
$dbUser->expects($this->once())
->method('getPassword')
->will($this->returnValue('foo'))
Expand All @@ -202,7 +240,7 @@ public function testCheckAuthenticationWhenTokenNeedsReauthenticationWorksWithou

public function testCheckAuthentication()
{
$encoder = $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
$encoder = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface');
$encoder->expects($this->once())
->method('isPasswordValid')
->will($this->returnValue(true))
Expand All @@ -218,12 +256,12 @@ public function testCheckAuthentication()
->will($this->returnValue('foo'))
;

$method->invoke($provider, $this->getMock('Symfony\Component\Security\Core\User\UserInterface'), $token);
$method->invoke($provider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'), $token);
}

protected function getSupportedToken()
{
$mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', array('getCredentials', 'getUser', 'getProviderKey'), array(), '', false);
$mock = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken', array('getCredentials', 'getUser', 'getProviderKey'), array(), '', false);
$mock
->expects($this->any())
->method('getProviderKey')
Expand All @@ -235,7 +273,7 @@ protected function getSupportedToken()

protected function getProvider($user = false, $userChecker = false, $passwordEncoder = null)
{
$userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
$userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
if (false !== $user) {
$userProvider->expects($this->once())
->method('loadUserByUsername')
Expand All @@ -244,14 +282,14 @@ protected function getProvider($user = false, $userChecker = false, $passwordEnc
}

if (false === $userChecker) {
$userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
$userChecker = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface');
}

if (null === $passwordEncoder) {
$passwordEncoder = new PlaintextPasswordEncoder();
}

$encoderFactory = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
$encoderFactory = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface');
$encoderFactory
->expects($this->any())
->method('getEncoder')
Expand Down

0 comments on commit 680b83c

Please sign in to comment.