Csrf-token are now refreshed on each page-load to avoid timeout.

This commit is contained in:
Simon Sessingø
2017-11-10 12:59:59 +01:00
parent 97753f5370
commit c3072e8886
2 changed files with 21 additions and 7 deletions
+17 -6
View File
@@ -1,4 +1,5 @@
<?php
namespace Pecee;
class CsrfToken
@@ -48,24 +49,34 @@ class CsrfToken
* Set csrf token cookie
* Overwrite this method to save the token to another storage like session etc.
*
* @param $token
* @param string $token
*/
public function setToken($token)
{
$this->token = $token;
setcookie(static::CSRF_KEY, $token, time() + 60 * 120, '/');
}
/**
* Get csrf token
* @param string|null $defaultValue
* @return string|null
*/
public function getToken()
public function getToken($defaultValue = null)
{
if ($this->hasToken() === true) {
return $_COOKIE[static::CSRF_KEY];
}
$this->token = ($this->hasToken() === true) ? $_COOKIE[static::CSRF_KEY] : null;
return null;
return ($this->token !== null) ? $this->token : $defaultValue;
}
/**
* Refresh existing token
*/
public function refresh()
{
if ($this->token !== null) {
$this->setToken($this->token);
}
}
/**
@@ -20,7 +20,7 @@ class BaseCsrfVerifier implements IMiddleware
$this->csrfToken = new CsrfToken();
// Generate or get the CSRF-Token from Cookie.
$this->token = ($this->hasToken() === false) ? $this->generateToken() : $this->csrfToken->getToken();
$this->token = $this->csrfToken->getToken($this->generateToken());
}
/**
@@ -73,6 +73,9 @@ class BaseCsrfVerifier implements IMiddleware
}
// Refresh existing token
$this->csrfToken->refresh();
}
public function generateToken()