ADR-0131: Keycloak grant broker and schema v39 account identities
Foundation / checks (checks) (push) Successful in 42m13s
Foundation / checks (deploy) (push) Successful in 4m5s
Foundation / checks (ecs) (push) Successful in 2m25s
Foundation / checks (network) (push) Successful in 19m18s
Foundation / checks (postgres) (push) Successful in 44m19s
Foundation / checks (tsan) (push) Successful in 9m48s
Foundation / checks (client) (push) Failing after 12m6s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped

evo-grant-broker verifies realm aeternalis access tokens offline (RS256 against
the pinned JWKS; audience aevolution-grants, client aevolution-game, verified
email) and issues 10-minute session grants:
- POST /grant routes to the region holding the account's life, or the default.
- POST /logout makes every issued grant stale and expires current control.
- GET /healthz.

Schema v39 maps (issuer, subject) to stable accounts with strictly increasing
serials. The binary ships in the server image. The DEV manifests are in
deploy/kubernetes/grant-broker-dev and are not yet enabled in region/dev; that
waits for the image promotion and the out-of-band Secrets and Ingress.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-15 10:20:27 -04:00
co-authored by Claude Opus 5.5
parent 817a830a8a
commit 68880ba3b4
29 changed files with 1967 additions and 18 deletions
@@ -0,0 +1,66 @@
#pragma once
#include <cstdint>
#include <expected>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <vector>
// ADR-0131: verifies Keycloak access tokens (RS256 JWTs) against the realm's public
// keys, offline. Only what the broker needs is checked and returned.
namespace evo::server::broker {
struct AccessTokenRules {
std::string issuer; // The realm's issuer URL, exact.
std::string audience; // Must appear in `aud` (string or array).
std::string authorized_party; // `azp`: the game's public client.
std::uint64_t leeway_seconds{30};
};
struct AccessTokenClaims {
std::string issuer, subject;
std::uint64_t expires_at{};
};
enum class TokenError : std::uint8_t {
malformed,
unsupported_algorithm,
unknown_key,
bad_signature,
wrong_issuer,
wrong_audience,
wrong_party,
wrong_type,
expired,
not_yet_valid,
unverified_email,
missing_subject
};
inline constexpr std::size_t max_access_token_bytes = 6144;
class TokenVerifier {
public:
// RSA signing keys (`use` sig or absent, `alg` RS256 or absent, at least 2048 bits)
// from a JWKS document; other keys are ignored. Fails when none is usable.
[[nodiscard]] static std::expected<TokenVerifier, TokenError> from_jwks(std::string_view jwks,
AccessTokenRules rules);
[[nodiscard]] std::expected<AccessTokenClaims, TokenError>
verify(std::string_view token, std::uint64_t now_seconds) const;
[[nodiscard]] std::size_t keys() const noexcept {
return keys_.size();
}
private:
struct Key {
std::string kid;
std::shared_ptr<void> public_key; // EVP_PKEY, freed by its deleter.
};
TokenVerifier(std::vector<Key> keys, AccessTokenRules rules)
: keys_{std::move(keys)}, rules_{std::move(rules)} {}
std::vector<Key> keys_;
AccessTokenRules rules_;
};
[[nodiscard]] std::optional<std::vector<std::uint8_t>> base64url_decode(std::string_view text);
[[nodiscard]] std::string base64_encode(std::span<const std::uint8_t> bytes);
} // namespace evo::server::broker
@@ -0,0 +1,54 @@
#pragma once
#include "evo/network/session_grant.hpp"
#include "evo/persistence/postgres.hpp"
#include "evo/server/broker/access_token.hpp"
#include "evo/server/ops_http.hpp"
#include <chrono>
#include <cstdint>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <vector>
// ADR-0131: turns a verified Keycloak access token into a short-lived session grant
// for the region holding the account's life, and revokes an account's grants on logout.
namespace evo::server::broker {
struct BrokerRoute {
std::uint64_t region{};
std::string endpoint, server_name; // "host:port" and the region's TLS name.
};
struct BrokerConfig {
std::uint64_t world{}, frame{}, default_region{};
std::vector<BrokerRoute> routes; // Every region a grant may name.
std::uint64_t grant_lifetime_seconds{600};
std::chrono::seconds min_interval{2}; // Per account, between grants.
std::chrono::milliseconds db_timeout{3000};
};
using BrokerConnect = std::function<std::unique_ptr<persistence::Postgres>()>;
// Supplies fresh 64-bit grant ids (random); false when randomness is unavailable.
using GrantIdSource = std::function<bool(std::uint64_t&)>;
class GrantBroker {
public:
GrantBroker(BrokerConfig config, TokenVerifier verifier, network::GrantSigningKey key,
BrokerConnect connect, GrantIdSource ids);
// GET /healthz, POST /grant and POST /logout. `now_seconds` is wall time.
[[nodiscard]] OpsResponse handle(const ApiRequest& request, std::uint64_t now_seconds);
private:
[[nodiscard]] OpsResponse issue(const AccessTokenClaims& claims, std::uint64_t now_seconds);
[[nodiscard]] OpsResponse logout(const AccessTokenClaims& claims);
persistence::Postgres* database();
BrokerConfig config_;
TokenVerifier verifier_;
network::GrantSigningKey key_;
BrokerConnect connect_;
GrantIdSource ids_;
std::unique_ptr<persistence::Postgres> db_;
std::map<std::string, std::uint64_t> last_grant_; // Subject to wall seconds; bounded.
};
} // namespace evo::server::broker
+44
View File
@@ -0,0 +1,44 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
// ADR-0131: a small, bounded JSON reader for access-token claims and the realm's JWKS.
// It accepts RFC 8259 JSON within the bounds below and nothing else (no comments, no
// trailing commas, no duplicate-key resolution beyond "first wins").
namespace evo::server::broker {
inline constexpr std::size_t max_json_bytes = std::size_t{16} * 1024;
inline constexpr std::size_t max_json_depth = 16;
class JsonValue {
public:
enum class Kind : std::uint8_t { null, boolean, number, string, array, object };
[[nodiscard]] Kind kind() const noexcept {
return kind_;
}
// Typed views; nullopt or nullptr when the value has another kind.
[[nodiscard]] std::optional<bool> boolean() const;
[[nodiscard]] std::optional<std::string_view> string() const;
// Integers only (no fraction or exponent), within 0..2^63-1.
[[nodiscard]] std::optional<std::uint64_t> integer() const;
[[nodiscard]] const JsonValue* member(std::string_view name) const;
[[nodiscard]] const std::vector<JsonValue>* array() const;
// Whole-document parse; nullopt for anything malformed or out of bounds.
[[nodiscard]] static std::optional<JsonValue> parse(std::string_view text);
private:
friend class JsonParser;
Kind kind_{Kind::null};
bool boolean_{};
std::string text_; // String value, or the number's literal.
std::vector<JsonValue> items_;
std::vector<std::pair<std::string, JsonValue>> members_;
};
} // namespace evo::server::broker
+11
View File
@@ -22,6 +22,12 @@ struct OpsResponse {
};
// Receives the request path (no query handling: probes use exact paths).
using OpsHandler = std::function<OpsResponse(std::string_view path)>;
// ADR-0131 API mode (the grant broker): GET or POST without a body, the path and the
// Authorization header value (empty when absent). Views are valid during the call only.
struct ApiRequest {
std::string_view method, path, authorization;
};
using ApiHandler = std::function<OpsResponse(const ApiRequest&)>;
enum class OpsError : std::uint8_t { address, socket };
class OpsServer {
@@ -31,6 +37,7 @@ class OpsServer {
// pressure cannot hold the host loop; the rest wait in the listen backlog.
static constexpr std::size_t max_accepts_per_serve = 4;
static constexpr std::size_t max_request_bytes = 4096;
static constexpr std::size_t max_api_request_bytes = 8192; // Room for an access token.
static constexpr std::size_t max_response_bytes = 16384;
static constexpr std::chrono::milliseconds connection_deadline{1000};
@@ -49,6 +56,8 @@ class OpsServer {
// expires connections without waiting. Connections beyond the bound are closed
// at once; slow or oversized ones are answered or dropped without delaying others.
void serve(const OpsHandler& handler);
// The same bounded loop for API requests (max_api_request_bytes).
void serve_api(const ApiHandler& handler);
[[nodiscard]] std::uint64_t refused() const noexcept {
return refused_;
}
@@ -65,6 +74,8 @@ class OpsServer {
};
OpsServer(int fd, std::uint16_t port) : fd_(fd), port_(port) {}
void respond(Connection& c, const OpsHandler& handler);
void respond_api(Connection& c, const ApiHandler& handler);
template <typename Answer> void serve_with(std::size_t request_limit, const Answer& answer);
int fd_;
std::uint16_t port_;
std::vector<Connection> connections_;