2009-11-17 62 views
1

我已经将旧的joomla安装迁移到了django。虽然密码哈希值是一个问题。我不得不修改contrib.auth.models中的get_hexdigest以使用额外的if语句来颠倒生成散列的方式。将代码添加到contrib.auth的最简洁方法

# Custom for Joomla 
if algorithm == 'joomla': 
    return md5_constructor(raw_password + salt).hexdigest() 
# Djangos original md5 
if algorithm == 'md5': 
    return md5_constructor(salt + raw_password).hexdigest() 

我还添加以下到用户模式登录后更新的密码,如果他们有旧的Joomla风格:

# Joomla Backwards compat 
algo, salt, hsh = self.password.split('$') 
if algo == 'joomla': 
    is_correct = (hsh == get_hexdigest(algo, salt, raw_password)) 
    if is_correct: 
     # Convert the password to the new more secure format. 
     self.set_password(raw_password) 
     self.save() 
    return is_correct 

一切工作完美,但我宁愿不修改这个代码直接在django树中。在我自己的项目中是否有更简单的方法来实现这一点?

感谢

+1

备案:就安全性而言,最好先用盐(见:http://programming.arantius.com/how-to-salt-your-hash) – Jiaaro 2009-11-17 13:22:14

+0

感谢您的支持。我没有意识到这一点。但在这种情况下,我不能改变它。我正在处理来自Joomla的遗留数据,并以另一种方式进行处理。 – Clarence 2009-11-18 03:36:34

回答

6

你最好的选择是推出一个自定义的身份验证后端,并在那里重写get_hexdigest。从来没有做过,但有关如何这样做的文档可在http://docs.djangoproject.com/en/dev/topics/auth/#authentication-backends

+1

+1:我花了几天的时间在我的场景中正确地做到了这一点,但在你的情况下,它应该很容易做,而且是最“Djangoic”的路要走。 – Boldewyn 2009-11-17 09:08:15

+0

谢谢。我知道自定义auth后端,但因为我在models.py中找到了这个东西,我认为它没有应用。在您的链接之后,我重新阅读了所有内容,这正是我需要的。 Circle获得广场! – Clarence 2009-11-18 03:37:43

0

感谢您的指导。对于需要以DJ密码进入其他方式(DJangoJoomla)的DJ,DJ格式为Sha1$salt$crypt

Joomla标准身份验证插件和Joomla核心JUserHelper不实现相同的SHA1 algorithum但它是很容易修补成joomla.php在该插件,这里的插件不正常的上':'爆炸。做一个由'$'salt = [1]三个部分爆炸,与$encrypted = sha1($salt.$plaintext)比较,匹配crypt [2]

相关问题