# majestev/mfa-auth-api architecture

> Architecture diagram of the GitHub repository majestev/mfa-auth-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/majestev/mfa-auth-api
- Repository: https://github.com/majestev/mfa-auth-api

## Overview

MFA Auth API is a FastAPI service for account registration, password login, TOTP enrollment and challenge verification, recovery codes, and JWT session management. Clients enter through versioned routes; dependency wiring supplies services and persistence, and services coordinate repositories with Redis-backed volatile state. The graph preserves the documented security, audit, rate-limit, middleware, and error-handling capabilities. Some route-to-service links and infrastructure details are based on the README and SOURCE INDEX rather than complete source excerpts.

Main entry points: [`app/api/deps.py`](https://github.com/majestev/mfa-auth-api/blob/main/app/api/deps.py) (Dependency wiring), [`app/services/mfa_service.py`](https://github.com/majestev/mfa-auth-api/blob/main/app/services/mfa_service.py) (MFA workflows), [`app/services/auth_service.py`](https://github.com/majestev/mfa-auth-api/blob/main/app/services/auth_service.py) (Account auth), [`app/services/token_service.py`](https://github.com/majestev/mfa-auth-api/blob/main/app/services/token_service.py) (Token sessions)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["API and request handling"]
  node_bootstrap["App setup<br/>[main.py]"]
  node_request_middleware["Request middleware<br/>[request_id.py]"]
  node_security_middleware["Security headers"]
  node_api_router["API router<br/>[router.py]"]
  node_auth_routes["Auth routes<br/>[auth.py]"]
  node_mfa_routes["MFA routes<br/>[mfa.py]"]
  node_user_routes["User routes<br/>[users.py]"]
  node_health_routes["Health route<br/>[health.py]"]
  node_dependencies["Dependency wiring<br/>[deps.py]"]
end

subgraph group_domain["Authentication workflows"]
  node_auth_service["Account auth<br/>[auth_service.py]"]
  node_mfa_service["MFA workflows<br/>[mfa_service.py]"]
  node_token_service["Token sessions<br/>[token_service.py]"]
end

subgraph group_security["Security capabilities"]
  node_totp["TOTP and QR<br/>[totp.py]"]
  node_crypto["Secret encryption<br/>[crypto.py]"]
  node_jwt["JWT encoding<br/>[jwt.py]"]
  node_rate_limit["Rate limiting<br/>[rate_limit.py]"]
  node_error_handling["Error responses<br/>[exceptions.py]"]
  node_logging["Structured logging<br/>[logging.py]"]
  node_config["Configuration<br/>[config.py]"]
end

subgraph group_persistence["Persistence and state"]
  node_user_repository["User persistence<br/>[user_repository.py]"]
  node_mfa_repository["MFA persistence<br/>[mfa_repository.py]"]
  node_audit_repository["Audit events"]
  node_models["Data models<br/>[models.py]"]
  node_postgres[("PostgreSQL")]
  node_redis[("Redis state")]
end

node_client(("API client"))

node_client -->|"sends requests"| node_security_middleware
node_security_middleware -->|"passes requests"| node_request_middleware
node_request_middleware -->|"forwards requests"| node_api_router
node_bootstrap -->|"mounts routes"| node_api_router
node_bootstrap -->|"configures"| node_security_middleware
node_bootstrap -->|"configures"| node_request_middleware
node_api_router -->|"routes to"| node_auth_routes
node_api_router -->|"routes to"| node_mfa_routes
node_api_router -->|"routes to"| node_user_routes
node_api_router -->|"routes to"| node_health_routes
node_auth_routes -->|"uses dependencies"| node_dependencies
node_mfa_routes -->|"uses dependencies"| node_dependencies
node_user_routes -->|"uses dependencies"| node_dependencies
node_health_routes -->|"uses dependencies"| node_dependencies
node_dependencies -->|"constructs"| node_auth_service
node_dependencies -->|"constructs"| node_mfa_service
node_dependencies -->|"constructs"| node_token_service
node_auth_routes -->|"handles auth"| node_auth_service
node_mfa_routes -->|"handles MFA"| node_mfa_service
node_user_routes -.->|"changes password"| node_auth_service
node_auth_service -->|"reads and writes"| node_user_repository
node_auth_service -->|"issues sessions"| node_token_service
node_auth_service -->|"records events"| node_audit_repository
node_mfa_service -->|"loads users"| node_user_repository
node_mfa_service -->|"manages MFA data"| node_mfa_repository
node_mfa_service -->|"issues sessions"| node_token_service
node_mfa_service -->|"records events"| node_audit_repository
node_mfa_service -->|"checks codes"| node_totp
node_mfa_service -->|"decrypts secrets"| node_crypto
node_mfa_service -->|"stores challenges"| node_redis
node_auth_service -->|"limits attempts"| node_rate_limit
node_auth_service -->|"stores challenges"| node_redis
node_token_service -->|"encodes tokens"| node_jwt
node_token_service -->|"blacklists tokens"| node_redis
node_token_service -->|"resolves users"| node_user_repository
node_token_service -->|"records events"| node_audit_repository
node_dependencies -->|"builds repositories"| node_user_repository
node_dependencies -->|"builds repositories"| node_mfa_repository
node_dependencies -->|"builds repository"| node_audit_repository
node_dependencies -->|"builds limiter"| node_rate_limit
node_dependencies -->|"loads settings"| node_config
node_user_repository -->|"queries users"| node_models
node_mfa_repository -->|"queries MFA records"| node_models
node_audit_repository -->|"writes events"| node_models
node_models -.->|"persists records"| node_postgres
node_audit_repository -->|"logs events"| node_logging
node_error_handling -->|"gets request ID"| node_request_middleware

click node_bootstrap "https://github.com/majestev/mfa-auth-api/blob/main/app/main.py"
click node_request_middleware "https://github.com/majestev/mfa-auth-api/blob/main/app/middleware/request_id.py"
click node_security_middleware "https://github.com/majestev/mfa-auth-api/blob/main/app/middleware/security_headers.py"
click node_api_router "https://github.com/majestev/mfa-auth-api/blob/main/app/api/v1/router.py"
click node_auth_routes "https://github.com/majestev/mfa-auth-api/blob/main/app/api/v1/auth.py"
click node_mfa_routes "https://github.com/majestev/mfa-auth-api/blob/main/app/api/v1/mfa.py"
click node_user_routes "https://github.com/majestev/mfa-auth-api/blob/main/app/api/v1/users.py"
click node_health_routes "https://github.com/majestev/mfa-auth-api/blob/main/app/api/v1/health.py"
click node_dependencies "https://github.com/majestev/mfa-auth-api/blob/main/app/api/deps.py"
click node_auth_service "https://github.com/majestev/mfa-auth-api/blob/main/app/services/auth_service.py"
click node_mfa_service "https://github.com/majestev/mfa-auth-api/blob/main/app/services/mfa_service.py"
click node_token_service "https://github.com/majestev/mfa-auth-api/blob/main/app/services/token_service.py"
click node_user_repository "https://github.com/majestev/mfa-auth-api/blob/main/app/repositories/user_repository.py"
click node_mfa_repository "https://github.com/majestev/mfa-auth-api/blob/main/app/repositories/mfa_repository.py"
click node_audit_repository "https://github.com/majestev/mfa-auth-api/blob/main/app/repositories/auth_event_repository.py"
click node_models "https://github.com/majestev/mfa-auth-api/blob/main/app/database/models.py"
click node_totp "https://github.com/majestev/mfa-auth-api/blob/main/app/core/totp.py"
click node_crypto "https://github.com/majestev/mfa-auth-api/blob/main/app/core/crypto.py"
click node_jwt "https://github.com/majestev/mfa-auth-api/blob/main/app/core/jwt.py"
click node_rate_limit "https://github.com/majestev/mfa-auth-api/blob/main/app/core/rate_limit.py"
click node_error_handling "https://github.com/majestev/mfa-auth-api/blob/main/app/core/exceptions.py"
click node_logging "https://github.com/majestev/mfa-auth-api/blob/main/app/core/logging.py"
click node_config "https://github.com/majestev/mfa-auth-api/blob/main/app/core/config.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_bootstrap,node_request_middleware,node_security_middleware,node_api_router,node_auth_routes,node_mfa_routes,node_user_routes,node_health_routes,node_dependencies,node_client toneBlue
class node_auth_service,node_mfa_service,node_token_service toneAmber
class node_totp,node_crypto,node_jwt,node_rate_limit,node_error_handling,node_logging,node_config toneMint
class node_user_repository,node_mfa_repository,node_audit_repository,node_models,node_postgres,node_redis toneRose
```

## Components

### API and request handling

- [App setup](https://github.com/majestev/mfa-auth-api/blob/main/app/main.py): `app/main.py`
- [Request middleware](https://github.com/majestev/mfa-auth-api/blob/main/app/middleware/request_id.py): `app/middleware/request_id.py`
- [Security headers](https://github.com/majestev/mfa-auth-api/blob/main/app/middleware/security_headers.py): `app/middleware/security_headers.py`
- [API router](https://github.com/majestev/mfa-auth-api/blob/main/app/api/v1/router.py): `app/api/v1/router.py`
- [Auth routes](https://github.com/majestev/mfa-auth-api/blob/main/app/api/v1/auth.py): `app/api/v1/auth.py`
- [MFA routes](https://github.com/majestev/mfa-auth-api/blob/main/app/api/v1/mfa.py): `app/api/v1/mfa.py`
- [User routes](https://github.com/majestev/mfa-auth-api/blob/main/app/api/v1/users.py): `app/api/v1/users.py`
- [Health route](https://github.com/majestev/mfa-auth-api/blob/main/app/api/v1/health.py): `app/api/v1/health.py`
- [Dependency wiring](https://github.com/majestev/mfa-auth-api/blob/main/app/api/deps.py): `app/api/deps.py`

### Authentication workflows

- [Account auth](https://github.com/majestev/mfa-auth-api/blob/main/app/services/auth_service.py): `app/services/auth_service.py`
- [MFA workflows](https://github.com/majestev/mfa-auth-api/blob/main/app/services/mfa_service.py): `app/services/mfa_service.py`
- [Token sessions](https://github.com/majestev/mfa-auth-api/blob/main/app/services/token_service.py): `app/services/token_service.py`

### Security capabilities

- [TOTP and QR](https://github.com/majestev/mfa-auth-api/blob/main/app/core/totp.py): `app/core/totp.py`
- [Secret encryption](https://github.com/majestev/mfa-auth-api/blob/main/app/core/crypto.py): `app/core/crypto.py`
- [JWT encoding](https://github.com/majestev/mfa-auth-api/blob/main/app/core/jwt.py): `app/core/jwt.py`
- [Rate limiting](https://github.com/majestev/mfa-auth-api/blob/main/app/core/rate_limit.py): `app/core/rate_limit.py`
- [Error responses](https://github.com/majestev/mfa-auth-api/blob/main/app/core/exceptions.py): `app/core/exceptions.py`
- [Structured logging](https://github.com/majestev/mfa-auth-api/blob/main/app/core/logging.py): `app/core/logging.py`
- [Configuration](https://github.com/majestev/mfa-auth-api/blob/main/app/core/config.py): `app/core/config.py`

### Persistence and state

- [User persistence](https://github.com/majestev/mfa-auth-api/blob/main/app/repositories/user_repository.py): `app/repositories/user_repository.py`
- [MFA persistence](https://github.com/majestev/mfa-auth-api/blob/main/app/repositories/mfa_repository.py): `app/repositories/mfa_repository.py`
- [Audit events](https://github.com/majestev/mfa-auth-api/blob/main/app/repositories/auth_event_repository.py): `app/repositories/auth_event_repository.py`
- [Data models](https://github.com/majestev/mfa-auth-api/blob/main/app/database/models.py): `app/database/models.py`
- **PostgreSQL**
- **Redis state**

### Other components

- **API client**

## Connections

- API client → Security headers: sends requests
- Security headers → Request middleware: passes requests
- Request middleware → API router: forwards requests
- App setup → API router: mounts routes (evidence: `app/main.py`)
- App setup → Security headers: configures (evidence: `app/main.py`)
- App setup → Request middleware: configures (evidence: `app/main.py`)
- API router → Auth routes: routes to (evidence: `app/api/v1/router.py`)
- API router → MFA routes: routes to (evidence: `app/api/v1/router.py`)
- API router → User routes: routes to (evidence: `app/api/v1/router.py`)
- API router → Health route: routes to (evidence: `app/api/v1/router.py`)
- Auth routes → Dependency wiring: uses dependencies (evidence: `app/api/v1/auth.py`)
- MFA routes → Dependency wiring: uses dependencies (evidence: `app/api/v1/mfa.py`)
- User routes → Dependency wiring: uses dependencies (evidence: `app/api/v1/users.py`)
- Health route → Dependency wiring: uses dependencies (evidence: `app/api/v1/health.py`)
- Dependency wiring → Account auth: constructs (evidence: `app/api/deps.py`)
- Dependency wiring → MFA workflows: constructs (evidence: `app/api/deps.py`)
- Dependency wiring → Token sessions: constructs (evidence: `app/api/deps.py`)
- Auth routes → Account auth: handles auth (evidence: `app/api/v1/auth.py`)
- MFA routes → MFA workflows: handles MFA
- User routes → Account auth: changes password
- Account auth → User persistence: reads and writes (evidence: `app/services/auth_service.py`)
- Account auth → Token sessions: issues sessions (evidence: `app/services/auth_service.py`)
- Account auth → Audit events: records events (evidence: `app/services/auth_service.py`)
- MFA workflows → User persistence: loads users (evidence: `app/services/mfa_service.py`)
- MFA workflows → MFA persistence: manages MFA data (evidence: `app/services/mfa_service.py`)
- MFA workflows → Token sessions: issues sessions (evidence: `app/services/mfa_service.py`)
- MFA workflows → Audit events: records events (evidence: `app/services/mfa_service.py`)
- MFA workflows → TOTP and QR: checks codes (evidence: `app/services/mfa_service.py`)
- MFA workflows → Secret encryption: decrypts secrets (evidence: `app/services/mfa_service.py`)
- MFA workflows → Redis state: stores challenges (evidence: `app/services/mfa_service.py`)
- Account auth → Rate limiting: limits attempts (evidence: `app/services/auth_service.py`)
- Account auth → Redis state: stores challenges (evidence: `app/services/auth_service.py`)
- Token sessions → JWT encoding: encodes tokens (evidence: `app/services/token_service.py`)
- Token sessions → Redis state: blacklists tokens (evidence: `app/services/token_service.py`)
- Token sessions → User persistence: resolves users (evidence: `app/services/token_service.py`)
- Token sessions → Audit events: records events (evidence: `app/services/token_service.py`)
- Dependency wiring → User persistence: builds repositories (evidence: `app/api/deps.py`)
- Dependency wiring → MFA persistence: builds repositories (evidence: `app/api/deps.py`)
- Dependency wiring → Audit events: builds repository (evidence: `app/api/deps.py`)
- Dependency wiring → Rate limiting: builds limiter (evidence: `app/api/deps.py`)
- Dependency wiring → Configuration: loads settings (evidence: `app/api/deps.py`)
- User persistence → Data models: queries users (evidence: `app/repositories/user_repository.py`)
- MFA persistence → Data models: queries MFA records (evidence: `app/repositories/mfa_repository.py`)
- Audit events → Data models: writes events (evidence: `app/repositories/auth_event_repository.py`)
- Data models → PostgreSQL: persists records
- Audit events → Structured logging: logs events (evidence: `app/repositories/auth_event_repository.py`)
- Error responses → Request middleware: gets request ID (evidence: `app/core/exceptions.py`)
