django-two-factor-auth can't access admin site

╄→尐↘猪︶ㄣ 提交于 2019-12-10 22:35:58

问题


I am using django-two-factor-auth for a webapp. I cannot access the admin page.

I know I am entering the correct credentials. When I input incorrect credentials, I get an appropriate error message.

When I input the correct credentials, the page simply reloads with this URL:

http://localhost:8080/account/login/?next=/inveskore/

These are my settings related to two_factor:

LOGIN_URL = 'two_factor:login'
LOGIN_REDIRECT_URL = '/inveskore'
TWO_FACTOR_SMS_GATEWAY = 'two_factor.gateways.twilio.gateway.Twilio'

This is the associated URL path:

path('admin/', admin.site.urls),

According to this, it results from the admin user not having 2FA set.

So, how do you set 2FA for the admin user if you can't access the site?

EDIT:

I took down the 2FA login requirements for the site and then added a phone device. No luck.


回答1:


I recently ran into this scenario and created this solution based on a comment there:

https://github.com/Bouke/django-two-factor-auth/issues/219#issuecomment-494382380

I subclassed AdminSiteOTPRequired and then specified it as the admin class to use

from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.views import redirect_to_login
from django.shortcuts import resolve_url
from django.urls import reverse
from django.utils.http import is_safe_url
from two_factor.admin import AdminSiteOTPRequired, AdminSiteOTPRequiredMixin


class AdminSiteOTPRequiredMixinRedirSetup(AdminSiteOTPRequired):
    def login(self, request, extra_context=None):
        redirect_to = request.POST.get(
            REDIRECT_FIELD_NAME, request.GET.get(REDIRECT_FIELD_NAME)
        )
        # For users not yet verified the AdminSiteOTPRequired.has_permission
        # will fail. So use the standard admin has_permission check:
        # (is_active and is_staff) and then check for verification.
        # Go to index if they pass, otherwise make them setup OTP device.
        if request.method == "GET" and super(
            AdminSiteOTPRequiredMixin, self
        ).has_permission(request):
            # Already logged-in and verified by OTP
            if request.user.is_verified():
                # User has permission
                index_path = reverse("admin:index", current_app=self.name)
            else:
                # User has permission but no OTP set:
                index_path = reverse("two_factor:setup", current_app=self.name)
            return HttpResponseRedirect(index_path)

        if not redirect_to or not is_safe_url(
            url=redirect_to, allowed_hosts=[request.get_host()]
        ):
            redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)

        return redirect_to_login(redirect_to)

Then in urls.py:

from django.contrib import admin
admin.site.__class__ = AdminSiteOTPRequiredMixinRedirSetup


来源:https://stackoverflow.com/questions/48600737/django-two-factor-auth-cant-access-admin-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!