Skip to content
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

[FrameworkBundle] make KernelBrowser::loginUser() session available for updating after login #47001

Open
wants to merge 1 commit into
base: 7.2
Choose a base branch
from

Conversation

arderyp
Copy link
Contributor

@arderyp arderyp commented Jul 21, 2022

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? no
Tickets #46961
License MIT
Doc PR symfony/symfony-docs#...

After upgrading from 4.4 to 5.4, I started running into deprecation warnings about fetching security.csrf.token_manager and security.csrf.token_storage from the test container. The issue was that, in my functional tests, I was logging in, then I needed to generate and apply CSRF tokens to the logged in session so that I could directly submit data arrays to POST controller action methods without having to crawl to the form page first.

Since I was not crawling to the form page first, no CSRF for the form was generated and applied to the session. Oddly enough, generating tokens from the CSRF token storage services did work, despite the deprecation warnings, and I'm not entirely sure how that was working without an activated session pointer. I prefer to not crawl to the form page first as it would double the amount of crawler requests in my test suite, so this approach is mostly for convenience (easier to abstract than crawler DOM interactions) and speed.

So, this is a simply PR that probably needs tweaks, and test coverage and docs, but I didn't want to invest the time into the later two if the Symfony team thinks this is not an idea they'd consider for implementation.

Anyways, all this PR really does is track an internal pointer to the generated test session on KernelBrowser, which can be manipulated after calling loginUser().

I've explained the workaround to my problem, which implements this kind of logic here: #46961

I suppose another simpler option would be to continue allowing the use of the csrf token storage services without an active session within the test container (to basically function as they do now but without the deprecation warnings). I suspect this might not be possible given the system migration towards RequestStack.

Given this PR, I could now do:

use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;

$user = ...;
$client = static::createClient();
$client->loginUser($user);
        
// Technically, you don't need to generate a real token here, and instead could use any test string
$tokenId = ...;
$csrfToken = static::getContainer()->get('security.csrf.token_generator')->generateToken();
$client->setLoginSessionValue(SessionTokenStorage::SESSION_NAMESPACE . "/$tokenId", $csrfToken);

I imagine there are wide applications for this feature beyond CSRF tokens.

@carsonbot carsonbot added this to the 6.2 milestone Jul 21, 2022
@carsonbot carsonbot changed the title [feature] make KernelBrowser::loginUser() session available for updating after login make KernelBrowser::loginUser() session available for updating after login Jul 21, 2022
@arderyp
Copy link
Contributor Author

arderyp commented Jul 21, 2022

On a related note, it would be nice to be able to tell loginUser() which token type to user, for example UsernamePasswordToken instead of TestBrowserToken.

@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@arderyp
Copy link
Contributor Author

arderyp commented Feb 29, 2024

Any thoughts on this?

@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@carsonbot carsonbot changed the title make KernelBrowser::loginUser() session available for updating after login [FrameworkBundle] make KernelBrowser::loginUser() session available for updating after login May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants