1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Shieldon package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Shieldon\Firewall\Integration; |
15
|
|
|
|
16
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
17
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
18
|
|
|
use Shieldon\Firewall\Firewall; |
19
|
|
|
use Shieldon\Firewall\HttpResolver; |
20
|
|
|
use const TMP; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* CakePHP Middleware |
24
|
|
|
* |
25
|
|
|
* This middleware has been tested succesfully with CakePHP 3.8 |
26
|
|
|
*/ |
27
|
|
|
class CakePhp |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The absolute path of the storage where stores Shieldon generated data. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $storage; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The entry point of Shieldon Firewall's control panel. |
38
|
|
|
* |
39
|
|
|
* For example: https://yoursite.com/firewall/panel/ |
40
|
|
|
* Just use the path component of a URI. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $panelUri; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor. |
48
|
|
|
* |
49
|
|
|
* @param string $storage See property `storage` explanation. |
50
|
|
|
* @param string $panelUri See property `panelUri` explanation. |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function __construct(string $storage = '', string $panelUri = '') |
55
|
|
|
{ |
56
|
|
|
// The constant TMP is the path of CakePHP's tmp folder. |
57
|
|
|
// The Shieldon generated data is stored at that place. |
58
|
|
|
$this->storage = TMP . 'shieldon_firewall'; |
|
|
|
|
59
|
|
|
$this->panelUri = '/firewall/panel/'; |
60
|
|
|
|
61
|
|
|
if ('' !== $storage) { |
62
|
|
|
$this->storage = $storage; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ('' !== $panelUri) { |
66
|
|
|
$this->panelUri = $panelUri; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Middleware invokable class. |
72
|
|
|
* |
73
|
|
|
* @param Request $request PSR7 request |
74
|
|
|
* @param Response $response PSR7 response |
75
|
|
|
* @param callable $next Next middleware |
76
|
|
|
* |
77
|
|
|
* @return Response |
78
|
|
|
*/ |
79
|
|
|
public function __invoke(Request $request, Response $response, $next): Response |
80
|
|
|
{ |
81
|
|
|
$firewall = new Firewall($request, $response); |
82
|
|
|
$firewall->configure($this->storage); |
83
|
|
|
$firewall->controlPanel($this->panelUri); |
84
|
|
|
|
85
|
|
|
// Pass CSRF token to the Captcha form. |
86
|
|
|
// Note: The CsrfProtectionMiddleware was added in 3.5.0 |
87
|
|
|
$firewall->getKernel()->setCaptcha( |
88
|
|
|
new \Shieldon\Captcha\Csrf([ |
|
|
|
|
89
|
|
|
'name' => '_csrfToken', |
90
|
|
|
'value' => $request->getParam('_csrfToken'), |
|
|
|
|
91
|
|
|
]) |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$response = $firewall->run(); |
95
|
|
|
|
96
|
|
|
if ($response->getStatusCode() !== 200) { |
97
|
|
|
$httpResolver = new HttpResolver(); |
98
|
|
|
$httpResolver($response); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $next($request, $response); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|