Add Keycloak authentication via Authorization Code + PKCE

- auth.proto: add ExchangeKeycloakToken RPC + request/response messages
- KeycloakOptions: binds Keycloak:Authority + Keycloak:ClientId from config
- KeycloakTokenValidator: validates Keycloak JWTs via OIDC discovery/JWKS;
  extracts sub, email, preferred_username, realm_access.roles
- AuthGrpcHandler.ExchangeKeycloakToken:
  - validates JWT and requires "player" realm role
  - lazy-migrates existing accounts by email on first Keycloak login
  - creates new passwordless accounts from Keycloak claims
  - syncs preferred_username → DisplayName on every login
  - single-session policy: revokes prior sessions before issuing SOR token
- AuthGrpcHandler.Register: assign "player" role to all new registrations
- ServiceRegistration: wire KeycloakOptions + KeycloakTokenValidator as singleton
- Admin SeedAsync: seed player, moderator, card_builder roles alongside Admin/SuperAdmin
- IGatewayAuthValidator / GatewayAuthValidator: add ExchangeKeycloakTokenAsync
- ConnectionLoop: handle auth.keycloak message type → auth.keycloak.ack
- appsettings.json: add Keycloak section (Authority + ClientId, overridable via env)

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 14:27:32 -04:00
co-authored by Claude Happy
parent 78f55930a0
commit d5363fa014
12 changed files with 328 additions and 13 deletions
+24
View File
@@ -0,0 +1,24 @@
namespace SOR.Auth;
/// <summary>
/// Bound from the "Keycloak" section of appsettings.
///
/// "Keycloak": {
/// "Authority": "https://auth.aeternalisgames.com/realms/aeternalis",
/// "ClientId": "sor-game"
/// }
///
/// Authority is the Keycloak realm URL — used as the JWT issuer and as the base
/// for OIDC discovery (Authority/.well-known/openid-configuration).
/// ClientId must match the "aud" claim in the access token.
/// </summary>
public sealed class KeycloakOptions
{
public const string Section = "Keycloak";
/// <summary>Keycloak realm URL, e.g. https://auth.aeternalisgames.com/realms/aeternalis</summary>
public string Authority { get; set; } = string.Empty;
/// <summary>Keycloak client ID configured for the game, e.g. sor-game</summary>
public string ClientId { get; set; } = string.Empty;
}