-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Adding a new RefreshableRolesTokenInterface to refresh security roles #24331
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
it By default, AbstractToken (and its sub-classes), have a mechanism to check if any extra tokens were added, beyond the user tokens. If there are none, then the roles are refreshed. If there are some, then they are not.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional; | ||
|
||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Security\Core\User\UserProviderInterface; | ||
|
||
class RefreshableRolesTest extends WebTestCase | ||
{ | ||
public function testRolesAreRefreshed() | ||
{ | ||
// log them in! | ||
$client = $this->createAuthenticatedClient('cool_user'); | ||
|
||
// refresh the page, roles have not changed yet | ||
$client->request('GET', '/profile'); | ||
$rolesData = $client->getProfile()->getCollector('security')->getRoles(); | ||
$this->assertCount(1, $rolesData); | ||
$this->assertEquals('ROLE_ORIGINAL', $rolesData[0]); | ||
|
||
// this will cause the refreshed user to have these new roles | ||
$client->request('GET', '/profile?new_role=ROLE_NEW'); | ||
$rolesData = $client->getProfile()->getCollector('security')->getRoles(); | ||
$this->assertCount(1, $rolesData); | ||
$this->assertEquals('ROLE_NEW', $rolesData[0]); | ||
|
||
// the change should be persistent | ||
$client->request('GET', '/profile'); | ||
$rolesData = $client->getProfile()->getCollector('security')->getRoles(); | ||
$this->assertCount(1, $rolesData); | ||
$this->assertEquals('ROLE_NEW', $rolesData[0]); | ||
} | ||
|
||
private function createAuthenticatedClient($username) | ||
{ | ||
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => 'refreshable_roles.yml')); | ||
$client->followRedirects(true); | ||
|
||
$form = $client->request('GET', '/login')->selectButton('login')->form(); | ||
$form['_username'] = $username; | ||
$form['_password'] = 'test'; | ||
$client->submit($form); | ||
|
||
return $client; | ||
} | ||
} | ||
|
||
class RefreshableRolesUserProvider implements UserProviderInterface | ||
{ | ||
private $requestStack; | ||
|
||
public function __construct(RequestStack $requestStack) | ||
{ | ||
$this->requestStack = $requestStack; | ||
} | ||
|
||
public function loadUserByUsername($username) | ||
{ | ||
return new RefreshableUser($username, array('ROLE_ORIGINAL')); | ||
} | ||
|
||
public function refreshUser(UserInterface $user) | ||
{ | ||
$request = $this->requestStack->getCurrentRequest(); | ||
// a sneaky way of faking the stored user's roles being changed | ||
if ($request->query->has('new_role')) { | ||
$user->setRoles(array($request->query->get('new_role'))); | ||
} | ||
|
||
return $user; | ||
} | ||
|
||
public function supportsClass($class) | ||
{ | ||
return RefreshableUser::class === $class; | ||
} | ||
} | ||
|
||
class RefreshableUser implements UserInterface | ||
{ | ||
private $username; | ||
private $roles; | ||
|
||
public function __construct($username, array $roles) | ||
{ | ||
$this->username = $username; | ||
$this->roles = $roles; | ||
} | ||
|
||
public function getUsername() | ||
{ | ||
return $this->username; | ||
} | ||
|
||
public function getRoles() | ||
{ | ||
return $this->roles; | ||
} | ||
|
||
public function setRoles($roles) | ||
{ | ||
$this->roles = $roles; | ||
} | ||
|
||
public function getPassword() | ||
{ | ||
return 'test'; | ||
} | ||
|
||
public function getSalt() | ||
{ | ||
} | ||
|
||
public function eraseCredentials() | ||
{ | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
imports: | ||
- { resource: ./../config/default.yml } | ||
|
||
services: | ||
refreshable_roles_user_provider: | ||
class: Symfony\Bundle\SecurityBundle\Tests\Functional\RefreshableRolesUserProvider | ||
arguments: ['@request_stack'] | ||
|
||
security: | ||
encoders: | ||
Symfony\Bundle\SecurityBundle\Tests\Functional\RefreshableUser: plaintext | ||
|
||
providers: | ||
all_users: | ||
id: refreshable_roles_user_provider | ||
|
||
firewalls: | ||
# This firewall doesn't make sense in combination with the rest of the | ||
# configuration file, but it's here for testing purposes (do not use | ||
# this file in a real world scenario though) | ||
login_form: | ||
pattern: ^/login$ | ||
security: false | ||
|
||
default: | ||
form_login: | ||
check_path: /login_check | ||
default_target_path: /profile | ||
anonymous: ~ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,12 +40,14 @@ class ContextListener implements ListenerInterface | |
private $tokenStorage; | ||
private $contextKey; | ||
private $sessionKey; | ||
private $refreshableRolesSessionKey; | ||
private $logger; | ||
private $userProviders; | ||
private $dispatcher; | ||
private $registered; | ||
private $trustResolver; | ||
private $logoutOnUserChange = false; | ||
private $refreshedToken; | ||
|
||
/** | ||
* @param TokenStorageInterface $tokenStorage | ||
|
@@ -65,6 +67,8 @@ public function __construct(TokenStorageInterface $tokenStorage, $userProviders, | |
$this->userProviders = $userProviders; | ||
$this->contextKey = $contextKey; | ||
$this->sessionKey = '_security_'.$contextKey; | ||
$this->refreshableRolesSessionKey = $this->sessionKey.'_refreshable_roles'; | ||
|
||
$this->logger = $logger; | ||
$this->dispatcher = $dispatcher; | ||
$this->trustResolver = $trustResolver ?: new AuthenticationTrustResolver(AnonymousToken::class, RememberMeToken::class); | ||
|
@@ -120,14 +124,16 @@ public function handle(GetResponseEvent $event) | |
$token = null; | ||
} | ||
|
||
if ($token instanceof RefreshableRolesTokenInterface && $token->getUser() instanceof UserInterface) { | ||
// see if this token wants the roles refreshed from the User | ||
if ($session->get($this->refreshableRolesSessionKey) && $token->getUser() instanceof UserInterface) { | ||
if (null !== $this->logger) { | ||
$this->logger->debug('Refreshing token roles from the User object'); | ||
} | ||
|
||
$token->updateRoles($token->getUser()->getRoles()); | ||
} | ||
|
||
$this->refreshedToken = $token; | ||
$this->tokenStorage->setToken($token); | ||
} | ||
|
||
E03C
|
@@ -155,13 +161,20 @@ public function onKernelResponse(FilterResponseEvent $event) | |
if ((null === $token = $this->tokenStorage->getToken()) || $this->trustResolver->isAnonymous($token)) { | ||
if ($request->hasPreviousSession()) { | ||
$session->remove($this->sessionKey); | ||
$session->remove($this->refreshableRolesSessionKey); | ||
} | ||
} else { | ||
$session->set($this->sessionKey, serialize($token)); | ||
|
||
if (null !== $this->logger) { | ||
$this->logger->debug('Stored the security token in the session.', array('key' => $this->sessionKey)); | ||
} | ||
|
||
// if the token changed, then re-set the refreshable key | ||
if ($token !== $this->refreshedToken) { | ||
$shouldUpdateRoles = $token instanceof RefreshableRolesTokenInterface && $token->shouldUpdateRoles(); | ||
$session->set($this->refreshableRolesSessionKey, $shouldUpdateRoles); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a tricky part. The return value from So, we instead store this value in a different session key. This logic only sets it if the token has changed. Basically, we want to set this session key during the original request when authentication occurred (that's when we know that This was the weirdest / hackiest part of this PR. I'm open to suggestions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If your upgrading there is a big chance you already need to clear existing session data. So this change should be acceptable. |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this is expected... this affects all tokens today no? Should we optin thru config instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was my way of trying to safely opt everyone in automatically... but I don't think that's possible anymore, thanks to #24331 (comment)
So yea, I think we'll need to have a way to opt into this somehow...