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)