Skip to content

Commit 80f5ea3

Browse files
committed
add password reset flow
1 parent 619df88 commit 80f5ea3

1 file changed

Lines changed: 43 additions & 21 deletions

File tree

www/idp/index.php

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@
131131
case "/login/":
132132
case "/register":
133133
case "/register/":
134+
case "/reset-password":
135+
case "/reset-password/":
136+
case "/change-password":
137+
case "/change-password/":
134138
include_once(FRONTENDDIR . "generated.html");
135139
break;
136140
case "/sharing":
@@ -151,23 +155,11 @@
151155
switch ($request) {
152156
case "/api/accounts/verify":
153157
case "/api/accounts/verify/":
154-
$email = $_POST['email'];
155158
$verifyData = [
156-
'email' => $email
159+
'email' => $_POST['email']
157160
];
158161

159-
$digits = 6;
160-
$code = rand(0,1000000);
161-
$code = str_pad($code, $digits, '0', STR_PAD_LEFT);
162-
163-
$verifyData['code'] = $code;
164-
$expires = new \DateTime();
165-
$expires->add(new \DateInterval('PT30M')); // expire after 30 minutes
166-
$verifyData['expires'] = $expires->getTimestamp();
167-
168-
User::saveVerifyToken($verifyData);
169-
$verifyToken = User::getVerifyToken($code);
170-
162+
$verifyToken = User::saveVerifyToken('verify', $verifyData);
171163
Mailer::sendVerify($verifyToken);
172164

173165
$responseData = "OK";
@@ -177,16 +169,19 @@
177169
break;
178170
case "/api/accounts/new":
179171
case "/api/accounts/new/":
180-
if (User::userEmailExists($_POST['email'])) {
172+
$verifyToken = User::getVerifyToken($_POST['confirm']);
173+
if (!$verifyToken) {
181174
header("HTTP/1.1 400 Bad Request");
182175
exit();
183176
}
184-
$verifyToken = User::getVerifyToken($_POST['confirm']);
185-
if (!$verifyToken) {
177+
if ($verifyToken['email'] !== $_POST['email']) {
178+
header("HTTP/1.1 400 Bad Request");
179+
exit();
180+
}
181+
if (User::userEmailExists($_POST['email'])) {
186182
header("HTTP/1.1 400 Bad Request");
187183
exit();
188184
}
189-
190185
if (!$_POST['password'] === $_POST['repeat_password']) {
191186
header("HTTP/1.1 400 Bad Request");
192187
exit();
@@ -207,6 +202,36 @@
207202
header("Content-type: application/json");
208203
echo json_encode($responseData, JSON_PRETTY_PRINT);
209204
break;
205+
case "/api/accounts/reset-password":
206+
case "/api/accounts/reset-password/":
207+
if (!User::userEmailExists($_POST['email'])) {
208+
header("HTTP/1.1 200 OK"); // Return OK even when user is not found;
209+
header("Content-type: application/json");
210+
echo json_encode("OK");
211+
exit();
212+
}
213+
$verifyData = [
214+
'email' => $_POST['email']
215+
];
216+
217+
$verifyToken = User::saveVerifyToken('passwordReset', $verifyData);
218+
Mailer::sendResetPassword($verifyToken);
219+
header("HTTP/1.1 200 OK");
220+
header("Content-type: application/json");
221+
echo json_encode("OK");
222+
break;
223+
case "/api/accounts/change-password":
224+
case "/api/accounts/change-password/":
225+
$verifyToken = User::getVerifyToken($_POST['token']);
226+
if (!$verifyToken) {
227+
header("HTTP/1.1 400 Bad Request");
228+
exit();
229+
}
230+
User::setUserPassword($verifyToken['email'], $_POST['newPassword']);
231+
header("HTTP/1.1 200 OK");
232+
header("Content-type: application/json");
233+
echo json_encode("OK");
234+
break;
210235
case "/login/password":
211236
case "/login/password/":
212237
if (User::checkPassword($_POST['username'], $_POST['password'])) {
@@ -272,9 +297,6 @@
272297
$returnUrl = urldecode($_POST['returnUrl']);
273298
header("Location: $returnUrl");
274299
}
275-
// FIXME: force user to be logged in
276-
// FIXME: save the allowed clients in the logged in user;
277-
278300
break;
279301
case "/token":
280302
case "/token/":

0 commit comments

Comments
 (0)