API Key Config ist fertig

Dieser Commit ist enthalten in:
2025-06-22 12:03:49 +02:00
Ursprung b420452551
Commit 1b5b7d0381
7 geänderte Dateien mit 398 neuen und 40 gelöschten Zeilen

Datei anzeigen

@@ -20,6 +20,7 @@ from app.schemas.license import (
)
from app.core.security import get_api_key
from app.core.config import settings
from app.core.api_key_auth import validate_api_key
router = APIRouter()
@@ -27,7 +28,7 @@ router = APIRouter()
async def activate_license(
request: LicenseActivationRequest,
db: Session = Depends(get_db),
api_key = Depends(get_api_key)
api_key: str = Depends(validate_api_key)
):
license = db.query(License).filter(
License.license_key == request.license_key,
@@ -106,7 +107,7 @@ async def activate_license(
async def verify_license(
request: LicenseVerificationRequest,
db: Session = Depends(get_db),
api_key = Depends(get_api_key)
api_key: str = Depends(validate_api_key)
):
activation = db.query(Activation).filter(
Activation.id == request.activation_id,
@@ -180,7 +181,7 @@ async def verify_license(
async def get_license_info(
license_key: str,
db: Session = Depends(get_db),
api_key = Depends(get_api_key)
api_key: str = Depends(validate_api_key)
):
license = db.query(License).filter(
License.license_key == license_key
@@ -219,7 +220,7 @@ async def get_license_info(
async def start_session(
request: SessionStartRequest,
db: Session = Depends(get_db),
api_key = Depends(get_api_key)
api_key: str = Depends(validate_api_key)
):
# Verify API key matches client config
from sqlalchemy import text
@@ -320,7 +321,7 @@ async def start_session(
async def session_heartbeat(
request: SessionHeartbeatRequest,
db: Session = Depends(get_db),
api_key = Depends(get_api_key)
api_key: str = Depends(validate_api_key)
):
# Update heartbeat
result = db.execute(
@@ -350,7 +351,7 @@ async def session_heartbeat(
async def end_session(
request: SessionEndRequest,
db: Session = Depends(get_db),
api_key = Depends(get_api_key)
api_key: str = Depends(validate_api_key)
):
# Get session info before deleting
session_info = db.execute(

Datei anzeigen

@@ -0,0 +1,45 @@
from fastapi import HTTPException, Request, Depends
from sqlalchemy.orm import Session
from datetime import datetime
import logging
from app.db.database import get_db
logger = logging.getLogger(__name__)
async def validate_api_key(request: Request, db: Session = Depends(get_db)):
"""Validate API key from X-API-Key header against system_api_key table"""
api_key = request.headers.get("X-API-Key")
if not api_key:
logger.warning("API request without API key")
raise HTTPException(
status_code=401,
detail="API key required",
headers={"WWW-Authenticate": "ApiKey"}
)
# Query the system API key
cursor = db.execute(
"SELECT api_key FROM system_api_key WHERE id = 1"
)
result = cursor.fetchone()
if not result or result[0] != api_key:
logger.warning(f"Invalid API key attempt: {api_key[:8]}...")
raise HTTPException(
status_code=401,
detail="Invalid API key"
)
# Update usage statistics
db.execute("""
UPDATE system_api_key
SET last_used_at = CURRENT_TIMESTAMP,
usage_count = usage_count + 1
WHERE id = 1
""")
db.commit()
return api_key