New token arch

This commit is contained in:
Urtzi Alfaro
2026-01-10 21:45:37 +01:00
parent cc53037552
commit bf1db7cb9e
26 changed files with 1751 additions and 107 deletions

View File

@@ -38,6 +38,7 @@ The API Gateway serves as the **centralized entry point** for all client request
2. **Token Refresh** - Automatic refresh token handling
3. **User Context Injection** - Attaches user and tenant information to requests
4. **Demo Account Detection** - Identifies and isolates demo sessions
5. **Subscription Data Extraction** - Extracts subscription tier from JWT payload (eliminates per-request HTTP calls)
### Request Processing Pipeline
```
@@ -82,6 +83,21 @@ Client Response
- **Real-Time Alerts** - Instant notifications for low stock, quality issues, and production problems
- **Secure Access** - Enterprise-grade security protects sensitive business data
- **Reliable Performance** - Rate limiting and caching ensure consistent response times
- **Faster Response Times** - JWT-embedded subscription data eliminates 520ms overhead per request
### Performance Impact
**Before JWT Subscription Embedding:**
- 5 synchronous HTTP calls per request to tenant-service
- 2,500ms notification endpoint latency
- 5,500ms subscription endpoint latency
- ~520ms overhead on EVERY tenant-scoped request
**After JWT Subscription Embedding:**
- **Zero HTTP calls** for subscription validation
- **<1ms subscription check latency** (JWT extraction only)
- **~200ms notification endpoint latency** (92% improvement)
- **~100ms subscription endpoint latency** (98% improvement)
- **100% reduction in tenant-service load** for subscription checks
### For Platform Operations
- **Cost Efficiency** - Caching reduces backend load by 60-70%
@@ -99,12 +115,59 @@ Client Response
- **Framework**: FastAPI (Python 3.11+) - Async web framework with automatic OpenAPI docs
- **HTTP Client**: HTTPx - Async HTTP client for service-to-service communication
- **Caching**: Redis 7.4 - Token cache, SSE pub/sub, rate limiting
- **Caching**: Redis 7.4 - Token cache, SSE pub/sub, rate limiting, token freshness tracking
- **Logging**: Structlog - Structured JSON logging for observability
- **Metrics**: Prometheus Client - Custom metrics for monitoring
- **Authentication**: JWT (JSON Web Tokens) - Token-based authentication
- **Authentication**: JWT (JSON Web Tokens) - Token-based authentication with embedded subscription data
- **WebSockets**: FastAPI WebSocket support - Real-time training updates
## JWT Subscription Architecture
### Overview
The gateway implements a **JWT-embedded subscription data** architecture that eliminates runtime HTTP calls to the tenant-service for subscription validation. This provides significant performance improvements while maintaining security.
### JWT Payload Structure
```json
{
"user_id": "uuid",
"email": "user@example.com",
"tenant_id": "uuid",
"tenant_role": "owner",
"subscription": {
"tier": "professional",
"status": "active",
"valid_until": "2025-12-31T23:59:59Z"
},
"tenant_access": [
{"id": "tenant-uuid", "role": "admin", "tier": "starter"}
],
"exp": 1735689599,
"iat": 1735687799,
"iss": "bakery-auth"
}
```
### Security Layers
The architecture implements **defense-in-depth** with multiple validation layers:
1. **Layer 1: JWT Signature Verification** - Gateway validates JWT signature
2. **Layer 2: Subscription Data Extraction** - Extracts subscription from verified JWT
3. **Layer 3: Token Freshness Check** - Detects stale tokens after subscription changes
4. **Layer 4: Database Verification** - For critical operations (optional)
5. **Layer 5: Audit Logging** - Comprehensive logging for anomaly detection
### Token Freshness Mechanism
- When subscription changes, gateway sets `tenant:{tenant_id}:subscription_changed_at` in Redis
- Gateway checks if token was issued before subscription change
- Stale tokens are rejected, forcing re-authentication
- Ensures users get fresh subscription data within token expiry window (15-30 min)
### Multi-Tenant Support
- JWT contains `tenant_access` array with all accessible tenants
- Each tenant entry includes role and subscription tier
- Gateway validates access to requested tenant
- Supports hierarchical tenant access patterns
## API Endpoints (Key Routes)
### Authentication Routes
@@ -163,6 +226,8 @@ All routes under `/api/v1/` are protected by JWT authentication:
- Token validation with cached results
- User/tenant context injection
- Demo account detection
- **Subscription tier extraction from JWT** - Eliminates 5 synchronous HTTP calls per request to tenant-service
- **Token freshness verification** - Detects stale tokens after subscription changes
### 5. Rate Limiting Middleware
- Token bucket algorithm
@@ -170,9 +235,11 @@ All routes under `/api/v1/` are protected by JWT authentication:
- 429 Too Many Requests response on limit exceeded
### 6. Subscription Middleware
- Validates tenant subscription status
- **JWT-based subscription validation** - Uses subscription data embedded in JWT tokens
- **Zero HTTP calls for subscription checks** - Subscription tier extracted from verified JWT
- Checks subscription expiry
- Allows grace period for expired subscriptions
- **Defense-in-depth verification** - Database verification for critical operations
### 7. Read-Only Middleware
- Enforces tenant-level write restrictions