mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
Merge pull request #52 from skipperbent/development
[TASK] Csrf-token fixes + readded BaseCsrfVerifier.
This commit is contained in:
@@ -7,12 +7,6 @@ class CsrfToken {
|
|||||||
|
|
||||||
protected $token;
|
protected $token;
|
||||||
|
|
||||||
public function __construct() {
|
|
||||||
if($this->getToken() === null) {
|
|
||||||
$this->setToken($this->generateToken());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate random identifier for CSRF token
|
* Generate random identifier for CSRF token
|
||||||
* @return string
|
* @return string
|
||||||
@@ -51,10 +45,18 @@ class CsrfToken {
|
|||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function getToken(){
|
public function getToken(){
|
||||||
if(isset($_COOKIE[self::CSRF_KEY])) {
|
if($this->hasToken()) {
|
||||||
return $_COOKIE[self::CSRF_KEY];
|
return $_COOKIE[self::CSRF_KEY];
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether the csrf token has been defined
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasToken() {
|
||||||
|
return isset($_COOKIE[self::CSRF_KEY]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Pecee\SimpleRouter;
|
namespace Pecee\SimpleRouter;
|
||||||
|
|
||||||
|
use Pecee\CsrfToken;
|
||||||
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
|
|
||||||
@@ -13,7 +14,7 @@ class RouterBase {
|
|||||||
protected $routes;
|
protected $routes;
|
||||||
protected $processedRoutes;
|
protected $processedRoutes;
|
||||||
protected $controllerUrlMap;
|
protected $controllerUrlMap;
|
||||||
protected $backstack;
|
protected $backStack;
|
||||||
protected $loadedRoute;
|
protected $loadedRoute;
|
||||||
protected $defaultNamespace;
|
protected $defaultNamespace;
|
||||||
protected $baseCsrfVerifier;
|
protected $baseCsrfVerifier;
|
||||||
@@ -23,14 +24,19 @@ class RouterBase {
|
|||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->routes = array();
|
$this->routes = array();
|
||||||
$this->backstack = array();
|
$this->backStack = array();
|
||||||
$this->controllerUrlMap = array();
|
$this->controllerUrlMap = array();
|
||||||
|
$this->baseCsrfVerifier = new BaseCsrfVerifier();
|
||||||
$this->request = Request::getInstance();
|
$this->request = Request::getInstance();
|
||||||
|
|
||||||
|
$csrf = new CsrfToken();
|
||||||
|
$token = ($csrf->hasToken()) ? $csrf->getToken() : $csrf->generateToken();
|
||||||
|
$csrf->setToken($token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addRoute(RouterEntry $route) {
|
public function addRoute(RouterEntry $route) {
|
||||||
if($this->currentRoute !== null) {
|
if($this->currentRoute !== null) {
|
||||||
$this->backstack[] = $route;
|
$this->backStack[] = $route;
|
||||||
} else {
|
} else {
|
||||||
$this->routes[] = $route;
|
$this->routes[] = $route;
|
||||||
}
|
}
|
||||||
@@ -87,9 +93,9 @@ class RouterBase {
|
|||||||
|
|
||||||
$this->currentRoute = null;
|
$this->currentRoute = null;
|
||||||
|
|
||||||
if(count($this->backstack)) {
|
if(count($this->backStack)) {
|
||||||
$backStack = $this->backstack;
|
$backStack = $this->backStack;
|
||||||
$this->backstack = array();
|
$this->backStack = array();
|
||||||
|
|
||||||
// Route any routes added to the backstack
|
// Route any routes added to the backstack
|
||||||
$this->processRoutes($backStack, $mergedSettings, $newPrefixes, true, $activeGroup);
|
$this->processRoutes($backStack, $mergedSettings, $newPrefixes, true, $activeGroup);
|
||||||
@@ -174,7 +180,7 @@ class RouterBase {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getBackstack() {
|
public function getBackstack() {
|
||||||
return $this->backstack;
|
return $this->backStack;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user