-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_middleware.php
More file actions
130 lines (107 loc) · 4.59 KB
/
example_middleware.php
File metadata and controls
130 lines (107 loc) · 4.59 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* @copyright ©2025 Maatify.dev
* @Library maatify/security-guard
* @Project maatify:security-guard
* @author Mohamed Abdulalim (megyptm) <mohamed@maatify.dev>
* @since 2025-12-11 09:32
* @see https://www.maatify.dev Maatify.dev
* @link https://github.com/Maatify/security-guard view Project on GitHub
* @note Distributed in the hope that it will be useful - WITHOUT WARRANTY.
*/
declare(strict_types=1);
/**
* Phase 5 – Slim Example: SECURITY MIDDLEWARE (STRICT)
*
* Demonstrates:
* - Blocking login routes when user is already blocked
* - Intercepting requests BEFORE reaching handlers
* - Using only Phase 5: handleAttempt(), isBlocked(), getRemainingBlockSeconds()
*/
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Server\MiddlewareInterface;
use Slim\Factory\AppFactory;
use Maatify\SecurityGuard\Service\SecurityGuardService;
use Maatify\SecurityGuard\DTO\LoginAttemptDTO;
$app = require __DIR__ . '/bootstrap.php';
// -------------------------------------------------------------
// SECURITY MIDDLEWARE (STRICT)
// -------------------------------------------------------------
final class SecurityMiddleware implements MiddlewareInterface
{
public function __construct(
private SecurityGuardService $guard
)
{
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// ---------------------------------------------------------
// 1) Determine subject (login username or route identifier)
// ---------------------------------------------------------
$subject = $request->getParsedBody()['subject'] ?? 'login';
// ---------------------------------------------------------
// 2) Determine IP
// ---------------------------------------------------------
$ip = $request->getServerParams()['REMOTE_ADDR'] ?? '0.0.0.0';
// ---------------------------------------------------------
// 3) Check if already blocked
// ---------------------------------------------------------
if ($this->guard->isBlocked($ip, $subject)) {
$rem = $this->guard->getRemainingBlockSeconds($ip, $subject);
$response = new \Slim\Psr7\Response();
$response->getBody()->write(json_encode([
'status' => 'blocked',
'ip' => $ip,
'subject' => $subject,
'remaining' => $rem,
]));
return $response->withHeader('Content-Type', 'application/json')
->withStatus(429);
}
// ---------------------------------------------------------
// Allow request to proceed
// ---------------------------------------------------------
return $handler->handle($request);
}
}
// -------------------------------------------------------------
// REGISTER MIDDLEWARE IN SLIM
// -------------------------------------------------------------
$app->add(function ($request, $handler) use ($app) {
$guard = $app->getContainer()->get(SecurityGuardService::class);
return (new SecurityMiddleware($guard))->process($request, $handler);
});
// -------------------------------------------------------------
// SAMPLE LOGIN ROUTE (STRICT)
// -------------------------------------------------------------
$app->post('/login', function (ServerRequestInterface $request) use ($app) {
/** @var SecurityGuardService $guard */
$guard = $app->getContainer()->get(SecurityGuardService::class);
$body = $request->getParsedBody();
$ip = $request->getServerParams()['REMOTE_ADDR'] ?? '0.0.0.0';
$subject = $body['subject'] ?? 'login';
// Build attempt DTO
$dto = LoginAttemptDTO::now(
ip : $ip,
subject : $subject,
resetAfter: $guard->getConfig()->windowSeconds(),
userAgent : $request->getHeaderLine('User-Agent'),
context : ['route' => '/login']
);
// Simulate FAILURE for testing
$result = $guard->handleAttempt($dto, false);
$response = new \Slim\Psr7\Response();
$response->getBody()->write(json_encode([
'status' => 'failed',
'failureCount' => $result
]));
return $response->withHeader('Content-Type', 'application/json');
});
// -------------------------------------------------------------
// READY
// -------------------------------------------------------------
echo "\n=== Slim Phase 5 — Security Middleware Example Loaded (STRICT) ===\n";
return $app;