自定义PHP函数来验证Joomla中的正确密码


Custom PHP function to verify correct password in Joomla

我在Joomla之外创建了一个脚本,可以成功生成Joomla密码:

// I copied the JUserHelper class from Joomla here
$salt = JUserHelper::genRandomPassword(32);
$crypt = JUserHelper::getCryptedPassword($password, $salt);
$psw = $crypt.':'.$salt;

我的问题是,我如何将我在上面生成的这个新的crypt:salt与Joomla数据库中现有用户的密码进行比较,并知道提供给上面脚本的密码是否是数据库中该用户的正确密码?

joomla 3.4.5:

if (!class_exists("JFactory")) {
    define('_JEXEC', 1);
    define('JPATH_BASE', dirname(__FILE__)); // specify path to joomla base directory here
    define('DS', DIRECTORY_SEPARATOR);
    require_once ( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
    require_once ( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
    $mainframe = & JFactory::getApplication('site');
    $mainframe->initialise();
}
$user = JFactory::getUser(); // or: getUser($id) to get the user with ID $id
$passwordMatch = JUserHelper::verifyPassword($entered_password, $user->password, $user->id);

编辑:我在之前的回复显示之前发布了这个。

您总是可以将存储的密码与盐分开,因为它们只是用':' ?


如果页面是在Joomla框架之外,你需要包括框架,应该能够完成这个(参考-下面的代码块)。如果您在Joomla框架内部,请跳过此块。但是,我没有测试引用的代码块:

define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', dirname(__FILE__).DS."..".DS.".." );
require_once ( JPATH_BASE.DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE.DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

在框架中,您需要通过ID或用户名查找用户:

$user  =& JFactory::getUser(username or id goes here);

如果你有一个匹配的$user,你可以简单地访问该用户的密码:

$user->password;

然后你可以把$psw


我相信这应该对你的道路有所帮助。

您是否希望使用此登录用户使用Joomla凭据登录到外部站点,或者您希望将他们登录到Joomla站点?

一种方法是直接查询Joomla数据库以获取用户的(加盐和散列的)密码,然后进行比较。我认为下面的查询应该工作,基于我从一些谷歌搜索中看到的。我在Wordpress中做过这个,所以我假设Joomla也会类似。

select 'password' from `jos_users` WHERE `username` = "Bob";

我很遗憾地说,上述建议的解决方案是最糟糕的

因为JUserHelper::genRandomPassword(32)函数是用来生成随机密码的,每次生成的密码都不一样所以注册用户时数据库中保存的密码和检查时生成的密码是不一样的而检查时生成的密码显然是不一样的

Joomla 2.5如何验证密码

查看插件文件:'plugins'authentication'joomla'joomla.php

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;
        }
        // Initialise variables.
        $conditions = '';
        // Get a database object
        $db     = JFactory::getDbo();
        $query  = $db->getQuery(true);
        $query->select('id, password');
        $query->from('#__users');
        $query->where('username=' . $db->Quote($credentials['username']));
        $db->setQuery($query);
        $result = $db->loadObject();
        if ($result) {
            $parts  = explode(':', $result->password);
            $crypt  = $parts[0];
            $salt   = @$parts[1];
            $testcrypt = JUserHelper::getCryptedPassword($credentials['password'], $salt);
            if ($crypt == $testcrypt) {
                $user = JUser::getInstance($result->id); // Bring this in line with the rest of the system
                $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 {
                $response->status = JAuthentication::STATUS_FAILURE;
                $response->error_message = JText::_('JGLOBAL_AUTH_INVALID_PASS');
            }
        } else {
            $response->status = JAuthentication::STATUS_FAILURE;
            $response->error_message = JText::_('JGLOBAL_AUTH_NO_USER');
        }
    }