From 6496bee5318af62e811c3b4b1b0ccc7bf5a4f574 Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 29 Sep 2025 00:27:16 +0300 Subject: [PATCH] fetch-profile --- auth/oauth.py | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/auth/oauth.py b/auth/oauth.py index 1837ed4b..6dc8dae6 100644 --- a/auth/oauth.py +++ b/auth/oauth.py @@ -358,14 +358,48 @@ async def _fetch_yandex_profile(client: Any, token: Any) -> dict: } +async def _fetch_google_profile(client: Any, token: Any) -> dict: + """Получает профиль из Google API""" + try: + # Извлекаем access_token из ответа + access_token = token.get("access_token") if isinstance(token, dict) else token + + if not access_token: + logger.error("No access_token found in Google token response") + return {} + + # Используем прямой HTTP запрос к Google API + headers = { + "Authorization": f"Bearer {access_token}", + "Accept": "application/json", + } + + async with httpx.AsyncClient() as http_client: + # Получаем профиль пользователя + profile_response = await http_client.get("https://www.googleapis.com/oauth2/v2/userinfo", headers=headers) + + if profile_response.status_code != 200: + logger.error(f"Google API error: {profile_response.status_code} - {profile_response.text}") + return {} + + profile_data = profile_response.json() + + return { + "id": str(profile_data.get("id", "")), + "email": profile_data.get("email"), + "name": profile_data.get("name", ""), + "picture": profile_data.get("picture", "").replace("=s96", "=s600"), + } + except Exception as e: + logger.error(f"Error fetching Google profile: {e}") + return {} + + async def get_user_profile(provider: str, client: Any, token: Any) -> dict: """Получает профиль пользователя от провайдера OAuth""" - # Простые провайдеры с обработкой через lambda - if provider in PROVIDER_HANDLERS: - return PROVIDER_HANDLERS[provider](token, None) - # Провайдеры требующие API вызовов profile_fetchers = { + "google": _fetch_google_profile, "github": _fetch_github_profile, "facebook": _fetch_facebook_profile, "x": _fetch_x_profile, @@ -376,6 +410,10 @@ async def get_user_profile(provider: str, client: Any, token: Any) -> dict: if provider in profile_fetchers: return await profile_fetchers[provider](client, token) + # Простые провайдеры с обработкой через lambda (только для telegram теперь) + if provider in PROVIDER_HANDLERS: + return PROVIDER_HANDLERS[provider](token, None) + return {}