diff --git a/src/Symfony/Component/Security/Core/Authentication/Token/SwitchUserToken.php b/src/Symfony/Component/Security/Core/Authentication/Token/SwitchUserToken.php index e575999374..ccccb5b51c 100644 --- a/src/Symfony/Component/Security/Core/Authentication/Token/SwitchUserToken.php +++ b/src/Symfony/Component/Security/Core/Authentication/Token/SwitchUserToken.php @@ -59,7 +59,12 @@ class SwitchUserToken extends UsernamePasswordToken */ public function __unserialize(array $data): void { - [$this->originalToken, $this->originatedFromUri, $parentData] = $data; + if (3 > \count($data)) { + // Support for tokens serialized with version 5.1 or lower of symfony/security-core. + [$this->originalToken, $parentData] = $data; + } else { + [$this->originalToken, $this->originatedFromUri, $parentData] = $data; + } $parentData = \is_array($parentData) ? $parentData : unserialize($parentData); parent::__unserialize($parentData); } diff --git a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/Fixtures/switch-user-token-4.4.txt b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/Fixtures/switch-user-token-4.4.txt new file mode 100644 index 0000000000..7b3f7c4092 Binary files /dev/null and b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/Fixtures/switch-user-token-4.4.txt differ diff --git a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/SwitchUserTokenTest.php b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/SwitchUserTokenTest.php index 00f1ac984a..8138f76596 100644 --- a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/SwitchUserTokenTest.php +++ b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/SwitchUserTokenTest.php @@ -84,4 +84,18 @@ class SwitchUserTokenTest extends TestCase $this->assertNull($unserializedToken->getOriginatedFromUri()); } + + public function testUnserializeOldToken() + { + /** @var SwitchUserToken $token */ + $token = unserialize(file_get_contents(__DIR__.'/Fixtures/switch-user-token-4.4.txt')); + + self::assertInstanceOf(SwitchUserToken::class, $token); + self::assertInstanceOf(UsernamePasswordToken::class, $token->getOriginalToken()); + self::assertSame('john', $token->getUsername()); + self::assertSame(['foo' => 'bar'], $token->getCredentials()); + self::assertSame('main', $token->getFirewallName()); + self::assertEquals(['ROLE_USER'], $token->getRoleNames()); + self::assertNull($token->getOriginatedFromUri()); + } }