-
Notifications
You must be signed in to change notification settings - Fork 96
/
CakePhp.php
103 lines (90 loc) · 2.66 KB
/
CakePhp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?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.
*/
declare(strict_types=1);
namespace Shieldon\Firewall\Integration;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Shieldon\Firewall\Firewall;
use Shieldon\Firewall\HttpResolver;
use Shieldon\Firewall\Captcha\Csrf;
use const TMP; // CakePHP
/**
* CakePHP Middleware
*
* This middleware has been tested succesfully with CakePHP 3.8
*/
class CakePhp
{
/**
* 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 = '')
{
// The constant TMP is the path of CakePHP's tmp folder.
// The Shieldon generated data is stored at that place.
$this->storage = TMP . 'shieldon_firewall';
$this->panelUri = '/firewall/panel/';
if ('' !== $storage) {
$this->storage = $storage;
}
if ('' !== $panelUri) {
$this->panelUri = $panelUri;
}
}
/**
* Middleware invokable class.
*
* @param Request $request PSR7 request
* @param Response $response PSR7 response
* @param callable $next Next middleware
*
* @return Response
*/
public function __invoke(Request $request, Response $response, $next): Response
{
$firewall = new Firewall($request);
$firewall->configure($this->storage);
$firewall->controlPanel($this->panelUri);
// Pass CSRF token to the Captcha form.
// Note: The CsrfProtectionMiddleware was added in 3.5.0
$firewall->getKernel()->setCaptcha(
new Csrf([
'name' => '_csrfToken',
'value' => $request->getParam('_csrfToken'),
])
);
$response = $firewall->run();
if ($response->getStatusCode() !== 200) {
$httpResolver = new HttpResolver();
$httpResolver($response);
}
return $next($request, $response);
}
}