feat: Implement multi-tenancy support in middleware and security layers
- Enhanced TenantMiddleware to validate tenant information from JWT tokens. - Added LicenseValidationMiddleware to check tenant licenses before processing requests. - Updated security utilities to extract tenant information from tokens and validate company access. - Introduced CompanyStore to manage active company state and handle company switching in the frontend. - Modified API routes to include company_id in requests for better resource management. - Improved logging and error handling throughout the middleware and API layers. - Updated frontend components to reflect changes in company management and selection. - Added new API route for fetching user's companies with proper authentication handling.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
Anexo76 - Aplicación SaaS para gestión de comercio exterior
|
||||
Backend API con FastAPI + Keycloak + SQLAlchemy
|
||||
"""
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
import logging
|
||||
@@ -10,7 +11,7 @@ from core.config import settings
|
||||
from core.middleware import (
|
||||
TenantMiddleware,
|
||||
LicenseValidationMiddleware,
|
||||
RequestLoggingMiddleware
|
||||
RequestLoggingMiddleware,
|
||||
)
|
||||
from core.database import init_db
|
||||
from api.v1.router import router as api_v1_router
|
||||
@@ -18,7 +19,7 @@ from api.v1.router import router as api_v1_router
|
||||
# Configurar logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO if not settings.DEBUG else logging.DEBUG,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -33,6 +34,7 @@ app = FastAPI(
|
||||
openapi_url="/api/openapi.json" if settings.DEBUG else None,
|
||||
)
|
||||
|
||||
|
||||
# Inicializar la base de datos
|
||||
@app.on_event("startup")
|
||||
async def on_startup():
|
||||
@@ -41,6 +43,7 @@ async def on_startup():
|
||||
init_db()
|
||||
logger.info("Base de datos inicializada correctamente.")
|
||||
|
||||
|
||||
# Configurar CORS
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
@@ -60,6 +63,7 @@ app.add_middleware(TenantMiddleware)
|
||||
# Registrar routers
|
||||
app.include_router(api_v1_router, prefix="/api/v1")
|
||||
|
||||
|
||||
@app.get("/api/")
|
||||
async def root():
|
||||
"""Root endpoint"""
|
||||
@@ -67,14 +71,11 @@ async def root():
|
||||
"name": "Anexo76 API",
|
||||
"version": settings.APP_VERSION,
|
||||
"status": "running",
|
||||
"docs": "/api/docs" if settings.DEBUG else "disabled in production"
|
||||
"docs": "/api/docs" if settings.DEBUG else "disabled in production",
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/health")
|
||||
async def health_check():
|
||||
"""Health check endpoint"""
|
||||
return {
|
||||
"status": "healthy",
|
||||
"environment": settings.ENVIRONMENT
|
||||
}
|
||||
return {"status": "healthy", "environment": settings.ENVIRONMENT}
|
||||
|
||||
Reference in New Issue
Block a user