Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
Compatibility with Symfony 4.3 (#225) and PHP 7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
scheb committed Jul 12, 2019
1 parent 8d6994e commit 5a53ad7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,31 @@ public function setMessageKey(string $messageKey): void

public function serialize()
{
return serialize(array(
$this->messageKey,
parent::serialize(),
));
return serialize($this->__serialize());
}

public function unserialize($str)
// Symfony 4.3 / PHP 7.4
public function __serialize(): array
{
list($this->messageKey, $parentData) = unserialize($str);
parent::unserialize($parentData);
$parentHasNewInterface = method_exists(get_parent_class($this), '__serialize');
$parentData = $parentHasNewInterface ? parent::__serialize() : parent::serialize();

return [$this->messageKey, $parentData];
}

public function unserialize($serialized)
{
$this->__unserialize(\is_array($serialized) ? $serialized : unserialize($serialized));
}

// Symfony 4.3 / PHP 7.4
public function __unserialize(array $data): void
{
[$this->messageKey, $parentData] = $data;
if (\is_array($parentData)) {
parent::__unserialize($parentData);
} else {
parent::unserialize($parentData);
}
}
}
22 changes: 20 additions & 2 deletions Security/Authentication/Token/TwoFactorToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public function getRoles()
return [];
}

// Symfony 4.3
public function getRoleNames(): array
{
return $this->getRoles();
}

public function getCredentials()
{
return $this->credentials;
Expand Down Expand Up @@ -127,14 +133,26 @@ public function setAuthenticated($isAuthenticated)
throw new \RuntimeException('Cannot change authenticated once initialized.');
}

// Symfony 4.3 / PHP 7.4
public function __serialize(): array
{
return [$this->authenticatedToken, $this->credentials, $this->providerKey, $this->attributes, $this->twoFactorProviders];
}

public function serialize()
{
return serialize([$this->authenticatedToken, $this->credentials, $this->providerKey, $this->attributes, $this->twoFactorProviders]);
return serialize($this->__serialize());
}

// Symfony 4.3 / PHP 7.4
public function __unserialize(array $data): void
{
[$this->authenticatedToken, $this->credentials, $this->providerKey, $this->attributes, $this->twoFactorProviders] = $data;
}

public function unserialize($serialized)
{
list($this->authenticatedToken, $this->credentials, $this->providerKey, $this->attributes, $this->twoFactorProviders) = unserialize($serialized);
$this->__unserialize(\is_array($serialized) ? $serialized : unserialize($serialized));
}

public function getAttributes()
Expand Down
14 changes: 14 additions & 0 deletions Tests/Security/Authentication/Token/TwoFactorTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Exception\UnknownTwoFactorProviderException;
use Scheb\TwoFactorBundle\Tests\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

class TwoFactorTokenTest extends TestCase
{
Expand Down Expand Up @@ -96,4 +97,17 @@ public function allTwoFactorProvidersAuthenticated_allComplete_returnTrue(): voi
$this->twoFactorToken->setTwoFactorProviderComplete('provider2');
$this->assertTrue($this->twoFactorToken->allTwoFactorProvidersAuthenticated());
}

/**
* @test
*/
public function serialize_tokenGiven_unserializeIdenticalToken(): void
{
$innerToken = new UsernamePasswordToken('username', 'credentials', 'firewallName', ['ROLE']);
$twoFactorToken = new TwoFactorToken($innerToken, 'twoFactorCode', 'firewallName', ['2faProvider']);

$unserializedToken = unserialize(serialize($twoFactorToken));

$this->assertEquals($twoFactorToken, $unserializedToken);
}
}

0 comments on commit 5a53ad7

Please sign in to comment.