Skip to content

Commit e1caa35

Browse files
committed
refactor(auth): complete migration, remove legacy verify_api_key
- Migrate remaining routes: /api/metrics, /api/keys/generate, /api/keys/usage - Remove legacy verify_api_key() function - Clean up unused Header import - All routes now use unified auth middleware Part of #12
1 parent df9ccf8 commit e1caa35

1 file changed

Lines changed: 8 additions & 43 deletions

File tree

backend/main.py

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
CodeIntel Backend API
33
FastAPI backend for codebase intelligence
44
"""
5-
from fastapi import FastAPI, HTTPException, Header, WebSocket, WebSocketDisconnect, Depends
5+
from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect, Depends
66
from fastapi.middleware.cors import CORSMiddleware
77
from pydantic import BaseModel
88
from typing import Optional, List
@@ -83,34 +83,6 @@ async def dispatch(self, request: Request, call_next):
8383
api_key_manager = APIKeyManager(get_supabase_service().client)
8484
cost_controller = CostController(get_supabase_service().client)
8585

86-
# Development API Key (for local testing only)
87-
DEV_API_KEY = os.getenv("API_KEY", "dev-secret-key")
88-
89-
90-
def verify_api_key(authorization: str = Header(None)):
91-
"""Verify API key and check rate limits"""
92-
if not authorization or not authorization.startswith("Bearer "):
93-
raise HTTPException(status_code=401, detail="Invalid authorization header")
94-
95-
token = authorization.replace("Bearer ", "")
96-
97-
# Allow dev key for local development
98-
if token == DEV_API_KEY and os.getenv("DEBUG", "false").lower() == "true":
99-
return {"key": token, "tier": "enterprise", "user_id": None, "name": "Development"}
100-
101-
# Verify production API key
102-
key_data = api_key_manager.verify_key(token)
103-
if not key_data:
104-
raise HTTPException(status_code=401, detail="Invalid API key")
105-
106-
# Check rate limits
107-
allowed, error_msg = rate_limiter.check_rate_limit(token, key_data.get("tier", "free"))
108-
if not allowed:
109-
raise HTTPException(status_code=429, detail=error_msg)
110-
111-
return key_data
112-
113-
11486
# Request/Response Models
11587
class SearchRequest(BaseModel):
11688
query: str
@@ -542,11 +514,9 @@ async def get_style_analysis(
542514

543515
@app.get("/api/metrics")
544516
async def get_performance_metrics(
545-
api_key: str = Header(None, alias="Authorization")
517+
auth: AuthContext = Depends(require_auth)
546518
):
547519
"""Get performance metrics and monitoring data"""
548-
verify_api_key(api_key)
549-
550520
return metrics.get_metrics()
551521

552522

@@ -560,16 +530,14 @@ class CreateAPIKeyRequest(BaseModel):
560530
@app.post("/api/keys/generate")
561531
async def generate_api_key(
562532
request: CreateAPIKeyRequest,
563-
api_key: str = Header(None, alias="Authorization")
533+
auth: AuthContext = Depends(require_auth)
564534
):
565535
"""Generate a new API key (requires existing valid key or dev mode)"""
566-
key_data = verify_api_key(api_key)
567-
568536
# Generate new key
569537
new_key = api_key_manager.generate_key(
570538
name=request.name,
571539
tier=request.tier,
572-
user_id=key_data.get("user_id")
540+
user_id=auth.user_id
573541
)
574542

575543
return {
@@ -582,21 +550,18 @@ async def generate_api_key(
582550

583551
@app.get("/api/keys/usage")
584552
async def get_api_usage(
585-
api_key: str = Header(None, alias="Authorization")
553+
auth: AuthContext = Depends(require_auth)
586554
):
587555
"""Get current API usage stats"""
588-
key_data = verify_api_key(api_key)
589-
token = api_key.replace("Bearer ", "")
590-
591-
usage = rate_limiter.get_usage(token)
556+
usage = rate_limiter.get_usage(auth.identifier)
592557

593558
return {
594-
"tier": key_data.get("tier", "free"),
559+
"tier": auth.tier,
595560
"limits": {
596561
"free": {"minute": 20, "hour": 200, "day": 1000},
597562
"pro": {"minute": 100, "hour": 2000, "day": 20000},
598563
"enterprise": {"minute": 500, "hour": 10000, "day": 100000}
599-
}[key_data.get("tier", "free")],
564+
}[auth.tier],
600565
"usage": usage
601566
}
602567

0 commit comments

Comments
 (0)