#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 #include #include #include #include #include #include // 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 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()>; // Supplies fresh 64-bit grant ids (random); false when randomness is unavailable. using GrantIdSource = std::function; 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 db_; std::map last_grant_; // Subject to wall seconds; bounded. }; } // namespace evo::server::broker