Skip to content

Commit

Permalink
fix: user auth using petercat user token (#219)
Browse files Browse the repository at this point in the history
- 统一用 `petercat_user token`
  • Loading branch information
RaoHai authored Aug 18, 2024
2 parents 3d74fd4 + a100feb commit 820748c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
10 changes: 10 additions & 0 deletions server/auth/get_user_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ async def getAnonymousUserInfoByToken(token: str):
rows = supabase.table("profiles").select("*").eq("id", token).execute()
return rows.data[0] if (len(rows.data) > 0) else None

async def get_user_id(petercat_user_token: Annotated[str | None, Cookie()] = None):
try:
if petercat_user_token is None:
return None
user_info = await getUserInfoByToken(petercat_user_token)
return user_info['id']

except Exception as e:
return None

async def get_user_access_token(petercat_user_token: Annotated[str | None, Cookie()] = None):
try:
if petercat_user_token is None:
Expand Down
4 changes: 2 additions & 2 deletions server/routers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Annotated, Optional
from fastapi import APIRouter, Cookie, Depends
from fastapi.responses import StreamingResponse
from auth.get_user_info import get_user_access_token
from auth.get_user_info import get_user_access_token, get_user_id
from petercat_utils.data_class import ChatData
from agent import qa_chat, bot_builder
from verify.rate_limit import verify_rate_limit
Expand Down Expand Up @@ -32,7 +32,7 @@ async def run_issue_helper(input_data: ChatData):


@router.post("/stream_builder", response_class=StreamingResponse, dependencies=[Depends(verify_rate_limit)])
def run_bot_builder(input_data: ChatData, user_id: str = Cookie(None), bot_id: Optional[str] = None):
def run_bot_builder(input_data: ChatData, bot_id: Optional[str] = None, user_id: Annotated[str | None, Depends(get_user_id)] = None):
if not user_id:
return StreamingResponse(generate_auth_failed_stream(), media_type="text/event-stream")
result = bot_builder.agent_stream_chat(input_data, user_id, bot_id)
Expand Down
8 changes: 5 additions & 3 deletions server/verify/rate_limit.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Annotated
from fastapi import Cookie, HTTPException
from datetime import datetime, timedelta

Expand All @@ -8,13 +9,14 @@
RATE_LIMIT_REQUESTS = get_env_variable("RATE_LIMIT_REQUESTS") or 100
RATE_LIMIT_DURATION = timedelta(minutes=int(get_env_variable("RATE_LIMIT_DURATION") or 1))

async def verify_rate_limit(petercat: str = Cookie(None)):
async def verify_rate_limit(petercat_user_token: Annotated[str | None, Cookie()] = None):
if not RATE_LIMIT_ENABLED:
return

if not petercat:
if not petercat_user_token:
raise HTTPException(status_code=403, detail="Must Login")
user = await getUserInfoByToken(petercat)
user = await getUserInfoByToken(petercat_user_token)

if user is None:
raise HTTPException(
status_code=429,
Expand Down

0 comments on commit 820748c

Please sign in to comment.