Skip to content

Commit b6bb84b

Browse files
committed
[Security] Fix BC layer for AbstractGuardAuthenticator subclasses
1 parent d484f72 commit b6bb84b

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/Symfony/Component/Security/Guard/Firewall/GuardAuthenticationListener.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\HttpFoundation\Response;
1616
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17+
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
1718
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
1819
use Symfony\Component\Security\Guard\GuardAuthenticatorInterface;
1920
use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken;
@@ -124,7 +125,13 @@ private function executeGuardAuthenticator($uniqueGuardKey, GuardAuthenticatorIn
124125
return;
125126
}
126127

127-
throw new \UnexpectedValueException(sprintf('The return value of "%s::getCredentials()" must not be null. Return false from "%s::supports()" instead.', get_class($guardAuthenticator), get_class($guardAuthenticator)));
128+
if ($guardAuthenticator instanceof AbstractGuardAuthenticator) {
129+
@trigger_error(sprintf('Returning null from "%1$s::getCredentials()" is deprecated since version 3.4 and will throw an \UnexpectedValueException in 4.0. Return false from "%1$s::supports()" instead.', get_class($guardAuthenticator)), E_USER_DEPRECATED);
130+
131+
return;
132+
}
133+
134+
throw new \UnexpectedValueException(sprintf('The return value of "%1$s::getCredentials()" must not be null. Return false from "%1$s::supports()" instead.', get_class($guardAuthenticator)));
128135
}
129136

130137
// create a token with the unique key, so that the provider knows which authenticator to use

src/Symfony/Component/Security/Guard/Tests/Firewall/GuardAuthenticationListenerTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
1717
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
18+
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
1819
use Symfony\Component\Security\Guard\AuthenticatorInterface;
1920
use Symfony\Component\Security\Guard\Firewall\GuardAuthenticationListener;
2021
use Symfony\Component\Security\Guard\GuardAuthenticatorInterface;
@@ -388,6 +389,37 @@ public function testReturnNullFromGetCredentials()
388389
$listener->handle($this->event);
389390
}
390391

392+
/**
393+
* @group legacy
394+
* @expectedDeprecation Returning null from "%s::getCredentials()" is deprecated since version 3.4 and will throw an \UnexpectedValueException in 4.0. Return false from "%s::supports()" instead.
395+
*/
396+
public function testReturnNullFromGetCredentialsTriggersForAbstractGuardAuthenticatorInstances()
397+
{
398+
$authenticator = $this->getMockBuilder(AbstractGuardAuthenticator::class)->getMock();
399+
$providerKey = 'my_firewall4';
400+
401+
$authenticator
402+
->expects($this->once())
403+
->method('supports')
404+
->will($this->returnValue(true));
405+
406+
// this will raise exception
407+
$authenticator
408+
->expects($this->once())
409+
->method('getCredentials')
410+
->will($this->returnValue(null));
411+
412+
$listener = new GuardAuthenticationListener(
413+
$this->guardAuthenticatorHandler,
414+
$this->authenticationManager,
415+
$providerKey,
416+
array($authenticator),
417+
$this->logger
418+
);
419+
420+
$listener->handle($this->event);
421+
}
422+
391423
protected function setUp()
392424
{
393425
$this->authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager')

0 commit comments

Comments
 (0)