Fix/pylint cleanup (#8)
* Fix pylint warnings across all 24 Python files in web_server - Add module, class, and function docstrings (C0114, C0115, C0116) - Fix import ordering: stdlib before third-party before local (C0411) - Replace wildcard imports with explicit named imports (W0401) - Remove trailing whitespace and add missing final newlines (C0303, C0304) - Replace dict() with dict literals (R1735) - Remove unused imports and variables (W0611, W0612) - Narrow broad Exception catches to specific exceptions (W0718) - Replace f-string logging with lazy % formatting (W1203) - Fix variable naming: UPPER_CASE for constants, snake_case for locals (C0103) - Add pylint disable comments for necessary global statements (W0603) - Fix no-else-return, simplifiable-if-expression, singleton-comparison - Fix bad indentation in stripe.py (W0311) - Add encoding="utf-8" to open() calls (W1514) - Add check=True to subprocess.run() calls (W1510) - Register Celery task modules via conf.include * Update `package-lock.json` add peer dependencies
This commit is contained in:
committed by
GitHub
parent
fed1a2f288
commit
2758be8680
@@ -1,15 +1,18 @@
|
||||
"""OAuth blueprint for Google authentication."""
|
||||
|
||||
from os import getenv
|
||||
from secrets import token_hex, token_urlsafe
|
||||
from random import randint
|
||||
|
||||
from authlib.integrations.flask_client import OAuth, OAuthError
|
||||
from flask import Blueprint, jsonify, session, redirect, request
|
||||
from blueprints.user import get_session_info_email
|
||||
from database.database import Database
|
||||
from dotenv import load_dotenv
|
||||
from secrets import token_hex, token_urlsafe
|
||||
from random import randint
|
||||
from utils.path_manager import PathManager
|
||||
|
||||
oauth_bp = Blueprint("oauth", __name__)
|
||||
google = None
|
||||
_google = None
|
||||
|
||||
load_dotenv()
|
||||
url_api = getenv("VITE_API_URL")
|
||||
@@ -23,8 +26,8 @@ def init_oauth(app):
|
||||
Initialise the OAuth functionality.
|
||||
"""
|
||||
oauth = OAuth(app)
|
||||
global google
|
||||
google = oauth.register(
|
||||
global _google # pylint: disable=global-statement
|
||||
_google = oauth.register(
|
||||
'google',
|
||||
client_id=app.config['GOOGLE_CLIENT_ID'],
|
||||
client_secret=app.config['GOOGLE_CLIENT_SECRET'],
|
||||
@@ -50,11 +53,11 @@ def login_google():
|
||||
session["nonce"] = token_urlsafe(16)
|
||||
session["state"] = token_urlsafe(32)
|
||||
session["origin"] = request.args.get("next")
|
||||
|
||||
|
||||
# Make sure session is saved before redirect
|
||||
session.modified = True
|
||||
|
||||
return google.authorize_redirect(
|
||||
|
||||
return _google.authorize_redirect(
|
||||
redirect_uri=f'{url}/api/google_auth',
|
||||
nonce=session['nonce'],
|
||||
state=session['state']
|
||||
@@ -70,23 +73,27 @@ def google_auth():
|
||||
# Check state parameter before authorizing
|
||||
returned_state = request.args.get('state')
|
||||
stored_state = session.get('state')
|
||||
|
||||
|
||||
if not stored_state or stored_state != returned_state:
|
||||
print(f"State mismatch: stored={stored_state}, returned={returned_state}", flush=True)
|
||||
print(
|
||||
f"State mismatch: stored={stored_state}, "
|
||||
f"returned={returned_state}", flush=True
|
||||
)
|
||||
return jsonify({
|
||||
'error': f"mismatching_state: CSRF Warning! State not equal in request and response.",
|
||||
'error': "mismatching_state: CSRF Warning! "
|
||||
"State not equal in request and response.",
|
||||
'message': 'Authentication failed'
|
||||
}), 400
|
||||
|
||||
|
||||
# State matched, proceed with token authorization
|
||||
token = google.authorize_access_token()
|
||||
token = _google.authorize_access_token()
|
||||
|
||||
# Verify nonce
|
||||
nonce = session.get('nonce')
|
||||
if not nonce:
|
||||
return jsonify({'error': 'Missing nonce in session'}), 400
|
||||
|
||||
user = google.parse_id_token(token, nonce=nonce)
|
||||
user = _google.parse_id_token(token, nonce=nonce)
|
||||
|
||||
# Check if email exists to login else create a database entry
|
||||
user_email = user.get("email")
|
||||
@@ -108,7 +115,7 @@ def google_auth():
|
||||
break
|
||||
|
||||
db.execute(
|
||||
"""INSERT INTO users
|
||||
"""INSERT INTO users
|
||||
(username, email, stream_key)
|
||||
VALUES (?, ?, ?)""",
|
||||
(
|
||||
@@ -124,16 +131,19 @@ def google_auth():
|
||||
origin = session.get("origin", f"{url.replace('/api', '')}")
|
||||
username = user_data["username"]
|
||||
user_id = user_data["user_id"]
|
||||
|
||||
|
||||
# Clear session and set new data
|
||||
session.clear()
|
||||
session["username"] = username
|
||||
session["user_id"] = user_id
|
||||
|
||||
|
||||
# Ensure session is saved
|
||||
session.modified = True
|
||||
|
||||
print(f"session: {session.get('username')}. user_id: {session.get('user_id')}", flush=True)
|
||||
|
||||
print(
|
||||
f"session: {session.get('username')}. "
|
||||
f"user_id: {session.get('user_id')}", flush=True
|
||||
)
|
||||
|
||||
return redirect(origin)
|
||||
|
||||
@@ -144,9 +154,9 @@ def google_auth():
|
||||
'error': str(e)
|
||||
}), 400
|
||||
|
||||
except Exception as e:
|
||||
except (ValueError, TypeError, KeyError) as e:
|
||||
print(f"Unexpected Error: {str(e)}", flush=True)
|
||||
return jsonify({
|
||||
'message': 'An unexpected error occurred',
|
||||
'error': str(e)
|
||||
}), 500
|
||||
}), 500
|
||||
|
||||
Reference in New Issue
Block a user