Skip to content

[Security] Rename User to InMemoryUser #40443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2021
Merged
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
[Security] Rename User to InMemoryUser
  • Loading branch information
chalasr committed Mar 15, 2021
commit 55b51d3f90ac4928579d50d387295364e2ac8336
4 changes: 4 additions & 0 deletions UPGRADE-5.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ Routing
Security
--------

* Deprecate class `User`, use `InMemoryUser` or your own implementation instead.
If you are using the `isAccountNonLocked()`, `isAccountNonExpired()` or `isCredentialsNonExpired()` method, consider re-implementing
them in your own user class, as they are not part of the `InMemoryUser` API
* Deprecate class `UserChecker`, use `InMemoryUserChecker` or your own implementation instead
* Deprecate `UserInterface::getPassword()`
If your `getPassword()` method does not return `null` (i.e. you are using password-based authentication),
you should implement `PasswordAuthenticatedUserInterface`.
Expand Down
4 changes: 4 additions & 0 deletions UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ Routing
Security
--------

* Remove class `User`, use `InMemoryUser` or your own implementation instead.
If you are using the `isAccountNonLocked()`, `isAccountNonExpired()` or `isCredentialsNonExpired()` method, consider re-implementing them
in your own user class as they are not part of the `InMemoryUser` API
* Remove class `UserChecker`, use `InMemoryUserChecker` or your own implementation instead
* Remove `UserInterface::getPassword()`
If your `getPassword()` method does not return `null` (i.e. you are using password-based authentication),
you should implement `PasswordAuthenticatedUserInterface`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\WebLink\Link;
use Twig\Environment;

Expand Down Expand Up @@ -137,7 +137,7 @@ public function testForward()

