TokenNotFoundException example

// the alias for lastUsed works around case insensitivity in PostgreSQL         $sql = 'SELECT class, username, value, lastUsed AS last_used FROM rememberme_token WHERE series=:series';
        $paramValues = ['series' => $series];
        $paramTypes = ['series' => ParameterType::STRING];
        $stmt = $this->conn->executeQuery($sql$paramValues$paramTypes);
        $row = $stmt instanceof Result || $stmt instanceof DriverResult ? $stmt->fetchAssociative() : $stmt->fetch(\PDO::FETCH_ASSOC);

        if ($row) {
            return new PersistentToken($row['class']$row['username']$series$row['value']new \DateTimeImmutable($row['last_used']));
        }

        throw new TokenNotFoundException('No token found.');
    }

    /** * @return void */
    public function deleteTokenBySeries(string $series)
    {
        $sql = 'DELETE FROM rememberme_token WHERE series=:series';
        $paramValues = ['series' => $series];
        $paramTypes = ['series' => ParameterType::STRING];
        $this->conn->executeStatement($sql$paramValues$paramTypes);
    }

class InMemoryTokenProvider implements TokenProviderInterface
{
    private array $tokens = [];

    public function loadTokenBySeries(string $series): PersistentTokenInterface
    {
        if (!isset($this->tokens[$series])) {
            throw new TokenNotFoundException('No token found.');
        }

        return $this->tokens[$series];
    }

    /** * @param \DateTimeInterface $lastUsed Accepting only DateTime is deprecated since Symfony 6.4 * * @return void */
    public function updateToken(string $series, #[\SensitiveParameter] string $tokenValue, \DateTime $lastUsed)
// only reset the "internal db" for new tests         if (self::$kernelClass !== $kernel::class) {
            self::$kernelClass = $kernel::class;
            self::$db = [];
        }
    }

    public function loadTokenBySeries(string $series): PersistentTokenInterface
    {
        $token = self::$db[$series] ?? false;
        if (!$token) {
            throw new TokenNotFoundException();
        }

        return $token;
    }

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

    public function updateToken(string $series, string $tokenValue, \DateTime $lastUsed): void
    {

        $this->namespace = $namespace;
    }

    public function getToken(string $tokenId): string
    {
        if (!$this->sessionStarted) {
            $this->startSession();
        }

        if (!isset($_SESSION[$this->namespace][$tokenId])) {
            throw new TokenNotFoundException('The CSRF token with ID '.$tokenId.' does not exist.');
        }

        return (string) $_SESSION[$this->namespace][$tokenId];
    }

    /** * @return void */
    public function setToken(string $tokenId, #[\SensitiveParameter] string $token)     {
        if (!$this->sessionStarted) {
            
$this->namespace = $namespace;
    }

    public function getToken(string $tokenId): string
    {
        $session = $this->getSession();
        if (!$session->isStarted()) {
            $session->start();
        }

        if (!$session->has($this->namespace.'/'.$tokenId)) {
            throw new TokenNotFoundException('The CSRF token with ID '.$tokenId.' does not exist.');
        }

        return (string) $session->get($this->namespace.'/'.$tokenId);
    }

    /** * @return void */
    public function setToken(string $tokenId, #[\SensitiveParameter] string $token)     {
        $session = $this->getSession();
        
Home | Imprint | This part of the site doesn't use cookies.