How python-social-auth/social-app-django works
Project Type and Purpose
• This is a Django “app” (library/plugin) called social_django, part of the Python Social Auth ecosystem.
• Purpose: integrate social-auth-core into Django projects, providing authentication via external OAuth/OIDC providers (e.g., Google, Facebook, GitHub).
File Structure Analysis
• Top‐level: configuration files (.gitignore, tox.ini, pyproject.toml), manage.py (entry point for tests/development), README, LICENSE.
• social_django/: the actual Django app:
• apps.py, admin.py, urls.py, views.py – standard Django hooks
• models.py, managers.py, storage.py – define UserSocialAuth, tokens, queries
• strategy.py, config.py – glue code to adapt social_core to Django
• middleware.py, context_processors.py, fields.py – integrate into request/response
• migrations/ – schema evolution for storing social auth data
• management/commands/clearsocial.py – admin utility
• tests/: isolated Django settings, unit tests for each component
Key Insights from README
• This module plugs into any Django project.
• It delegates core logic to social-auth-core and focuses on Django integration.
• Configuration via Django settings, automatic URL patterns, template context.
a. Main Components
• Client (browser or mobile)
• Django Web Application
• URL router (urls.py)
• Views (views.py)
• Middleware (middleware.py)
• Context Processors (context_processors.py)
• Social Auth App (social_django)
• Django’s Auth System
• Database (User, UserSocialAuth models)
• social-auth-core library (pip‐installed dependency)
• External Identity Providers (OAuth/OIDC endpoints)
• Optional Cache/Session store
b. Relationships & Data Flow
1. User clicks “Login with X” on frontend → HTTP GET to Django URL (urls.py).
2. Django View delegates to social_django.strategy → calls social-core pipeline.
3. Redirect to External Provider’s OAuth endpoint.
4. Provider returns callback → social_django’s callback view.
5. social-core pipeline runs: fetch/populate user info → storage.save() via storage.py.
6. Create or update UserSocialAuth & link to Django User (models.py).
7. Django Auth logs user in → session saved in DB or cache.
8. Context Processor injects social data into templates.
c. Architectural Patterns & Principles
• Plugin architecture: social_django interfaces via Django’s INSTALLED_APPS.
• Pipeline pattern: configurable sequence of steps in social-core.
• Adapter/Strategy pattern: strategy.py adapts social-core to Django.
• Separation of concerns: storage layer, config, views, middleware each isolated.
d. Relevant Technologies
• Django (URL routing, ORM, middleware)
• social-auth-core (authentication pipeline)
• Python ORM (models, migrations)
• OAuth2/OIDC protocols