feat(auth): Add endpoint to get user info by token
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -48,6 +48,35 @@ async def exchange_code_for_token(request: TokenRequest):
|
|||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.opt(exception=True).error("An unexpected error occurred during token exchange.")
|
logger.opt(exception=True).error("An unexpected error occurred during token exchange.")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail="An internal server error occurred."
|
||||||
|
)
|
||||||
|
|
||||||
|
# Получает информацию о пользователе Яндекса
|
||||||
|
@router.get("/userinfo", response_model=UserInfoResponse, summary="Получение информации о пользователе")
|
||||||
|
async def get_user_info(credentials: Annotated[HTTPAuthorizationCredentials, Depends(bearer_scheme)]):
|
||||||
|
|
||||||
|
token = credentials.credentials
|
||||||
|
headers = {'Authorization': f'OAuth {token}'}
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
response = await client.get(YANDEX_USERINFO_URL, headers=headers)
|
||||||
|
response.raise_for_status()
|
||||||
|
user_data = response.json()
|
||||||
|
logger.info(f"Fetched user info for login: {user_data.get('login')}")
|
||||||
|
return UserInfoResponse(**user_data)
|
||||||
|
|
||||||
|
except httpx.HTTPStatusError as e:
|
||||||
|
error_details = e.response.json()
|
||||||
|
logger.error(f"Yandex UserInfo API error: {e.response.status_code} - {error_details}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=e.response.status_code,
|
||||||
|
detail=f"Yandex API Error: {error_details.get('error_description', 'Unknown error')}"
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
logger.opt(exception=True).error("An unexpected error occurred while fetching user info.")
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
detail="An internal server error occurred."
|
detail="An internal server error occurred."
|
||||||
|
|||||||
@@ -7,4 +7,11 @@ class TokenRequest(BaseModel):
|
|||||||
class TokenResponse(BaseModel):
|
class TokenResponse(BaseModel):
|
||||||
"""Схема ответа с токеном доступа."""
|
"""Схема ответа с токеном доступа."""
|
||||||
access_token: str
|
access_token: str
|
||||||
token_type: str
|
token_type: str
|
||||||
|
|
||||||
|
class UserInfoResponse(BaseModel):
|
||||||
|
"""Схема ответа с информацией о пользователе Яндекса."""
|
||||||
|
id: str
|
||||||
|
login: str
|
||||||
|
display_name: str
|
||||||
|
default_email: str
|
||||||
Reference in New Issue
Block a user