From 60d6098c119ed8d760312857142af974e0b31be7 Mon Sep 17 00:00:00 2001 From: dingo Date: Thu, 2 Apr 2026 16:20:45 -0400 Subject: [PATCH] Fix Keycloak login: force fresh WebSocket before auth.keycloak The server closes idle unauthenticated connections after AuthTimeoutMs (10 s). A user authenticating in the browser takes longer than that, so the socket the client held while the browser was open is already dead when token_received fires. Sending auth.keycloak on that stale connection produces no reply and the scene never changes. Fix: login_with_keycloak() now calls Network.force_reconnect() instead of sending immediately. _on_network_connected() then sends auth.keycloak as the guaranteed-first message on the fresh connection. Also: DISCONNECTED errors in the auth.keycloak error callback no longer clear keycloak_refresh_token, preserving silent re-auth on transient drops. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .claude/worktrees/agent-ac72c5f4 | 1 + Client/origin-client/autoloads/Auth.gd | 15 ++++++++++++--- Client/origin-client/autoloads/Keycloak.gd | 4 ++-- Client/origin-client/autoloads/Keycloak.gd.uid | 1 + 4 files changed, 16 insertions(+), 5 deletions(-) create mode 160000 .claude/worktrees/agent-ac72c5f4 create mode 100644 Client/origin-client/autoloads/Keycloak.gd.uid diff --git a/.claude/worktrees/agent-ac72c5f4 b/.claude/worktrees/agent-ac72c5f4 new file mode 160000 index 0000000..c756b9c --- /dev/null +++ b/.claude/worktrees/agent-ac72c5f4 @@ -0,0 +1 @@ +Subproject commit c756b9cffa5a357b6d35af523562b02ec6948760 diff --git a/Client/origin-client/autoloads/Auth.gd b/Client/origin-client/autoloads/Auth.gd index dcd328e..2e91986 100644 --- a/Client/origin-client/autoloads/Auth.gd +++ b/Client/origin-client/autoloads/Auth.gd @@ -79,9 +79,13 @@ func login_with_keycloak(access_token: String, refresh_token: String) -> void: keycloak_refresh_token = refresh_token _save_session() _pending_keycloak_token = access_token - if Network.is_online(): - _send_keycloak_auth() - # else: _on_network_connected will send it once the socket connects. + # Always force a fresh WebSocket connection so auth.keycloak is the very + # first message the server sees. The socket that was open while the user + # was in the browser has likely already been closed by the server's + # AuthTimeoutMs (10 s of silence) — sending on that stale connection + # yields no reply. force_reconnect() closes it immediately; the next + # _on_network_connected() will call _send_keycloak_auth(). + Network.force_reconnect() ## Clear all auth state and disconnect, returning to the login screen. @@ -146,6 +150,11 @@ func _send_keycloak_auth() -> void: { "keycloakToken": token }, func(_type: String, p: Variant): _on_auth_ok(p), func(code: String, msg: String): + if code == "DISCONNECTED": + # Transient drop — keep the refresh token and surface the error. + # _on_network_connected will retry auth.keycloak on the next reconnect. + auth_failed.emit(code, msg) + return # Keycloak token rejected (invalid, expired, missing role). keycloak_refresh_token = "" _save_session() diff --git a/Client/origin-client/autoloads/Keycloak.gd b/Client/origin-client/autoloads/Keycloak.gd index 4882ae2..fb3bd1c 100644 --- a/Client/origin-client/autoloads/Keycloak.gd +++ b/Client/origin-client/autoloads/Keycloak.gd @@ -243,7 +243,7 @@ func _exchange_code(code: String) -> void: func _on_exchange_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 exchange failed (HTTP %d)." % status) return @@ -257,7 +257,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/autoloads/Keycloak.gd.uid b/Client/origin-client/autoloads/Keycloak.gd.uid new file mode 100644 index 0000000..ea94c4d --- /dev/null +++ b/Client/origin-client/autoloads/Keycloak.gd.uid @@ -0,0 +1 @@ +uid://bqnvbrk2p83g1