From 02f006ab00357adb64f8920be584b777a99a9d0b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 6 Jun 2026 16:14:21 +0000 Subject: [PATCH 1/2] Refactor _oauth_error_response using guard clauses This refactoring replaces a deeply nested if conditional in `_oauth_error_response` with guard clauses and early returns. This flattens the logical flow and makes the method much easier to read and maintain, without altering the functionality. A local `fallback` function is used to handle standard exit behavior to reduce duplication. Co-authored-by: DaTiC0 <13198638+DaTiC0@users.noreply.github.com> --- routes.py | 50 +++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/routes.py b/routes.py index 6d0efe5..5547b9c 100644 --- a/routes.py +++ b/routes.py @@ -19,32 +19,40 @@ def _oauth_error_response(exc): """Return OAuth errors in a client-friendly way when redirect URI is valid.""" + def fallback(): + status_code = getattr(exc, 'status_code', 400) or 400 + return jsonify(error=exc.error, error_description=exc.description), status_code + client_id = request.values.get('client_id') redirect_uri = request.values.get('redirect_uri') state = request.values.get('state') client = load_client(client_id) if client_id else None - if client: - if not redirect_uri: - redirect_uri = client.get_default_redirect_uri() - if redirect_uri and client.check_redirect_uri(redirect_uri): - parsed = urlparse(redirect_uri) - if parsed.scheme and parsed.netloc: - params = {'error': exc.error} - if exc.description: - params['error_description'] = exc.description - if state: - params['state'] = state - - existing_query = dict(parse_qsl(parsed.query, keep_blank_values=True)) - existing_query.update(params) - safe_redirect_uri = urlunparse( - parsed._replace(query=urlencode(existing_query)) - ) - return redirect(safe_redirect_uri) - - status_code = getattr(exc, 'status_code', 400) or 400 - return jsonify(error=exc.error, error_description=exc.description), status_code + if not client: + return fallback() + + if not redirect_uri: + redirect_uri = client.get_default_redirect_uri() + + if not redirect_uri or not client.check_redirect_uri(redirect_uri): + return fallback() + + parsed = urlparse(redirect_uri) + if not (parsed.scheme and parsed.netloc): + return fallback() + + params = {'error': exc.error} + if exc.description: + params['error_description'] = exc.description + if state: + params['state'] = state + + existing_query = dict(parse_qsl(parsed.query, keep_blank_values=True)) + existing_query.update(params) + safe_redirect_uri = urlunparse( + parsed._replace(query=urlencode(existing_query)) + ) + return redirect(safe_redirect_uri) def _resolve_smarthome_user_scope(req): From b7d3a59552b9dd17b6afcd861a1b62069d82aeef Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 6 Jun 2026 16:27:33 +0000 Subject: [PATCH 2/2] Refactor _oauth_error_response using guard clauses This refactoring replaces a deeply nested if conditional in `_oauth_error_response` with guard clauses and early returns. This flattens the logical flow and makes the method much easier to read and maintain, without altering the functionality. Pre-constructs the default return value directly to avoid unnecessary local function overhead. --- routes.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/routes.py b/routes.py index 5547b9c..c3c63fa 100644 --- a/routes.py +++ b/routes.py @@ -19,9 +19,8 @@ def _oauth_error_response(exc): """Return OAuth errors in a client-friendly way when redirect URI is valid.""" - def fallback(): - status_code = getattr(exc, 'status_code', 400) or 400 - return jsonify(error=exc.error, error_description=exc.description), status_code + status_code = getattr(exc, 'status_code', 400) or 400 + error_response = jsonify(error=exc.error, error_description=exc.description), status_code client_id = request.values.get('client_id') redirect_uri = request.values.get('redirect_uri') @@ -29,17 +28,17 @@ def fallback(): client = load_client(client_id) if client_id else None if not client: - return fallback() + return error_response if not redirect_uri: redirect_uri = client.get_default_redirect_uri() if not redirect_uri or not client.check_redirect_uri(redirect_uri): - return fallback() + return error_response parsed = urlparse(redirect_uri) if not (parsed.scheme and parsed.netloc): - return fallback() + return error_response params = {'error': exc.error} if exc.description: