向contrib.auth添加代码的最简洁方法是什么?

Cla*_*nce 1 django joomla overriding subclass

我已将旧的joomla安装迁移到django.密码哈希是一个问题.我不得不修改contrib.auth.models中的get_hexdigest以使用额外的if语句来反转生成哈希的方式.

# 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()
Run Code Online (Sandbox Code Playgroud)

我还在User模型中添加了以下内容,以便在登录后更新密码,如果它们具有旧的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
Run Code Online (Sandbox Code Playgroud)

一切都很完美,但我不想直接在django树中编辑这段代码.在我自己的项目中有更清洁的方法吗?

谢谢

Ris*_*cha 6

您最好的选择是滚动自定义身份验证后端并在那里重写get_hexdigest.我自己从未这样做过,但有关如何操作的文档可以在http://docs.djangoproject.com/en/dev/topics/auth/#authentication-backends上找到.