Skip to content

[Security] pass the current token to the checkPostAuth() method of user checkers #57773

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
Jul 25, 2024
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
1 change: 1 addition & 0 deletions UPGRADE-7.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ If you're upgrading from a version below 7.1, follow the [7.1 upgrade guide](UPG
Security
--------

* Add `$token` argument to `UserCheckerInterface::checkPostAuth()`
* Deprecate argument `$secret` of `RememberMeToken` and `RememberMeAuthenticator`

String
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Security/Core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
7.2
---

* Add `$token` argument to `UserCheckerInterface::checkPostAuth()`
* Deprecate argument `$secret` of `RememberMeToken`

7.0
Expand Down
12 changes: 10 additions & 2 deletions src/Symfony/Component/Security/Core/User/ChainUserChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Security\Core\User;

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

final class ChainUserChecker implements UserCheckerInterface
{
/**
Expand All @@ -27,10 +29,16 @@ public function checkPreAuth(UserInterface $user): void
}
}

public function checkPostAuth(UserInterface $user): void
public function checkPostAuth(UserInterface $user /*, TokenInterface $token*/): void
{
$token = 1 < \func_num_args() ? func_get_arg(1) : null;

foreach ($this->checkers as $checker) {
$checker->checkPostAuth($user);
if ($token instanceof TokenInterface) {
$checker->checkPostAuth($user, $token);
} else {
$checker->checkPostAuth($user);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ public function checkPreAuth(UserInterface $user): void;
*
* @throws AccountStatusException
*/
public function checkPostAuth(UserInterface $user): void;
public function checkPostAuth(UserInterface $user /*, TokenInterface $token*/): void;
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Security/Http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
7.2
---

* Pass the current token to the `checkPostAuth()` method of user checkers
* Deprecate argument `$secret` of `RememberMeAuthenticator`

7.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function postCheckCredentials(AuthenticationSuccessEvent $event): void
return;
}

$this->userChecker->checkPostAuth($user);
$this->userChecker->checkPostAuth($user, $event->getAuthenticationToken());
}

public static function getSubscribedEvents(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private function attemptSwitchUser(Request $request, string $username): ?TokenIn

$this->logger?->info('Attempting to switch to user.', ['username' => $username]);

$this->userChecker->checkPostAuth($user);
$this->userChecker->checkPostAuth($user, $token);

$roles = $user->getRoles();
$originatedFromUri = str_replace('/&', '/?', preg_replace('#[&?]'.$this->usernameParameter.'=[^&]*#', '', $request->getRequestUri()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public function testPostAuthValidCredentials()
$this->listener->postCheckCredentials(new AuthenticationSuccessEvent(new PostAuthenticationToken($this->user, 'main', [])));
}

public function testTokenIsPassedToPost()
{
$token = new PostAuthenticationToken($this->user, 'main', []);
$this->userChecker->expects($this->once())->method('checkPostAuth')->with($this->user, $token);

$this->listener->postCheckCredentials(new AuthenticationSuccessEvent($token));
}

private function createCheckPassportEvent($passport = null)
{
$passport ??= new SelfValidatingPassport(new UserBadge('test', fn () => $this->user));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function testSwitchUser()
->willReturn(true);

$this->userChecker->expects($this->once())
->method('checkPostAuth')->with($this->callback(fn ($user) => 'kuba' === $user->getUserIdentifier()));
->method('checkPostAuth')->with($this->callback(fn ($user) => 'kuba' === $user->getUserIdentifier()), $token);

$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
$listener($this->event);
Expand Down