-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_auto_block.php
More file actions
70 lines (56 loc) · 1.97 KB
/
example_auto_block.php
File metadata and controls
70 lines (56 loc) · 1.97 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
<?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:01
* @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 #2
* AUTO BLOCK DEMONSTRATION (STRICT)
*
* Matches the Native auto-block example but implemented for Slim.
*
* Demonstrates:
* - Consecutive failures
* - Automatic blocking when threshold exceeded
* - Querying remaining block time
*/
use Maatify\SecurityGuard\DTO\LoginAttemptDTO;
use Maatify\SecurityGuard\Service\SecurityGuardService;
// Load Slim + DI container wiring
$app = require __DIR__ . '/bootstrap.php';
/** @var SecurityGuardService $guard */
$guard = $app->getContainer()->get(SecurityGuardService::class);
// Load dynamic config
$config = $guard->getConfig();
$window = $config->windowSeconds();
$maxFailures = $config->maxFailures();
echo "\n=== Slim Example #2 — AUTO BLOCK ===\n\n";
echo "Config: maxFailures = {$maxFailures}, blockSeconds = {$config->blockSeconds()}\n\n";
$ip = '192.168.100.50';
$subject = 'login_auto_block';
for ($i = 1; $i <= $maxFailures + 3; $i++) {
echo "Attempt #{$i}...\n";
$dto = LoginAttemptDTO::now(
ip : $ip,
subject : $subject,
resetAfter: $window,
userAgent : 'CLI',
context : ['attempt' => $i]
);
$result = $guard->handleAttempt($dto, false);
// If auto-block triggered
if ($guard->isBlocked($ip, $subject)) {
$remaining = $guard->getRemainingBlockSeconds($ip, $subject);
echo "🚫 AUTO BLOCK TRIGGERED at attempt {$i}\n";
echo "Remaining block seconds: {$remaining}\n\n";
break;
}
echo "Failure count = {$result}\n\n";
}