From cb53785ae68c6188aec216a23859d286e6f88b4b Mon Sep 17 00:00:00 2001 From: Theo Weijmer Date: Sun, 25 Aug 2024 09:34:40 +0200 Subject: [PATCH] Refactor two-factor authentication logic Updated two-factor authentication to streamline the time format handling using `time()` instead of `DateTime`, reducing code complexity. Also fixed a potential bug in `MY_Model.php` by checking the query object before accessing `num_rows()`. --- application/controllers/Login.php | 11 +++++------ application/core/MY_Model.php | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/application/controllers/Login.php b/application/controllers/Login.php index 321479b..1a92820 100644 --- a/application/controllers/Login.php +++ b/application/controllers/Login.php @@ -134,8 +134,7 @@ class Login extends MY_Controller { for ($i = 1; $i < 7; $i ++){ $code .= rand(1,9); } - $dateTime = new DateTime(); - $asString = $dateTime->format('Y-m-d H:i:s'); + $asString = time(); $this->Users_model->update_record(['twofactorcode' =>$code, 'twofactortime'=>$asString ],$userID); return $code; @@ -146,7 +145,7 @@ class Login extends MY_Controller { $tmpuserid = $this->session->userdata( 'tmpuserid' ); $code = $this->clean_input('code'); $hoe = $this->clean_input('hoe'); - $user = $this->Users_model->get([],false,true); + $user = $this->Users_model->get($tmpuserid); if ($hoe == "GOOGLE"){ $message = $this->checkTwoFactorGoogle($user, $code); } @@ -160,9 +159,9 @@ class Login extends MY_Controller { function checkTwoFactorEmail($user,$code){ $message = false; - $now = strtotime('-10 minutes'); - $test = strtotime($user->twofactortime); - if ($now > strtotime($user->twofactortime) ) + $now = time() - (10 * 60); + $test = $user->twofactortime; + if ($now > $test ) $message = "U heeft te lang gewacht, de code is niet meer geldig."; else if ($user->twofactorcode !== $code) $message = "Code is incorrect."; diff --git a/application/core/MY_Model.php b/application/core/MY_Model.php index 3a75726..6566b4d 100644 --- a/application/core/MY_Model.php +++ b/application/core/MY_Model.php @@ -176,7 +176,7 @@ class MY_Model extends CI_Model{ if ( $fieldname ) $this->db->select( $fieldname ); $query = $this->db->get( $this->tablename . ' AS T1' ); - if ( $query->num_rows() == 0 ) return FALSE; + if ( ! $query || $query->num_rows() == 0 ) return FALSE; if ( $row ){ $row = $query->row();