-
Notifications
You must be signed in to change notification settings - Fork 107
Staging #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Staging #129
Changes from all commits
26bbf4a
ffd0ed9
bbb097b
776b295
f21bfb5
8037ec5
e4da6a8
23b9f5a
f687e0c
ecf1d75
ddb3bd3
e60ddcc
ffe4b7b
2b89e5d
cacd35e
87a34d2
64986b1
9787d7f
f17ba66
4a29294
51679ae
9dfab3f
b477c97
071f90d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| import logging | ||
| from typing import Dict, Any | ||
|
|
||
| from fastapi import FastAPI, Request | ||
| from fastapi import FastAPI | ||
| from authlib.integrations.starlette_client import OAuth | ||
|
|
||
| from .user_management import ensure_user_in_organizations | ||
|
|
@@ -19,62 +19,32 @@ def setup_oauth_handlers(app: FastAPI, oauth: OAuth): | |
| # Store oauth in app state for access in routes | ||
| app.state.oauth = oauth | ||
|
|
||
| async def handle_google_callback(_request: Request, | ||
| _token: Dict[str, Any], | ||
| user_info: Dict[str, Any]): | ||
| """Handle Google OAuth callback processing""" | ||
| async def handle_callback(provider: str, user_info: Dict[str, Any], api_token: str): | ||
| """Handle Provider OAuth callback processing""" | ||
| try: | ||
| user_id = user_info.get("id") | ||
| email = user_info.get("email") | ||
| name = user_info.get("name") | ||
|
|
||
| # Validate required fields | ||
| if not user_id or not email: | ||
| logging.error("Missing required fields from Google OAuth response") | ||
| logging.error("Missing required fields from %s OAuth response", provider) | ||
| return False | ||
|
|
||
| # Check if identity exists in Organizations graph, create if new | ||
| _, _ = await ensure_user_in_organizations( | ||
| user_id, | ||
| email, | ||
| name, | ||
| "google", | ||
| provider, | ||
| api_token, | ||
| user_info.get("picture"), | ||
| ) | ||
|
|
||
| return True | ||
| except Exception as exc: # capture exception for logging | ||
| logging.error("Error handling Google OAuth callback: %s", exc) | ||
| return False | ||
|
|
||
| async def handle_github_callback(_request: Request, | ||
| _token: Dict[str, Any], | ||
| user_info: Dict[str, Any]): | ||
| """Handle GitHub OAuth callback processing""" | ||
| try: | ||
| user_id = user_info.get("id") | ||
| email = user_info.get("email") | ||
| name = user_info.get("name") or user_info.get("login") | ||
|
|
||
| # Validate required fields | ||
| if not user_id or not email: | ||
| logging.error("Missing required fields from GitHub OAuth response") | ||
| return False | ||
|
|
||
| # Check if identity exists in Organizations graph, create if new | ||
| _, _ = await ensure_user_in_organizations( | ||
| user_id, | ||
| email, | ||
| name, | ||
| "github", | ||
| user_info.get("picture"), | ||
| ) | ||
|
|
||
| return True | ||
| except Exception as exc: # capture exception for logging | ||
| logging.error("Error handling GitHub OAuth callback: %s", exc) | ||
| logging.error("Error handling %s OAuth callback: %s", provider, exc) | ||
| return False | ||
|
Comment on lines
+46
to
47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainNarrow exception handling and log stack traces Addresses Pylint “broad-exception-caught” and improves debuggability by logging tracebacks. Keep a final broad catch if you want maximal safety. - except Exception as exc: # capture exception for logging
- logging.error("Error handling %s OAuth callback: %s", provider, exc)
- return False
+ except (KeyError, ValueError) as exc:
+ logging.error("Invalid OAuth payload for %s: %s", provider, exc)
+ return False
+ except Exception as exc: # pylint: disable=broad-except
+ logging.exception("Error handling %s OAuth callback", provider)
+ return FalseOptionally, also import and catch Authlib’s OAuthError for even better specificity: 🏁 Script executed: #!/bin/bash
# Verify if Authlib's OAuthError is available in this project version.
python - <<'PY'
try:
from authlib.oauth2.rfc6749.errors import OAuthError
print("OAuthError available")
except Exception as e:
print("OAuthError not available:", e)
PYLength of output: 62 🏁 Script executed: #!/bin/bash
# Search for authlib in dependency files and codebase
echo "Listing potential dependency files:"
find . -maxdepth 2 -type f \( -name "requirements*.txt" -o -name "Pipfile" -o -name "setup.py" -o -name "pyproject.toml" \) -print
echo
echo "Checking for 'authlib' in requirements files:"
grep -R "authlib" -n requirements*.txt 2>/dev/null || echo " → No authlib entry in requirements*.txt"
echo
echo "Checking for 'authlib' in Pipfile:"
grep -R "authlib" -n Pipfile 2>/dev/null || echo " → No authlib entry in Pipfile"
echo
echo "Checking for 'authlib' in setup.py and pyproject.toml:"
grep -R "authlib" -n setup.py pyproject.toml 2>/dev/null || echo " → No authlib entry in setup.py or pyproject.toml"
echo
echo "Searching codebase for any authlib imports or references:"
rg -n "authlib" . || echo " → No authlib references found in code"
echo
echo "Searching codebase for OAuthError usage:"
rg -n "OAuthError" . || echo " → No OAuthError references found in code"Length of output: 1509 Narrow exception handling and log full tracebacks To address the Pylint “broad-exception-caught” warning and improve debuggability, catch only the errors you expect and log full tracebacks for anything unexpected. Since Authlib is declared in your Pipfile ( • Add at the top of from authlib.integrations.base_client.errors import OAuthError• Replace the broad - except Exception as exc: # capture exception for logging
- logging.error("Error handling %s OAuth callback: %s", provider, exc)
- return False
+ except (KeyError, ValueError) as exc:
+ logging.error("Invalid OAuth payload for %s: %s", provider, exc)
+ return False
+ except OAuthError as exc:
+ logging.error("OAuth error during %s callback: %s", provider, exc)
+ return False
+ except Exception as exc: # pylint: disable=broad-except
+ logging.exception("Unexpected error handling %s OAuth callback", provider)
+ return False– The first two blocks handle common payload or client errors cleanly. 🤖 Prompt for AI Agents |
||
|
|
||
| # Store handlers in app state for use in route callbacks | ||
| app.state.google_callback_handler = handle_google_callback | ||
| app.state.github_callback_handler = handle_github_callback | ||
| app.state.callback_handler = handle_callback | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Use ensure_user_in_organizations result to decide success
Right now True is returned even if persistence fails. Wire the returned tuple to control flow so routes can react correctly.
📝 Committable suggestion
🧰 Tools
🪛 GitHub Actions: Pylint
[warning] 45-45: Pylint: Catching too general exception (broad-exception-caught).
🤖 Prompt for AI Agents