添加跨域请求

This commit is contained in:
龙澳
2026-03-23 15:41:37 +08:00
parent f21ca3b085
commit f92822952d
2 changed files with 14 additions and 3 deletions

View File

@@ -1,7 +1,7 @@
AUTH_DB_PATH=~/.nanobot/auth_service.sqlite3
AUTH_JWT_SECRET=change-this-secret
AUTH_TOKEN_TTL_HOURS=24
AUTH_CORS_ORIGINS=*
AUTH_CORS_ORIGINS=http://127.0.0.1:5173,http://localhost:5173,http://47.122.113.65:5173
AUTH_VERIFICATION_CODES=code-a,code-b
AUTH_ADMIN_KEY=change-this-admin-key
AUTH_HOST=0.0.0.0

View File

@@ -35,6 +35,16 @@ AUTH_VERIFICATION_CODES = {
AUTH_ADMIN_KEY = os.getenv("AUTH_ADMIN_KEY", "")
def _parse_cors_origins(raw: str) -> tuple[bool, list[str]]:
value = str(raw or "").strip()
if not value:
return True, ["*"]
items = [o.strip() for o in value.split(",") if o.strip()]
if not items or "*" in items:
return True, ["*"]
return False, items
def _ensure_db(path_str: str) -> Path:
path = Path(path_str).expanduser()
path.parent.mkdir(parents=True, exist_ok=True)
@@ -268,11 +278,12 @@ def _reject_request_with_conn(conn: sqlite3.Connection, request_id: int, note: s
app = FastAPI(title="nanobot-auth-service", version="0.1.0")
origins = ["*"] if AUTH_CORS_ORIGINS.strip() == "*" else [o.strip() for o in AUTH_CORS_ORIGINS.split(",") if o.strip()]
allow_all_origins, origins = _parse_cors_origins(AUTH_CORS_ORIGINS)
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
# Browsers reject credentialed CORS with wildcard origin.
allow_credentials=not allow_all_origins,
allow_methods=["*"],
allow_headers=["*"],
)