From c4dbcb514e2530046a5dd88fe3594cb51829e084 Mon Sep 17 00:00:00 2001 From: "raoha.rh" Date: Tue, 17 Dec 2024 17:09:22 +0800 Subject: [PATCH] feat: support locally auth --- server/auth/get_user_info.py | 11 ++++------- server/auth/router.py | 6 ++---- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/server/auth/get_user_info.py b/server/auth/get_user_info.py index 64fff60f..168f6c07 100644 --- a/server/auth/get_user_info.py +++ b/server/auth/get_user_info.py @@ -1,15 +1,12 @@ -from fastapi import Request +from fastapi import Depends, Request from auth.clients import get_auth_client +from auth.clients.base import BaseAuthClient from core.models.user import User from petercat_utils import get_env_variable AUTH0_DOMAIN = get_env_variable("AUTH0_DOMAIN") -async def getUserAccessToken(user_id: str, provider="github"): - auth_client = get_auth_client() - return await auth_client.get_access_token(user_id=user_id, provider=provider) - async def get_user_id(request: Request): user_info = request.session.get("user") try: @@ -21,7 +18,7 @@ async def get_user_id(request: Request): return None -async def get_user(request: Request) -> User | None: +async def get_user(request: Request, auth_client: BaseAuthClient = Depends(get_auth_client)) -> User | None: user_info = request.session.get("user") if user_info is None: return None @@ -29,5 +26,5 @@ async def get_user(request: Request) -> User | None: if user_info["sub"].startswith("client|"): return User(**user_info, anonymous=True) - access_token = await getUserAccessToken(user_id=user_info["sub"]) + access_token = await auth_client.get_access_token(user_id=user_info["sub"]) return User(**user_info, access_token=access_token, anonymous=False) diff --git a/server/auth/router.py b/server/auth/router.py index af22dd78..d9fc65db 100644 --- a/server/auth/router.py +++ b/server/auth/router.py @@ -43,7 +43,6 @@ async def logout(request: Request): @router.get("/callback") async def callback(request: Request, auth_client: BaseAuthClient = Depends(get_auth_client)): user_info = await auth_client.get_user_info(request) - print(f"user_info={user_info}") if user_info: request.session["user"] = dict(user_info) supabase = get_client() @@ -96,12 +95,11 @@ async def bot_generator( @router.get("/repos") -async def get_user_repos(user_id: Optional[str] = Depends(get_user_id)): +async def get_user_repos(user_id: Optional[str] = Depends(get_user_id), auth_client: BaseAuthClient = Depends(get_auth_client)): if not user_id: raise HTTPException(status_code=401, detail="User not found") try: - client = get_auth_client() - access_token = await client.get_access_token(user_id=user_id) + access_token = await auth_client.get_access_token(user_id=user_id) g = Github(access_token) user = g.get_user() repos = user.get_repos()