public function testGetUser()
{
$user = new User('user', 'pass');
$user = new InMemoryUser('user', 'pass');
$token = new UsernamePasswordToken($user, 'pass', 'default', ['ROLE_USER']);

$controller = $this->createController();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\InMemoryUser;

class SecurityTest extends AbstractWebTestCase
{
Expand All @@ -20,7 +20,7 @@ class SecurityTest extends AbstractWebTestCase
*/
public function testLoginUser(string $username, array $roles, ?string $firewallContext)
{
$user = new User($username, 'the-password', $roles);
$user = new InMemoryUser($username, 'the-password', $roles);
$client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']);

if (null === $firewallContext) {
Expand All @@ -45,7 +45,7 @@ public function getUsers()

public function testLoginUserMultipleRequests()
{
$user = new User('the-username', 'the-password', ['ROLE_FOO']);
$user = new InMemoryUser('the-username', 'the-password', ['ROLE_FOO']);
$client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']);
$client->loginUser($user);

Expand All @@ -58,7 +58,7 @@ public function testLoginUserMultipleRequests()

public function testLoginInBetweenRequests()
{
$user = new User('the-username', 'the-password', ['ROLE_FOO']);
$user = new InMemoryUser('the-username', 'the-password', ['ROLE_FOO']);
$client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']);

$client->request('GET', '/main/user_profile');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function configure()
# app/config/security.yml
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
Symfony\Component\Security\Core\User\InMemoryUser: plaintext
App\Entity\User: auto
</comment>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\ChainUserProvider;
use Symfony\Component\Security\Core\User\InMemoryUserChecker;
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
use Symfony\Component\Security\Core\User\MissingUserProvider;
use Symfony\Component\Security\Core\User\UserChecker;
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\Controller\UserValueResolver;
Expand Down Expand Up @@ -126,7 +126,7 @@
->alias(UserPasswordEncoderInterface::class, 'security.password_encoder')
->deprecate('symfony/security-bundle', '5.3', 'The "%alias_id%" service is deprecated, use "'.UserPasswordHasherInterface::class.'" instead.')

->set('security.user_checker', UserChecker::class)
->set('security.user_checker', InMemoryUserChecker::class)

->set('security.expression_language', ExpressionLanguage::class)
->args([service('cache.security_expression_language')->nullOnInvalid()])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserChecker;
use Symfony\Component\Security\Core\User\InMemoryUserChecker;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
Expand Down Expand Up @@ -626,7 +626,7 @@ public function testUserCheckerWithAuthenticatorManager(array $config, string $e

public function provideUserCheckerConfig()
{
yield [[], UserChecker::class];
yield [[], InMemoryUserChecker::class];
yield [['user_checker' => TestUserChecker::class], TestUserChecker::class];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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\User\User;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
Expand Down Expand Up @@ -46,7 +46,7 @@ public function authenticate(Request $request): PassportInterface

$userLoader = null;
if ($this->selfLoadingUser) {
$userLoader = function ($username) { return new User($username, 'test', ['ROLE_USER']); };
$userLoader = function ($username) { return new InMemoryUser($username, 'test', ['ROLE_USER']); };
}

return new SelfValidatingPassport(new UserBadge($email, $userLoader));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
Expand All @@ -22,7 +22,7 @@ class AuthenticationController
{
public function manualLoginAction(GuardAuthenticatorHandler $guardAuthenticatorHandler, Request $request)
{
$guardAuthenticatorHandler->authenticateWithToken(new PostAuthenticationGuardToken(new User('Jane', 'test', ['ROLE_USER']), 'secure', ['ROLE_USER']), $request, 'secure');
$guardAuthenticatorHandler->authenticateWithToken(new PostAuthenticationGuardToken(new InMemoryUser('Jane', 'test', ['ROLE_USER']), 'secure', ['ROLE_USER']), $request, 'secure');

return new Response('Logged in.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Symfony\Bundle\SecurityBundle\Tests\Functional\UserWithoutEquatable;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;

Expand Down Expand Up @@ -52,11 +52,11 @@ public function refreshUser(UserInterface $user)
$storedUser = $this->getUser($user->getUsername());
$class = \get_class($storedUser);

return new $class($storedUser->getUsername(), $storedUser->getPassword(), $storedUser->getRoles(), $storedUser->isEnabled(), $storedUser->isAccountNonExpired(), $storedUser->isCredentialsNonExpired() && $storedUser->getPassword() === $user->getPassword(), $storedUser->isAccountNonLocked());
return new $class($storedUser->getUsername(), $storedUser->getPassword(), $storedUser->getRoles(), $storedUser->isEnabled());
}

public function supportsClass($class)
{
return User::class === $class || UserWithoutEquatable::class === $class;
return InMemoryUser::class === $class || UserWithoutEquatable::class === $class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;

Expand Down Expand Up @@ -73,7 +73,7 @@ public function refreshUser(UserInterface $user)
{
$user = $this->inner->refreshUser($user);

$alterUser = \Closure::bind(function (User $user) { $user->password = 'foo'; }, null, User::class);
$alterUser = \Closure::bind(function (InMemoryUser $user) { $user->password = 'foo'; }, null, InMemoryUser::class);
$alterUser($user);

return $user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Http\LoginLink\LoginLinkHandler;
use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;

Expand All @@ -36,7 +36,7 @@ public function testLoginLinkSuccess()

/** @var LoginLinkHandlerInterface $loginLinkHandler */
$loginLinkHandler = self::getContainer()->get(LoginLinkHandlerInterface::class);
$user = new User('weaverryan', 'foo');
$user = new InMemoryUser('weaverryan', 'foo');
$loginLink = $loginLinkHandler->createLoginLink($user);
$this->assertStringContainsString('user=weaverryan', $loginLink);
$this->assertStringContainsString('hash=', $loginLink);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\SecuredPageBundle\Security\Core\User\ArrayUserProvider;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\UserInterface;

class SecurityTest extends AbstractWebTestCase
Expand All @@ -26,7 +26,7 @@ public function testServiceIsFunctional()
$container = $kernel->getContainer();

// put a token into the storage so the final calls can function
$user = new User('foo', 'pass');
$user = new InMemoryUser('foo', 'pass');
$token = new UsernamePasswordToken($user, '', 'provider', ['ROLE_USER']);
$container->get('security.token_storage')->setToken($token);

Expand All @@ -39,8 +39,8 @@ public function userWillBeMarkedAsChangedIfRolesHasChangedProvider()
{
return [
[
new User('user1', 'test', ['ROLE_ADMIN']),
new User('user1', 'test', ['ROLE_USER']),
new InMemoryUser('user1', 'test', ['ROLE_ADMIN']),
new InMemoryUser('user1', 'test', ['ROLE_USER']),
],
[
new UserWithoutEquatable('user1', 'test', ['ROLE_ADMIN']),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Security\Core\Encoder\NativePasswordEncoder;
use Symfony\Component\Security\Core\Encoder\Pbkdf2PasswordEncoder;
use Symfony\Component\Security\Core\Encoder\SodiumPasswordEncoder;
use Symfony\Component\Security\Core\User\InMemoryUser;

/**
* Tests UserPasswordEncoderCommand.
Expand All @@ -36,7 +37,7 @@ public function testEncodePasswordEmptySalt()
$this->passwordEncoderCommandTester->execute([
'command' => 'security:encode-password',
'password' => 'password',
'user-class' => 'Symfony\Component\Security\Core\User\User',
'user-class' => InMemoryUser::class,
'--empty-salt' => true,
], ['decorated' => false]);
$expected = str_replace("\n", \PHP_EOL, file_get_contents(__DIR__.'/app/PasswordEncode/emptysalt.txt'));
Expand Down Expand Up @@ -189,7 +190,7 @@ public function testEncodePasswordEmptySaltOutput()
$this->passwordEncoderCommandTester->execute([
'command' => 'security:encode-password',
'password' => 'p@ssw0rd',
'user-class' => 'Symfony\Component\Security\Core\User\User',
'user-class' => InMemoryUser::class,
'--empty-salt' => true,
]);

Expand Down Expand Up @@ -282,7 +283,7 @@ public function testEncodePasswordAsksNonProvidedUserClass()
[0] Custom\Class\Native\User
[1] Custom\Class\Pbkdf2\User
[2] Custom\Class\Test\User
[3] Symfony\Component\Security\Core\User\User
[3] Symfony\Component\Security\Core\User\InMemoryUser
EOTXT
, $this->passwordEncoderCommandTester->getDisplay(true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ security:
enable_authenticator_manager: true

password_hashers:
Symfony\Component\Security\Core\User\User: plaintext
Symfony\Component\Security\Core\User\InMemoryUser: plaintext

providers:
in_memory:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ imports:

security:
password_hashers:
Symfony\Component\Security\Core\User\User: plaintext
Symfony\Component\Security\Core\User\InMemoryUser: plaintext

providers:
in_memory:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ services:

security:
password_hashers:
Symfony\Component\Security\Core\User\User: plaintext
Symfony\Component\Security\Core\User\InMemoryUser: plaintext

providers:
in_memory:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ security:
users:
john: { password: doe, roles: [ROLE_SECURE] }
password_hashers:
Symfony\Component\Security\Core\User\User: plaintext
Symfony\Component\Security\Core\User\InMemoryUser: plaintext
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:

security:
password_hashers:
Symfony\Component\Security\Core\User\User: plaintext
Symfony\Component\Security\Core\User\InMemoryUser: plaintext

providers:
in_memory:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ framework:

security:
password_hashers:
Symfony\Component\Security\Core\User\User: plaintext
Symfony\Component\Security\Core\User\InMemoryUser: plaintext

providers:
in_memory:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ imports:

security:
password_hashers:
Symfony\Component\Security\Core\User\User: plaintext
Symfony\Component\Security\Core\User\InMemoryUser: plaintext

providers:
in_memory:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ imports:

security:
password_hashers:
Symfony\Component\Security\Core\User\User: plaintext
Symfony\Component\Security\Core\User\InMemoryUser: plaintext

providers:
in_memory:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ imports:

security:
password_hashers:
Symfony\Component\Security\Core\User\User: plaintext
Symfony\Component\Security\Core\User\InMemoryUser: plaintext

providers:
in_memory:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ imports:

security:
password_hashers:
Symfony\Component\Security\Core\User\User: plaintext
Symfony\Component\Security\Core\User\InMemoryUser: plaintext

providers:
in_memory:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ imports:

security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
Symfony\Component\Security\Core\User\InMemoryUser: plaintext
Custom\Class\Native\User:
algorithm: native
cost: 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ framework:

security:
password_hashers:
Symfony\Component\Security\Core\User\User: plaintext
Symfony\Component\Security\Core\User\InMemoryUser: plaintext

providers:
in_memory:
Expand Down
Loading