无法从登录页面进一步移动,[django-otp]

时间:2014-10-28 13:24:17

标签: python django python-2.7 otp two-factor-authentication

问题:

在提供用户名和密码

后,我将重定向回登录页面,并显示以下错误消息

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

详情:

我已遵循Django Two-Factor Authentication的文档。我必须错过配置中的某些内容,但并不完全清楚。贝娄是我的代码中的细节。

settings.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

记录器看起来也很好

urls.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中注册它们除了这个问题我也粘贴该文件也很清楚。

admin.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)

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

2 个答案:

答案 0 :(得分:1)

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

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

  1. 创建信号以同步用户和设备表;
  2. 使用旧版本的用户自定义(OneToOneField with User model)。
  3. 我认为这对auth来说太复杂了:三个应用程序(two_factor,otp和默认auth)正在使用类似的auth技术。

答案 1 :(得分:0)

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

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

的理由