Easily add Google SSO login to Django Admin
Project description
Welcome to Django Google SSO
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
auth2clientversion.
Install
$ pip install django-google-sso
Versions
For django 4.x use version 2.x
For django 3.x use version 1.x
Configuration
- Add the following to your
settings.pyINSTALLED_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
]
- 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"
- 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"]
- In
urls.pyplease 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")
),
]
-
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/, whereyour-domain.comis the domain you defined in Django Sites Framework. For example, if you change your Site object domain tolocalhost:8000, then your callback must behttp://localhost:8000/google_sso/callback/. If you don't want to use Sites Framework, or need a different domain, you can use theGOOGLE_SSO_CALLBACK_DOMAINsetting to define the callback domain. For exampleGOOGLE_SSO_CALLBACK_DOMAIN="my-other-domain.com". -
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-google-sso-2.1.0.tar.gz.
File metadata
- Download URL: django-google-sso-2.1.0.tar.gz
- Upload date:
- Size: 14.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/37.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.64.0 importlib-metadata/4.12.0 keyring/23.9.0 rfc3986/2.0.0 colorama/0.4.5 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
871a82dd903135be6359a14f097fdc412c4e71f8572d88d82d8b526a50ade3f5
|
|
| MD5 |
0f920f38cfb696c69eaf1a3373cf1046
|
|
| BLAKE2b-256 |
97fe96cbe8d1f2b7b92461dcc47fa19d352a2bea46751da6a4366d46be5fb968
|
File details
Details for the file django_google_sso-2.1.0-py3-none-any.whl.
File metadata
- Download URL: django_google_sso-2.1.0-py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/37.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.64.0 importlib-metadata/4.12.0 keyring/23.9.0 rfc3986/2.0.0 colorama/0.4.5 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e18ab20cdc2e2e195823f13a0ef2b2fa0784ae00ee93ace45752273b6a053df2
|
|
| MD5 |
6a510cbf2e6e5407ddec3138438b910e
|
|
| BLAKE2b-256 |
e67caf45f68364cc3fd68e6bb8ad2c16a3e9228e883077e88d64a6d4673a3a0d
|