From 1024befd393e439c9a599734a428996f943eabdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Bostr=C3=B6m?= Date: Sat, 23 May 2026 15:29:38 +0200 Subject: [PATCH] Fix pw recovery without totp (#1032) Check if user has TOTP active, if so display input field in pw-change form and evaluate it. --- public/users/login-mfa.php | 2 +- public/users/password-change.php | 46 ++++++++++++++++++++++---------- templates/password-change.tpl | 16 +++++++++-- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/public/users/login-mfa.php b/public/users/login-mfa.php index 712a1e23..b037ed31 100644 --- a/public/users/login-mfa.php +++ b/public/users/login-mfa.php @@ -46,7 +46,7 @@ if (isset($_GET["abort"]) && $_GET["abort"] == "1" && authentication_mfa_incompl $CONF = Config::getInstance()->getAll(); $smarty = PFASmarty::getInstance(); $smarty->configureTheme('../'); - + if ($_SERVER['REQUEST_METHOD'] == "POST") { CsrfToken::assertValid(safepost('CSRF_Token')); diff --git a/public/users/password-change.php b/public/users/password-change.php index 7f18c309..62fddfd6 100644 --- a/public/users/password-change.php +++ b/public/users/password-change.php @@ -75,21 +75,36 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!$handler->checkPasswordRecoveryCode($tUsername, $tCode)) { flash_error(Config::lang('pPassword_code_text_error')); } else { - init_session($tUsername, $context === 'admin'); - if (!$handler->init($tUsername)) { - flash_error($handler->errormsg); - } else { - $values = $handler->result; - $values['password'] = $fPassword; - $values['password2'] = $fPassword2; - if ($handler->set($values) && $handler->save()) { - flash_info(Config::lang_f('pPassword_result_success', $tUsername)); - $handler->wipePasswordRecoveryCode($tUsername); // so we only wipe the recovery token if they've managed to change their password. - header('Location: main.php'); - exit(0); + $totppf = new TotpPf($context === 'admin' ? 'admin' : 'mailbox', new Login($context === 'admin' ? 'admin' : 'mailbox')); + + if ($totppf->usesTOTP($tUsername)) { + $fTotp = safepost('fTOTP_code'); + + if (!$totppf->checkUserTOTP($tUsername, $fTotp)) { + flash_error(Config::lang('pTotp_failed')); + $error = true; + } + } + + if (!$error) { + init_session($tUsername, $context === 'admin', true); + + if (!$handler->init($tUsername)) { + flash_error($handler->errormsg); } else { - foreach ($handler->errormsg as $msg) { - flash_error($msg); + $values = $handler->result; + $values['password'] = $fPassword; + $values['password2'] = $fPassword2; + + if ($handler->set($values) && $handler->save()) { + flash_info(Config::lang_f('pPassword_result_success', $tUsername)); + $handler->wipePasswordRecoveryCode($tUsername); + header('Location: main.php'); + exit(0); + } else { + foreach ($handler->errormsg as $msg) { + flash_error($msg); + } } } } @@ -97,6 +112,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { } } +$totppf = new TotpPf($context === 'admin' ? 'admin' : 'mailbox', new Login($context === 'admin' ? 'admin' : 'mailbox')); +$tTotpRequired = $totppf->usesTOTP($tUsername); +$smarty->assign('tTotpRequired', $tTotpRequired); $smarty->assign('language_selector', language_selector(), false); $smarty->assign('tUsername', $tUsername); $smarty->assign('tCode', $tCode); diff --git a/templates/password-change.tpl b/templates/password-change.tpl index cc47ba05..79e9d35c 100644 --- a/templates/password-change.tpl +++ b/templates/password-change.tpl @@ -27,7 +27,19 @@ - + + {if $tTotpRequired} +
+ + +
+ {/if} + + + +