check password always returns false django

toe95027  于 2023-05-19  发布在  Go
关注(0)|答案(3)|浏览(117)

我使用django并将用户密码存储为使用make_password创建的散列值。当我运行check_password时,我总是得到一个错误的返回,我不知道如何解决这个问题。
任何帮助将不胜感激。
我的用户注册看起来像这样:

def user_reg_view(request):
    form = UserForm(request.POST or None)
    if form.is_valid():
        password = hashers.make_password(form.cleaned_data.get('password'))
        fname = form.cleaned_data.get('first_name')
        lname = form.cleaned_data.get('last_name')
        email = form.cleaned_data.get('email')
        company_id = form.cleaned_data.get('company')
        User.objects.create_user(
            email = email,
            password=password,
            first_name = fname,
            last_name = lname,
            username = email,
            company_id = company_id)
        form = UserForm()
    var = {'form': form}
    return render(request, 'user_registry.html', var)

而我的登录功能失败的部分看起来是这样的(假设用户存在并且输入的密码总是相同的):

def login_view(request):
    form = LoginForm(request.POST or None)
    if form.is_valid():

        username = form.cleaned_data.get('username')
        user = User.objects.get(username=username)
        password = form.cleaned_data.get('password')
        encoded_password =  user.password
        print(hashers.make_password(password) == user.password)
        #returns false
        print(hashers.check_password(password=password, encoded=hashers.make_password(password), setter=None))
        #returns true
        print(hashers.check_password(password=password, encoded=encoded_password))
        # returns false

我不明白第一次打印与第二次打印有何不同,当然,每次生成的密码哈希值对于相同的字符串都不同,但check_password不应该能够处理吗?
如果错误可能是在传递的寄存器表单值中,这里也是该函数的一个片段:

class UserForm(forms.ModelForm):
    company = forms.CharField(max_length=50, label='', widget=forms.TextInput(attrs={'placeholder': 'your company'}))
    first_name = forms.CharField(max_length=30, label='', widget=forms.TextInput(attrs={'placeholder': 'first name'}))
    last_name = forms.CharField(max_length=30, label='', widget=forms.TextInput(attrs={'placeholder': 'last name'}))
    email = forms.CharField(max_length=40, label='', widget=forms.TextInput(attrs={'placeholder': 'email'}))
    password1 = forms.CharField(
        strip=False,
        widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
        validators=[validate_password],
        label='password')
    password2 = forms.CharField(
        label= 'please confirm password',
        widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
        strip=False
    )

    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'email']
        
    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError('password_mismatch')
        return password2
sdnqo3pr

sdnqo3pr1#

create_user方法对作为参数传递给它的密码进行散列。这意味着您在将纯文本密码存储到数据库之前对它进行了两次散列。此外,不需要使用hashers.check_password,只需使用authenticate函数即可- Django docs
因此,您的注册视图将是:

def user_reg_view(request):
    form = UserForm(request.POST or None)
    if form.is_valid():
        password = form.cleaned_data.get('password')
        fname = form.cleaned_data.get('first_name')
        lname = form.cleaned_data.get('last_name')
        email = form.cleaned_data.get('email')
        company_id = form.cleaned_data.get('company')
        User.objects.create_user(
            email = email,
            password=password,
            first_name = fname,
            last_name = lname,
            username = email,
            company_id = company_id)
        form = UserForm()
    var = {'form': form}
    return render(request, 'user_registry.html', var)

登录视图:

from django.contrib.auth import authenticate, login

def login_view(request):
    form = LoginForm(request.POST or None)
    if form.is_valid():

        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        user = authenticate(username=username, password=password)
        if user is not None:
            login(request, user) # login if valid credentials
    # other code
6jjcrrmo

6jjcrrmo2#

当你调用create_user()时,Django再次对你提供的值进行了哈希,你可以检查this answer,它说你可以先使用create(),然后使用save()作为密码,而不需要对你提供的值进行哈希。

eaf3rand

eaf3rand3#

双重散列是问题之一,另一个问题是我在不知不觉中散列了一个None值(而不是实际输入的密码)。
在我的注册表单(用户表单)中,我使用password1和password2,在匹配它们之后,我返回password2。但是,在我创建用户的user_reg_view中,我是这样设置密码的:

password = form.cleaned_data.get('password')

这应该是password2或password1,密码设置为None,并且没有抛出错误。

相关问题