Django, mozilla-django-oidc and admin

后端 未结 2 1709
感动是毒
感动是毒 2021-01-07 11:28

i am trying to connect Okta with a custom Django (v.3.0.2) app i am coding, using the mozilla-django-oidc library. So far the initial user authentication and account creatio

2条回答
  •  孤城傲影
    2021-01-07 11:56

    I've come up with a solution for using the mozilla-django-oidc login with the django admin. It's a little hacky but it's a lot less intimidating to redirect the admin login page than to override AdminSite.

    In my top-level urls.py I have

    class CustomLogin(View):
        def get(self, request, **kwargs):
            return HttpResponseRedirect(
                reverse('oidc_authentication_init') + (
                    '?next={}'.format(request.GET['next']) if 'next' in request.GET else ''
                )
            )
    
    urlpatterns = [
        path('oidc/', include("mozilla_django_oidc.urls")),
        path('admin/login/', CustomLogin.as_view()),
        path('admin/', admin.site.urls),
        # the rest of my urls...
    ]
    

    If you don't care about passing the ?next= value correctly you can skip the CustomLogin class and do the following instead

    urlpatterns = [
        path('oidc/', include("mozilla_django_oidc.urls")),
    ]
    # This only works if you break up urlpatterns so the reverse below can find what it needs
    urlpatterns += [
        path('admin/login/', RedirectView.as_view(
            url=reverse('oidc_authentication_init') + ?next=/admin/,
            permanent=False
        )),
        path('admin/', admin.site.urls),
        # the rest of my urls...
    ]
    

    I added ?next=/admin/ because by default once you log in you will be redirected to settings.LOGIN_REDIRECT_URL which I'm already using for something else

提交回复
热议问题