Skip to content

Commit

Permalink
[Security] fix switch user without having current token
Browse files Browse the repository at this point in the history
  • Loading branch information
alamirault committed Feb 8, 2019
1 parent bb54e40 commit 27006ca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Expand Up @@ -83,6 +83,10 @@ public function handle(GetResponseEvent $event)
return;
}

if(null === $this->tokenStorage->getToken()){
throw new AuthenticationCredentialsNotFoundException('Could not find original Token object.');
}

if (self::EXIT_VALUE === $username) {
$this->tokenStorage->setToken($this->attemptExitUser($request));
} else {
Expand Down Expand Up @@ -164,7 +168,7 @@ private function attemptSwitchUser(Request $request, $username)
*/
private function attemptExitUser(Request $request)
{
if (null === ($currentToken = $this->tokenStorage->getToken()) || false === $original = $this->getOriginalToken($currentToken)) {
if (false === $original = $this->getOriginalToken($this->tokenStorage->getToken())) {
throw new AuthenticationCredentialsNotFoundException('Could not find original Token object.');
}

Expand Down
Expand Up @@ -267,6 +267,17 @@ public function testSwitchUserWithReplacedToken()
$this->assertSame($replacedToken, $this->tokenStorage->getToken());
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException
*/
public function testSwitchtUserThrowsAuthenticationExceptionIfNoCurrentToken()
{
$this->tokenStorage->setToken(null);
$this->request->query->set('_switch_user', 'username');
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
$listener->handle($this->event);
}

public function testSwitchUserStateless()
{
$token = new UsernamePasswordToken('username', '', 'key', ['ROLE_FOO']);
Expand Down

0 comments on commit 27006ca

Please sign in to comment.