Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Security/Authentication/LdapAuthenticationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function checkAuthentication(UserInterface $user, UsernamePasswordToke
$currentUser = $token->getUser();
$presentedPassword = $token->getCredentials();
if ($currentUser instanceof UserInterface) {
if ('' === $presentedPassword) {
if ('' === $presentedPassword || null === $presentedPassword) {
throw new BadCredentialsException(
'The password in the token is empty. You may forgive turn off `erase_credentials` in your `security.yml`'
);
Expand All @@ -77,7 +77,7 @@ protected function checkAuthentication(UserInterface $user, UsernamePasswordToke
throw new BadCredentialsException('The credentials were changed from another session.');
}
} else {
if ('' === $presentedPassword) {
if ('' === $presentedPassword || null === $presentedPassword) {
throw new BadCredentialsException('The presented password cannot be empty.');
}

Expand Down
37 changes: 37 additions & 0 deletions Tests/Security/Authentication/LdapAuthenticationProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ public function testCheckAuthenticationKnownUserCredentialsAreErased(): void
$this->ldapAuthenticationProvider->authenticate($token);
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
* @expectedExceptionMessage The password in the token is empty. You may forgive turn off `erase_credentials` in your `security.yml`
*/
public function testCheckAuthenticationKnownUserCredentialsAreNull(): void
{
$password = null;
$user = $this->createUserMock();

$token = $this->createTokenWithNullPassword($user, $password);

$this->ldapAuthenticationProvider->authenticate($token);
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
* @expectedExceptionMessage The credentials were changed from another session.
Expand Down Expand Up @@ -196,6 +210,21 @@ public function testCheckAuthenticationUnknownUserPasswordEmpty(): void
$this->ldapAuthenticationProvider->authenticate($token);
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
* @expectedExceptionMessage The presented password cannot be empty.
*/
public function testCheckAuthenticationUnknownUserPasswordNull(): void
{
$username = 'test_username';
$user = $this->createUserMock();

$this->willRetrieveUser($username, $user);
$token = $this->createTokenWithNullPassword($username, null);

$this->ldapAuthenticationProvider->authenticate($token);
}

/**
* @return UserInterface|MockObject
*/
Expand Down Expand Up @@ -228,6 +257,14 @@ private function createToken($user, string $credentials): UsernamePasswordToken
return new UsernamePasswordToken($user, $credentials, 'provider_key');
}

/**
* @param UserInterface|string|object $user
*/
private function createTokenWithNullPassword($user, ?string $credentials): UsernamePasswordToken
{
return new UsernamePasswordToken($user, $credentials, 'provider_key');
}

private function willBind(UserInterface $user, string $password, bool $result = true): void
{
$this->ldapManager->expects($this->once())
Expand Down