refactored the credential checking logic into a shared helper function to reduce code duplication

This commit is contained in:
Taylor Wilsdon
2025-06-28 12:56:43 -07:00
parent 5f08c89468
commit 33eaa47d0c
4 changed files with 298 additions and 289 deletions

View File

@@ -232,6 +232,19 @@ def load_client_secrets(client_secrets_path: str) -> Dict[str, Any]:
except (IOError, json.JSONDecodeError) as e:
logger.error(f"Error loading client secrets file {client_secrets_path}: {e}")
raise
def check_client_secrets() -> Optional[str]:
"""
Checks for the presence of OAuth client secrets, either as environment
variables or as a file.
Returns:
An error message string if secrets are not found, otherwise None.
"""
env_config = load_client_secrets_from_env()
if not env_config and not os.path.exists(CONFIG_CLIENT_SECRETS_PATH):
logger.error(f"OAuth client credentials not found. No environment variables set and no file at {CONFIG_CLIENT_SECRETS_PATH}")
return "OAuth client credentials not found. Please set GOOGLE_OAUTH_CLIENT_ID and GOOGLE_OAUTH_CLIENT_SECRET environment variables or provide client_secret.json file."
return None
def create_oauth_flow(scopes: List[str], redirect_uri: str, state: Optional[str] = None) -> Flow:
"""Creates an OAuth flow using environment variables or client secrets file."""

View File

@@ -15,7 +15,7 @@ import socket
from fastapi import FastAPI, Request
import uvicorn
from auth.google_auth import handle_auth_callback, CONFIG_CLIENT_SECRETS_PATH
from auth.google_auth import handle_auth_callback, check_client_secrets
from auth.scopes import OAUTH_STATE_TO_SESSION_ID_MAP, SCOPES
from auth.oauth_responses import create_error_response, create_success_response, create_server_error_response
@@ -60,11 +60,9 @@ class MinimalOAuthServer:
try:
# Check if we have credentials available (environment variables or file)
from auth.google_auth import load_client_secrets_from_env
env_config = load_client_secrets_from_env()
if not env_config and not os.path.exists(CONFIG_CLIENT_SECRETS_PATH):
logger.error(f"OAuth client credentials not found. No environment variables set and no file at {CONFIG_CLIENT_SECRETS_PATH}")
return create_server_error_response("OAuth client credentials not found. Please set GOOGLE_OAUTH_CLIENT_ID and GOOGLE_OAUTH_CLIENT_SECRET environment variables or provide client_secret.json file.")
error_message = check_client_secrets()
if error_message:
return create_server_error_response(error_message)
logger.info(f"OAuth callback: Received code (state: {state}). Attempting to exchange for tokens.")