Skip to content

Internals Router Security Csrf

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

router\security\Csrf

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/router/security/Csrf.php · Snapshot 8411f4287706.

Routing separates definition, HTTP request snapshot, matching, method policy, execution and error responses. Collaborators can be replaced through constructor injection.

Imports and dependencies

use router\interface\ICsrfTokenManager;
use router\interface\ICsrfTokenStore;

Csrf

Declaration and architectural role

final class Csrf implements ICsrfTokenManager

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
Constructor-promoted public function __construct(private readonly ICsrfTokenStore $store = new SessionTokenStore()) Initialized from the constructor argument; default expression is evaluated when the argument is omitted.

Method index

Method Signature
__construct public function __construct(private readonly ICsrfTokenStore $store = new SessionTokenStore())
token public function token(string $name = '_token'): string
regenerate public function regenerate(string $name = '_token'): string
validateToken public function validateToken(mixed $token, string $name = '_token'): bool

__construct

Source line 10

public function __construct(private readonly ICsrfTokenStore $store = new SessionTokenStore());

Parameters

Parameter Type, modifiers and default Purpose
$store private readonly ICsrfTokenStore $store = new SessionTokenStore() Operation input; the exact use, validation and propagation are shown in the implementation trace.

Concrete no-op/default hook: this method performs no work. Override it where the lifecycle or extension contract calls for behavior.

token

Source line 12

public function token(string $name = '_token'): string

Parameters

Parameter Type, modifiers and default Purpose
$name string $name = '_token' Named key/label selected by this API; its exact scope is shown by the implementation trace.

Return expressions (including local callbacks): $token !== null && $token !== '' ? $token : $this->regenerate($name).

Implementation, validation guards and interaction order
public function token(string $name = '_token'): string
    {
        $token = $this->store->get($name);
        return $token !== null && $token !== '' ? $token : $this->regenerate($name);
    }

regenerate

Source line 18

public function regenerate(string $name = '_token'): string

Parameters

Parameter Type, modifiers and default Purpose
$name string $name = '_token' Named key/label selected by this API; its exact scope is shown by the implementation trace.

Return expressions (including local callbacks): $token.

Implementation, validation guards and interaction order
public function regenerate(string $name = '_token'): string
    {
        $token = bin2hex(random_bytes(32));
        $this->store->put($name, $token);
        return $token;
    }

validateToken

Source line 25

public function validateToken(mixed $token, string $name = '_token'): bool

Parameters

Parameter Type, modifiers and default Purpose
$token mixed $token Operation input; the exact use, validation and propagation are shown in the implementation trace.
$name string $name = '_token' Named key/label selected by this API; its exact scope is shown by the implementation trace.

Return expressions (including local callbacks): $expected !== null && $expected !== '' && is_string($token) && hash_equals($expected, $token).

Implementation, validation guards and interaction order
public function validateToken(mixed $token, string $name = '_token'): bool
    {
        $expected = $this->store->get($name);
        return $expected !== null && $expected !== '' && is_string($token)
            && hash_equals($expected, $token);
    }

Related types

Clone this wiki locally