Skip to content

Commit

Permalink
[SecurityBundle] Default signature_properties to the previous behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Oct 23, 2021
1 parent b755ed5 commit 550e5db
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Security/Factory/RememberMeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public function addConfiguration(NodeDefinition $node)
->requiresAtLeastOneElement()
->info('An array of properties on your User that are used to sign the remember-me cookie. If any of these change, all existing cookies will become invalid.')
->example(['email', 'password'])
->defaultValue(['password'])
->end()
->arrayNode('token_provider')
->beforeNormalization()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,40 @@ class UserChangingUserProvider implements UserProviderInterface
{
private $inner;

public static $changePassword = false;

public function __construct(InMemoryUserProvider $inner)
{
$this->inner = $inner;
}

public function loadUserByUsername($username)
{
return $this->inner->loadUserByUsername($username);
return $this->changeUser($this->inner->loadUserByUsername($username));
}

public function loadUserByIdentifier(string $userIdentifier): UserInterface
{
return $this->inner->loadUserByIdentifier($userIdentifier);
return $this->changeUser($this->inner->loadUserByIdentifier($userIdentifier));
}

public function refreshUser(UserInterface $user)
{
$user = $this->inner->refreshUser($user);

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

return $user;
return $this->changeUser($this->inner->refreshUser($user));
}

public function supportsClass($class)
{
return $this->inner->supportsClass($class);
}

private function changeUser(UserInterface $user): UserInterface
{
if (self::$changePassword) {
$alterUser = \Closure::bind(function (InMemoryUser $user) { $user->password = 'changed!'; }, null, class_exists(User::class) ? User::class : InMemoryUser::class);
$alterUser($user);
}

return $user;
}
}
27 changes: 25 additions & 2 deletions Tests/Functional/RememberMeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@

namespace Symfony\Bundle\SecurityBundle\Tests\Functional;

use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\RememberMeBundle\Security\UserChangingUserProvider;

class RememberMeTest extends AbstractWebTestCase
{
protected function setUp(): void
{
UserChangingUserProvider::$changePassword = false;
}

/**
* @dataProvider provideConfigs
*/
Expand Down Expand Up @@ -51,11 +58,19 @@ public function testUserChangeClearsCookie()

$this->assertSame(302, $client->getResponse()->getStatusCode());
$cookieJar = $client->getCookieJar();
$this->assertNotNull($cookieJar->get('REMEMBERME'));
$this->assertNotNull($cookie = $cookieJar->get('REMEMBERME'));

UserChangingUserProvider::$changePassword = true;

// change password (through user provider), this deauthenticates the session
$client->request('GET', '/profile');
$this->assertRedirect($client->getResponse(), '/login');
$this->assertNull($cookieJar->get('REMEMBERME'));

// restore the old remember me cookie, it should no longer be valid
$cookieJar->set($cookie);
$client->request('GET', '/profile');
$this->assertRedirect($client->getResponse(), '/login');
}

public function testSessionLessRememberMeLogout()
Expand Down Expand Up @@ -121,11 +136,19 @@ public function testLegacyUserChangeClearsCookie()

$this->assertSame(302, $client->getResponse()->getStatusCode());
$cookieJar = $client->getCookieJar();
$this->assertNotNull($cookieJar->get('REMEMBERME'));
$this->assertNotNull($cookie = $cookieJar->get('REMEMBERME'));

UserChangingUserProvider::$changePassword = true;

// change password (through user provider), this deauthenticates the session
$client->request('GET', '/profile');
$this->assertRedirect($client->getResponse(), '/login');
$this->assertNull($cookieJar->get('REMEMBERME'));

// restore the old remember me cookie, it should no longer be valid
$cookieJar->set($cookie);
$client->request('GET', '/profile');
$this->assertRedirect($client->getResponse(), '/login');
}

/**
Expand Down

0 comments on commit 550e5db

Please sign in to comment.