From bfbbf72959faa385b97d250484be1d25e4fd22aa Mon Sep 17 00:00:00 2001 From: Jerome Jutteau Date: Wed, 24 Aug 2022 22:09:15 +0200 Subject: [PATCH 1/1] [TASK] rework user auth This is a cleaner way to upload files and also prepare for #311. Signed-off-by: Jerome Jutteau --- admin.php | 2 +- index.php | 50 +++++++++++++++----------------------------- lib/functions.js.php | 18 ++++++---------- lib/functions.php | 13 +++++++++++- script.php | 19 ++++++++++------- 5 files changed, 47 insertions(+), 55 deletions(-) diff --git a/admin.php b/admin.php index d05ce35..d196f3f 100644 --- a/admin.php +++ b/admin.php @@ -57,7 +57,7 @@ if (php_sapi_name() == "cli") { /* Logout if requested. */ if (jirafeau_admin_session_logged() && isset($_POST['action']) && (strcmp($_POST['action'], 'logout') == 0)) { - jirafeau_admin_session_end(); + jirafeau_session_end(); } if (!jirafeau_admin_session_logged()) { diff --git a/index.php b/index.php index 33bb83e..f7aeb04 100644 --- a/index.php +++ b/index.php @@ -32,39 +32,36 @@ if (has_error()) { require(JIRAFEAU_ROOT . 'lib/template/footer.php'); exit; } - require(JIRAFEAU_ROOT . 'lib/template/header.php'); +// Logout action +if (isset($_POST['action']) && (strcmp($_POST['action'], 'logout') == 0)) { + jirafeau_session_end(); +} + /* Check if user is allowed to upload. */ -// First check: Challenge by IP NO PASSWORD -if (true === jirafeau_challenge_upload_ip_without_password($cfg, get_ip_address($cfg))) { - $_SESSION['upload_auth'] = true; - $_POST['upload_password'] = ''; - $_SESSION['user_upload_password'] = $_POST['upload_password']; +// First check: Is user already logged +if (jirafeau_user_session_logged()) {} +// Second check: Challenge by IP NO PASSWORD +elseif (true === jirafeau_challenge_upload_ip_without_password($cfg, get_ip_address($cfg))) { + jirafeau_user_session_start(); } -// Second check: Challenge by IP +// Third check: Challenge by IP elseif (true === jirafeau_challenge_upload_ip($cfg, get_ip_address($cfg))) { // Is an upload password required? if (jirafeau_has_upload_password($cfg)) { - // Logout action - if (isset($_POST['action']) && (strcmp($_POST['action'], 'logout') == 0)) { - session_unset(); - } - // Challenge by password - // …save successful logins in session if (isset($_POST['upload_password'])) { if (jirafeau_challenge_upload_password($cfg, $_POST['upload_password'])) { - $_SESSION['upload_auth'] = true; - $_SESSION['user_upload_password'] = $_POST['upload_password']; + jirafeau_user_session_start(); } else { - $_SESSION['admin_auth'] = false; + jirafeau_session_end(); jirafeau_fatal_error(t('BAD_PSW'), $cfg); } } // Show login form if user session is not authorized yet - if (true === empty($_SESSION['upload_auth'])) { + if (!jirafeau_user_session_logged()) { ?>