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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
<?php /* Copyright (c) 2024 Freya Murphy */
class Router {
private $ldap;
private $auth;
private $domain;
function __construct() {
$this->ldap = new LDAPHelper();
$this->auth = new AuthHelper();
$this->domain = getenv("HTTP_HOST");
}
/**
* Displays a page to the user
* @param string $file
* @param array<string,mixed> $data
*/
private function send_page(
string $file,
array $data = array()
): void {
extract($data);
$webroot = $GLOBALS['webroot'];
require($webroot . '/views/header.php');
require($webroot . "/views/$file.php");
require($webroot . '/views/footer.php');
}
/**
* Displays a message to the user (message page)
* @param string $title
* @param string $msg
* @param int $code
*/
private function send_message(
string $title,
string $msg
): void {
$this->send_page('message', array(
'title' => $title,
'msg' => $msg
));
}
/**
* Gets the HTTP request information
*/
private function get_req(): array {
$path = $_SERVER['REQUEST_URI'];
$method = $_SERVER['REQUEST_METHOD'];
return [$method, $path];
}
/**
* @param array<string> $fields
*/
private function get_post_info(
string ...$fields
): ?array {
$values = array();
try {
$temp = NULL;
parse_str(file_get_contents('php://input'), $temp);
foreach ($temp as $key => $value) {
$_POST[$key] = $value;
}
} catch (Exception $_e) {}
foreach ($fields as $key) {
if (!isset($_POST[$key]))
return NULL;
$values[$key] = $_POST[$key];
}
return $values;
}
private function handle_login(): void {
$info = $this->get_post_info('username', 'password');
if ($info == NULL) {
http_response_code(400);
$this->send_message('400', 'Credentials were not supplied');
return;
}
$user = $this->ldap->search($info['username']);
if ($user == NULL || !count($user)) {
http_response_code(400);
$this->send_message('Error', 'User does not exist');
return;
}
$user = $user[0];
if ($this->ldap->bind(
$user->dn,
$info['password']
)) {
http_response_code(400);
$this->send_message('Error', 'Invalid Credentials');
return;
}
$session = $this->auth->create_session($user);
http_response_code(200);
$session->write_headers();
$this->send_message('Success', 'Authenticated. You can now go back to your content');
}
private function handle_logout(): void {
$session = $this->auth->get_session();
if ($session == NULL) {
http_response_code(303);
header("Location: http://{$this->domain}/login");
return;
}
if ($this->auth->delete_session($session)) {
http_response_code(500);
$this->send_message('Error',
'Could not log out.');
return;
}
http_response_code(200);
$this->send_message('Logged Out',
'You have been logged out successfully.');
}
private function handle_auth(): void {
$session = $this->auth->get_session();
if ($session == NULL) {
// redirect them to login
http_response_code(303);
header("Location: http://{$this->domain}/login");
} else {
// update session expiry
$session->reset_expiry();
$this->auth->save_session($session);
// send auth info
http_response_code(200);
$session->write_headers();
$this->send_message('Authenticated',
'You are already logged in.<br><br><a href="logout">Log Out</a>');
}
}
private function page_login(): void {
http_response_code(200);
$this->send_page('login', array(
'title' => 'Login'
));
}
private function page_not_found(): void {
http_response_code(404);
$this->send_message('404', 'Not Found');
}
public function handle(): void {
match ($this->get_req()) {
['GET', '/'] => $this->handle_auth(),
['GET', '/login'] => $this->page_login(),
['POST', '/login'] => $this->handle_login(),
['GET', '/logout'] => $this->handle_logout(),
_ => $this->page_not_found()
};
}
}
|