File size: 980 Bytes
e63c592
b09b8a3
e63c592
b09b8a3
e63c592
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b09b8a3
 
e63c592
b09b8a3
e63c592
 
 
 
 
 
 
 
 
b09b8a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
from typing import List

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from app.core.logging import get_logger

logger = get_logger(__name__)


def _get_allowed_origins() -> List[str]:
    raw = os.getenv("ALLOWED_ORIGINS")
    if not raw:
        # Default: permissive for local development and simple frontends.
        origins = ["*"]
    else:
        origins = [item.strip() for item in raw.split(",") if item.strip()]
        if not origins:
            origins = ["*"]
    return origins


def configure_security(app: FastAPI) -> None:
    """Configure CORS on the FastAPI app.

    API key enforcement is handled via dependencies in app.core.auth.
    """
    origins = _get_allowed_origins()
    app.add_middleware(
        CORSMiddleware,
        allow_origins=origins,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    logger.info("CORS configured allow_origins=%s", origins)