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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user