Repository navigation
refactor routes break to files #26
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
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
783ce1d
refactor routes
gkorland 87c8e74
Merge branch 'main' into refactor
gkorland e243ade
refactor code
gkorland 18c79cd
refactor j2 file
gkorland 77c00ff
fix light mode in warning message
gkorland c46be44
handle security risks
gkorland 09ba7d8
don't return internal info on error
gkorland b21f878
don't return internal info on error
gkorland 9e9cc86
sanitized_query before logging
gkorland 7591ce1
fix CodeQL errors
gkorland 7f08177
set noopener
gkorland cfc80be
Don't fetch graphs if not authenticated
gkorland b1c9845
break js files to modules
gkorland 5ccb46c
break css to modules
gkorland 890228a
break CSS to modules
gkorland 1002d36
security warnings
gkorland df33de8
fix coderabbit comments
gkorland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Text2SQL API package.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| """Application factory for the text2sql Flask app.""" | ||
|
|
||
| import logging | ||
| import os | ||
|
|
||
| from dotenv import load_dotenv | ||
| from flask import Flask, redirect, url_for | ||
| from flask_dance.contrib.google import make_google_blueprint | ||
| from flask_dance.contrib.github import make_github_blueprint | ||
| from flask_dance.consumer.storage.session import SessionStorage | ||
|
|
||
| from api.auth.oauth_handlers import setup_oauth_handlers | ||
| from api.routes.auth import auth_bp | ||
| from api.routes.graphs import graphs_bp | ||
| from api.routes.database import database_bp | ||
|
|
||
| # Load environment variables from .env file | ||
| load_dotenv() | ||
| logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") | ||
|
|
||
|
|
||
| def create_app(): | ||
| """Create and configure the Flask application.""" | ||
| app = Flask(__name__) | ||
| app.secret_key = os.getenv("FLASK_SECRET_KEY") | ||
| if not app.secret_key: | ||
| import secrets | ||
| app.secret_key = secrets.token_hex(32) | ||
| logging.warning("FLASK_SECRET_KEY not set, using generated key. Set this in production!") | ||
|
|
||
| # Google OAuth setup | ||
| GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID") | ||
| GOOGLE_CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET") | ||
| google_bp = make_google_blueprint( | ||
| client_id=GOOGLE_CLIENT_ID, | ||
| client_secret=GOOGLE_CLIENT_SECRET, | ||
| scope=[ | ||
| "https://www.googleapis.com/auth/userinfo.email", | ||
| "https://www.googleapis.com/auth/userinfo.profile", | ||
| "openid" | ||
| ] | ||
| ) | ||
| app.register_blueprint(google_bp, url_prefix="/login") | ||
|
|
||
| # GitHub OAuth setup | ||
| GITHUB_CLIENT_ID = os.getenv("GITHUB_CLIENT_ID") | ||
| GITHUB_CLIENT_SECRET = os.getenv("GITHUB_CLIENT_SECRET") | ||
| github_bp = make_github_blueprint( | ||
| client_id=GITHUB_CLIENT_ID, | ||
| client_secret=GITHUB_CLIENT_SECRET, | ||
| scope="user:email", | ||
| storage=SessionStorage() | ||
| ) | ||
| app.register_blueprint(github_bp, url_prefix="/login") | ||
|
|
||
| # Set up OAuth signal handlers | ||
| setup_oauth_handlers(google_bp, github_bp) | ||
|
|
||
| # Register blueprints | ||
| app.register_blueprint(auth_bp) | ||
| app.register_blueprint(graphs_bp) | ||
| app.register_blueprint(database_bp) | ||
|
|
||
| @app.errorhandler(Exception) | ||
| def handle_oauth_error(error): | ||
| """Handle OAuth-related errors gracefully""" | ||
| # Check if it's an OAuth-related error | ||
| if "token" in str(error).lower() or "oauth" in str(error).lower(): | ||
| logging.warning("OAuth error occurred: %s", error) | ||
| from flask import session | ||
| session.clear() | ||
| return redirect(url_for("auth.home")) | ||
|
|
||
| # For other errors, let them bubble up | ||
| raise error | ||
|
|
||
| return app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Authentication module for text2sql API | ||
|
|
||
| from .user_management import ( | ||
| ensure_user_in_organizations, | ||
| update_identity_last_login, | ||
| validate_and_cache_user, | ||
| token_required | ||
| ) | ||
| from .oauth_handlers import setup_oauth_handlers | ||
|
|
||
| __all__ = [ | ||
| "ensure_user_in_organizations", | ||
| "update_identity_last_login", | ||
| "validate_and_cache_user", | ||
| "token_required", | ||
| "setup_oauth_handlers" | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| """OAuth signal handlers for Google and GitHub authentication.""" | ||
|
|
||
| import logging | ||
| import time | ||
|
|
||
| import requests | ||
| from flask import session | ||
| from flask_dance.consumer import oauth_authorized | ||
| from flask_dance.contrib.google import google | ||
| from flask_dance.contrib.github import github | ||
|
|
||
| from .user_management import ensure_user_in_organizations | ||
|
|
||
|
|
||
| def setup_oauth_handlers(google_bp, github_bp): | ||
| """Set up OAuth signal handlers for both Google and GitHub blueprints.""" | ||
|
|
||
| @oauth_authorized.connect_via(google_bp) | ||
| def google_logged_in(blueprint, token): # pylint: disable=unused-argument | ||
| """Handle Google OAuth authorization callback.""" | ||
| if not token: | ||
| return False | ||
|
|
||
| try: | ||
| # Get user profile | ||
| resp = google.get("/oauth2/v2/userinfo") | ||
| if resp.ok: | ||
| google_user = resp.json() | ||
| user_id = google_user.get("id") | ||
| email = google_user.get("email") | ||
| name = google_user.get("name") | ||
|
|
||
| # Validate required fields | ||
| if not user_id or not email: | ||
| logging.error("Missing required fields from Google OAuth response") | ||
| return False | ||
|
|
||
| if user_id and email: | ||
| # Check if identity exists in Organizations graph, create if new | ||
| _, _ = ensure_user_in_organizations( | ||
| user_id, email, name, "google", google_user.get("picture") | ||
| ) | ||
|
|
||
| # If existing identity, just update last login | ||
| # (already done in ensure_user_in_organizations) | ||
|
|
||
| # Normalize user info structure for session | ||
| user_info_session = { | ||
| "id": str(user_id), # Ensure string type | ||
| "name": name or "", | ||
| "email": email, | ||
| "picture": google_user.get("picture", ""), | ||
| "provider": "google" | ||
| } | ||
| session["user_info"] = user_info_session | ||
| session["token_validated_at"] = time.time() | ||
| return False # Don't create default flask-dance entry in session | ||
|
|
||
| except (requests.RequestException, KeyError, ValueError, AttributeError) as e: | ||
| logging.error("Google OAuth signal error: %s", e) | ||
|
|
||
| return False | ||
|
|
||
| @oauth_authorized.connect_via(github_bp) | ||
| def github_logged_in(blueprint, token): # pylint: disable=unused-argument | ||
| """Handle GitHub OAuth authorization callback.""" | ||
| if not token: | ||
| return False | ||
|
|
||
| try: | ||
| # Get user profile | ||
| resp = github.get("/user") | ||
| if resp.ok: | ||
| github_user = resp.json() | ||
|
|
||
| # Get user email (GitHub may require separate call for email) | ||
| email_resp = github.get("/user/emails") | ||
| email = None | ||
| if email_resp.ok: | ||
| emails = email_resp.json() | ||
| # Find primary email | ||
| for email_obj in emails: | ||
| if email_obj.get("primary", False): | ||
| email = email_obj.get("email") | ||
| break | ||
| # If no primary email found, use the first one | ||
| if not email and emails: | ||
| email = emails[0].get("email") | ||
|
|
||
| user_id = str(github_user.get("id")) | ||
| name = github_user.get("name") or github_user.get("login") | ||
|
|
||
| # Validate required fields | ||
| if not user_id or not email: | ||
| logging.error("Missing required fields from GitHub OAuth response") | ||
| return False | ||
|
|
||
| if user_id and email: | ||
| # Check if identity exists in Organizations graph, create if new | ||
| _, _ = ensure_user_in_organizations( | ||
| user_id, email, name, "github", github_user.get("avatar_url") | ||
| ) | ||
|
|
||
| # If existing identity, just update last login | ||
| # (already done in ensure_user_in_organizations) | ||
|
|
||
| # Normalize user info structure for session | ||
| user_info_session = { | ||
| "id": user_id, | ||
| "name": name or "", | ||
| "email": email, | ||
| "picture": github_user.get("avatar_url", ""), | ||
| "provider": "github" | ||
| } | ||
| session["user_info"] = user_info_session | ||
| session["token_validated_at"] = time.time() | ||
| return False # Don't create default flask-dance entry in session | ||
|
|
||
| except (requests.RequestException, KeyError, ValueError, AttributeError) as e: | ||
| logging.error("GitHub OAuth signal error: %s", e) | ||
|
|
||
| return False | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.