This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.php
112 lines (94 loc) · 2.67 KB
/
bootstrap.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
104
105
106
107
108
109
110
111
112
<?php
/**
* Cockpit user flood addon
*
* @author Paulo Gomes
* @package CockpitCMS-UserFlood
* @license MIT
*
* @source https://github.com/pauloamgomes/CockpitCMS-UserFlood
* @see { README.md } for usage info.
*/
$this->helpers['flood'] = 'UserFlood\\Helper\\Flood';
/**
* Invalid logins.
*
* @see { Cockpit\Controller\Auth::check() }
* @see { Cockpit\Controller\RestAPI::authUser() }
*/
$this->on('cockpit.authentication.failed', function($data = []) use ($app) {
if (is_array($data) && !empty($data['user'])) {
$app->helper('flood')->add($data['user']);
}
});
/**
* Succesful logins.
*
* @see { Cockpit\Controller\Auth::check() }
* @see { Cockpit\Controller\RestAPI::authUser() }
*/
$this->on('cockpit.authentication.success', function(&$data = []) use ($app) {
if (is_array($data) && !empty($data['user'])) {
$app->helper('flood')->reset($data['user']);
}
});
/**
* Accounts updates.
*
* @see { Cockpit\Controller\Accounts::save() }
*/
$this->on('cockpit.accounts.save', function(&$user, $update) use ($app) {
if ($user['active'] && !empty($app->helper('flood')->get($user['user']))) {
$app->helper('flood')->reset($user['user']);
}
});
/**
* Add Flood entry.
*
* @see { UserFlood\Helper\Flood::add() }
*/
$this->on('flood.insert', function($user, &$entry, &$settings) {
// search for saved user
$_user = $this->storage->findOne('cockpit/accounts', ['user' => $user]);
// de-activated user or invalid user name
if (empty($_user) || empty($_user['active'])) {
return;
}
$flood = $this->helper('flood');
// parse flood entries
$login = $flood->info($entry, $settings);
// automatically ban malicious ip
if ($login['malicious_ip'] && $settings['failban']) {
$flood->blacklist($login['ip']);
}
// lockout user after 4 retries
if ($login['errors'] >= $settings['errors']) {
$flood->lock($_user, $entry['timestamp']);
}
// deactivate user after 4 consecutive lockouts
if ($login['blocks'] >= $settings['block']) {
$flood->block($_user);
}
// save debug info
if ($this['debug']) {
$entry['debug.info'] = $login;
}
});
/**
* Reset Flood history.
*
* @see { UserFlood\Helper\Flood::reset() }
*/
$this->on('flood.reset', function($user) {
if (!empty($_user = $this->storage->findOne('cockpit/accounts', ['user' => $user]))) {
$this->helper('flood')->unlock($_user);
}
});
/**
* Lockout banned IPs
*/
$this->on('admin.init', function() {
$malicous_ip = !$this->helper('flood')->isTrustedIp();
$this->bind('/auth/login', function() { return $this->stop(404); }, $malicous_ip);
$this->bind('/api/cockpit/authUser', function() { return $this->stop(404); }, $malicous_ip);
}, 100);