Address review comments: make _get_github_user_info pure function and tighten exception handling

This commit is contained in:
ehhuang 2025-07-08 00:54:35 -07:00
parent 9ece598705
commit 871021adc4

View file

@ -327,7 +327,10 @@ class GitHubTokenAuthProvider(AuthProvider):
This validates tokens issued by GitHub (personal access tokens or OAuth tokens).
"""
try:
user_info = await self._get_github_user_info(token)
user_info = await _get_github_user_info(token, self.config.github_api_base_url)
except Exception as e:
logger.warning(f"GitHub token validation failed: {e}")
raise ValueError("Invalid GitHub token") from e
principal = user_info["user"]["login"]
@ -344,11 +347,16 @@ class GitHubTokenAuthProvider(AuthProvider):
attributes=access_attributes,
)
except Exception as e:
logger.warning(f"GitHub token validation failed: {e}")
raise ValueError("Invalid GitHub token") from e
async def close(self):
"""Clean up any resources."""
pass
async def _get_github_user_info(self, access_token: str) -> dict:
def get_auth_error_message(self, scope: dict | None = None) -> str:
"""Return GitHub-specific authentication error message."""
return "Authentication required. Please provide a valid GitHub access token (https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens) in the Authorization header (Bearer <token>)"
async def _get_github_user_info(access_token: str, github_api_base_url: str) -> dict:
"""Fetch user info and organizations from GitHub API."""
headers = {
"Authorization": f"Bearer {access_token}",
@ -357,7 +365,7 @@ class GitHubTokenAuthProvider(AuthProvider):
}
async with httpx.AsyncClient() as client:
user_response = await client.get(f"{self.config.github_api_base_url}/user", headers=headers, timeout=10.0)
user_response = await client.get(f"{github_api_base_url}/user", headers=headers, timeout=10.0)
user_response.raise_for_status()
user_data = user_response.json()
@ -365,14 +373,6 @@ class GitHubTokenAuthProvider(AuthProvider):
"user": user_data,
}
async def close(self):
"""Clean up any resources."""
pass
def get_auth_error_message(self, scope: dict | None = None) -> str:
"""Return GitHub-specific authentication error message."""
return "Authentication required. Please provide a valid GitHub access token (https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens) in the Authorization header (Bearer <token>)"
def create_auth_provider(config: AuthenticationConfig) -> AuthProvider:
"""Factory function to create the appropriate auth provider."""