diff --git a/server/auth/get_user_info.py b/server/auth/get_user_info.py index b0171ddc..cd920036 100644 --- a/server/auth/get_user_info.py +++ b/server/auth/get_user_info.py @@ -30,7 +30,7 @@ async def getUserInfoByToken(token): } return data else : - return {} + return None async def getUserAccessToken(user_id: str, provider = 'github'): token = await get_oauth_token() @@ -67,6 +67,8 @@ async def get_user_access_token(petercat_user_token: Annotated[str | None, Cooki if petercat_user_token is None: return None user_info = await getUserInfoByToken(petercat_user_token) + if user_info is None: + return None access_token = await getUserAccessToken(user_id=user_info['id']) print(f"get_user_access_token: user_info={user_info}, access_token={access_token}") return access_token \ No newline at end of file diff --git a/server/routers/auth.py b/server/routers/auth.py index 21ad78c4..770c4647 100644 --- a/server/routers/auth.py +++ b/server/routers/auth.py @@ -71,6 +71,8 @@ async def callback(request: Request, response: Response): if not token: raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Missing authorization token") data = await getUserInfoByToken(token) + if data is None: + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Missing authorization token") supabase = get_client() supabase.table("profiles").upsert(data).execute() print(f"auth_callback: {data}, token={token}") diff --git a/server/tools/bot_builder.py b/server/tools/bot_builder.py index b742b5af..d4ec5dcb 100644 --- a/server/tools/bot_builder.py +++ b/server/tools/bot_builder.py @@ -4,8 +4,6 @@ from langchain.tools import tool from github import Github from petercat_utils import get_client -from prompts.bot_template import generate_prompt_by_repo_name -from auth.get_user_info import getUserInfoByToken from bot.builder import bot_builder g = Github() diff --git a/server/verify/rate_limit.py b/server/verify/rate_limit.py index c1a5818e..4d0c7319 100644 --- a/server/verify/rate_limit.py +++ b/server/verify/rate_limit.py @@ -15,6 +15,12 @@ async def verify_rate_limit(petercat: str = Cookie(None)): if not petercat: raise HTTPException(status_code=403, detail="Must Login") user = await getUserInfoByToken(petercat) + if user is None: + raise HTTPException( + status_code=429, + detail="Rate Limit Exceeded, Try It Later", + headers={"Retry-After": "60"} + ) user_id = user["id"] supabase = get_client() table = supabase.table("user_token_usage")