Skip to content

Commit

Permalink
Removal of Identity subclass leads to login removal instead of exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Nov 28, 2020
1 parent 8360a71 commit 29cbe58
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/Authentication/Data/BaseLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
use __PHP_Incomplete_Class;
use Brick\DateTime\Instant;
use Orisai\Auth\Authentication\Identity;
use Orisai\Exceptions\Logic\InvalidState;
use Orisai\Exceptions\Message;
use function sprintf;

abstract class BaseLogin
{

protected Identity $identity;
private Instant $authenticationTime;
private bool $hasInvalidIdentity = false;

public function __construct(Identity $identity, Instant $authenticationTime)
{
Expand All @@ -31,6 +29,11 @@ public function getAuthenticationTime(): Instant
return $this->authenticationTime;
}

public function hasInvalidIdentity(): bool
{
return $this->hasInvalidIdentity;
}

/**
* @return array<mixed>
*/
Expand All @@ -48,20 +51,11 @@ public function __serialize(): array
public function __unserialize(array $data): void
{
if ($data['identity'] instanceof __PHP_Incomplete_Class) {
$message = Message::create()
->withContext('Trying to deserialize Identity data.')
->withProblem(
sprintf('Deserialized class %s does not exist.', $data['identity']->__PHP_Incomplete_Class_Name),
)
->withSolution(
'Ensure class actually exists and is autoloadable or remove logins which use that class from storage.',
);

throw InvalidState::create()
->withMessage($message);
$this->hasInvalidIdentity = true;
} else {
$this->identity = $data['identity'];
}

$this->identity = $data['identity'];
$this->authenticationTime = Instant::of($data['authenticationTime']);
}

Expand Down
10 changes: 10 additions & 0 deletions src/Authentication/Data/Logins.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ public function __unserialize(array $data): void
{
$this->currentLogin = $data['currentLogin'];
$this->expiredLogins = $data['expiredLogins'];

if ($this->currentLogin !== null && $this->currentLogin->hasInvalidIdentity()) {
$this->currentLogin = null;
}

foreach ($this->expiredLogins as $key => $expiredLogin) {
if ($expiredLogin->hasInvalidIdentity()) {
unset($this->expiredLogins[$key]);
}
}
}

}

0 comments on commit 29cbe58

Please sign in to comment.