8000 [Security] Fixed auth provider authenticate() cannot return void · symfony/symfony@6e18b56 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e18b56

Browse files
committed
[Security] Fixed auth provider authenticate() cannot return void
The AuthenticationManagerInterface requires that authenticate() must return a TokenInterface, never null. Several authentication providers are violating this. Changed to throw exception instead.
1 parent 45e677e commit 6e18b56

8 files changed

+27
-8
lines changed

src/Symfony/Component/Security/Core/Authentication/Provider/AnonymousAuthenticationProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Security\Core\Authentication\Provider;
1313

1414
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1516
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
1617
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
1718

@@ -38,7 +39,7 @@ public function __construct($key)
3839
public function authenticate(TokenInterface $token)
3940
{
4041
if (!$this->supports($token)) {
41-
return;
42+
throw new AuthenticationException('The token is not supported by this authentication provider.');
4243
}
4344

4445
if ($this->key !== $token->getKey()) {

src/Symfony/Component/Security/Core/Authentication/Provider/PreAuthenticatedAuthenticationProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Security\Core\User\UserProviderInterface;
1515
use Symfony\Component\Security\Core\User\UserCheckerInterface;
16+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1617
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
1718
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
1819
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -51,7 +52,7 @@ public function __construct(UserProviderInterface $userProvider, UserCheckerInte
5152
public function authenticate(TokenInterface $token)
5253
{
5354
if (!$this->supports($token)) {
54-
return;
55+
throw new AuthenticationException('The token is not supported by this authentication provider.');
5556
}
5657

5758
if (!$user = $token->getUser()) {

src/Symfony/Component/Security/Core/Authentication/Provider/RememberMeAuthenticationProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Security\Core\User\UserCheckerInterface;
1515
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1616
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
17+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1718
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
1819

1920
class RememberMeAuthenticationProvider implements AuthenticationProviderInterface
@@ -40,7 +41,7 @@ public function __construct(UserCheckerInterface $userChecker, $key, $providerKe
4041
public function authenticate(TokenInterface $token)
4142
{
4243
if (!$this->supports($token)) {
43-
return;
44+
throw new AuthenticationException('The token is not supported by this authentication provider.');
4445
}
4546

4647
if ($this->key !== $token->getKey()) {

src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct(UserCheckerInterface $userChecker, $providerKey, $hi
5656
public function authenticate(TokenInterface $token)
5757
{
5858
if (!$this->supports($token)) {
59-
return;
59+
throw new AuthenticationException('The token is not supported by this authentication provider.');
6060
}
6161

6262
$username = $token->getUsername();

src/Symfony/Component/Security/Core/Tests/Authentication/Provider/AnonymousAuthenticationProviderTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ public function testSupports()
2424
$this->assertFalse($provider->supports($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()));
2525
}
2626

27+
/**
28+
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException
29+
* @expectedExceptionMessage The token is not supported by this authentication provider.
30+
*/
2731
public function testAuthenticateWhenTokenIsNotSupported()
2832
{
2933
$provider = $this->getProvider('foo');
3034

31-
$this->assertNull($provider->authenticate($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()));
35+
$provider->authenticate($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock());
3236
}
3337

3438
/**

src/Symfony/Component/Security/Core/Tests/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ public function testSupports()
3636
$this->assertFalse($provider->supports($token));
3737
}
3838

39+
/**
40+
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException
41+
* @expectedExceptionMessage The token is not supported by this authentication provider.
42+
*/
3943
public function testAuthenticateWhenTokenIsNotSupported()
4044
{
4145
$provider = $this->getProvider();
4246

43-
$this->assertNull($provider->authenticate($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()));
47+
$provider->authenticate($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock());
4448
}
4549

4650
/**

src/Symfony/Component/Security/Core/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ public function testSupports()
2626
$this->assertFalse($provider->supports($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()));
2727
}
2828

29+
/**
30+
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException
31+
* @expectedExceptionMessage The token is not supported by this authentication provider.
32+
*/
2933
public function testAuthenticateWhenTokenIsNotSupported()
3034
{
3135
$provider = $this->getProvider();
3236

3337
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
34-
$this->assertNull($provider->authenticate($token));
38+
$provider->authenticate($token);
3539
}
3640

3741
/**

src/Symfony/Component/Security/Core/Tests/Authentication/Provider/UserAuthenticationProviderTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ public function testSupports()
2929
$this->assertFalse($provider->supports($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()));
3030
}
3131

32+
/**
33+
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException
34+
* @expectedExceptionMessage The token is not supported by this authentication provider.
35+
*/
3236
public function testAuthenticateWhenTokenIsNotSupported()
3337
{
3438
$provider = $this->getProvider();
3539

36-
$this->assertNull($provider->authenticate($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()));
40+
$provider->authenticate($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock());
3741
}
3842

3943
/**

0 commit comments

Comments
 (0)
0