-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAuthentication.php
74 lines (65 loc) · 1.94 KB
/
Authentication.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
<?php
namespace Shaack\Reboot;
use Shaack\Htpasswd;
use Shaack\Logger;
class Authentication extends AddOn
{
private Htpasswd $htpasswd;
/**
* @see AddOn::init()
*/
protected function init() {
session_start();
$this->htpasswd = new Htpasswd($this->reboot->getBaseFsPath() . "/local/.htpasswd");
}
/**
* @see AddOn::preRender()
*/
public function preRender(Request $request): bool
{
$user = $this->getUser();
if (!$user && $request->getPath() !== "/login") {
Logger::info("No user found, redirect to the login");
$this->reboot->redirect( $this->site->getWebPath() . "/login");
return false;
} else if ($user) {
if (@$_SESSION['checksum'] !== $this->getChecksum()) {
$this->logout();
return false;
}
}
return true;
}
/**
* Calculates a checksum for the admin session. Detects, if the .htpasswd was changed, the IP-Address or
* the user agent of the user.
* @return string md5 checksum
*/
private function getChecksum(): string
{
return md5($this->htpasswd->getChecksum() . $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_ACCEPT_LANGUAGE']);
}
public function login($username, $password): bool
{
if ($this->htpasswd->validate($username, $password)) {
$_SESSION['user'] = $username;
$_SESSION['checksum'] = $this->getChecksum();
return true;
}
return false;
}
public function logout()
{
Logger::info("logout " . $this->getUser());
$_SESSION['user'] = null;
$_SESSION['checksum'] = null;
$this->reboot->redirect($this->reboot->getBaseWebPath() . "/admin");
}
/**
* @return mixed|null Returns the username, if logged in or null if not
*/
public function getUser()
{
return @$_SESSION['user'];
}
}