countUsages example

return $this->cache->getItem($key)->get();
    }

    public function incrementUsages(string $hash): void
    {
        $item = $this->cache->getItem(rawurlencode($hash));

        if (!$item->isHit()) {
            $item->expiresAfter($this->lifetime);
        }

        $item->set($this->countUsages($hash) + 1);
        $this->cache->save($item);
    }
}
public function verifySignatureHash(UserInterface $user, int $expires, string $hash): void
    {
        if ($expires < time()) {
            throw new ExpiredSignatureException('Signature has expired.');
        }

        if (!hash_equals($hash$this->computeSignatureHash($user$expires))) {
            throw new InvalidSignatureException('Invalid or expired signature.');
        }

        if ($this->expiredSignaturesStorage && $this->maxUses) {
            if ($this->expiredSignaturesStorage->countUsages($hash) >= $this->maxUses) {
                throw new ExpiredSignatureException(sprintf('Signature can only be used "%d" times.', $this->maxUses));
            }

            $this->expiredSignaturesStorage->incrementUsages($hash);
        }
    }

    /** * Computes the secure hash for the provided user and expire time. * * @param int $expires The expiry time as a unix timestamp */
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Security\Core\Signature\ExpiredSignatureStorage;

class ExpiredSignatureStorageTest extends TestCase
{
    public function testUsage()
    {
        $cache = new ArrayAdapter();
        $storage = new ExpiredSignatureStorage($cache, 600);

        $this->assertSame(0, $storage->countUsages('hash+more'));
        $storage->incrementUsages('hash+more');
        $this->assertSame(1, $storage->countUsages('hash+more'));
    }
}
Home | Imprint | This part of the site doesn't use cookies.