From 79fa08e1929c6ecf5b56d964849bfe1d8a56a54e Mon Sep 17 00:00:00 2001 From: dingo Date: Thu, 2 Apr 2026 19:20:04 -0400 Subject: [PATCH] Fix Keycloak: scene reload on error, web signal missed, redirect URI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PC — scene reloading on auth error: Auth.gd's keycloak error handler called change_scene_to_file(LoginScreen) after emitting auth_failed, which reloaded the scene and wiped the error message before the user could see it. Removed the scene change — the login screen is already active and handles auth_failed itself. Web — token_received signal never handled after page redirect: login_screen._ready() returned early when Keycloak.auth_in_progress was true, before connecting Keycloak.token_received. When the exchange completed nobody was listening, so the game sat frozen. Signals are now connected before the early-return check. Web — fragile redirect URI and sessionStorage injection: JavaScriptBridge.eval return value could stringify as "". Now uses separate origin + pathname evals with a null check. sessionStorage items and the final navigation use uri_encode() + decodeURIComponent() instead of manual quote-escaping, preventing injection from PKCE values. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- Client/origin-client/autoloads/Auth.gd | 4 ++- Client/origin-client/autoloads/Keycloak.gd | 27 ++++++++++++------- .../scenes/login/login_screen.gd | 1 + 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Client/origin-client/autoloads/Auth.gd b/Client/origin-client/autoloads/Auth.gd index 2729c8e..0c80161 100644 --- a/Client/origin-client/autoloads/Auth.gd +++ b/Client/origin-client/autoloads/Auth.gd @@ -159,7 +159,9 @@ func _send_keycloak_auth() -> void: keycloak_refresh_token = "" _save_session() auth_failed.emit(code, msg) - get_tree().change_scene_to_file("res://scenes/login/LoginScreen.tscn") + # Do NOT change scene here — the login screen is already active and + # handles auth_failed by showing the error and re-enabling the button. + # Calling change_scene_to_file would reload the screen and wipe the error. ) diff --git a/Client/origin-client/autoloads/Keycloak.gd b/Client/origin-client/autoloads/Keycloak.gd index 3831984..75205e4 100644 --- a/Client/origin-client/autoloads/Keycloak.gd +++ b/Client/origin-client/autoloads/Keycloak.gd @@ -91,8 +91,12 @@ func start_auth_flow() -> void: if OS.get_name() == "Web": # Redirect the page; on return _check_web_callback() handles the code. - _redirect_uri = JavaScriptBridge.eval( - "window.location.origin + window.location.pathname", true) + var origin : String = str(JavaScriptBridge.eval("window.location.origin", true)) + var path : String = str(JavaScriptBridge.eval("window.location.pathname", true)) + if origin.is_empty() or origin == "": + auth_error.emit("Could not determine page origin for redirect URI.") + return + _redirect_uri = origin + path params = { "client_id": CLIENT_ID, "response_type": "code", @@ -102,12 +106,17 @@ func start_auth_flow() -> void: "code_challenge": challenge, "code_challenge_method": "S256", } + var auth_url := AUTH_URL + "?" + _encode_params(params) # Persist PKCE state to sessionStorage before the page navigates away. - JavaScriptBridge.eval( - "sessionStorage.setItem('sor_cv','%s');" % _code_verifier.replace("'", "\\'") - + "sessionStorage.setItem('sor_st','%s');" % _state.replace("'", "\\'") - + "sessionStorage.setItem('sor_ru','%s');" % _redirect_uri.replace("'", "\\'") - + "window.location.href='%s';" % (AUTH_URL + "?" + _encode_params(params)).replace("'", "\\'")) + # Each item is set in a separate eval so no value can escape the string. + JavaScriptBridge.eval("sessionStorage.setItem('sor_cv', decodeURIComponent('%s'));" \ + % _code_verifier.uri_encode()) + JavaScriptBridge.eval("sessionStorage.setItem('sor_st', decodeURIComponent('%s'));" \ + % _state.uri_encode()) + JavaScriptBridge.eval("sessionStorage.setItem('sor_ru', decodeURIComponent('%s'));" \ + % _redirect_uri.uri_encode()) + JavaScriptBridge.eval("window.location.href = decodeURIComponent('%s');" \ + % auth_url.uri_encode()) return # ── Desktop / mobile: loopback TCP ──────────────────────────────────────── @@ -328,7 +337,7 @@ func _on_exchange_completed( _result: int, status: int, _headers: PackedStringArray, body: PackedByteArray) -> void: auth_in_progress = false - var json := JSON.parse_string(body.get_string_from_utf8()) + var json :Variant= JSON.parse_string(body.get_string_from_utf8()) if status != 200 or json == null or not (json is Dictionary): auth_error.emit("Token exchange failed (HTTP %d)." % status) return @@ -342,7 +351,7 @@ func _on_exchange_completed( func _on_refresh_completed( _result: int, status: int, _headers: PackedStringArray, body: PackedByteArray) -> void: - var json := JSON.parse_string(body.get_string_from_utf8()) + var json :Variant= JSON.parse_string(body.get_string_from_utf8()) if status != 200 or json == null or not (json is Dictionary): auth_error.emit("Token refresh failed (HTTP %d)." % status) return diff --git a/Client/origin-client/scenes/login/login_screen.gd b/Client/origin-client/scenes/login/login_screen.gd index a4af017..f7f3872 100644 --- a/Client/origin-client/scenes/login/login_screen.gd +++ b/Client/origin-client/scenes/login/login_screen.gd @@ -33,6 +33,7 @@ func _ready() -> void: sign_in_btn.pressed.connect(_on_sign_in_pressed) # Web export: returning from Keycloak redirect — exchange is already in flight. + # Signals are connected above before this check so token_received is handled. if Keycloak.auth_in_progress: _set_status("Completing sign-in…", false) _set_enabled(false)