Only getnetworkgeometries needs auth
This commit is contained in:
29
main.py
29
main.py
@@ -63,10 +63,31 @@ async def global_auth(request: Request):
|
|||||||
if token != "Bearer 567e33c876a2" and token != "Bearer 38b3be72b8af":
|
if token != "Bearer 567e33c876a2" and token != "Bearer 38b3be72b8af":
|
||||||
raise HTTPException(status_code=401, detail="Invalid token")
|
raise HTTPException(status_code=401, detail="Invalid token")
|
||||||
|
|
||||||
# 全局依赖项
|
# 简易令牌验证(实际项目中应替换为 JWT/OAuth2 等)
|
||||||
app = FastAPI(dependencies=[Depends(global_auth)])
|
AUTH_TOKEN = "567e33c876a2" # 预设的有效令牌
|
||||||
|
|
||||||
# app = FastAPI()
|
async def verify_token(authorization: Annotated[str, Header()] = None):
|
||||||
|
# 检查请求头是否存在
|
||||||
|
if not authorization:
|
||||||
|
raise HTTPException(status_code=401, detail="Authorization header missing")
|
||||||
|
|
||||||
|
# 提取 Bearer 后的令牌 (格式: Bearer <token>)
|
||||||
|
try:
|
||||||
|
token_type, token = authorization.split(" ", 1)
|
||||||
|
if token_type.lower() != "bearer":
|
||||||
|
raise ValueError
|
||||||
|
except ValueError:
|
||||||
|
raise HTTPException(status_code=401, detail="Invalid authorization format. Use: Bearer <token>")
|
||||||
|
|
||||||
|
# 验证令牌
|
||||||
|
if token != AUTH_TOKEN:
|
||||||
|
raise HTTPException(status_code=403, detail="Invalid authentication token")
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
# 全局依赖项
|
||||||
|
# app = FastAPI(dependencies=[Depends(global_auth)])
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
access_tokens = []
|
access_tokens = []
|
||||||
|
|
||||||
@@ -1771,7 +1792,7 @@ async def fastapi_get_node_coord(network: str, node: str) -> dict[str, float] |
|
|||||||
# links: id:type:node1:node2
|
# links: id:type:node1:node2
|
||||||
# node type: junction, reservoir, tank
|
# node type: junction, reservoir, tank
|
||||||
# link type: pipe, pump, valve
|
# link type: pipe, pump, valve
|
||||||
@app.get("/getnetworkgeometries/")
|
@app.get("/getnetworkgeometries/", dependencies=[Depends(verify_token)])
|
||||||
async def fastapi_get_network_geometries(network: str) -> dict[str, Any] | None:
|
async def fastapi_get_network_geometries(network: str) -> dict[str, Any] | None:
|
||||||
|
|
||||||
# 获取所有节点坐标# 缓存查询结果提高性能
|
# 获取所有节点坐标# 缓存查询结果提高性能
|
||||||
|
|||||||
Reference in New Issue
Block a user