Skip to content

Commit d652b54

Browse files
committed
feat: security api key
1 parent 9b682fa commit d652b54

5 files changed

Lines changed: 32 additions & 5 deletions

File tree

.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ API_VERSION=0.1.0
1818

1919
# CRITICAL, FATAL, ERROR, WARN, WARNING, INFO, DEBUG, NOTSET
2020
LOG_LEVEL=DEBUG
21-
LOG_FORMAT=json
21+
LOG_FORMAT=json
22+
23+
INTERNAL_API_KEY=xxx
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
API dependencies package.
3+
"""
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Authentication dependencies for API routes.
3+
"""
4+
from fastapi import HTTPException, Security, status
5+
from fastapi.security import APIKeyHeader
6+
from ...config.settings import settings
7+
8+
9+
api_key_header = APIKeyHeader(name="X-Internal-API-Key", auto_error=False)
10+
11+
12+
async def verify_api_key(api_key: str = Security(api_key_header)) -> None:
13+
"""
14+
Verify internal API key sent in request headers.
15+
"""
16+
if not api_key or api_key != settings.INTERNAL_API_KEY:
17+
raise HTTPException(
18+
status_code=status.HTTP_401_UNAUTHORIZED,
19+
detail="Invalid or missing API key"
20+
)

src/sum_impact_assessment/api/main.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
FastAPI application instance and configuration.
33
"""
44
import time
5-
from fastapi import FastAPI, Request
5+
from fastapi import Depends, FastAPI, Request
66
from fastapi.middleware.cors import CORSMiddleware
77
from .routes import kpis, jobs, simulation, health
8+
from .dependencies.auth import verify_api_key
89
from ..config.settings import settings
910
from ..utils.logger import get_logger
1011
from ..database.connection import Base, engine
@@ -74,9 +75,9 @@ async def log_requests(request: Request, call_next):
7475

7576
# Include routers
7677
app.include_router(health.router, tags=["Health"])
77-
app.include_router(kpis.router, tags=["KPIs"])
78-
app.include_router(jobs.router, tags=["Jobs"])
79-
app.include_router(simulation.router, tags=["Simulation (NON-prod only)⚠️"])
78+
app.include_router(kpis.router, tags=["KPIs"], dependencies=[Depends(verify_api_key)])
79+
app.include_router(jobs.router, tags=["Jobs"], dependencies=[Depends(verify_api_key)])
80+
app.include_router(simulation.router, tags=["Simulation (NON-prod only)⚠️"], dependencies=[Depends(verify_api_key)])
8081

8182

8283
@app.on_event("startup")

src/sum_impact_assessment/config/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Settings(BaseSettings):
2222
API_PORT: int = 8000
2323
API_TITLE: str = "SUM Impact Assessment API"
2424
API_VERSION: str = __version__
25+
INTERNAL_API_KEY: str = ""
2526

2627
# Application configuration
2728
LOG_LEVEL: str = "INFO"

0 commit comments

Comments
 (0)