-
-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
src/Symfony/Bundle/SecurityBundle/Tests/Functional/RefreshableRolesTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ymfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/refreshable_roles.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/Symfony/Component/Security/Core/Authentication/Token/RefreshableRolesTokenInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?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\Component\Security\Core\Authentication\Token; | ||
|
||
/** | ||
* Allows the roles of a token to be updated when security is persisted across a session. | ||
* | ||
* @author Ryan Weaver <ryan@knpuniversity.com> | ||
*/ | ||
interface RefreshableRolesTokenInterface | ||
{ | ||
/** | ||
* @param array $roles An array of roles | ||
*/ | ||
public function updateRoles(array $roles); | ||
|
||
/** | ||
* Returns whether or not roles *should* be updated on this token. | ||
* | ||
* This can be useful if your token is adding custom roles, | ||
* and so you purposely do not want the roles in the token to | ||
* be automatically reset. | ||
* | ||
* @return bool | ||
*/ | ||
public function shouldUpdateRoles(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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...