【问题标题】:Invalid password format or unknown hashing algorithm密码格式无效或散列算法未知
【发布时间】:2012-05-10 13:13:44
【问题描述】:

我网站上的一位用户最近在尝试登录时成功触发了此回溯。在 Django Admin 中,他的密码为 Invalid password format or unknown hashing algorithm.

我不知道是什么原因造成的。到目前为止,这是一个孤立的案例,我和其他用户已成功注册并登录该网站。

追溯

Traceback (most recent call last):

 File "/home/gituser/.virtualenvs/bbox/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
   response = callback(request, *callback_args, **callback_kwargs)

 File "/var/git/bbox/userprofile/views.py", line 67, in login_view
   if form.is_valid():

 File "/home/gituser/.virtualenvs/bbox/lib/python2.7/site-packages/django/forms/forms.py", line 124, in is_valid
   return self.is_bound and not bool(self.errors)

 File "/home/gituser/.virtualenvs/bbox/lib/python2.7/site-packages/django/forms/forms.py", line 115, in _get_errors
   self.full_clean()

 File "/home/gituser/.virtualenvs/bbox/lib/python2.7/site-packages/django/forms/forms.py", line 271, in full_clean
   self._clean_form()

 File "/home/gituser/.virtualenvs/bbox/lib/python2.7/site-packages/django/forms/forms.py", line 299, in _clean_form
   self.cleaned_data = self.clean()

 File "/var/git/bbox/userprofile/forms.py", line 83, in clean
   self.user_cache = authenticate(username=username, password=password)

 File "/home/gituser/.virtualenvs/bbox/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 45, in authenticate
   user = backend.authenticate(**credentials)

 File "/home/gituser/.virtualenvs/bbox/lib/python2.7/site-packages/django/contrib/auth/backends.py", line 15, in authenticate
   if user.check_password(password):

 File "/home/gituser/.virtualenvs/bbox/lib/python2.7/site-packages/django/contrib/auth/models.py", line 304, in check_password
   return check_password(raw_password, self.password, setter)

 File "/home/gituser/.virtualenvs/bbox/lib/python2.7/site-packages/django/contrib/auth/hashers.py", line 42, in check_password
   hasher = get_hasher(algorithm)

 File "/home/gituser/.virtualenvs/bbox/lib/python2.7/site-packages/django/contrib/auth/hashers.py", line 115, in get_hasher
   "setting?" % algorithm)

ValueError: Unknown password hashing algorithm ''. Did you specify it in the PASSWORD_HASHERS setting?

【问题讨论】:

  • 您使用的是哪个版本的 Django?我的第一个猜测是密码存储字符串以某种方式损坏,因为它以固定格式存储。
  • @okm 嗯,我认为这可能是相关的,但问题是,到目前为止,这只是一个孤立的案例。在此人之后,其他较新的用户已成功注册并登录。有什么想法吗?
  • 那么是否有任何代码错误地对密码进行操作?或任何LogEntry 记录更改密码?

标签: django


【解决方案1】:

不要将密码设置为空字符串,而是使用 set_unusable_password() method of User model 将密码设置为不可用:

user.set_unusable_password()

在文档中查看更多信息:https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.models.User.set_unusable_password

【讨论】:

    【解决方案2】:

    原来该用户是“受邀”用户。在我的邀请代码中,我正在使用以下代码块创建用户:

        user = User.objects.create(
                username=cd['email'],
                email=cd['email'],
                first_name=cd['first_name'],
                last_name=cd['last_name'],
                is_active=False)
    

    如您所见,我没有设置密码。解决方法是应用一个临时密码,因为一旦用户到达电子邮件中发送给他的验证链接,就会要求他创建一个新密码。

        # set a random pw. user will be prompted to change
        # on log in
        user.set_unusable_password()
        user.save()
    

    因此,基本上,如果您在没有设置密码的情况下创建用户对象并尝试将他登录到您的站点(使用 django 的身份验证系统),您将获得此回溯。

    【讨论】:

    • -1 不,因为两个原因:1)您应该使用user.set_unusable_password(),如the docs 所述,2)将密码设置为random.randint(100000, 999999) 非常容易猜到(您有机会高于1 到百万第一次猜,你只需要少于899999 尝试确定密码是什么)。不要走那条路。
    • 非常感谢。不知道那个方法。会改为走那条路。
    • 仅供参考,至少从 django v1.4 开始,set_unusable_password() 相当于将密码哈希设置为“!”。感叹号是为了与“未设置”的密码区分开来。这将允许您在一个步骤中添加批量用户(create 语句中的密码 =“!”)
    猜你喜欢
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 2017-07-04
    • 2020-11-27
    • 2013-01-09
    • 2014-07-06
    • 2021-09-20
    相关资源
    最近更新 更多