Slim

Slim framework is one of my favorites. Since Slim is a micro-framework, implementing Shieldon Firewall is easy as well. Without further ado, let's get started.

Firewall in Slim Framework

Installation

Use PHP Composer:

composer require shieldon/shieldon

This will also install dependencies required for Shieldon:

Implementing

Slim 4

(1) Create a Firewall Middleware.

You can create a middleware by yourself or just use the integration class.

Example:

class FirewallMiddleware
{
    /**
     * Example middleware invokable class
     *
     * @param ServerRequest  $request PSR-7 request
     * @param RequestHandler $handler PSR-15 request handler
     *
     * @return Response
     */
    public function __invoke(Request $request, RequestHandler $handler): Response
    {
        $response = $handler->handle($request);

        $firewall = new \Shieldon\Firewall\Firewall($request, $response);

        // The directory in where Shieldon Firewall will place its files.
        $firewall->configure(__DIR__ . '/../cache/shieldon_firewall');
        $response = $firewall->run();

        if ($response->getStatusCode() !== 200) {
            $httpResolver = new \Shieldon\Firewall\HttpResolver();
            $httpResolver($response);
        }

        return $response;
    }
}

(2) Add the Firewall Middleware in your Application.

For example, if you are using the Slim 4 framework, the code should look like this.

Example:

$app->add(new FirewallMiddleware());

Or, if you prefer to use the integration class, here is the code.

Example:

$app->add(new \Shieldon\Firewall\Integration\Slim4);

(3) Create a Route for Control Panel.

For example, if you are using the Slim 4 framework, the code should look like this. Then you can access the URL https://yourwebsite.com/firewall/panel/ to log in to the control panel.

Example:

$app->any('/firewall/panel[/{params:.*}]', function (Request $request, Response $response, $args) {

$firewall = new \Shieldon\Firewall\Firewall($request);

    // The directory in where Shieldon Firewall will place its files.
    // Must be the same as firewallMiddleware.
    $firewall->configure(__DIR__ . '/../cache/shieldon_firewall');

    // The base url for the control panel.
    $firewall->controlPanel('/firewall/panel/');

    $panel = new \Shieldon\Firewall\Panel();

    // Begin - Need to set up CSRF fields if you have enabled Slim-CSRF

    $csrf = new \Slim\Csrf\Guard();
    $nameKey = $csrf->getTokenNameKey();
    $valueKey = $csrf->getTokenValueKey();

    $csrfName = $request->getAttribute('csrf_name');
    $csrfVale = $request->getAttribute('csrf_value');

    $panel->csrf(
        [$nameKey => $csrfName],
        [$valueKey => $csrfVale]
    );

    // End - Slim-CSRF

    $panel->entry();
});

Note:

  • The HTTP methods POST and GET should both be applied to your website.
  • POST method is needed for solving CAPTCHA by users who were temporarily blocked.

That's it.

Slim 3

(1) Add the Firewall Middleware in your Application.

Shieldon has an integration class ready for this middleware, just use it by the following step.

Example:

$app->add(new \Shieldon\Firewall\Integration\Slim3);

For example, if you are using the Slim3 skeleton, the code in middleware.php will look like this:

Example:

<?php

use Slim\App;

return function (App $app) {
    $app->add(new \Shieldon\Firewall\Integration\Slim3);
    $app->add(new \Slim\Csrf\Guard);
};

(2) Create a Route for Control Panel.

For example, if you are using the Slim 4 framework, the code should look like this. Then you can access the URL https://yourwebsite.com/firewall/panel/ to log in to the control panel.

Example:

$app->map(['GET', 'POST'], '/firewall/panel[/{params:.*}]', function (Request $request, Response $response, array $args) {

    $firewall = new \Shieldon\Firewall\Firewall($request);

    // The directory in where Shieldon Firewall will place its files.
    // Must be the same as firewallMiddleware.
    $firewall->configure(__DIR__ . '/../cache/shieldon_firewall');

    // The base url for the control panel.
    $firewall->controlPanel('/firewall/panel/');

    $panel = new \Shieldon\Firewall\Panel();

    // Begin - Need to set up CSRF fields if you have enabled Slim-CSRF

    $csrf = new \Slim\Csrf\Guard();
    $nameKey = $csrf->getTokenNameKey();
    $valueKey = $csrf->getTokenValueKey();

    $csrfName = $request->getAttribute('csrf_name');
    $csrfVale = $request->getAttribute('csrf_value');

    $panel->csrf(
        [$nameKey => $csrfName],
        [$valueKey => $csrfVale]
    );

    // End - Slim-CSRF

    $panel->entry();
});

That's it.

Controll Panel

You can access the Firewall Panel by /firewall/panel/, to see the page, go to this URL in your browser.

https://yourwebsite.com/firewall/panel/

The default login is shieldon_user, and the password is shieldon_pass. After logging into the Firewall Panel, the first thing you should do is change the login and password.

If Shieldon Firewall is enabled in the Daemon setting section, it will start monitoring your website. Make sure you have correctly set up the settings.