Silex 2/Symfony: Check CSRF token from security login form -
i not use "form service provider" , manually output csrf token twig login form:
$csrf_token = $app['csrf.token_manager']->gettoken('token_id'); //'token'
and in login.html.twig:
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}">
the manual (https://silex.symfony.com/doc/2.0/providers/csrf.html) says, it's possible check token this:
$app['csrf.token_manager']->istokenvalid(new csrftoken('token_id', 'token'));
but whole login process handled security component. how add csrf check it?
this firewall setup:
$app['security.firewalls'] = array( 'login' => array( 'pattern' => '^/user/login$', ), 'secured_area' => array( 'pattern' => '^.*$', 'anonymous' => false, 'remember_me' => array(), 'form' => array( 'login_path' => '/user/login', 'check_path' => '/user/login_check', ), 'logout' => array( 'logout_path' => '/user/logout', 'invalidate_session' => true ), 'users' => function () use ($app) { return new userprovider($app['db']); }, ));
and login controller:
$app->get('/user/login', function(request $request) use ($app) { $csrf_token = $app['csrf.token_manager']->gettoken('token_id'); //'token' return $app['twig']->render('login.html.twig', array( 'csrf_token' => $csrf_token, )); });
try add csrf
options security config:
$app['security.firewalls'] = array( .... 'form' => array( 'login_path' => '/user/login', 'check_path' => '/user/login_check', 'with_csrf' => true, 'csrf_parameter' => '_csrf_token', // form field name 'csrf_token_id' => 'token_id' ), ....
Comments
Post a Comment