Skip to content

Commit

Permalink
feat: add bot token usage analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
RaoHai committed Dec 24, 2024
1 parent e98d628 commit 5bca5ac
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions server/auth/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,37 @@ async def oauth(self, request: Request):
return False

async def dispatch(self, request: Request, call_next: Callable[[Request], Awaitable[Response]]) -> Response:

if ENVIRONMENT == "development":
return await call_next(request)
try:
if ENVIRONMENT == "development":
return await call_next(request)

# Auth 相关的直接放过
if request.url.path.startswith("/api/auth"):
return await call_next(request)
# Auth 相关的直接放过
if request.url.path.startswith("/api/auth"):
return await call_next(request)

if request.url.path in ALLOW_LIST:
return await call_next(request)
if request.url.path in ALLOW_LIST:
return await call_next(request)

if await self.oauth(request=request):
return await call_next(request)
if await self.oauth(request=request):
return await call_next(request)

# 获取 session 中的用户信息
user = request.session.get("user")
if not user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized")
# 获取 session 中的用户信息
user = request.session.get("user")
if not user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized")

if user['sub'].startswith("client|"):
if request.url.path in ANONYMOUS_USER_ALLOW_LIST:
return await call_next(request)
else:
# 如果没有用户信息,返回 401 Unauthorized 错误
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Anonymous User Not Allow")

return await call_next(request)
# except HTTPException as e:
# print(traceback.format_exception(e))
# # 处理 HTTP 异常
# return JSONResponse(status_code=e.status_code, content={"detail": e.detail})
# except Exception as e:
# # 处理其他异常
# return JSONResponse(status_code=500, content={"detail": f"Internal Server Error: {e}"})
if user['sub'].startswith("client|"):
if request.url.path in ANONYMOUS_USER_ALLOW_LIST:
return await call_next(request)
else:
# 如果没有用户信息,返回 401 Unauthorized 错误
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Anonymous User Not Allow")

return await call_next(request)
except HTTPException as e:
print(traceback.format_exception(e))
# 处理 HTTP 异常
return JSONResponse(status_code=e.status_code, content={"detail": e.detail})
except Exception as e:
# 处理其他异常
return JSONResponse(status_code=500, content={"detail": f"Internal Server Error: {e}"})

0 comments on commit 5bca5ac

Please sign in to comment.