Skip to content

Instantly share code, notes, and snippets.

@mdestafadilah
Last active October 31, 2024 07:41
Show Gist options
  • Save mdestafadilah/9b3cab601c9492d552ebf59a2d698667 to your computer and use it in GitHub Desktop.
Save mdestafadilah/9b3cab601c9492d552ebf59a2d698667 to your computer and use it in GitHub Desktop.
Shieldon Codeigniter 4
<?php
/*
* This file is part of the Shieldon package.
*
* (c) Terry L. <contact@terryl.in>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* File Correction: APP\vendor\Shieldon\src\Firewall\Integration\CodeIgniter4
* Source instalation: https://shieldon.io/en/guide/codeigniter.html#codeigniter-4
* Source Correction: https://forum.codeigniter.com/showthread.php?tid=78109&pid=382427#pid382427
*
*/
declare(strict_types=1);
namespace Shieldon\Firewall\Integration;
use Shieldon\Firewall\Firewall;
use Shieldon\Firewall\HttpResolver;
use CodeIgniter\HTTP\RequestInterface as Request;
use CodeIgniter\HTTP\ResponseInterface as Response;
use CodeIgniter\Filters\FilterInterface;
use Shieldon\Firewall\Captcha\Csrf;
use function csrf_token; // CodeIgniter 4
use function csrf_hash; // CodeIgniter 4
/**
* CodeIgniter 4 Middleware of Shieldon Firewall.
*/
class CodeIgniter4 implements FilterInterface
{
/**
* The absolute path of the storage where stores Shieldon generated data.
*
* @var string
*/
protected $storage;
/**
* The entry point of Shieldon Firewall's control panel.
*
* For example: https://yoursite.com/firewall/panel/
* Just use the path component of a URI.
*
* @var string
*/
protected $panelUri;
/**
* Constructor.
*
* @param string $storage See property `storage` explanation.
* @param string $panelUri See property `panelUri` explanation.
*
* @return void
*/
public function __construct(string $storage = '', string $panelUri = '')
{
$this->storage = WRITEPATH . 'shieldon_firewall';
$this->panelUri = '/firewall/panel/';
if ('' !== $storage) {
$this->storage = $storage;
}
if ('' !== $panelUri) {
$this->panelUri = $panelUri;
}
}
/**
* Shieldon middleware invokable class.
*
* @param Request $request
*
* @return mixed
*/
public function before(Request $request, $arguments = null)
{
if ($request->isCLI()) {
return;
}
// CodeIgniter 4 is not a PSR-7 compatible framework, therefore we don't
// pass the Reqest and Reposne to Firewall instance.
// Shieldon will create them by its HTTP factory.
$firewall = new Firewall();
$firewall->configure($this->storage);
$firewall->controlPanel($this->panelUri);
// Pass CodeIgniter CSRF Token to Captcha form.
$firewall->getKernel()->setCaptcha(
new Csrf([
'name' => csrf_token(),
'value' => csrf_hash(),
])
);
$response = $firewall->run();
if ($response->getStatusCode() !== 200) {
$httpResolver = new HttpResolver();
$httpResolver($response);
}
}
/**
* We don't have anything to do here.
*
* @param Response $request
* @param Response $response
*
* @return mixed
*/
public function after(Request $request, Response $response, $arguments = null)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment