@@ -102,34 +102,8 @@ function recoverpasswordAction()
102102 }
103103 }
104104
105- // Create a new password
106- $ keychars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 " ;
107- $ length = 10 ;
108-
109- /** make_seed_recoverpass */
110- function make_seed_recoverpass ()
111- {
112- list ($ usec , $ sec ) = explode (' ' , microtime ());
113- return (float ) $ sec + ((float ) $ usec * 100000 );
114- }
115- srand (make_seed_recoverpass ());
116-
117- $ pass = "" ;
118- $ max = strlen ($ keychars ) - 1 ;
119- for ($ i = 0 ; $ i <= $ length ; $ i ++)
120- {
121- $ pass .= substr ($ keychars , rand (0 , $ max ), 1 );
122- }
123- $ encrypted = md5 ($ pass );
124-
125- $ passwordPrefix = Zend_Registry::get ('configGlobal ' )->password ->prefix ;
126- $ salted = $ pass ;
127- if (isset ($ passwordPrefix ) && !empty ($ passwordPrefix ))
128- {
129- $ salted = $ passwordPrefix .$ pass ;
130- }
131-
132- $ user ->setPassword (md5 ($ salted ));
105+ $ pass = UtilityComponent::generateRandomString (10 );
106+ $ this ->User ->changePassword ($ user , $ pass );
133107
134108 // Send the email
135109 $ url = $ this ->getServerURL ().$ this ->view ->webroot ;
@@ -384,7 +358,7 @@ function verifyemailAction()
384358 session_start ();
385359 }
386360 $ this ->userSession ->Dao = $ this ->User ->createUser (
387- $ email , $ pendingUser -> getPassword () , $ pendingUser ->getFirstname (), $ pendingUser ->getLastname (), 0 , true );
361+ $ email , null , $ pendingUser ->getFirstname (), $ pendingUser ->getLastname (), 0 , $ pendingUser -> getSalt () );
388362 session_write_close ();
389363
390364 $ this ->PendingUser ->delete ($ pendingUser );
@@ -408,8 +382,15 @@ function ajaxloginAction()
408382 return ;
409383 }
410384 $ userDao = $ this ->User ->getByEmail ($ form ->getValue ('email ' ));
411- $ passwordPrefix = Zend_Registry::get ('configGlobal ' )->password ->prefix ;
412- if ($ userDao !== false && md5 ($ passwordPrefix .$ form ->getValue ('password ' )) == $ userDao ->getPassword ())
385+ if ($ userDao === false )
386+ {
387+ echo JsonComponent::encode (array ('status ' => 'error ' , 'message ' => 'Invalid username or password ' ));
388+ return ;
389+ }
390+ $ instanceSalt = Zend_Registry::get ('configGlobal ' )->password ->prefix ;
391+ $ passwordHash = hash ($ userDao ->getHashAlg (), $ instanceSalt .$ userDao ->getSalt ().$ form ->getValue ('password ' ));
392+
393+ if ($ this ->User ->hashExists ($ passwordHash ))
413394 {
414395 $ notifications = Zend_Registry::get ('notifier ' )->callback ('CALLBACK_CORE_AUTH_INTERCEPT ' , array ('user ' => $ userDao ));
415396 foreach ($ notifications as $ module => $ value )
@@ -420,7 +401,11 @@ function ajaxloginAction()
420401 return ;
421402 }
422403 }
423- setcookie ('midasUtil ' , $ userDao ->getKey ().'- ' .md5 ($ userDao ->getPassword ()), time () + 60 * 60 * 24 * 30 , '/ ' ); //30 days
404+ if ($ userDao ->getSalt () == '' )
405+ {
406+ $ passwordHash = $ this ->User ->convertLegacyPasswordHash ($ userDao , $ form ->getValue ('password ' ));
407+ }
408+ setcookie ('midasUtil ' , $ userDao ->getKey ().'- ' .$ passwordHash , time () + 60 * 60 * 24 * 30 , '/ ' ); //30 days
424409 Zend_Session::start ();
425410 $ user = new Zend_Session_Namespace ('Auth_User ' );
426411 $ user ->setExpirationSeconds (60 * Zend_Registry::get ('configGlobal ' )->session ->lifetime );
@@ -431,7 +416,7 @@ function ajaxloginAction()
431416 }
432417 else
433418 {
434- echo JsonComponent::encode (array ('status ' => 'error ' , 'message ' => 'Login failed ' ));
419+ echo JsonComponent::encode (array ('status ' => 'error ' , 'message ' => 'Invalid username or password ' ));
435420 }
436421 }
437422
@@ -473,10 +458,16 @@ function loginAction()
473458 if (!$ authModule )
474459 {
475460 $ userDao = $ this ->User ->getByEmail ($ form ->getValue ('email ' ));
461+ if ($ userDao === false )
462+ {
463+ echo JsonComponent::encode (array ('status ' => false , 'message ' => 'Invalid email or password ' ));
464+ return ;
465+ }
476466 }
477467
478- $ passwordPrefix = Zend_Registry::get ('configGlobal ' )->password ->prefix ;
479- if ($ authModule || $ userDao !== false && md5 ($ passwordPrefix .$ form ->getValue ('password ' )) == $ userDao ->getPassword ())
468+ $ instanceSalt = Zend_Registry::get ('configGlobal ' )->password ->prefix ;
469+ $ passwordHash = hash ($ userDao ->getHashAlg (), $ instanceSalt .$ userDao ->getSalt ().$ form ->getValue ('password ' ));
470+ if ($ authModule || $ this ->User ->hashExists ($ passwordHash ))
480471 {
481472 $ notifications = Zend_Registry::get ('notifier ' )->callback ('CALLBACK_CORE_AUTH_INTERCEPT ' , array ('user ' => $ userDao ));
482473 foreach ($ notifications as $ module => $ value )
@@ -487,12 +478,16 @@ function loginAction()
487478 return ;
488479 }
489480 }
481+ if ($ userDao ->getSalt () == '' )
482+ {
483+ $ passwordHash = $ this ->User ->convertLegacyPasswordHash ($ userDao , $ form ->getValue ('password ' ));
484+ }
490485 $ remember = $ form ->getValue ('remerberMe ' );
491486 if (isset ($ remember ) && $ remember == 1 )
492487 {
493488 if (!$ this ->isTestingEnv ())
494489 {
495- setcookie ('midasUtil ' , $ userDao ->getKey ().'- ' .md5 ( $ userDao -> getPassword ()) , time () + 60 * 60 * 24 * 30 , '/ ' ); //30 days
490+ setcookie ('midasUtil ' , $ userDao ->getKey ().'- ' .$ passwordHash , time () + 60 * 60 * 24 * 30 , '/ ' ); //30 days
496491 }
497492 }
498493 else
@@ -533,7 +528,7 @@ function loginAction()
533528 {
534529 echo JsonComponent::encode (array (
535530 'status ' => false ,
536- 'message ' => 'Invalid login ' ));
531+ 'message ' => 'Invalid email or password ' ));
537532 }
538533 }
539534 } // end method login
@@ -673,22 +668,23 @@ public function settingsAction()
673668 }
674669 $ oldPass = $ this ->_getParam ('oldPassword ' );
675670 $ newPass = $ this ->_getParam ('newPassword ' );
676- $ passwordPrefix = Zend_Registry::get ('configGlobal ' )->password ->prefix ;
677- $ userDao = $ this ->User ->load ($ userDao ->getKey ());
678- if ($ userDao != false && ((!$ userDao ->isAdmin () && $ this ->userSession ->Dao ->isAdmin ()) || md5 ($ passwordPrefix .$ oldPass ) == $ userDao ->getPassword ()))
671+ $ instanceSalt = Zend_Registry::get ('configGlobal ' )->password ->prefix ;
672+ $ hashedPasswordOld = hash ($ userDao ->getHashAlg (), $ instanceSalt .$ userDao ->getSalt ().$ oldPass );
673+
674+ if ((!$ userDao ->isAdmin () && $ this ->userSession ->Dao ->isAdmin ()) || $ this ->User ->hashExists ($ hashedPasswordOld ))
679675 {
680- $ userDao ->setPassword (md5 ($ passwordPrefix .$ newPass ));
681- $ this ->User ->save ($ userDao );
676+ $ this ->User ->changePassword ($ userDao , $ newPass );
682677 if (!isset ($ userId ))
683678 {
684679 $ this ->userSession ->Dao = $ userDao ;
685680 }
686681 echo JsonComponent::encode (array (true , $ this ->t ('Changes saved ' )));
687- Zend_Registry::get ('notifier ' )->callback ('CALLBACK_CORE_PASSWORD_CHANGED ' , array ('userDao ' => $ userDao ));
682+ Zend_Registry::get ('notifier ' )->callback ('CALLBACK_CORE_PASSWORD_CHANGED ' , array ('userDao ' => $ userDao, ' password ' => $ newPass ));
688683 }
689684 else
690685 {
691686 echo JsonComponent::encode (array (false , $ this ->t ('The old password is incorrect ' )));
687+ return ;
692688 }
693689 }
694690
0 commit comments