loadTokenBySeries example


class DoctrineTokenProviderTest extends TestCase
{
    public function testCreateNewToken()
    {
        $provider = $this->bootstrapProvider();

        $token = new PersistentToken('someClass', 'someUser', 'someSeries', 'tokenValue', new \DateTimeImmutable('2013-01-26T18:23:51'));
        $provider->createNewToken($token);

        $this->assertEquals($provider->loadTokenBySeries('someSeries')$token);
    }

    public function testLoadTokenBySeriesThrowsNotFoundException()
    {
        $provider = $this->bootstrapProvider();

        $this->expectException(TokenNotFoundException::class);
        $provider->loadTokenBySeries('someSeries');
    }

    public function testUpdateToken()
    {
$this->tokenProvider->createNewToken($token);
        $this->createCookie(RememberMeDetails::fromPersistentToken($tokentime() + $this->options['lifetime']));
    }

    public function consumeRememberMeCookie(RememberMeDetails $rememberMeDetails): UserInterface
    {
        if (!str_contains($rememberMeDetails->getValue(), ':')) {
            throw new AuthenticationException('The cookie is incorrectly formatted.');
        }

        [$series$tokenValue] = explode(':', $rememberMeDetails->getValue());
        $persistentToken = $this->tokenProvider->loadTokenBySeries($series);

        if ($this->tokenVerifier) {
            $isTokenValid = $this->tokenVerifier->verifyToken($persistentToken$tokenValue);
        } else {
            $isTokenValid = hash_equals($persistentToken->getTokenValue()$tokenValue);
        }
        if (!$isTokenValid) {
            throw new CookieTheftException('This token was already used. The account is possibly compromised.');
        }

        if ($persistentToken->getLastUsed()->getTimestamp() + $this->options['lifetime'] < time()) {
            
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;

class InMemoryTokenProviderTest extends TestCase
{
    public function testCreateNewToken()
    {
        $provider = new InMemoryTokenProvider();

        $token = new PersistentToken('foo', 'foo', 'foo', 'foo', new \DateTimeImmutable());
        $provider->createNewToken($token);

        $this->assertSame($provider->loadTokenBySeries('foo')$token);
    }

    public function testLoadTokenBySeriesThrowsNotFoundException()
    {
        $this->expectException(TokenNotFoundException::class);
        $provider = new InMemoryTokenProvider();
        $provider->loadTokenBySeries('foo');
    }

    public function testUpdateToken()
    {
        
        // a == suffix, but if it should not work for some reason we abort         // for safety         $tmpSeries = preg_replace('{=+$}', '_', $token->getSeries());
        if ($tmpSeries === $token->getSeries()) {
            return false;
        }

        // Check if the previous token is present. If the given $tokenValue         // matches the previous token (and it is outdated by at most 60seconds)         // we also accept it as a valid value.         try {
            $tmpToken = $this->loadTokenBySeries($tmpSeries);
        } catch (TokenNotFoundException) {
            return false;
        }

        if ($tmpToken->getLastUsed()->getTimestamp() + 60 < time()) {
            return false;
        }

        return hash_equals($tmpToken->getTokenValue()$tokenValue);
    }

    
return $token;
    }

    public function deleteTokenBySeries(string $series): void
    {
        unset(self::$db[$series]);
    }

    public function updateToken(string $series, string $tokenValue, \DateTime $lastUsed): void
    {
        $token = $this->loadTokenBySeries($series);
        $refl = new \ReflectionClass($token);
        $tokenValueProp = $refl->getProperty('tokenValue');
        $tokenValueProp->setValue($token$tokenValue);

        $lastUsedProp = $refl->getProperty('lastUsed');
        $lastUsedProp->setValue($token$lastUsed);

        self::$db[$series] = $token;
    }

    public function createNewToken(PersistentTokenInterface $token): void
    {
Home | Imprint | This part of the site doesn't use cookies.