使用Salt Hashing进行Moodle外部数据库身份验证

时间:2015-12-21 14:04:03

标签: cakephp hash moodle salt cakephp-2.6

我正在设置Moodle 3.0以允许来自我的CMS(使用CakePHP 2.6框架开发)的用户登录Moodle。这两个数据库位于同一台服务器上。我正在使用选项"外部数据库"来自Moodle,它似乎是最简单的解决方案。

根据Moodle Docs,脚本/path/to/moodle/auth/db/cli/sync_users.php将用户从外部数据库(CMS)导入Moodle,这非常有效,但使用外部数据库中的凭据(用户名和密码)登录Moodle并不是# 39;工作。

我认为问题可能是"格式化密码" (我在"外部数据库"中使用SHA-1哈希)建立。因为在CakePHP中有密码散列(使用salt的SHA1)。当我设置"格式化密码"到"纯文本"它有效,但那不是我想要的。有什么方法可以解决这个问题吗?

https://docs.moodle.org/27/en/External_database_authentication

1 个答案:

答案 0 :(得分:1)

我想出了一个解决我问题的方法,但我不得不改变Moodle代码的一行,这是我试图避免的。我已经尝试了“格式密码”来“加密单向散列”选项,但没有成功。

我服务器上的CakePHP以这种方式散列密码:

$string = $salt_key . $password;
sha1($string);

因此,在使用“外部数据库”的Moodle身份验证中,我更改了文件/path/to/moodle/auth/db/auth.php并添加了以下内容:

// I've just add this line:
// $extpassword = '<your_salt_key_here>' . $extpassword;

if ($this->config->passtype === 'plaintext') {
    return ($fromdb == $extpassword);
} else if ($this->config->passtype === 'md5') {
    return (strtolower($fromdb) == md5($extpassword));
} else if ($this->config->passtype === 'sha1') {
    $extpassword = '<your_salt_key_here>' . $extpassword; // Add this line
    return (strtolower($fromdb) == sha1($extpassword));
} else if ($this->config->passtype === 'saltedcrypt') {
    require_once($CFG->libdir.'/password_compat/lib/password.php');
    return password_verify($extpassword, $fromdb);
} else {
    return false;
}
相关问题