-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.php
More file actions
190 lines (170 loc) · 5.51 KB
/
Account.php
File metadata and controls
190 lines (170 loc) · 5.51 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Pdsinterop\PhpSolid\Routes;
use Pdsinterop\PhpSolid\Server;
use Pdsinterop\PhpSolid\ClientRegistration;
use Pdsinterop\PhpSolid\User;
use Pdsinterop\PhpSolid\Session;
use Pdsinterop\PhpSolid\Mailer;
use Pdsinterop\PhpSolid\IpAttempts;
use Pdsinterop\PhpSolid\StorageServer;
class Account {
public static function requireLoggedInUser() {
$user = User::getUser(Session::getLoggedInUser());
if (!$user) {
switch ($_SERVER['REQUEST_METHOD']) {
case "GET":
header("Location: /login/?redirect_uri=" . urlencode($_SERVER['REQUEST_URI']));
exit();
break;
default:
header("HTTP/1.0 400 Bad Request");
exit();
break;
}
}
}
public static function respondToDashboard() {
$user = User::getUser(Session::getLoggedInUser());
include_once(FRONTENDDIR . "bootloader.html");
}
public static function respondToLogout() {
$user = User::getUser(Session::getLoggedInUser());
if ($user) {
session_destroy();
}
header("Location: /login/");
exit();
}
public static function respondToAccountVerify() {
$verifyData = [
'email' => $_POST['email']
];
$verifyToken = User::saveVerifyToken('verify', $verifyData);
Mailer::sendVerify($verifyToken);
$responseData = "OK";
header("HTTP/1.1 201 Created");
header("Content-type: application/json");
echo json_encode($responseData, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR);
}
public static function respondToAccountNew() {
$verifyToken = User::getVerifyToken($_POST['confirm']);
if (!$verifyToken) {
error_log("Could not read verify token");
header("HTTP/1.1 400 Bad Request");
exit();
}
if ($verifyToken['email'] !== $_POST['email']) {
error_log("Verify token does not match email");
header("HTTP/1.1 400 Bad Request");
exit();
}
if (User::userEmailExists($_POST['email'])) {
error_log("Account already exists");
header("HTTP/1.1 400 Bad Request");
exit();
}
if (!$_POST['password'] === $_POST['repeat_password']) {
error_log("Password repeat does not match");
header("HTTP/1.1 400 Bad Request");
exit();
}
$newUser = [
"email" => $_POST['email'],
"password" => $_POST['password']
];
$createdUser = User::createUser($newUser);
if (!$createdUser) {
error_log("Failed to create user");
header("HTTP/1.1 400 Bad Request");
exit();
}
$createdStorage = StorageServer::createStorage($createdUser['webId']);
User::setStorage($createdUser['userId'], $createdStorage['storageUrl']);
Mailer::sendAccountCreated($createdUser);
$responseData = array(
"webId" => $createdUser['webId'],
"storageUrl" => $createdStorage['storageUrl']
);
header("HTTP/1.1 201 Created");
header("Content-type: application/json");
Session::start($_POST['email']);
echo json_encode($responseData, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR);
}
public static function respondToAccountResetPassword() {
if (!User::userEmailExists($_POST['email'])) {
header("HTTP/1.1 200 OK"); // Return OK even when user is not found;
header("Content-type: application/json");
echo json_encode("OK");
exit();
}
$verifyData = [
'email' => $_POST['email']
];
$verifyToken = User::saveVerifyToken('passwordReset', $verifyData);
Mailer::sendResetPassword($verifyToken);
header("HTTP/1.1 200 OK");
header("Content-type: application/json");
echo json_encode("OK");
}
public static function respondToAccountChangePassword() {
$verifyToken = User::getVerifyToken($_POST['token']);
if (!$verifyToken) {
header("HTTP/1.1 400 Bad Request");
exit();
}
$result = User::setUserPassword($verifyToken['email'], $_POST['newPassword']);
if (!$result) {
header("HTTP/1.1 400 Bad Request");
exit();
}
header("HTTP/1.1 200 OK");
header("Content-type: application/json");
echo json_encode("OK");
}
public static function respondToAccountDelete() {
if (!User::userEmailExists($_POST['email'])) {
header("HTTP/1.1 200 OK"); // Return OK even when user is not found;
header("Content-type: application/json");
echo json_encode("OK");
exit();
}
$verifyData = [
'email' => $_POST['email']
];
$verifyToken = User::saveVerifyToken('deleteAccount', $verifyData);
Mailer::sendDeleteAccount($verifyToken);
header("HTTP/1.1 200 OK");
header("Content-type: application/json");
echo json_encode("OK");
}
public static function respondToAccountDeleteConfirm() {
$verifyToken = User::getVerifyToken($_POST['token']);
if (!$verifyToken) {
header("HTTP/1.1 400 Bad Request");
exit();
}
User::deleteAccount($verifyToken['email']);
header("HTTP/1.1 200 OK");
header("Content-type: application/json");
echo json_encode("OK");
}
public static function respondToLogin() {
$failureCount = IpAttempts::getAttemptsCount($_SERVER['REMOTE_ADDR'], "login");
if ($failureCount > 5) {
header("HTTP/1.1 400 Bad Request");
exit();
}
if (User::checkPassword($_POST['username'], $_POST['password'])) {
Session::start($_POST['username']);
$user = User::getUser($_POST['username']);
if (!isset($_POST['redirect_uri']) || $_POST['redirect_uri'] === '') {
header("Location: /dashboard/#autologin/" . urlencode($user['webId']));
exit();
}
header("Location: " . urldecode($_POST['redirect_uri'])); // FIXME: Do we need to harden this?
} else {
IpAttempts::logFailedAttempt($_SERVER['REMOTE_ADDR'], "login", time() + 3600);
header("Location: /login/");
}
}
}