INNER CODE UNIT · Python
_require_api_key
simonlin1212/vibe-astock · vr/app.py:53
async def _require_api_key(request: Request, call_next):
if (
_API_KEY
and request.method != "OPTIONS"
and request.url.path.startswith("/api/")
and request.url.path != "/api/health"
):
if request.headers.get("authorization", "") != f"Bearer {_API_KEY}":
return JSONResponse({"detail": "未授权:缺少或错误的 API Key(VR_API_KEY)"}, status_code=401)
return await call_next(request)
_CODE_RE = r"^\d{6}$"
def _validate(code: str) -> str:
code = (code or "").strip()
if not code.isdigit() or len(code) != 6:
raise HTTPException(400, "代码必须是 6 位数字")