2012-05-10 74 views
5

我的網站上的其中一位用戶最近設法在嘗試登錄時觸發此追蹤。在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? 
+0

着Django的版本您使用的?我的第一個猜測是密碼存儲字符串以某種方式被破壞,因爲它以固定格式存儲。 – jhonkola

+1

可能重複w/http://stackoverflow.com/questions/10246463/password-hashers-setting-in-django/10246947#10246947 – okm

+0

@okm嗯我認爲這可能是相關的,但事情是,它是迄今爲止一直是孤立的案件。其他新用戶已成功註冊並登錄後,這傢伙..任何想法? – super9

回答

0

原來的用戶是一個 '邀請' 用戶。在我的邀請碼,我用這個代碼塊創建用戶:

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的身份驗證系統)得到這個回溯。

+3

-1不,因爲兩個原因:1)您應該使用['user.set_unusable_password()'](https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth。如[文檔]所述(https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.models.User.set_unusable_password),2)設置密碼(例如models.User.set_unusable_password)到'random.randint(100000,999999)'可以非常容易地猜出(你第一次有機會高於'1到百萬級的猜測值,而你只需要小於'899999'就可以確定密碼是什麼是)。不要這樣。 – Tadeck

+0

很感謝。沒有意識到這種方法。反而會走那條路。 – super9

+1

+1,因爲我認爲應該鼓勵你的解決方案... –