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
|
<?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 {
return array(
'path' => $_SERVER['REQUEST_URI'],
'method' => $_SERVER['REQUEST_METHOD'],
);
}
/**
* @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('Bad Requet', 'Credentials were not supplied');
return;
}
$user = $this->ldap->search($info['username']);
if ($user == NULL || !count($user)) {
http_response_code(400);
$this->send_message('Bad Requst', 'User does not exist');
return;
}
$user = $user[0];
if ($this->ldap->bind(
$user->dn,
$info['password']
)) {
http_response_code(400);
$this->send_message('Bad Requst', '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');
}
/**
* Handles the HTTP request
* @param array<string,string> $req
*/
private function handle_req(array $req): void {
if ($req['method'] == 'POST') {
$this->handle_login();
return;
}
$session = $this->auth->get_session();
if ($session == NULL) {
// user is NOT authenticated
if ($req['path'] == '/login') {
// user is requesting login page
http_response_code(200);
$this->send_page('login', array(
'title' => 'Login'
));
} else {
// user is trying to forward auth
// redirect them to login
http_response_code(303);
header("Location: http://{$this->domain}/login");
}
} else {
// user is authenticated
$session->reset_expiry();
$session->write_headers();
$this->auth->save_session($session);
}
}
public function handle(): void {
$req = $this->get_req();
$this->handle_req($req);
}
}
|