Skip to content

Commit d829bc1

Browse files
committed
Re-use success code for recovery code flow
1 parent 12deed6 commit d829bc1

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

interface/web/login/otp.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@
5959
// FIXME What's the deal with otp_enabled=v ??
6060

6161

62+
function finish_2fa_success($msg = '') {
63+
global $app;
64+
$_SESSION['s'] = $_SESSION['s_pending'];
65+
unset($_SESSION['s_pending']);
66+
unset($_SESSION['otp']);
67+
$username = $_SESSION['s']['user']['username'];
68+
if (!empty($msg)) {
69+
$msg = ' ' . $msg;
70+
}
71+
$app->auth_log('Successful login for user \''. $username .'\' ' . $msg . ' from '. $_SERVER['REMOTE_ADDR'] .' at '. date('Y-m-d H:i:s') . ' with session ID ' .session_id());
72+
$app->db->query('UPDATE `sys_user` SET otp_attempts=0 WHERE userid = ?', $_SESSION['s']['user']['userid']);
73+
session_write_close();
74+
header('Location: ../index.php');
75+
die();
76+
}
6277

6378
//* Handle recovery code
6479
if(isset($_POST['code']) && strlen($_POST['code']) == $otp_recovery_code_length && $_SESSION['otp']['recovery']) {
@@ -68,12 +83,12 @@
6883

6984
//* We allow one more try to enter recovery code
7085
if($user['otp_attempts'] > $max_global_code_retry + 1) {
71-
86+
die("Sorry, contact your administrator.");
7287
}
7388

74-
// show reset form to create a new 2fa secret?
75-
76-
die('Handle recovery code');
89+
if ($_SESSION['otp']['recovery'] == $_POST['code']) {
90+
finish_2fa_success('via 2fa recovery code');
91+
}
7792
}
7893

7994

@@ -104,13 +119,7 @@
104119

105120
//* 2fa success
106121
if($_POST['code'] == $_SESSION['otp']['code']) {
107-
$_SESSION['s'] = $_SESSION['s_pending'];
108-
// Reset the attempt counter.
109-
$app->db->query('UPDATE `sys_user` SET otp_attempts=0 WHERE userid = ?', $_SESSION['s']['user']['userid']);
110-
unset($_SESSION['s_pending']);
111-
unset($_SESSION['otp']);
112-
header('Location: ../index.php');
113-
die();
122+
finish_2fa_success();
114123
} else {
115124
//* 2fa wrong code
116125
$_SESSION['otp']['session_attempts']++; // FIXME can't we skip this and rely on the DB only?

0 commit comments

Comments
 (0)