# indominusbyte/fastapi-jwt-auth architecture

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

- Interactive diagram: https://gitdiagram.com/indominusbyte/fastapi-jwt-auth
- Repository: https://github.com/indominusbyte/fastapi-jwt-auth

## Overview

##### Main Components

a) Core Authentication Components:
- JWT Token Manager
- Configuration Manager
- Exception Handler
- Token Validator/Decoder

b) Integration Components:
- FastAPI Integration Layer
- WebSocket Support
- Cookie Management
- CSRF Protection

**┌─────────────────────────────────────────────┐**
- Client Application              │
- ┬─────────────────────────┘
- ↓
**┌─────────────────────────────────────────────┐**
- FastAPI Application             │
- ┬─────────────────────────┘
- ↓
**┌─────────────────────────────────────────────┐**
- FastAPI JWT Auth Layer            │
- ┬─────────────┬────────────────┤
- Auth JWT   │   Config    │  Exceptions    │
- ┴─────────────┴────────────────┘

##### Key Relationships to Highlight

a) Token Flow:
- Both symmetric and asymmetric token handling paths
- Denylist/revocation flow

b) Integration Points:
- How the library interfaces with FastAPI's dependency injection system
- WebSocket authentication flow
- Demonstrate cookie-based token storage mechanism

##### Detailed Component Breakdown

**┌─────────────────────────────────────────────────────────┐**
- AuthJWT Class                       │
- ┤
- create_access_token()                                 │
- create_refresh_token()                                │
- get_jwt()                                            │
- get_raw_jwt()                                        │
- jwt_required()                                        │
- jwt_optional()                                        │
- fresh_jwt_required()                                  │
- jwt_refresh_token_required()                          │
- ┘

##### Special Features

- Token Freshness System
- Revocation Mechanism
- Custom Claims Support
- CSRF Protection Flow
- WebSocket Authentication

##### Implementation Details

- Configuration flow from auth_config.py
- Exception handling pattern from exceptions.py
- Token validation middleware
- Integration with FastAPI's security system

##### External Dependencies

Connections to:
- PyJWT library
- Redis (for denylist implementation)
- FastAPI security framework

##### Notes for Implementation

- Use standard UML notation where appropriate
- Notes for important configuration options
- Both synchronous and WebSocket paths

Remember to include version compatibility information and note any critical security considerations in supplementary documentation.

This structure will provide a clear understanding of how the library integrates with FastAPI while maintaining security and providing flexibility for different authentication scenarios.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        CA[Client Application]
    end

    subgraph FastAPI
        FA[FastAPI Application]:::fastapi
        click FA "https://github.com/IndominusByte/fastapi-jwt-auth/blob/master/examples/multiple_files/app.py"
        WS[WebSocket Support]:::websocket
        click WS "https://github.com/IndominusByte/fastapi-jwt-auth/blob/master/examples/websocket.py"
    end

    subgraph "FastAPI JWT Auth"
        TM[JWT Token Manager]:::core
        click TM "https://github.com/IndominusByte/fastapi-jwt-auth/blob/master/fastapi_jwt_auth/auth_jwt.py"
        
        subgraph "Configuration"
            CM[Configuration Manager]:::config
            click CM "https://github.com/IndominusByte/fastapi-jwt-auth/blob/master/fastapi_jwt_auth/auth_config.py"
        end
        
        subgraph "Token Handling"
            TV[Token Validator]:::token
            click TV "https://github.com/IndominusByte/fastapi-jwt-auth/blob/master/tests/test_decode_token.py"
            TC[Token Creator]:::token
            click TC "https://github.com/IndominusByte/fastapi-jwt-auth/blob/master/tests/test_create_token.py"
            AT[Asymmetric Tokens]:::token
            click AT "https://github.com/IndominusByte/fastapi-jwt-auth/blob/master/examples/asymmetric.py"
        end
        
        subgraph "Security Features"
            DL[Denylist System]:::security
            click DL "https://github.com/IndominusByte/fastapi-jwt-auth/blob/master/examples/denylist.py"
            CSRF[CSRF Protection]:::security
            click CSRF "https://github.com/IndominusByte/fastapi-jwt-auth/blob/master/examples/csrf_protection_cookies.py"
            CK[Cookie Management]:::security
            click CK "https://github.com/IndominusByte/fastapi-jwt-auth/blob/master/fastapi_jwt_auth/auth_jwt.py"
        end
        
        EH[Exception Handler]:::error
        click EH "https://github.com/IndominusByte/fastapi-jwt-auth/blob/master/fastapi_jwt_auth/exceptions.py"
    end

    %% Relationships
    CA --> FA
    FA --> TM
    FA --> WS
    
    TM --> TV
    TM --> TC
    TM --> AT
    
    TM --> DL
    TM --> CSRF
    TM --> CK
    
    CM --> TM
    TV --> EH
    TC --> EH
    
    %% Styles
    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef config fill:#4CAF50,stroke:#2E7D32,color:white
    classDef token fill:#FF9800,stroke:#EF6C00,color:white
    classDef security fill:#FFC107,stroke:#FFA000,color:black
    classDef error fill:#F44336,stroke:#C62828,color:white
    classDef fastapi fill:#9C27B0,stroke:#6A1B9A,color:white
    classDef websocket fill:#00BCD4,stroke:#00838F,color:white

    %% Legend
    subgraph Legend
        L1[Core Components]:::core
        L2[Configuration]:::config
        L3[Token Operations]:::token
        L4[Security Features]:::security
        L5[Error Handling]:::error
        L6[FastAPI Integration]:::fastapi
        L7[WebSocket Support]:::websocket
    end
```
