Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Rudra\Controller;

use Rudra\Container\Facades\Session;
use Rudra\Container\Interfaces\RudraInterface;

class Controller implements ControllerInterface
{
Expand All @@ -24,40 +23,45 @@ public function __construct()
/**
* @return void
*/
#[\Override]
public function init(): void {}

/**
* @return void
*/
#[\Override]
public function before(): void {}

/**
* @return void
*/
#[\Override]
public function after(): void {}

/**
* Method to protect against CSRF attack
* ---------------
* Метод защиты от CSRF-атак
*
* @return void
*/
public function csrfProtection(): void
{
if (!isset($_SESSION)) {
$local = (php_sapi_name() == "cli-server");
$local = (php_sapi_name() === "cli-server");
session_set_cookie_params([
'lifetime' => 604800, // 7 days
'path' => '/',
'secure' => !$local,
'path' => '/',
'secure' => !$local,
'httponly' => true,
'samesite' => 'Lax'
]);
session_start();
}

if (Session::has("csrf_token")) {
unset($_SESSION["csrf_token"][count($_SESSION["csrf_token"]) - 1]);
array_unshift($_SESSION["csrf_token"], md5(uniqid((string)mt_rand(), true)));
array_pop($_SESSION["csrf_token"]);
array_unshift($_SESSION["csrf_token"], bin2hex(random_bytes(32)));
return;
}

Expand Down
7 changes: 3 additions & 4 deletions tests/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@

namespace Rudra\Controller\Tests;

use PHPUnit\Framework\TestCase;
use Rudra\Container\Facades\Session;
use Rudra\Container\Facades\Rudra as Rudra;
use Rudra\Container\Interfaces\RudraInterface;
use Rudra\Controller\{Controller, ControllerInterface};
use Rudra\Controller\Controller;
use Rudra\Controller\ControllerInterface;

class ControllerTest extends TestCase
class ControllerTest extends \PHPUnit\Framework\TestCase
{
protected ControllerInterface $controller;

Expand Down
Loading