Csrf
Csrf
CSRF token manager.
class CsrfEach client gets a random secret, held in a cookie. A token is an HMAC of that secret and an optional binding (the signed-in identity), so nothing is stored on the server and the same scheme works with the cookie and JWT session backends. Because the identity is part of the HMAC, a token issued before login stops validating after it, which rotates tokens on login without any extra step.
Set $key to a server-side secret to stop an attacker who can plant cookies (for example from a sibling subdomain) from minting valid tokens.
Constants
FIELD
public const FIELD = '_csrf'HEADER
public const HEADER = 'X-Csrf-Token'Properties
current
public Csrf $currentType: Hazaar\Security\Csrf
secret
public string $secretType: string
issued
private bool $issuedType: bool
binding
public string $bindingType: string
key
private string $keyType: string
cookieName
private string $cookieName = 'hazaar-csrf'Type: string
secure
private bool $secure = trueType: bool
samesite
private string $samesite = 'Lax'Type: string
path
private string $path = '/'Type: string
Methods
__construct
public __construct(string $key, string $cookieName = 'hazaar-csrf', bool $secure = true, string $samesite = 'Lax', string $path = '/'): voidParameters
| Parameter | Type | Description |
|---|---|---|
$key | string | |
$cookieName | string | |
$secure | bool | |
$samesite | string | |
$path | string |
current
The instance for the current request, set by the CSRF middleware. Used by the view helper.
public current(): ?CsrfReturns: Hazaar\Security\Csrf
setCurrent
public setCurrent(?Csrf $csrf): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$csrf | Hazaar\Security\Csrf |
bind
Sets what tokens are tied to, normally the signed-in identity.
public bind(?string $binding): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$binding | string |
token
Returns the token for the current client and binding, starting a new secret if the client has none.
public token(): stringReturns: string
validate
Checks a submitted token in constant time.
public validate(?string $token): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$token | string |
commit
Sets the secret cookie on the response if this request started a new one.
public commit(Response $response): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$response | Hazaar\Controller\Response |
compute
private compute(string $secret): stringReturns: string
Parameters
| Parameter | Type | Description |
|---|---|---|
$secret | string |
Generated by Hazaar API Doc Generator on Sun, 08 Nov 2026 03:36:14 +0000