Skip to content

RandomnessBackedTokenGenerator

A drop-in replacement for Symfony’s default UriSafeTokenGenerator, generating the same URI-safe base64 shape but through RandomnessInterface instead of a direct random_bytes() call — so a cassette that records the RandomnessInterface reads behind a CSRF token can reproduce that exact token value on replay, and a request whose form POST depends on it does not fail the CSRF check purely because the token could not be regenerated deterministically.

final class RandomnessBackedTokenGenerator implements TokenGeneratorInterface

ImplementsTokenGeneratorInterface
SourceRandomnessBackedTokenGenerator.php

public function __construct(RandomnessInterface $randomness = new SystemRandomness(…), int $entropy = 256): mixed

ParameterTypeDescription
$randomnessRandomnessInterface
$entropyint

Returns mixed

MethodDescription
generateToken(): stringGenerates a CSRF token.

public function generateToken(): string

Generates a CSRF token.

Returns string