How openedx/auth-backends works
Project type and purpose
• This is not a full-stack application but a pluggable Django authentication library.
• Purpose: Provide custom OAuth2 authentication backends, views, and pipeline steps for edX services (single sign-on/out).
File structure
• Top-level packaging files: setup.py, setup.cfg, MANIFEST.in, Makefile, requirements/, tox.ini, CI/CD workflows in .github/workflows.
• Main code lives in the auth_backends/ package:
• backends.py – custom Django authentication backends (EdXOAuth2).
• pipeline.py – social-auth pipeline steps for user data processing.
• strategies.py – custom social-auth strategy (EdxDjangoStrategy).
• views.py, urls.py – ready‐to‐use OAuth2 login/logout views and URL patterns.
• docs/decisions – architecture decisions (changelog use).
• Tests under auth_backends/tests exercise backends, pipeline, strategies, views.
• CI configuration, linting, publishing setups reflect best practices for a reusable library.
README for additional insights
• Confirms compatibility (Python 3.8+, Django 2.2/3.0).
• Describes configuration steps: INSTALLED_APPS, SOCIAL_AUTH_STRATEGY, AUTHENTICATION_BACKENDS, URL patterns.
• Emphasizes use of python-social-auth components, OAuth2 provider endpoints, JWT signing/caching, scopes.
a. Main components to represent
1) Client (end-user browser)
2) Django Service (your application)
– Django App Core
– auth_backends package modules:
• URL Router (auth_backends.urls → oauth2_urlpatterns)
• OAuth2LoginView / LogoutView (views.py)
• Social-auth Strategy (strategies.EdX DjangoStrategy)
• Pipeline Steps (pipeline.py)
• Authentication Backends (backends.EdXOAuth2)
3) python-social-auth library (external library)
4) External Identity Provider (edX OAuth2 server)
5) Database (User model, social_auth tables)
6) (Optional) Cache/service for provider metadata and JWKS
b. Relationships and data flow
1. Client → Django OAuth2LoginView: “/login/edx-oauth2/”
2. Django LoginView redirects → Identity Provider (AUTHORIZATION_ENDPOINT)
3. Client authenticates at Provider → Redirect back (CALLBACK URL)
4. Django callback:
– social-auth strategy loads provider config (with caching)
– python-social-auth pipeline invoked → pipeline.py steps
– backends.EdXOAuth2 exchanges code for token (TOKEN_ENDPOINT)
– pipeline extracts extra_data (user_id, JWT) → User record in DB
5. Django issues session/auth cookie → Client
6. Logout: Django LogoutView redirects → Provider logout → callback
c. Architectural patterns and principles
• Pluggable library pattern: decouples authentication logic from host application.
• Strategy pattern: custom EdxDjangoStrategy for social-auth settings.
• Pipeline pattern: sequential processing of OAuth2 response.
• Convention over configuration: default URL patterns, default settings.
• Caching for provider configuration and JWKS (performance, resilience).
d. Key technologies / libraries
• Django (URL routing, views, auth frameworks)
• python-social-auth (social_django)
• OAuth2 (authorization code flow, JWT handling)
• Redis or in-memory cache for provider metadata (if configured)
• PyPI packaging (setup.py), pytest / tox for testing, GitHub Actions for CI/CD