How thevickypedia/fastapiauthenticator works
Project type and purpose
• This is an open-source Python library (“FastAPIAuthenticator”) that plugs into a user’s FastAPI application to add username/password authentication to selected HTTP or WebSocket routes.
• The README and samples show how a developer calls fastapiauthenticator.protect(...) to wrap existing route handlers.
File structure
• Top-level:
• fastapiauthenticator/ – the core library package
• base/ – likely boilerplate or example scripts (e.g., api.py, api.sh)
• samples/ – example FastAPI apps demonstrating usage
• Configuration: pyproject.toml, requirements.txt, pre-commit-config.yaml
• Inside fastapiauthenticator/:
• endpoints.py – login, logout, unauthorized handlers
• service.py – orchestration of authentication logic
• secure.py – credential verification, hashing, session management
• models.py, enums.py – Pydantic models & enums for parameters, responses
• utils.py – cookie/session utilities
• templates/ – HTML pages for login, unauthorized, session info
• No database adapter; sessions are likely in-memory or signed cookies.
Key insights from the README
• One-line integration: a developer calls protect(app, Parameters(...))
• Supports both HTTP (APIRoute) and WebSocket (APIWebSocketRoute)
• Samples show form-post login flow and programmatic API use
• No complex microservices—this is a middleware/plugin library.
Main components
a. Client (Browser or WebSocket client)
b. FastAPI Application (developer’s code)
c. FastAPIAuthenticator Library
• Protect wrapper
• Service layer
• Secure layer
• Endpoints (login/logout/unauthorized)
• Templates (HTML)
• Pydantic Models & Enums
• Utils (cookie/session)
d. Protected Route Handler (user’s private function)
e. Optional External Systems (e.g., user storage if extended)
Relationships and data flow
1. Client → FastAPI app
2. Incoming request hits Protect wrapper (middleware or decorator)
3. Wrapper checks session via secure + utils
4. If session valid → call original protected route → return response
5. If not valid → redirect to Authenticator login endpoint
6. Client posts credentials → endpoints.login → service.validate_user → secure.verify_credentials
7. On success → set session cookie (utils) → redirect back to protected route
8. On failure → serve unauthorized template
Architectural patterns & design principles
• Separation of concerns:
• Protect wrapper isolates authentication logic
• service.py orchestrates flow, calls secure.py
• secure.py handles all low-level security (hashing, session tokens)
• endpoints.py only defines HTTP/WebSocket routes for auth
• Plug-in/Middleware pattern: integrates into FastAPI’s routing
• Template-based views for HTML forms
Technologies & frameworks
• Python 3.11+
• FastAPI (Starlette under the hood)
• Pydantic for data models
• Jinja2 (via FastAPI templates)
• Standard library / itsdangerous or cookie signing for sessions