configureTheme($smarty->getRelPath()); $required_role = authentication_has_role('admin') ? 'admin' : 'user'; authentication_require_role($required_role); $username = authentication_get_username(); $pPassword_password_current_text = ""; // check if totp is enabled if (Config::bool('totp') === false) { header("Location: main.php"); exit(0); } if (authentication_has_role('admin')) { $login = new Login('admin'); $totppf = new TotpPf('admin', $login); $admin = true; } else { $login = new Login('mailbox'); $totppf = new TotpPf('mailbox', $login); $admin = false; } if ($_SERVER['REQUEST_METHOD'] == "POST") { CsrfToken::assertValid(safepost('CSRF_Token')); if (isset($_POST['fCancel'])) { header("Location: main.php"); exit(0); } $fPassword_current = $_POST['fPassword_current']; $fTOTP_secret = $_POST['fTOTP_secret']; $fTOTP_code = $_POST['fTOTP_code']; $error = 0; if (!$login->login($username, $fPassword_current)) { $error++; $pPassword_password_current_text = $PALANG['pPassword_password_current_text_error']; } // An empty code means that the user wants to remove TOTP. if ($fTOTP_code === '') { if ($error == 0) { try { $totppf->changeTOTP_secret($username, null, $fPassword_current); flash_info($PALANG['pTotp_stored']); } catch (\Exception $e) { flash_error($e->getMessage()); } } $error++; } else { if (false == $totppf->checkTOTP($fTOTP_secret, $fTOTP_code)) { $error++; flash_error($PALANG['pTOTP_code_mismatch']); } } // If TOTP checks out -> store secret in DB if ($error == 0) { try { if ($totppf->changeTOTP_secret($username, $fTOTP_secret, $fPassword_current)) { flash_info($PALANG['pTotp_stored']); } else { flash_error(Config::Lang_f('pTOTP_secret_result_error', $username)); } } catch (\Exception $e) { flash_error($e->getMessage()); } } } if ($totppf->usesTOTP($username)) { $smarty->assign('show_form', 'hidden'); } else { $smarty->assign('show_form', 'visible'); } // Create new OTP-object // Generate random secret and resulting QR code list($pTOTP_secret, $pQR_raw) = $totppf->generate($username); $smarty->assign('SESSID_USERNAME', $username); $smarty->assign('admin', $admin); $smarty->assign('pPassword_password_current_text', $pPassword_password_current_text); $smarty->assign('pQR_raw', $pQR_raw); $smarty->assign('pTOTP_secret', $pTOTP_secret); $smarty->assign('smarty_template', 'totp'); $smarty->display('index.tpl'); /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */