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
+3 -11
View File
@@ -5,28 +5,25 @@
This prevents version drift — if you need to upgrade a package, change it here only. -->
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<!-- ── Dependency Injection ──────────────────────────────────────────── -->
<!-- DI abstractions (IServiceCollection, IServiceProvider) for class library projects.
Web SDK projects (Microsoft.NET.Sdk.Web) get this transitively from the framework.
Pure class libraries need to reference it directly. -->
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<!-- ── gRPC ────────────────────────────────────────────────────────────── -->
<!-- ASP.NET Core gRPC server + client + Protobuf code generation -->
<PackageVersion Include="Grpc.AspNetCore" Version="2.62.0" />
<!-- gRPC client factory — integrates with ASP.NET Core DI + HttpClientFactory -->
<PackageVersion Include="Grpc.Net.ClientFactory" Version="2.62.0" />
<!-- ── NATS ───────────────────────────────────────────────────────────── -->
<!-- Official NATS .NET client (NATS.Net is the modern unified package) -->
<PackageVersion Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.0.0" />
<PackageVersion Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="8.0.0" />
<PackageVersion Include="NATS.Net" Version="2.3.0" />
<!-- ── Redis ─────────────────────────────────────────────────────────── -->
<!-- StackExchange.Redis: battle-tested, high-performance Redis client for .NET -->
<PackageVersion Include="StackExchange.Redis" Version="2.7.33" />
<!-- ── Entity Framework Core ─────────────────────────────────────────── -->
<!-- EF Core ORM: maps C# classes to database tables, generates SQL queries -->
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
@@ -36,11 +33,9 @@
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0" />
<!-- Identity with EF Core: user/role store backed by a DbContext -->
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
<!-- ── AWS SDK ────────────────────────────────────────────────────────── -->
<!-- S3-compatible object storage client (also works with Ceph RadosGW) -->
<PackageVersion Include="AWSSDK.S3" Version="3.7.414.0" />
<!-- ── Logging ────────────────────────────────────────────────────────── -->
<!-- Serilog: structured logging library (replaces Microsoft's default logger) -->
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.0" />
@@ -48,16 +43,13 @@
<PackageVersion Include="Serilog.Sinks.Console" Version="5.0.0" />
<!-- Seq sink: sends logs to a Seq log server for querying and alerting -->
<PackageVersion Include="Serilog.Sinks.Seq" Version="7.0.0" />
<!-- ── Serialization ─────────────────────────────────────────────────── -->
<!-- MessagePack: binary serialization, faster + smaller than JSON.
Registered as an alternative IMessageSerializer when performance matters. -->
<PackageVersion Include="MessagePack" Version="2.5.140" />
<!-- ── Health Checks ─────────────────────────────────────────────────── -->
<!-- Redis health check for Kubernetes liveness/readiness probes -->
<PackageVersion Include="AspNetCore.HealthChecks.Redis" Version="8.0.1" />
<!-- ── Testing ───────────────────────────────────────────────────────── -->
<!-- xUnit: the test framework (test discovery, runner, assertions) -->
<PackageVersion Include="xunit" Version="2.6.6" />
@@ -72,4 +64,4 @@
<!-- In-process test server for integration tests -->
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.0" />
</ItemGroup>
</Project>
</Project>