Skip to content

Commit 7152f0e

Browse files
committed
[Security] Store original token in token storage when implicitly exiting impersonation
1 parent 8a4986d commit 7152f0e

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

Firewall/SwitchUserListener.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function authenticate(RequestEvent $event)
109109
}
110110

111111
if (self::EXIT_VALUE === $username) {
112-
$this->tokenStorage->setToken($this->attemptExitUser($request));
112+
$this->attemptExitUser($request);
113113
} else {
114114
try {
115115
$this->tokenStorage->setToken($this->attemptSwitchUser($request, $username));
@@ -221,6 +221,8 @@ private function attemptExitUser(Request $request): TokenInterface
221221
$original = $switchEvent->getToken();
222222
}
223223

224+
$this->tokenStorage->setToken($original);
225+
224226
return $original;
225227
}
226228

Tests/Firewall/SwitchUserListenerTest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\HttpKernel\HttpKernelInterface;
1919
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
2020
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
21+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2122
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2223
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
2324
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
@@ -228,7 +229,10 @@ public function testSwitchUserAlreadySwitched()
228229

229230
$targetsUser = $this->callback(function ($user) { return 'kuba' === $user->getUserIdentifier(); });
230231
$this->accessDecisionManager->expects($this->once())
231-
->method('decide')->with($originalToken, ['ROLE_ALLOWED_TO_SWITCH'], $targetsUser)
232+
->method('decide')->with(self::callback(function (TokenInterface $token) use ($originalToken, $tokenStorage) {
233+
// the token storage should also contain the original token for voters depending on it
234+
return $token === $originalToken && $tokenStorage->getToken() === $originalToken;
235+
}), ['ROLE_ALLOWED_TO_SWITCH'], $targetsUser)
232236
->willReturn(true);
233237

234238
$this->userChecker->expects($this->once())

0 commit comments

Comments
 (0)