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

标签 python django python-2.7 two-factor-authentication one-time-password

问题:

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

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

详细信息:

我已遵循 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

Logger 看起来也不错

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)

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

最佳答案

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

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

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

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

关于python - 无法从登录页面进一步移动,[django-otp],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26609517/

相关文章:

python - 如何将带有重复文本的 CSV 列拆分为每个可能的文本变体的 0-1 列?

python - 对 pyspark 数据帧执行 PCA

python - Django super 函数不向类添加变量

python - beautifulsoup 不打印链接

每天在同一时间做某事的 Python 脚本

javascript - 从 python 脚本获取数据时,在 Heroku 上运行的 NodeJS 应用程序失败

python - 在 Python 中验证 Windows 8 应用内购买收据签名

python - 如何处理 AJAX 中的预期错误(包括向用户披露错误)

Django 自定义命令未显示在 Heroku 上

python - 如何使用 Python 格式处理空值