3

问题:

提供用户名和密码后,我重定向回登录页面并显示以下错误消息

“请输入正确的电子邮件和密码。请注意,这两个字段可能区分大小写。”

细节:

我遵循了Django Two-Factor Authentication的文档。我必须错过配置中的某些内容,但不知道确切。下面是我的代码中的详细信息。

设置.py

# in middle ware classes
    ...
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django_otp.middleware.OTPMiddleware',
    ...

# in installed apps
    ...
    'django_otp',
    'django_otp.plugins.otp_totp',
    'django_otp.plugins.otp_hotp',
    'django_otp.plugins.otp_static',
    'two_factor',
    'otp_yubikey',
    ...

LOGIN_URL = reverse_lazy('two_factor:login')

TWO_FACTOR_PATCH_ADMIN = True
TWO_FACTOR_CALL_GATEWAY = 'two_factor.gateways.fake.fake'
TWO_FACTOR_SMS_GATEWAY = 'two_factor.gateways.fake.Fake'
TWO_FACTOR_QR_FACTORY = 'qrcode.image.pil.PilImage'
LOGIN_REDIRECT_URL = reverse_lazy('two_factor:profile')
AUTHENTICATION_BACKENDS = ('myapp.backends.EmailAuthBackend',)
AUTH_USER_MODEL = 'users.User'
OTP_LOGIN_URL = LOGIN_URL

记录器看起来也不错

网址.py

url(r'', include(tf_urls + tf_twilio_urls, 'two_factor')),

views.py中

@otp_required()
def home(request):
    # some logic with HTTP response

注意:我的应用程序中有自定义用户模型,并在 admin.py 中注册了它们,除了这个问题之外,我还粘贴了该文件以便清楚。

管理员.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from apps.users.models import User
from django import forms


class MyUserChangeForm(UserChangeForm):
    class Meta(UserChangeForm.Meta):
        model = User


class MyUserCreationForm(UserCreationForm):
    class Meta(UserCreationForm.Meta):
        model = User

    def clean_username(self):
        username = self.cleaned_data['username']
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError(self.error_messages['duplicate_username'])


class MyUserAdmin(UserAdmin):
    form = MyUserChangeForm
    add_form = MyUserCreationForm
    # fieldsets = UserAdmin.fieldsets + (
    #     (None, {'fields': ('extra_field1', 'extra_field2',)}),
    # )

admin.site.register(User, MyUserAdmin)

请建议我哪里出错了。如果我不太清楚,请告诉我。谢谢!

4

2 回答 2

1

好的,我的意思是:由于您使用从 User 模型继承,因此您需要使用 AUTH_USER_MODEL 作为身份验证路由。这意味着,您已经有两种身份验证方式。也许您应该检查两个表:otp_device 和 auth_users。我的意思是,当您将日志/通行证放入表单时,它正在检查 auth_users 表。但在设备表中,您没有请求用户。因此,otp auth 系统无法与 None 记录匹配并在登录页面上重定向您。

如果我有类似的情况,我决定尝试两种解决方案:

  1. 创建一个信号来同步用户和设备表;
  2. 使用旧版本的用户自定义(OneToOneField 和用户模型)。

我认为身份验证的方式太复杂了:三个应用程序(two_factor、otp 和默认身份验证)都在使用类似的身份验证技术。

于 2014-10-29T10:27:20.577 回答
0

正如我在文档中看到的那样:“请务必删除任何其他登录路径,否则可能会绕过双重身份验证。应自动修补管理界面以使用新的登录方法。”

尝试找出使用 AUTH_USER_MODEL = 'users.User' 的原因

于 2014-10-28T14:37:15.637 回答