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