1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-15 01:24:25 +02:00

Flextype Core: Csrf Twig Extension - added

This commit is contained in:
Awilum
2019-03-09 15:34:42 +03:00
parent 00327eaf61
commit 50078155e1
2 changed files with 68 additions and 1 deletions

View File

@@ -67,6 +67,18 @@ $app = new \Slim\App($config);
*/
$flextype = $app->getContainer();
/**
* Add CSRF (cross-site request forgery) protection service to Flextype container
*/
$flextype['csrf'] = function ($container) {
return new \Slim\Csrf\Guard;
};
/**
* Add middleware CSRF (cross-site request forgery) protection for all routes
*/
$app->add($flextype->get('csrf'));
/**
* Add emitter service to Flextype container
*/
@@ -276,7 +288,7 @@ $flextype['view'] = function ($container) {
// Add Emitter Twig Extension
$view->addExtension(new EmitterTwigExtension($container));
// Add Emitter Twig Extension
// Add Flash Twig Extension
$view->addExtension(new FlashTwigExtension($container));
// Add I18n Twig Extension
@@ -285,6 +297,9 @@ $flextype['view'] = function ($container) {
// Add Assets Twig Extension
$view->addExtension(new AssetsTwigExtension());
// Add Csrf Twig Extension
$view->addExtension(new CsrfTwigExtension($container->get('csrf')));
// Return view
return $view;
};

View File

@@ -0,0 +1,52 @@
<?php
/**
* @package Flextype
*
* @author Sergey Romanenko <hello@romanenko.digital>
* @link http://romanenko.digital
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flextype;
class CsrfTwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
{
/**
* @var \Slim\Csrf\Guard
*/
protected $csrf;
public function __construct(\Slim\Csrf\Guard $csrf)
{
$this->csrf = $csrf;
}
public function getGlobals()
{
// CSRF token name and value
$csrfNameKey = $this->csrf->getTokenNameKey();
$csrfValueKey = $this->csrf->getTokenValueKey();
$csrfName = $this->csrf->getTokenName();
$csrfValue = $this->csrf->getTokenValue();
return [
'csrf' => [
'keys' => [
'name' => $csrfNameKey,
'value' => $csrfValueKey
],
'name' => $csrfName,
'value' => $csrfValue
]
];
}
public function getName()
{
return 'slim/csrf';
}
}