Fix Keycloak auth failures: audience validation + packet drain race

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 <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-04-02 19:36:09 -04:00
co-authored by Claude Happy
parent 79fa08e192
commit cfb10ae4d0
2 changed files with 15 additions and 5 deletions
+9 -2
View File
@@ -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."
@@ -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;
}