Skip to main content

Easily add Google SSO login to Django Admin

Project description

Welcome to Django Google SSO

PyPI Build PyPI - Python Version PyPI - Django Version Conventional Commits Code style: black pre-commit

This library aims to simplify the process of authenticating users with Google in Django Admin pages, inspired by libraries like django_microsoft_auth and django-admin-sso

Why another library?

  • This library aims for simplicity and ease of use. django-allauth is de facto solution for Authentication in Django, but add lots of boilerplate, specially the html templates. Django-Google-SSO just add the "Login with Google" button in the default login page.
  • django-admin-sso is a good solution, but it uses a deprecated google auth2client version.

Install

$ pip install django-google-sso

Configuration

  1. Add the following to your settings.py INSTALLED_APPS:
INSTALLED_APPS = [
    # other django apps
    "django.contrib.messages",  # Need for Auth messages
    "django.contrib.sites",  # Add Sites framework
    "django_google_sso",  # Add django_google_sso
]
  1. In Google Console at Api -> Credentials, retrieve your Project Credentials and add them in your settings.py:
GOOGLE_SSO_CLIENT_ID = "your client id here"
GOOGLE_SSO_PROJECT_ID = "your project id here"
GOOGLE_SSO_CLIENT_SECRET = "your client secret here"
  1. Add the default site and allowed domains to auto-create users:
SITE_ID = 1  # Optional, just add if you want to use sites without request.
GOOGLE_SSO_ALLOWABLE_DOMAINS = ["example.com"]
  1. In urls.py please add the Django-Google-SSO views:
from django.urls import include, path

urlpatterns = [
    # other urlpatterns...
    path(
        "google_sso/", include("django_google_sso.urls", namespace="django_google_sso")
    ),
]
  1. In Google Console at Api -> Credentials -> Oauth2 Client, please add Django-Google-SSO callback url, using this format: https://your-domain.com/google_sso/callback/, where your-domain.com is the domain you defined in Django Sites Framework. For example, if you change your Site object domain to localhost:8000, then your callback must be http://localhost:8000/google_sso/callback/.

  2. Run migrations:

$ python manage.py migrate

How Django-Google-SSO works

First, the user is redirected to the Django login page. If settings GOOGLE_SSO_ENABLED is True, the "Login with Google" button will be added to default form.

On click, Django-Google-SSO will add, in current session, the next_path and Google Flow state. This session will expire in 10 minutes. Then user will be redirected to Google login page.

On callback, Django-Google-SSO will check code and state received. If they are valid, Google's UserInfo will be retrieved. If the user is already registered in Django, the user will be logged in.

Otherwise, the user will be created and logged in, if his email domain, matches one of the GOOGLE_SSO_ALLOWABLE_DOMAINS. On creation only, this user can be set the staff or superuser status, if his email are in GOGGLE_SSO_STAFF_LIST or GOGGLE_SSO_SUPERUSER_LIST respectively.

Please note if you add an email to one of these lists, the email domain must be added to GOOGLE_SSO_ALLOWABLE_DOMAINS too.

This session will expire in 1 hour, or the time defined, in seconds, in GOOGLE_SSO_SESSION_COOKIE_AGE.

Browser will be redirected to next_path if operation succeeds, or the login page, if operation fails.

Further customization

Please add the following variables to your settings.py:

GOOGLE_SSO_ENABLED = True  # default value
GOOGLE_SSO_SESSION_COOKIE_AGE = 3600  # default value

# Mark as True, to add superuser status to first user
# created with email domain in `GOOGLE_SSO_ALLOWABLE_DOMAINS`
GOGGLE_SSO_AUTO_CREATE_FIRST_SUPERUSER = True

GOGGLE_SSO_STAFF_LIST = ["email@example.com"]
GOGGLE_SSO_SUPERUSER_LIST = ["another-email@example.com"]
GOOGLE_SSO_TIMEOUT = 10  # Time before timeout Google requests. Default value: 10 seconds
GOOGLE_SSO_SCOPES = [  # Google default scope
    "openid",
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/userinfo.profile",
]

Running behind a Reverse Proxy

Please make sure you're passing the correct X-Forwarded-Proto header.

Using the login_required decorator

To use the login_required decorator, or his Class Based View equivalent, you can redirect the accounts/login route to the modified login form page, adding this to your urls.py:

from django.conf.urls import url
from django.contrib.auth.views import LoginView

urlpatterns = [
    url(
        r"^accounts/login/$",
        LoginView.as_view(
            template_name="admin_sso/login.html"  # The modified form with google button
        ),
    ),
]

Example App

To test this library please check the Example App provided here.

Not working?

Don't panic. Get a towel and, please, open an issue.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-google-sso-1.0.1.tar.gz (25.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_google_sso-1.0.1-py3-none-any.whl (27.5 kB view details)

Uploaded Python 3

File details

Details for the file django-google-sso-1.0.1.tar.gz.

File metadata

  • Download URL: django-google-sso-1.0.1.tar.gz
  • Upload date:
  • Size: 25.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for django-google-sso-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2769d561c245f4d71b7a9f64efb293d0fe0d3a71c7ade16917cbc796de79db39
MD5 c24fe062340d0e378e152b9301ca74d9
BLAKE2b-256 1fc16e562b4abbb64e8356cd71585199d8595dd9b8a657afbc744b03e0d09db7

See more details on using hashes here.

File details

Details for the file django_google_sso-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_google_sso-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 27.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for django_google_sso-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2862a23b43cfa9f08aba3fbaec035160412bbc98419951ae45b3a2182311f70f
MD5 57a53de8ca075fbbafe5f6688255ab72
BLAKE2b-256 7891897b66b3abcfc438a40bfbf02abcabecbb5c25744784881e5999aa0b6e81

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page