Refactored Csrf form widget. + Added unit tests for Csrt.
This commit is contained in:
parent
d3a390d3f8
commit
7abd3febc1
9 changed files with 175 additions and 103 deletions
|
|
@ -6,17 +6,16 @@ use PHPCensor\View;
|
|||
|
||||
class Csrf extends Hidden
|
||||
{
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $rows = 4;
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function validate()
|
||||
{
|
||||
if ($this->value != $_COOKIE[$this->getName()]) {
|
||||
$sessionToken = isset($_SESSION['csrf_tokens'][$this->getName()])
|
||||
? $_SESSION['csrf_tokens'][$this->getName()]
|
||||
: null;
|
||||
|
||||
if ($this->value !== $sessionToken) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -30,9 +29,12 @@ class Csrf extends Hidden
|
|||
{
|
||||
parent::onPreRender($view);
|
||||
|
||||
$csrf = md5(microtime(true));
|
||||
$view->csrf = $csrf;
|
||||
$this->setValue(
|
||||
rtrim(strtr(base64_encode(random_bytes(32)), '+/', '-_'), '=')
|
||||
);
|
||||
|
||||
setcookie($this->getName(), $csrf);
|
||||
$view->value = $this->getValue();
|
||||
|
||||
$_SESSION['csrf_tokens'][$this->getName()] = $this->getValue();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue