# nakacya/llm-gateway-oauth architecture

> Architecture diagram of the GitHub repository nakacya/llm-gateway-oauth, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nakacya/llm-gateway-oauth
- Repository: https://github.com/nakacya/llm-gateway-oauth

## Overview

##### Project type and purpose
- From the file tree and README (“llm-gateway-oauth”), this is an API/Gateway service that sits in front of an LLM endpoint, enforcing OAuth2 authentication and issuing/verifying JWTs.
- It is neither a monolithic full-stack app nor a compiler; it is an authentication proxy/API-gateway with some lightweight management UI.

##### File structure
- Top-level:
- Dockerfile, docker-compose.yml → containerized deployment
- nginx.conf → Nginx reverse proxy + Lua module integration
- lua/*.lua → Lua scripts implementing all OAuth flows, JWT validation, token management, keys
- oauth2_proxy.cfg.sample, litellm_config.yaml.sample, .env_sample → config templates
- admin_manager.html, token_manager.html → static HTML UIs for admins and token viewers
- scripts/ → setup.sh (initial provisioning), test_api.sh (smoke tests)
- No traditional “frontend” or “backend” directories; Nginx+Lua acts as backend logic, HTML files are static UIs.

##### README for additional insights
- Very minimal; implies you must infer architecture from naming conventions and config samples.

a. Main components
1. User clients (e.g., browser or LLM client library)
2. Nginx Gateway Container
– Lua auth_handler.lua, jwt_validator.lua, token_*.lua, key_generate_handler.lua
– Serves admin_manager.html & token_manager.html
3. External OAuth 2.0 Provider (e.g., GitHub, Google, Keycloak)
4. Downstream LLM service or API (could be an OpenAI, self-hosted LLM, etc.)
5. (Optional) Token store or cache (in-memory or external, not explicit here)
6. Configuration & Secrets (env files, litellm_config.yaml, oauth2_proxy.cfg)
7. Docker / Docker-Compose orchestration
b. Relationships & interactions
1. Client → Nginx Gateway: initial request
2. Nginx + Lua → JWT Validator: checks authorization header
3. If no valid JWT → redirect to External OAuth Provider
4. OAuth callback → token_generator.lua issues JWT, stores it
5. JWT returned to Client
6. Client → Nginx with JWT → Nginx forwards request to LLM service
7. Admin client → Nginx serves admin_manager.html → admin uses token_list.lua, token_revoke.lua
c. Architectural patterns & principles
– Reverse-proxy gateway pattern
– Layered security: OAuth2 at edge, JWT for downstream calls
– Modular functions in Lua for each concern (validation, generation, revocation)
– Containerized microservice with externalized config
d. Key technologies
– Nginx + OpenResty (Lua)
– OAuth2 protocol
– JWT (json web tokens)
– Docker & Docker-Compose
– Shell scripts for setup and testing
– Static HTML UIs for management

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Docker Compose Orchestration"
        direction TB
        Gateway["Nginx Gateway Container"]:::compute
        Config["Config & Secrets"]:::config
        Scripts["Setup & Testing Scripts"]:::scripts
    end

    subgraph "Nginx Gateway"
        direction TB
        AuthHandler["Auth Handler (auth_handler.lua, oauth_check.lua)"]:::compute
        JWTValidator["JWT Validator (jwt_validator.lua)"]:::compute
        TokenGen["Token Generator (token_generator.lua, key_generate_handler.lua)"]:::compute
        TokenMgmt["Token Management (token_list.lua, token_info.lua, token_revoke.lua)"]:::compute
        AdminAuth["Admin Access Control (admin_check.lua, admin_manager.lua)"]:::compute
        UIAdmin["Admin Manager UI (admin_manager.html)"]:::static
        UIToken["Token Manager UI (token_manager.html)"]:::static
    end

    Client["User Client"]:::external
    AdminClient["Admin Client"]:::external
    OAuthProvider["External OAuth2 Provider"]:::external
    LLMService["Downstream LLM Service"]:::external

    Client -->|"request"| Gateway
    Gateway --> AuthHandler
    AuthHandler --> JWTValidator
    JWTValidator -->|"valid JWT"| Gateway
    JWTValidator -->|"invalid JWT"| OAuthProvider
    OAuthProvider -->|"OAuth Callback"| TokenGen
    TokenGen --> Gateway
    Gateway -->|"forward request"| LLMService

    AdminClient --> UIAdmin
    UIAdmin --> AdminAuth
    AdminAuth --> TokenMgmt
    TokenMgmt --> Gateway

    Gateway --> Config
    Gateway --> Scripts

    click Gateway "https://github.com/nakacya/llm-gateway-oauth/tree/main/Dockerfile"
    click Gateway "https://github.com/nakacya/llm-gateway-oauth/blob/main/docker-compose.yml"
    click Gateway "https://github.com/nakacya/llm-gateway-oauth/blob/main/nginx.conf"

    click AuthHandler "https://github.com/nakacya/llm-gateway-oauth/blob/main/lua/auth_handler.lua"
    click AuthHandler "https://github.com/nakacya/llm-gateway-oauth/blob/main/lua/oauth_check.lua"

    click JWTValidator "https://github.com/nakacya/llm-gateway-oauth/blob/main/lua/jwt_validator.lua"

    click TokenGen "https://github.com/nakacya/llm-gateway-oauth/blob/main/lua/token_generator.lua"
    click TokenGen "https://github.com/nakacya/llm-gateway-oauth/blob/main/lua/key_generate_handler.lua"

    click TokenMgmt "https://github.com/nakacya/llm-gateway-oauth/blob/main/lua/token_list.lua"
    click TokenMgmt "https://github.com/nakacya/llm-gateway-oauth/blob/main/lua/token_info.lua"
    click TokenMgmt "https://github.com/nakacya/llm-gateway-oauth/blob/main/lua/token_revoke.lua"

    click AdminAuth "https://github.com/nakacya/llm-gateway-oauth/blob/main/lua/admin_check.lua"
    click AdminAuth "https://github.com/nakacya/llm-gateway-oauth/blob/main/lua/admin_manager.lua"

    click UIAdmin "https://github.com/nakacya/llm-gateway-oauth/blob/main/admin_manager.html"
    click UIToken "https://github.com/nakacya/llm-gateway-oauth/blob/main/token_manager.html"

    click Config "https://github.com/nakacya/llm-gateway-oauth/blob/main/.env_sample"
    click Config "https://github.com/nakacya/llm-gateway-oauth/blob/main/litellm_config.yaml.sample"
    click Config "https://github.com/nakacya/llm-gateway-oauth/blob/main/oauth2_proxy.cfg.sample"

    click Scripts "https://github.com/nakacya/llm-gateway-oauth/blob/main/scripts/setup.sh"
    click Scripts "https://github.com/nakacya/llm-gateway-oauth/blob/main/scripts/test_api.sh"

    classDef compute fill:#ADD8E6,stroke:#000,color:#000
    classDef external fill:#90EE90,stroke:#000,color:#000
    classDef config fill:#D3D3D3,stroke:#000,color:#000
    classDef static fill:#D3D3D3,stroke:#000,color:#000
    classDef scripts fill:#F5DEB3,stroke:#000,color:#000
```
