monkut
monkut

Reputation: 43832

Integrate python-social-auth (social-app-django) to be used with the Django admin

I want to create an app that uses the django admin, but allows logins via google (my company google account) in place of the django default ModelAdmin.

Currently, it looks like social-app-django (google) is the way to go, but after having installed and setup a project, it's not clear to me how I can allow django admin logins to use the social-app-django authentication. I've attempted to configure my project as described here http://python-social-auth.readthedocs.io/en/latest/configuration/django.html but it isn't clear how this can be integrated with the django admin.

I found this snippit (which seems out-of-date), and added it, but get a 404 when I try to goto /admin/:

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/accounts/login/?next=/admin/login/%3Fnext%3D/admin/ Using the URLconf defined in telos.urls, Django tried these URL patterns, in this order: ^login/(?P[^/]+)/$ [name='begin'] ^complete/(?P[^/]+)/$ [name='complete'] ^disconnect/(?P[^/]+)/$ [name='disconnect'] ^disconnect/(?P[^/]+)/(?P[^/]+)/$ [name='disconnect_individual'] ^admin/ The current path, accounts/login/, didn't match any of these.

If I remove the snippit, /admin/ will redirect to /admin/login/ and on login attempt return the ERROR text:

Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive.

In addition to the configuration, I've added the following to my settings.py:

projects/models.py (MyUser)

from django.contrib.auth.models import AbstractUser

class MyUser(AbstractUser):
    pass

settings.py

# for identification of SOCIAL_AUTH_USER
# http://python-social-auth.readthedocs.io/en/latest/configuration/settings.html#user-model
SOCIAL_AUTH_USER_MODEL = 'projects.MyUser'
AUTH_USER_MODEL = 'projects.MyUser'  # not sure if this is needed

Can anyone direct me on how I can setup my project to allow me to login to the django admin through social-app-django (google)?

Upvotes: 5

Views: 1504

Answers (1)

omab
omab

Reputation: 3701

Django Admin uses the auth contrib application, so any authentication process triggers the same mechanism than a user logging in to a non-admin section and it will be processed by python-social-auth backends if they are defined in AUTHENTICATION_BACKENDS setting.

In order to make it work you will need to:

  1. Add a Login with Google link (linking to /login/google-oauth2) to the login form. You can override the default login form by adding a admin/login.html template or by defining a custom AdminSite
  2. Ensure that the user is flagged as is_staff, otherwise access to the admin will be forbidden.

Upvotes: 3

Related Questions