Quickstart
It's an app to for social authentication and other security integrations.
Requirements
These packages are required:
- Python (3.9+)
- Django (3.2+)
We highly recommend and only officially support the latest patch release of each Python and Django series.
Installation
-
Install using
pip, including any optional packages you want...pip install django-sp...or clone the project from github.
git clone https://github.com/DLRSP/django-sp/ -
Add
"socialprofile"and dependencies apps to yourINSTALLED_APPSsetting.settings.pyINSTALLED_APPS = [ # ...other apps "django_errors", # Needed by Wrap Errors "axes", # Needed by SocialProfile [Monitor Logins] "user_sessions", # Needed by SocialProfile [One-Time-Password] "django_otp", # Needed by SocialProfile [One-Time-Password] "django_otp.plugins.otp_static", # Needed by SocialProfile [One-Time-Password] "django_otp.plugins.otp_totp", # Needed by SocialProfile [One-Time-Password] "two_factor", # Needed by SocialProfile [One-Time-Password] "social_django", # Needed by SocialProfile [OAuth] "rest_framework.authtoken", # Needed by SocialProfile [Token] "rest_framework", # Needed by SocialProfile "oauth2_provider", # Needed by SocialProfile [OAuth2 Token] "corsheaders", # Needed by SocialProfile [OAuth2 Token] "crispy_forms", # Needed by SocialProfile [Edit Form] "django_countries", # Needed by SocialProfile [Edit Form] "sweetify", # Needed by SocialProfile [Edit Form] "socialprofile", # ...other apps ] -
Add the following to your root
urls.pyfile.urls.py# ...other imports... from django.conf.urls import url, include urlpatterns = [ url(r"^sp/", include("socialprofile.urls")), # ...other urls... ] -
Add and modify the following middleware inside your
MIDDLEWAREsetting.settings.pyMIDDLEWARE = ( "django.middleware.security.SecurityMiddleware", # "django.contrib.sessions.middleware.SessionMiddleware", # Need Commente-Out By user_sessions "user_sessions.middleware.SessionMiddleware", # Need By SocialProfile "django.middleware.cache.UpdateCacheMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", "oauth2_provider.middleware.OAuth2TokenMiddleware", # Need By SocialProfile "django.middleware.locale.LocaleMiddleware", "django_otp.middleware.OTPMiddleware", # Need By SocialProfile "corsheaders.middleware.CorsMiddleware", # Need By SocialProfile "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", "django.middleware.cache.FetchFromCacheMiddleware", "social_django.middleware.SocialAuthExceptionMiddleware", # Need By SocialProfile "axes.middleware.AxesMiddleware", # Need By SocialProfile ) -
Add the following admin error inside your
SILENCED_SYSTEM_CHECKSsetting.settings.pySILENCED_SYSTEM_CHECKS = ["admin.E410"] # Need By SocialProfile -
Add the following context processor inside your
TEMPLATESsetting.settings.pyTEMPLATES = [ { # ...other template options "OPTIONS": { "context_processors": [ "django.template.context_processors.debug", "django.template.context_processors.request", "django.contrib.auth.context_processors.auth", "django.contrib.messages.context_processors.messages", "django.template.context_processors.media", "social_django.context_processors.backends", # Need By SocialProfile "social_django.context_processors.login_redirect", # Need By SocialProfile ], }, }, ] -
Add the following ....
XxXsetting.settings.py# Need by Social Profile / user_sessions GEOIP_PATH = "/home/sh/SH_CusData/intf/in/geoip2" SESSION_ENGINE = "user_sessions.backends.db" -
Add the following ....
XxXsetting.settings.py# Core Authentication Settings LOGIN_URL = "/sp/select/" LOGIN_ERROR_URL = "/sp/select/" LOGIN_REDIRECT_URL = "/sp/" DEFAULT_RETURNTO_PATH = "/sp/" # Social Auth Redirects Settings SOCIAL_AUTH_LOGIN_REDIRECT_URL = "/sp/" SOCIAL_AUTH_LOGIN_ERROR_URL = "/sp/login-error/" SOCIAL_AUTH_LOGIN_URL = "/sp/select/" SOCIAL_AUTH_NEW_USER_REDIRECT_URL = "/sp/new-profile/" SOCIAL_AUTH_NEW_ASSOCIATION_REDIRECT_URL = "/sp/new-association/" -
Add the following ....
XxXsetting.settings.py# Custom Social Profile User AUTH_USER_MODEL = "socialprofile.socialprofile" SOCIAL_AUTH_USER_MODEL = "socialprofile.socialprofile" -
Add the following ....
XxXsetting.settings.pySOCIAL_AUTH_ALWAYS_ASSOCIATE = True SOCIAL_AUTH_PROTECTED_USER_FIELDS = [ "username", "email", "first_name", "last_name", ] -
Add the following ....
XxXsetting.settings.pyAUTHENTICATION_BACKENDS = ( "social_core.backends.google.GoogleOAuth2", "social_core.backends.twitter.TwitterOAuth", "social_core.backends.email.EmailAuth", "axes.backends.AxesBackend", "django.contrib.auth.backends.ModelBackend", ) -
Add the following ....
XxXsetting.settings.pySOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ( "aaaaaaaaaaaa-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.apps.googleusercontent.com" ) SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxx" SOCIAL_AUTH_TWITTER_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaa" SOCIAL_AUTH_TWITTER_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -
Recommended: call
apply_sp_defaultsat the end ofsettings.py(afterDEBUG, HTTPS headers, andAPP_CONFIG). It fills in redirect URLs, pipeline, Axes proxy defaults, and production OAuth HTTPS flags without overwriting explicit project values.settings.pyAPP_CONFIG = { "socialprofile": { # optional — only if you do not set CSRF_TRUSTED_ORIGINS yourself "csrf_trusted_origins": [ "https://www.example.com", "https://example.com", ], }, } # ... your existing DEBUG / SECURE_PROXY_SSL_HEADER / SOCIAL_AUTH_* keys ... from socialprofile.conf import apply_sp_defaults apply_sp_defaults(globals())Keep provider secrets (
SOCIAL_AUTH_*_KEY/_SECRET) in the project or env repo — the helper never invents credentials.
Production / reverse proxy (required since social-auth-app-django 6.0.0)
| Setting | When |
|---|---|
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') |
nginx/gunicorn terminates TLS |
SOCIAL_AUTH_REDIRECT_IS_HTTPS = True |
production OAuth callback URLs (set automatically by apply_sp_defaults when DEBUG is false and the proxy header is https) |
USE_X_FORWARDED_HOST = True |
multi-host or canonical public hostname differs from internal |
SESSION_COOKIE_SAMESITE = 'Lax' |
required for OAuth — Strict drops the session on return from the IdP (AuthStateMissing / 500). apply_sp_defaults sets/coerces Lax when unset, None, or Strict. CSRF_COOKIE_SAMESITE may stay Strict (same-site POST begin). |
CSRF_TRUSTED_ORIGINS |
Django 4+ CSRF on HTTPS login forms |
Social login buttons must POST. Version 6.0.0 of social-auth-app-django removed
GET on social:begin (and SOCIAL_AUTH_REQUIRE_POST). Use the packaged templates
(socialprofile/includes/social_begin_form.html) or equivalent CSRF-protected
<form method="post"> — never <a href="{% url 'social:begin' ... %}">.
Example
Let's take a look at a quick example of using this project to build a simple App with custom social login.
Quickstart
Can't wait to get started? The quickstart guide is the fastest way to get up and running and building a demo App.
Customize
Do you want custom solutions? The [customize][customize] section is an overview of which part are easy to design. If you find how to personalize different scenarios or behaviors, a pull request is welcome!
Development
See the Contribution guidelines for information on how to clone the repository, run the test suite and contribute changes back to django-errors.
Security
If you believe you’ve found something in this project which has security implications, please do not raise the issue in a public forum.
Send a description of the issue via email to dlrsp.issue@gmail.com. The project maintainers will then work with you to resolve any issues where required, prior to any public disclosure.
License
MIT License
Copyright (c) 2010-present DLRSP (https://dlrsp.org) and other contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.