diff --git a/Authentication/Provider/RememberMeAuthenticationProvider.php b/Authentication/Provider/RememberMeAuthenticationProvider.php index af0ec53c..670390e4 100644 --- a/Authentication/Provider/RememberMeAuthenticationProvider.php +++ b/Authentication/Provider/RememberMeAuthenticationProvider.php @@ -15,7 +15,9 @@ use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\BadCredentialsException; +use Symfony\Component\Security\Core\Exception\LogicException; use Symfony\Component\Security\Core\User\UserCheckerInterface; +use Symfony\Component\Security\Core\User\UserInterface; class RememberMeAuthenticationProvider implements AuthenticationProviderInterface { @@ -48,6 +50,11 @@ public function authenticate(TokenInterface $token) } $user = $token->getUser(); + + if (!$token->getUser() instanceof UserInterface) { + throw new LogicException(sprintf('Method "%s::getUser()" must return a "%s" instance, "%s" returned.', \get_class($token), UserInterface::class, \is_object($user) ? \get_class($user) : \gettype($user))); + } + $this->userChecker->checkPreAuth($user); $this->userChecker->checkPostAuth($user); diff --git a/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php b/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php index 15aecb9b..3875522c 100644 --- a/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php +++ b/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php @@ -13,7 +13,9 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Security\Core\Authentication\Provider\RememberMeAuthenticationProvider; +use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken; use Symfony\Component\Security\Core\Exception\DisabledException; +use Symfony\Component\Security\Core\User\User; class RememberMeAuthenticationProviderTest extends TestCase { @@ -23,6 +25,7 @@ public function testSupports() $this->assertTrue($provider->supports($this->getSupportedToken())); $this->assertFalse($provider->supports($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock())); + $this->assertFalse($provider->supports($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken')->disableOriginalConstructor()->getMock())); } public function testAuthenticateWhenTokenIsNotSupported() @@ -44,6 +47,17 @@ public function testAuthenticateWhenSecretsDoNotMatch() $provider->authenticate($token); } + public function testAuthenticateThrowsOnNonUserInterfaceInstance() + { + $this->expectException('Symfony\Component\Security\Core\Exception\LogicException'); + $this->expectExceptionMessage('Method "Symfony\Component\Security\Core\Authentication\Token\RememberMeToken::getUser()" must return a "Symfony\Component\Security\Core\User\UserInterface" instance, "string" returned.'); + + $provider = $this->getProvider(); + $token = new RememberMeToken(new User('dummyuser', null), 'foo', 'test'); + $token->setUser('stringish-user'); + $provider->authenticate($token); + } + public function testAuthenticateWhenPreChecksFails() { $this->expectException('Symfony\Component\Security\Core\Exception\DisabledException'); diff --git a/User/UserInterface.php b/User/UserInterface.php index bb0b8b93..2b5b28a3 100644 --- a/User/UserInterface.php +++ b/User/UserInterface.php @@ -21,10 +21,10 @@ * password (for checking against a submitted password), assigning roles * and so on. * - * Regardless of how your user are loaded or where they come from (a database, - * configuration, web service, etc), you will have a class that implements - * this interface. Objects that implement this interface are created and - * loaded by different objects that implement UserProviderInterface + * Regardless of how your users are loaded or where they come from (a database, + * configuration, web service, etc.), you will have a class that implements + * this interface. Objects that implement this interface are created and + * loaded by different objects that implement UserProviderInterface. * * @see UserProviderInterface *