Hi, @mraspor
I'm not sure if the CSRF token cheching really works. following is the code
services.php:
$di->setShared('security', function () {
$security = new Security();
$security->setWorkFactor(12);
$security->setDefaultHash(Security::CRYPT_BLOWFISH_Y); // choose default hash
return $security;
});
$di->set('crypt', function () use ($config) {
$crypt = new Crypt();
$crypt->setKey($config->application->cryptSalt);
return $crypt;
});
FormBase.php and OtherForm.php, just like what you show
class FormBase extends Form
{
protected $_csrf;
public function initialize()
{
$csrf = new Hidden($this->getCsrfName());
//$csrf->clear();
$csrf->setDefault($this->security->getToken())
->addValidator(new Identical([
'accepted' => $this->security->checkToken(),
'message' => 'CSRF forgery detected!'
]));
//var_dump($this->security->getToken());
$this->add($csrf);
}
// Generates CSRF token key
public function getCsrfName()
{
if (empty($this->_csrf)) {
$this->_csrf = $this->security->getTokenKey();
}
return $this->_csrf;
}
}
//other forms
class RegisterForm extends FormBase
{
public function initialize($entity = null)
{
parent::initialize();
}
}
And in the controller:
$form = new RegisterForm();
if ($this->request->isPost()) {
if ($form->isValid($this->request->getPost()) == false) {
foreach ($form->getMessages() as $message) {
$this->flash->error((string) $message);
}
} else {
$this->flash->success('ok');
var_dump($this->request->getPost());
var_dump($this->security->checkToken());
}
}
when I fill the form, and manually modify the token value, and submit....It just says 'ok', seems the addValidator(new Identical(...))
can't work!
why?