feat(security): configure cors
Browse files- app/main.py +6 -3
app/main.py
CHANGED
@@ -18,6 +18,9 @@ logger = get_logger("main")
|
|
18 |
API_KEY = os.getenv("API_KEY")
|
19 |
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=True)
|
20 |
|
|
|
|
|
|
|
21 |
|
22 |
async def get_api_key(api_key_header: str = Security(api_key_header)):
|
23 |
if not API_KEY:
|
@@ -38,8 +41,8 @@ app = FastAPI(
|
|
38 |
|
39 |
app.add_middleware(
|
40 |
CORSMiddleware,
|
41 |
-
allow_origins=["*"
|
42 |
-
allow_credentials=
|
43 |
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"],
|
44 |
allow_headers=["*"],
|
45 |
expose_headers=[
|
@@ -59,7 +62,7 @@ async def handle_redirects(request: Request, call_next):
|
|
59 |
"""Ensure CORS headers are in redirect responses and force https in the 'Location' header."""
|
60 |
response = await call_next(request)
|
61 |
|
62 |
-
response.headers["Access-Control-Allow-Origin"] =
|
63 |
response.headers["Access-Control-Allow-Methods"] = (
|
64 |
"GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH"
|
65 |
)
|
|
|
18 |
API_KEY = os.getenv("API_KEY")
|
19 |
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=True)
|
20 |
|
21 |
+
# Add this near the top with other environment variables
|
22 |
+
FRONTEND_URL = os.getenv("FRONTEND_URL", "http://localhost:3000") # Add default for local development
|
23 |
+
|
24 |
|
25 |
async def get_api_key(api_key_header: str = Security(api_key_header)):
|
26 |
if not API_KEY:
|
|
|
41 |
|
42 |
app.add_middleware(
|
43 |
CORSMiddleware,
|
44 |
+
allow_origins=[FRONTEND_URL], # Replace "*" with specific frontend URL
|
45 |
+
allow_credentials=True, # Changed to True since we're restricting origins
|
46 |
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"],
|
47 |
allow_headers=["*"],
|
48 |
expose_headers=[
|
|
|
62 |
"""Ensure CORS headers are in redirect responses and force https in the 'Location' header."""
|
63 |
response = await call_next(request)
|
64 |
|
65 |
+
response.headers["Access-Control-Allow-Origin"] = FRONTEND_URL
|
66 |
response.headers["Access-Control-Allow-Methods"] = (
|
67 |
"GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH"
|
68 |
)
|