sessionStorage = $storageInterface; } /** * Returns the current CSRF token, if none set it will create a new one. */ public function getToken(): CsrfToken { if (!\is_null($this->csrfToken)) { return $this->csrfToken; } if ($this->sessionStorage->hasToken()) { $value = $this->sessionStorage->getToken(); } else { $value = $this->tokenGenerator->generateToken(); $this->sessionStorage->setToken($value); } $this->csrfToken = new CsrfToken($value); return $this->csrfToken; } /** * Invalidates any current token and sets a new one. */ public function refreshToken(): CsrfToken { $value = $this->tokenGenerator->generateToken(); $this->sessionStorage->setToken($value); $this->csrfToken = new CsrfToken($value); return $this->csrfToken; } /** * Remove the current token from the storage. */ public function removeToken(): void { $this->csrfToken = null; $this->sessionStorage->removeToken(); } /** * Verifies whether the provided token is valid. */ public function isTokenValid(CsrfToken $token): bool { if (!$this->sessionStorage->hasToken()) { return false; } return hash_equals( $this->sessionStorage->getToken(), $token->getDecryptedValue() ); } }