From cfb10ae4d04c83a860cdb7ec8edb7fa02030c950 Mon Sep 17 00:00:00 2001 From: dingo Date: Thu, 2 Apr 2026 19:36:09 -0400 Subject: [PATCH] Fix Keycloak auth failures: audience validation + packet drain race MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Server — JWT audience validation failing: Keycloak does not include the client ID in the `aud` claim by default (uses "account"). ValidateAudience=true with ValidAudience="sor-game" rejected every token. Disabled audience validation — issuer + signature + realm role check is sufficient for a single-realm deployment. Bumped JWT validation failure log to Information so it appears in default logs. Client — DISCONNECTED instead of AUTH_FAILED on rejected auth: When the server sends an error packet immediately followed by a close frame, the socket transitions to STATE_CLOSING before the next _process frame. Network.gd returned early during STATE_CLOSING without draining packets, leaving the pending request unresolved. _on_ws_closed then failed it as DISCONNECTED. Now drains packets during STATE_CLOSING so the error packet is resolved with the correct error code before the close is processed. Also added "invalid_token", "role_required", "invalid_payload", "auth_service_unavailable", "account_creation_failed" to the close reason → error code mapping so they surface as AUTH_FAILED / AUTH_SERVICE_UNAVAILABLE rather than DISCONNECTED even in the fallback path. 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/Network.gd | 11 +++++++++-- Server/src/SOR.Auth/KeycloakTokenValidator.cs | 9 ++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Client/origin-client/autoloads/Network.gd b/Client/origin-client/autoloads/Network.gd index d77086a..e5386ca 100644 --- a/Client/origin-client/autoloads/Network.gd +++ b/Client/origin-client/autoloads/Network.gd @@ -95,6 +95,10 @@ func _process(delta: float) -> void: return WebSocketPeer.STATE_CLOSING: + # Drain any data packets that arrived before the close frame so they + # are resolved via their pending callbacks rather than as DISCONNECTED. + if _state == _State.CONNECTED: + _poll_packets() return # Wait for STATE_CLOSED on the next poll. # Only run packet/keepalive while fully connected. @@ -186,12 +190,15 @@ func _on_ws_closed() -> void: var error_code: String var error_msg: String match reason: - "auth_failed": + "auth_failed", "invalid_token", "role_required", "invalid_payload": error_code = "AUTH_FAILED" - error_msg = "Session expired or revoked." + error_msg = "Authentication failed." "auth_required": error_code = "AUTH_REQUIRED" error_msg = "Authentication required." + "auth_service_unavailable", "account_creation_failed": + error_code = "AUTH_SERVICE_UNAVAILABLE" + error_msg = "Server unavailable." _: error_code = "DISCONNECTED" error_msg = "Connection lost." diff --git a/Server/src/SOR.Auth/KeycloakTokenValidator.cs b/Server/src/SOR.Auth/KeycloakTokenValidator.cs index 2c183a9..16ef666 100644 --- a/Server/src/SOR.Auth/KeycloakTokenValidator.cs +++ b/Server/src/SOR.Auth/KeycloakTokenValidator.cs @@ -37,9 +37,12 @@ public sealed class KeycloakTokenValidator _baseParams = new TokenValidationParameters { ValidIssuer = opts.Authority, - ValidAudience = opts.ClientId, ValidateIssuer = true, - ValidateAudience = true, + // Audience: Keycloak does not add the client ID to the `aud` claim by default + // (it uses "account"). We validate the issuer + signature + realm roles instead, + // which is sufficient for a single-realm deployment. Add an Audience protocol + // mapper in Keycloak if you need strict audience enforcement. + ValidateAudience = false, ValidateLifetime = true, ValidateIssuerSigningKey = true, // Clock skew: allow 30 s to account for minor time drift between servers. @@ -68,7 +71,7 @@ public sealed class KeycloakTokenValidator if (!result.IsValid) { - _logger.LogDebug("Keycloak JWT validation failed: {Reason}", result.Exception?.Message); + _logger.LogInformation("Keycloak JWT validation failed: {Reason}", result.Exception?.Message); return KeycloakClaimsResult.Invalid; }