/** * This method should handle any authentication and report back to the subject * * @param array $credentials Array holding the user credentials * @param array $options Array of extra options * @param object &$response Authentication response object * * @return boolean * * @since 1.5 */ public function onUserAuthenticate($credentials, $options, &$response) { $response->type = 'Joomla'; // Joomla does not like blank passwords if (empty($credentials['password'])) { $response->status = JAuthentication::STATUS_FAILURE; $response->error_message = JText::_('JGLOBAL_AUTH_EMPTY_PASS_NOT_ALLOWED'); return false; } // Get a database object $db = JFactory::getDbo(); $query = $db->getQuery(true)->select('id, password')->from('#__users')->where('username='******'username'])); $db->setQuery($query); $result = $db->loadObject(); if ($result) { $match = JUserHelper::verifyPassword($credentials['password'], $result->password, $result->id); if ($match === true) { // Bring this in line with the rest of the system $user = JUser::getInstance($result->id); $response->email = $user->email; $response->fullname = $user->name; if (JFactory::getApplication()->isAdmin()) { $response->language = $user->getParam('admin_language'); } else { $response->language = $user->getParam('language'); } $response->status = JAuthentication::STATUS_SUCCESS; $response->error_message = ''; } else { // Invalid password $response->status = JAuthentication::STATUS_FAILURE; $response->error_message = JText::_('JGLOBAL_AUTH_INVALID_PASS'); } } else { // Invalid user $response->status = JAuthentication::STATUS_FAILURE; $response->error_message = JText::_('JGLOBAL_AUTH_NO_USER'); } // Check the two factor authentication if ($response->status == JAuthentication::STATUS_SUCCESS) { require_once JPATH_ADMINISTRATOR . '/components/com_users/helpers/users.php'; $methods = UsersHelper::getTwoFactorMethods(); if (count($methods) <= 1) { // No two factor authentication method is enabled return; } require_once JPATH_ADMINISTRATOR . '/components/com_users/models/user.php'; $model = new UsersModelUser(); // Load the user's OTP (one time password, a.k.a. two factor auth) configuration if (!array_key_exists('otp_config', $options)) { $otpConfig = $model->getOtpConfig($result->id); $options['otp_config'] = $otpConfig; } else { $otpConfig = $options['otp_config']; } // Check if the user has enabled two factor authentication if (empty($otpConfig->method) || $otpConfig->method == 'none') { // Warn the user if he's using a secret code but he has not // enabed two factor auth in his account. if (!empty($credentials['secretkey'])) { try { $app = JFactory::getApplication(); $this->loadLanguage(); $app->enqueueMessage(JText::_('PLG_AUTH_JOOMLA_ERR_SECRET_CODE_WITHOUT_TFA'), 'warning'); } catch (Exception $exc) { // This happens when we are in CLI mode. In this case // no warning is issued return; } } return; } // Load the Joomla! RAD layer if (!defined('FOF_INCLUDED')) { include_once JPATH_LIBRARIES . '/fof/include.php'; } // Try to validate the OTP FOFPlatform::getInstance()->importPlugin('twofactorauth'); $otpAuthReplies = FOFPlatform::getInstance()->runPlugins('onUserTwofactorAuthenticate', array($credentials, $options)); $check = false; /* * This looks like noob code but DO NOT TOUCH IT and do not convert * to in_array(). During testing in_array() inexplicably returned * null when the OTEP begins with a zero! o_O */ if (!empty($otpAuthReplies)) { foreach ($otpAuthReplies as $authReply) { $check = $check || $authReply; } } // Fall back to one time emergency passwords if (!$check) { // Did the user use an OTEP instead? if (empty($otpConfig->otep)) { if (empty($otpConfig->method) || $otpConfig->method == 'none') { // Two factor authentication is not enabled on this account. // Any string is assumed to be a valid OTEP. return true; } else { /* * Two factor authentication enabled and no OTEPs defined. The * user has used them all up. Therefore anything he enters is * an invalid OTEP. */ return false; } } // Clean up the OTEP (remove dashes, spaces and other funny stuff // our beloved users may have unwittingly stuffed in it) $otep = $credentials['secretkey']; $otep = filter_var($otep, FILTER_SANITIZE_NUMBER_INT); $otep = str_replace('-', '', $otep); $check = false; // Did we find a valid OTEP? if (in_array($otep, $otpConfig->otep)) { // Remove the OTEP from the array $otpConfig->otep = array_diff($otpConfig->otep, array($otep)); $model->setOtpConfig($result->id, $otpConfig); // Return true; the OTEP was a valid one $check = true; } } if (!$check) { $response->status = JAuthentication::STATUS_FAILURE; $response->error_message = JText::_('JGLOBAL_AUTH_INVALID_SECRETKEY'); } } }
public function getOtpConfig($user_id = null) { $user_id = !empty($user_id) ? $user_id : (int) $this->getState('user.id'); $model = new UsersModelUser(); return $model->getOtpConfig($user_id); }
/** * Checks if the Two Factor Authentication method is globally enabled and if the * user has enabled a specific TFA method on their account. Only if both conditions * are met will this method return true; * * @param integer $userId The user ID to check. Skip to use the current user. * * @return boolean True if TFA is enabled for this user */ public function isTFAEnabled($userId = null) { if (!version_compare(JVERSION, '3.2', '>=')) { return false; } // Include the necessary user model and helper require_once JPATH_ADMINISTRATOR . '/components/com_users/helpers/users.php'; require_once JPATH_ADMINISTRATOR . '/components/com_users/models/user.php'; // Is TFA globally turned off? $twoFactorMethods = UsersHelper::getTwoFactorMethods(); if (count($twoFactorMethods) <= 1) { return false; } // Do we need to get the User ID? if (empty($userId)) { $userId = JFactory::getUser()->id; } // Has this user turned on TFA on their account? $model = new UsersModelUser(); $otpConfig = $model->getOtpConfig($userId); return !(empty($otpConfig->method) || $otpConfig->method == 'none'); }
public function save() { // Check for request forgeries $mainframe = JFactory::getApplication(); $jinput = $mainframe->input; JRequest::checkToken() or jexit(JText::_('COM_COMMUNITY_INVALID_TOKEN')); JFactory::getLanguage()->load(COM_USER_NAME); $user = JFactory::getUser(); $userid = $jinput->post->get('id', 0, 'int'); // preform security checks if ($user->get('id') == 0 || $userid == 0 || $userid != $user->get('id')) { echo $this->blockUnregister(); return; } $username = $user->get('username'); //if joomla settings allow change login name if (JComponentHelper::getParams('com_users')->get('change_login_name')) { $username = $jinput->get('username'); } //clean request $post = JRequest::get('post'); $post['username'] = $username; $post['password'] = JRequest::getVar('password', '', 'post', 'string', JREQUEST_ALLOWRAW); $post['password2'] = JRequest::getVar('password2', '', 'post', 'string', JREQUEST_ALLOWRAW); //check email $post['email'] = $post['jsemail']; $email = $post['email']; $emailPass = $post['emailpass']; $modelReg = $this->getModel('register'); //CFactory::load( 'helpers', 'validate' ); if (!CValidateHelper::email($email)) { $msg = JText::sprintf('COM_COMMUNITY_INVITE_EMAIL_INVALID', $email); $mainframe->redirect(CRoute::_('index.php?option=com_community&view=profile&task=editDetails', false), $msg, 'error'); return false; } if (!empty($email) && $email != $emailPass && $modelReg->isEmailExists(array('email' => $email))) { $msg = JText::sprintf('COM_COMMUNITY_EMAIL_EXIST', $email); $msg = stripslashes($msg); $mainframe->redirect(CRoute::_('index.php?option=com_community&view=profile&task=editDetails', false), $msg, 'error'); return false; } // get the redirect $return = CRoute::_('index.php?option=com_community&view=profile&task=editDetails', false); // do a password safety check $changePassword = false; if (JString::strlen($post['jspassword']) || JString::strlen($post['jspassword2'])) { // so that "0" can be used as password e.g. if ($post['jspassword'] != $post['jspassword2']) { $msg = JText::_('PASSWORDS_DO_NOT_MATCH'); $mainframe->redirect(CRoute::_('index.php?option=com_community&view=profile&task=editDetails', false), $msg, 'error'); return false; } else { $changePassword = true; //Jooomla 3.2.0 fix. TO be remove in future if (version_compare(JVERSION, '3.2.0', '>=')) { $salt = JUserHelper::genRandomPassword(32); $crypt = JUserHelper::getCryptedPassword($post['jspassword'], $salt); $password = $crypt . ':' . $salt; } else { // Don't re-encrypt the password // JUser bind has encrypted the password if (class_exists(JUserHelper) && method_exists(JUserHelper, 'hashpassword')) { $password = JUserHelper::hashPassword($post['jspassword']); } else { $password = $post['jspassword']; } } } } // Handle the two factor authentication setup $data = $post['jform']; if (array_key_exists('twofactor', $data)) { if (!class_exists('UsersModelUser')) { require JPATH_ROOT . '/administrator/components/com_users/models/user.php'; } $model = new UsersModelUser(); $twoFactorMethod = $data['twofactor']['method']; $userId = CFactory::getUser()->id; // Get the current One Time Password (two factor auth) configuration $otpConfig = $model->getOtpConfig($userId); if ($twoFactorMethod != 'none') { // Run the plugins FOFPlatform::getInstance()->importPlugin('twofactorauth'); $otpConfigReplies = FOFPlatform::getInstance()->runPlugins('onUserTwofactorApplyConfiguration', array($twoFactorMethod)); // Look for a valid reply foreach ($otpConfigReplies as $reply) { if (!is_object($reply) || empty($reply->method) || $reply->method != $twoFactorMethod) { continue; } $otpConfig->method = $reply->method; $otpConfig->config = $reply->config; break; } // Save OTP configuration. $model->setOtpConfig($userId, $otpConfig); // Generate one time emergency passwords if required (depleted or not set) if (empty($otpConfig->otep)) { $oteps = $model->generateOteps($userId); } } else { $otpConfig->method = 'none'; $otpConfig->config = array(); $model->setOtpConfig($userId, $otpConfig); } // Unset the raw data unset($data['twofactor']); } // we don't want users to edit certain fields so we will unset them unset($post['gid']); unset($post['block']); unset($post['usertype']); unset($post['registerDate']); unset($post['activation']); //update CUser param 1st so that the new value will not be replace wif the old one. $my = CFactory::getUser(); $params = $my->getParams(); $postvars = $post['daylightsavingoffset']; $params->set('daylightsavingoffset', $postvars); // Store FB prefernce o ly FB connect data $connectModel = CFactory::getModel('Connect'); if ($connectModel->isAssociated($user->id)) { $postvars = !empty($post['postFacebookStatus']) ? 1 : 0; $my->_cparams->set('postFacebookStatus', $postvars); } if ($changePassword) { $my->set('password', $password); } /* Save for CUser */ $my->save(); $model = CFactory::getModel('profile'); $editSuccess = true; $msg = JText::_('COM_COMMUNITY_SETTINGS_SAVED'); $jUser = JFactory::getUser(); // Bind the form fields to the user table if (!$jUser->bind($post)) { $msg = $jUser->getError(); $editSuccess = false; } // Store the web link table to the database if (!$jUser->save()) { $msg = $jUser->getError(); $editSuccess = false; } if ($editSuccess) { /* Update Joomla! User session */ $session = JFactory::getSession(); $session->set('user', $jUser); // User with FB Connect, store post preference //execute the trigger $appsLib = CAppPlugins::getInstance(); $appsLib->loadApplications(); $userRow = array(); $userRow[] = $jUser; $appsLib->triggerEvent('onUserDetailsUpdate', $userRow); } $mainframe->redirect(CRoute::_('index.php?option=com_community&view=profile&task=edit', false), $msg); }
public function getTwofactorform($user_id = null) { if (!class_exists('UsersModelUser')) { require JPATH_ROOT . '/administrator/components/com_users/models/user.php'; } $user_id = CFactory::getUser()->id; $userModel = new UsersModelUser(); $otpConfig = $userModel->getOtpConfig($user_id); FOFPlatform::getInstance()->importPlugin('twofactorauth'); return FOFPlatform::getInstance()->runPlugins('onUserTwofactorShowConfiguration', array($otpConfig, $user_id)); }
/** * Generate the hashed/salted/encoded password for the database * and to check the password at login: * if $row provided, it is checking the existing password (and update if needed) * if not provided, it will generate a new hashed password * * @param string $passwd cleartext * @return boolean TRUE/FALSE on password check */ public function verifyPassword($passwd) { global $_CB_framework; jimport('joomla.user.authentication'); $authenticate = \JAuthentication::getInstance(); // We're just checking the password so we need to make sure two step authentication is off: if (checkJversion('3.2+')) { /** @noinspection PhpIncludeInspection */ require_once $_CB_framework->getCfg('absolute_path') . '/administrator/components/com_users/models/user.php'; $userModel = new \UsersModelUser(); $twoStep = $userModel->getOtpConfig(0); $twoStep->model = 'none'; $options = array('otp_config' => $twoStep); } else { $options = array(); } $response = $authenticate->authenticate(array('username' => $this->username, 'password' => $passwd), $options); if ($response->status === \JAuthentication::STATUS_SUCCESS) { return true; } else { return false; } }