Skip to content

Commit 97d57c5

Browse files
committed
Implemented log in whitelist
1 parent 9ddb443 commit 97d57c5

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

index.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
use OpCacheGUI\Format\Byte as ByteFormatter;
1313
use OpCacheGUI\Storage\Session;
14+
use OpCacheGUI\Auth\Ip;
1415
use OpCacheGUI\Security\Generator\Factory;
1516
use OpCacheGUI\Security\CsrfToken;
1617
use OpCacheGUI\Auth\User;
@@ -47,10 +48,23 @@
4748
$sessionStorage = new Session();
4849
$csrfToken = new CsrfToken($sessionStorage, new Factory());
4950

51+
/**
52+
* Setup the IP whitelist
53+
*/
54+
$whitelist = new Ip([
55+
new \OpCacheGUI\Network\Ip\Any(),
56+
new \OpCacheGUI\Network\Ip\Localhost(),
57+
new \OpCacheGUI\Network\Ip\Single(),
58+
new \OpCacheGUI\Network\Ip\Wildcard(),
59+
new \OpCacheGUI\Network\Ip\Range(),
60+
new \OpCacheGUI\Network\Ip\Cidr(),
61+
]);
62+
$whitelist->buildWhitelist($login['whitelist']);
63+
5064
/**
5165
* Setup the authentication object
5266
*/
53-
$user = new User($sessionStorage, $login['username'], $login['password']);
67+
$user = new User($sessionStorage, $login['username'], $login['password'], $whitelist);
5468

5569
/**
5670
* Setup URL renderer

init.example.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,26 @@
4545

4646
/**
4747
* Login credentials
48-
*
48+
*
4949
* The password can be any password hash which contains the hash algorithm, the cost and the salt
5050
* (e.g as returned by password_hash() or crypt())
51+
*
52+
* Only addresses on the whitelist are allowed to log in
53+
* The whitelist can contain a list of IP addresses of ranges in one of the following formats:
54+
*
55+
* * allows any IP address to log in (effectively disabling the whitelist and allowing access from any IP)
56+
* localhost or 127.0.0.1 allows only log ins from the machine on which the application runs
57+
* 10.0.0.5 allows a single address access
58+
* 10.0.0.* allows any log in from the range starting from 10.0.0.0 to 10.0.0.255. All octets but the first can be a wildcard
59+
* 10.0.0.1-10.0.0.24 defines a range of IP addresses which are allowed to log in (including the IP addresses defining the range)
60+
* 10.0.0.10/24 defines a range of IP addresses in the CIDR format
61+
*
62+
* Multiple addresses or ranges can be defined
5163
*/
5264
$login = [
53-
'username' => 'peehaa',
54-
'password' => '$2y$14$kHoRlbxPF7Bf1903cDMTgeYBsFgF8aJA46LIH9Nsg4/ocDa9HTTbe',
65+
'username' => 'peehaa',
66+
'password' => '$2y$14$kHoRlbxPF7Bf1903cDMTgeYBsFgF8aJA46LIH9Nsg4/ocDa9HTTbe',
67+
'whitelist' => [
68+
'localhost',
69+
],
5570
];

routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
});
5656

5757
$router->post('', function() use ($htmlTemplate, $csrfToken, $request, $user, $request) {
58-
if ($csrfToken->validate($request->post('csrfToken')) && $user->login($request->post('username'), $request->post('password'))) {
58+
if ($csrfToken->validate($request->post('csrfToken')) && $user->login($request->post('username'), $request->post('password'), $request->getIp())) {
5959
header('Location: ' . $request->getUrl());
6060
exit;
6161
}

0 commit comments

Comments
 (0)