# openedx/auth-backends architecture

> Architecture diagram of the GitHub repository openedx/auth-backends, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openedx/auth-backends
- Repository: https://github.com/openedx/auth-backends

## Overview

##### 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

## Diagram (Mermaid)

```mermaid
flowchart TB
    classDef django fill:#D6EAF8,stroke:#1B4F72,color:#1B2631
    classDef external fill:#FAD7A0,stroke:#935116,color:#4D2600
    classDef db fill:#ABEBC6,stroke:#196F3D,color:#0E3E1D
    classDef cache fill:#A9DFBF,stroke:#1D8348,color:#0E3E1D
    classDef client fill:#D2B4DE,stroke:#6C3483,color:#4A235A
    classDef config fill:#F5B7B1,stroke:#7D6608,color:#4D2600
    classDef repo fill:#F2F3F4,stroke:#7B7D7D,color:#2C3E50

    Client["Client\n(User Browser)"]:::client

    subgraph "Host Django Service"
      direction TB
      Config["Settings\n(SOCIAL_AUTH_*)"]:::config
      UR["URL Router\n(oauth2_urlpatterns)"]:::django
      Views["OAuth2LoginView & LogoutView"]:::django
      Strategy["EdxDjangoStrategy"]:::django
      Pipeline["Pipeline Steps"]:::django
      Backend["EdXOAuth2 Backend"]:::django
    end

    ExternalLib["python-social-auth\n(social_django)"]:::external
    Provider["edX OAuth2 Provider"]:::external
    ProviderToken["Token Endpoint\n(edX)"]:::external
    DB["Database\n(User & social_auth tables)"]:::db
    Cache["Cache\n(provider metadata & JWKS)"]:::cache

    Client -->|"/login/edx-oauth2"| UR
    UR --> Views
    Views -->|redirect to AUTHORIZATION_ENDPOINT| Provider
    Provider -->|Authorization Code| Views
    Views --> Strategy
    Strategy --> ExternalLib
    ExternalLib --> Strategy
    Strategy --> Pipeline
    Pipeline --> Backend
    Backend -->|exchange code for token| ProviderToken
    ProviderToken -->|Access Token + JWT| Pipeline
    Pipeline --> DB
    Strategy --> Cache

    Client -->|"/logout/edx-oauth2"| Views
    Views -->|redirect to logout endpoint| Provider

    subgraph "Repository"
      direction TB
      ArchDec["Architecture Decisions"]:::repo
      Tests["Unit Tests"]:::repo
      Setup1["setup.py"]:::repo
      Setup2["setup.cfg"]:::repo
      Manifest["MANIFEST.in"]:::repo
      CI["CI/CD Workflows"]:::repo
      ReqBase["requirements/base.txt"]:::repo
      ReqDev["requirements/dev.txt"]:::repo
    end

    class Client client
    class Config config
    class UR,Views,Strategy,Pipeline,Backend django
    class ExternalLib,Provider,ProviderToken external
    class DB db
    class Cache cache
    class ArchDec,Tests,Setup1,Setup2,Manifest,CI,ReqBase,ReqDev repo

    click UR "https://github.com/openedx/auth-backends/blob/master/auth_backends/urls.py"
    click Views "https://github.com/openedx/auth-backends/blob/master/auth_backends/views.py"
    click Strategy "https://github.com/openedx/auth-backends/blob/master/auth_backends/strategies.py"
    click Pipeline "https://github.com/openedx/auth-backends/blob/master/auth_backends/pipeline.py"
    click Backend "https://github.com/openedx/auth-backends/blob/master/auth_backends/backends.py"
    click ArchDec "https://github.com/openedx/auth-backends/blob/master/auth_backends/docs/decisions/0001-use-changelog.rst"
    click Tests "https://github.com/openedx/auth-backends/tree/master/auth_backends/tests/"
    click Setup1 "https://github.com/openedx/auth-backends/blob/master/setup.py"
    click Setup2 "https://github.com/openedx/auth-backends/blob/master/setup.cfg"
    click Manifest "https://github.com/openedx/auth-backends/blob/master/MANIFEST.in"
    click CI "https://github.com/openedx/auth-backends/blob/master/.github/workflows/ci.yml"
    click ReqBase "https://github.com/openedx/auth-backends/blob/master/requirements/base.txt"
    click ReqDev "https://github.com/openedx/auth-backends/blob/master/requirements/dev.txt"
```
