Django Social Auth
social-auth-app-django is the official Django component of the Python Social Auth ecosystem, providing an easy-to-set-up social authentication and registration mechanism for Django projects. It integrates `social-auth-core` to support a wide array of OAuth and OpenID providers. The library is actively maintained, with version 5.7.0 being the latest, and focuses on supporting current Django releases.
Warnings
- breaking Version 5.7.0 integrated with `social_core` using a registry instead of monkey patching. While generally an internal change, custom integrations relying on previous monkey-patching behavior might require adjustments. Always review the changelog for details if you have highly customized setups.
- breaking Support for older Django and Python versions has been progressively dropped in recent releases. Version 5.2.0 removed support for Django < 3.2, and 5.5.0 dropped support for additional older Django versions. The library now requires Python >= 3.10 and is compatible with Django versions 4.2, 5.0, 5.1, and 5.2.
- gotcha A security vulnerability (CVE-2025-61783) in versions prior to 5.6.0 allowed for potentially unsafe account association via email, even if the `associate_by_email` pipeline was not explicitly enabled. Version 5.6.0 fixed this issue, and also introduced a change where storage now filters for active users; you might need to customize `SOCIAL_AUTH_ACTIVE_USERS_FILTER` if your custom user model lacks an `is_active` field.
- gotcha A security vulnerability (CVE-2024-32879) in versions prior to 5.4.1 addressed improper handling of case sensitivity with MySQL/MariaDB databases, where the default case-insensitive collation could cause different user IDs to match. This could lead to account spoofing.
- gotcha SQLite has field length limitations that can cause issues, especially with UIDs from social providers. For production environments, PostgreSQL or MySQL are recommended. If using MySQL InnoDB or SQLite, you might need to add `SOCIAL_AUTH_UID_LENGTH = 223` to your settings to avoid database errors.
- gotcha The `SOCIAL_AUTH_PIPELINE` setting, if configured with `social_core.pipeline.social_auth.associate_by_email`, can be insecure. This is because not all social providers validate the user's email address, potentially allowing a malicious user to claim an existing account by registering with a non-validated email on a third-party provider that matches an email in your system.
- gotcha Sensitive credentials (like `SOCIAL_AUTH_GOOGLE_OAUTH2_KEY` and `SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET`) should never be committed to version control. Always use environment variables or a secure configuration management system.
Install
-
pip install social-auth-app-django
Imports
- social_django
INSTALLED_APPS = ['social_django']
- GoogleOAuth2
from social_core.backends.google import GoogleOAuth2 AUTHENTICATION_BACKENDS = ('social_core.backends.google.GoogleOAuth2', ...) - social_django.urls
from django.urls import include, path urlpatterns = [path('oauth/', include('social_django.urls', namespace='social'))] - SocialAuthExceptionMiddleware
MIDDLEWARE = [..., 'social_django.middleware.SocialAuthExceptionMiddleware']
Quickstart
import os
# settings.py
INSTALLED_APPS = [
# ... existing apps ...
'django.contrib.auth',
'django.contrib.sessions',
'social_django',
]
MIDDLEWARE = [
# ... existing middleware ...
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'social_django.middleware.SocialAuthExceptionMiddleware',
]
AUTHENTICATION_BACKENDS = (
'social_core.backends.google.GoogleOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.environ.get('GOOGLE_OAUTH2_KEY', '')
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.environ.get('GOOGLE_OAUTH2_SECRET', '')
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = ['email', 'profile']
LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# ... existing context processors ...
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
],
},
},
]
# urls.py
from django.urls import include, path
urlpatterns = [
path('oauth/', include('social_django.urls', namespace='social')),
# ... other paths ...
]
# In your login template (e.g., login.html)
# <a href="{% url 'social:begin' 'google-oauth2' %}">Login with Google</a>