mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-25 11:38:44 +02:00
N°8771 - Add Symfony form component to iTop core (#760)
- Add Symfony Form Component - Add Symfony CSRF security component - Add iTop default form template - Add Twig debug extension to Twig Environment - Add iTop abstract controller facility to get form builder - Add Twig filter to make trans an alias of dict_s filter
This commit is contained in:
112
lib/symfony/security-csrf/TokenStorage/SessionTokenStorage.php
Normal file
112
lib/symfony/security-csrf/TokenStorage/SessionTokenStorage.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Security\Csrf\TokenStorage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\Security\Csrf\Exception\TokenNotFoundException;
|
||||
|
||||
/**
|
||||
* Token storage that uses a Symfony Session object.
|
||||
*
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
class SessionTokenStorage implements ClearableTokenStorageInterface
|
||||
{
|
||||
/**
|
||||
* The namespace used to store values in the session.
|
||||
*/
|
||||
public const SESSION_NAMESPACE = '_csrf';
|
||||
|
||||
private RequestStack $requestStack;
|
||||
private string $namespace;
|
||||
|
||||
/**
|
||||
* Initializes the storage with a RequestStack object and a session namespace.
|
||||
*
|
||||
* @param string $namespace The namespace under which the token is stored in the requestStack
|
||||
*/
|
||||
public function __construct(RequestStack $requestStack, string $namespace = self::SESSION_NAMESPACE)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->namespace = $namespace;
|
||||
}
|
||||
|
||||
public function getToken(string $tokenId): string
|
||||
{
|
||||
$session = $this->getSession();
|
||||
if (!$session->isStarted()) {
|
||||
$session->start();
|
||||
}
|
||||
|
||||
if (!$session->has($this->namespace.'/'.$tokenId)) {
|
||||
throw new TokenNotFoundException('The CSRF token with ID '.$tokenId.' does not exist.');
|
||||
}
|
||||
|
||||
return (string) $session->get($this->namespace.'/'.$tokenId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function setToken(string $tokenId, #[\SensitiveParameter] string $token)
|
||||
{
|
||||
$session = $this->getSession();
|
||||
if (!$session->isStarted()) {
|
||||
$session->start();
|
||||
}
|
||||
|
||||
$session->set($this->namespace.'/'.$tokenId, $token);
|
||||
}
|
||||
|
||||
public function hasToken(string $tokenId): bool
|
||||
{
|
||||
$session = $this->getSession();
|
||||
if (!$session->isStarted()) {
|
||||
$session->start();
|
||||
}
|
||||
|
||||
return $session->has($this->namespace.'/'.$tokenId);
|
||||
}
|
||||
|
||||
public function removeToken(string $tokenId): ?string
|
||||
{
|
||||
$session = $this->getSession();
|
||||
if (!$session->isStarted()) {
|
||||
$session->start();
|
||||
}
|
||||
|
||||
return $session->remove($this->namespace.'/'.$tokenId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$session = $this->getSession();
|
||||
foreach (array_keys($session->all()) as $key) {
|
||||
if (str_starts_with($key, $this->namespace.'/')) {
|
||||
$session->remove($key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SessionNotFoundException
|
||||
*/
|
||||
private function getSession(): SessionInterface
|
||||
{
|
||||
return $this->requestStack->getSession();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user