ADR-0131: Keycloak sign-in in the Windows engineering client
Foundation / checks (ecs) (push) Successful in 2m21s
Foundation / checks (deploy) (push) Successful in 4m1s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 7m21s
Foundation / checks (network) (push) Canceled after 9m43s
Foundation / checks (checks) (push) Canceled after 9m49s
Foundation / checks (postgres) (push) Canceled after 9m43s
Foundation / checks (client) (push) Canceled after 9m48s

Sign In / Register opens the system browser (PKCE S256, loopback 127.0.0.1:47600)
and keeps the refresh token in Windows Credential Manager. The client then fetches
a 10-minute grant from grants.aeternalisgames.com and connects to the cellular DEV
region. It renews the grant about a minute before expiry through SessionRefresh,
and again after a lost connection. Sign Out closes the session and revokes the
login at the broker and at Keycloak. Grant replies are cross-checked against the
grant record before use. The manual grant-bundle path stays for developers.

The bounded JSON reader moves to evo_json (evo::serialization), shared by the
broker and the client. Adds the client-auth tests. Completes work started in
another session: main.cpp wiring, refresh expiry tracking, no requests for a
superseded session, and clang-tidy and Windows header-order fixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-15 15:31:33 -04:00
co-authored by Claude Opus 5.5
parent dec34f39e4
commit 2a5c0d0e22
22 changed files with 1622 additions and 72 deletions
+85
View File
@@ -1,6 +1,7 @@
#include "cellular_scene.hpp"
#include "controls_flow.hpp"
#include "evo/client/application.hpp"
#include "evo/client/auth.hpp"
#include "evo/client/launcher.hpp"
#include "evo/client/selection.hpp"
#include "evo/client/session_host.hpp"
@@ -19,6 +20,7 @@
#include <filesystem>
#include <memory>
#include <optional>
#include <span>
#include <string_view>
#include <thread>
#include <utility>
@@ -229,6 +231,12 @@ std::uint64_t milliseconds(std::chrono::steady_clock::time_point t) {
return static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(t.time_since_epoch()).count());
}
// Wall-clock seconds: grant expiry is UTC (the session compares the server's welcome).
std::uint64_t utc_seconds() {
return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count());
}
} // namespace
int main(int argc, char** argv) try {
@@ -351,6 +359,36 @@ int main(int argc, char** argv) try {
return host.connect(std::move(opened));
}
};
// ADR-0131: a broker grant names its region route; only the region CA comes from the
// form. The grant stays in memory and the session asks for the next one itself.
const auto grant_material = [&](const evo::client::AuthGrant& grant) {
auto f = buffers.form();
f.endpoint = grant.endpoint;
f.server_name = grant.server_name;
return evo::client::load_connect(f, std::span{&grant.bytes, 1});
};
const auto connect_signed_in = [&](const evo::client::AuthGrant& grant) {
form_error.reset();
if constexpr (!evo::client_app::remote_available) {
form_error = evo::client::LaunchError::no_session;
return false;
} else {
const auto material = grant_material(grant);
if (!material) {
form_error = material.error();
return false;
}
cellular_trial = true; // The DEV broker world is the cellular trial (ADR-0131).
auto opened = evo::client_app::RemoteSession::open(*material, cellular_trial);
if (!opened) {
form_error = evo::client::LaunchError::endpoint;
return false;
}
opened->manage_grants();
cellular_framed = false;
return host.connect(std::move(opened));
}
};
const auto enter_fixture = [&] {
if (!authority) {
authority = make_authority();
@@ -419,6 +457,10 @@ int main(int argc, char** argv) try {
return &remote->organisms();
return nullptr;
};
// ADR-0131 browser sign-in (Windows); elsewhere the panel offers manual grants only.
evo::client::AuthClient auth{evo::client::native_auth_platform()};
bool auth_connect = false; // Connect when the requested grant arrives.
auto grant_requested = std::chrono::steady_clock::time_point{};
int drawn_organisms = -1;
evo::client_app::PanelActions actions;
(*app)->set_overlay([&] {
@@ -428,6 +470,7 @@ int main(int argc, char** argv) try {
view.form_error = form_error;
view.remote_available = evo::client_app::remote_available;
view.cellular_trial = cellular_trial;
view.auth = auth.status();
view.environment = shown_environment();
view.organisms = shown_organisms();
view.selection = &selection;
@@ -512,6 +555,48 @@ int main(int argc, char** argv) try {
remote->set_cellular_feed_target(cellular_trial ? cellular_feed_target : std::nullopt);
remote->set_cellular_controls(buffers.cellular_controls);
}
// ADR-0131 sign-in. One request at a time; grants arrive on a later frame.
if (actions.sign_in && auth.sign_in()) {
auth_connect = true;
grant_requested = now;
}
if (actions.auth_connect && auth.request_grant()) {
auth_connect = true;
grant_requested = now;
}
if (actions.cancel_sign_in) {
auth.cancel();
auth_connect = false;
}
if (actions.sign_out) {
host.cancel(); // Logout revokes every grant; do not keep a session behind it.
cellular_feed_target.reset();
selection.clear();
auth_connect = false;
static_cast<void>(auth.sign_out());
}
if (auto grant = auth.take_grant()) {
if (auth_connect) {
auth_connect = false;
if (connect_signed_in(*grant))
selection.clear();
} else if (auto* remote = host.session()) {
// Refresh or reconnect material for the live session (same account).
if (const auto material = grant_material(*grant))
static_cast<void>(remote->supply_grant(*material));
}
grant->bytes.fill(0);
}
if (const auto status = auth.status(); !status.busy && auth_connect && !status.signed_in)
auth_connect = false; // Sign-in failed or was cancelled; the panel shows why.
// About a minute before expiry, or after a lost connection; never for a session the
// launcher has given up on (superseded by another client, cancelled).
const bool live = launcher.phase() == evo::client::LaunchPhase::connecting ||
launcher.phase() == evo::client::LaunchPhase::connected;
if (const auto* remote = host.session();
live && remote && remote->needs_grant(utc_seconds()) &&
now - grant_requested >= std::chrono::seconds{5} && auth.request_grant())
grant_requested = now;
controls.step(actions, **app, ui);
actions = {};
// The launcher and modal dialogs own input: no world movement or selection.
+50 -12
View File
@@ -121,18 +121,48 @@ void launcher(const PanelView& v, FormBuffers& form, PanelActions& a) {
"server, not an ecosystem, not authoritative.");
ImGui::Separator();
ImGui::TextUnformatted("Connect to a dev region");
ImGui::BeginDisabled(!v.remote_available || phase == LaunchPhase::connecting);
ImGui::InputText("Endpoint (host:port)", form.endpoint.data(), form.endpoint.size());
ImGui::InputText("Server name", form.server_name.data(), form.server_name.size());
ImGui::InputText("Dev CA file", form.authority.data(), form.authority.size());
ImGui::InputText("Grant bundle file", form.grants.data(), form.grants.size());
bool cellular_trial = v.cellular_trial;
if (ImGui::Checkbox("Cellular play (v10)", &cellular_trial))
a.cellular_trial = cellular_trial;
const bool start = phase == LaunchPhase::choosing && ImGui::Button("Connect", {px(160), 0});
ImGui::InputText("Region CA file", form.authority.data(), form.authority.size());
ImGui::BeginDisabled(!v.remote_available || !v.auth.supported || v.auth.busy ||
phase == LaunchPhase::connecting);
if (ImGui::Button(v.auth.signed_in ? "Play in DEV" : "Sign in / Register", {-1, px(34)})) {
a.sign_in = !v.auth.signed_in;
a.auth_connect = v.auth.signed_in;
}
ImGui::EndDisabled();
if (start)
a.connect = true;
if (v.auth.busy) {
ImGui::TextWrapped(
"%s",
v.auth.phase == client::AuthPhase::signing_out ? "Signing out..."
: v.auth.phase == client::AuthPhase::renewing
? "Renewing your connection..."
: "Complete sign-in in your browser, then return here. Connecting is automatic.");
if (v.auth.phase == client::AuthPhase::signing_in && ImGui::Button("Cancel sign-in"))
a.cancel_sign_in = true;
} else if (v.auth.signed_in && ImGui::SmallButton("Sign out"))
a.sign_out = true;
if (v.auth.error) {
ImGui::PushStyleColor(ImGuiCol_Text, amber);
const auto text = client::auth_error_text(*v.auth.error);
ImGui::PushTextWrapPos(0.0F);
ImGui::TextUnformatted(text.data(), text.data() + text.size());
ImGui::PopTextWrapPos();
ImGui::PopStyleColor();
}
ImGui::TextWrapped(
"Your password stays in the system browser. Windows Credential Manager stores your login.");
if (ImGui::CollapsingHeader("Manual connection (developer grants)")) {
ImGui::BeginDisabled(!v.remote_available || phase == LaunchPhase::connecting);
ImGui::InputText("Endpoint (host:port)", form.endpoint.data(), form.endpoint.size());
ImGui::InputText("Server name", form.server_name.data(), form.server_name.size());
ImGui::InputText("Grant bundle file", form.grants.data(), form.grants.size());
bool cellular_trial = v.cellular_trial;
if (ImGui::Checkbox("Cellular play (v10)", &cellular_trial))
a.cellular_trial = cellular_trial;
const bool start = phase == LaunchPhase::choosing && ImGui::Button("Connect", {px(160), 0});
ImGui::EndDisabled();
if (start)
a.connect = true;
}
if (!v.remote_available)
ImGui::TextColored(amber, "Remote mode needs a build with EVO_BUILD_NETWORK.");
if (v.form_error)
@@ -150,7 +180,8 @@ void launcher(const PanelView& v, FormBuffers& form, PanelActions& a) {
if (ImGui::Button("Cancel", {px(120), 0}))
a.cancel = true;
}
ImGui::TextDisabled("Files are read when you connect; nothing is saved.");
ImGui::TextWrapped("The region CA is still required to verify the game server. Manual grant "
"files are only read when connecting.");
ImGui::End();
}
@@ -168,6 +199,13 @@ void banner(const PanelView& v, PanelActions& a) {
ImGui::SameLine();
if (ImGui::SmallButton("Controls"))
a.open_controls = true;
if (v.auth.signed_in) {
ImGui::SameLine();
ImGui::BeginDisabled(v.auth.busy);
if (ImGui::SmallButton("Sign out"))
a.sign_out = true;
ImGui::EndDisabled();
}
} else {
ImGui::TextColored(v.remote && v.remote->phase == "connected" ? green : amber,
"DEV REGION - %s%s", v.remote ? v.remote->phase.c_str() : "?",
+3
View File
@@ -2,6 +2,7 @@
// M9.8c engineering panels (ADR-0084). Drawn inside Application's overlay callback;
// they read presentation copies and report what the user asked for. Only this file
// pair and the platform adapter use the toolkit.
#include "evo/client/auth.hpp"
#include "evo/client/bindings.hpp"
#include "evo/client/environment_view.hpp"
#include "evo/client/launcher.hpp"
@@ -41,6 +42,7 @@ struct PanelView {
std::optional<client::LaunchError> form_error;
bool remote_available{};
bool cellular_trial{};
client::AuthStatus auth;
const client::EnvironmentLayer* environment{};
const client::OrganismLayer* organisms{};
const client::Selection* selection{};
@@ -59,6 +61,7 @@ struct PanelView {
};
struct PanelActions {
bool fixture{}, connect{}, cancel{}, retry{}, back{}, ask_disconnect{};
bool sign_in{}, sign_out{}, auth_connect{}, cancel_sign_in{};
std::optional<std::uint64_t> dismiss_notice, confirm_dialog, cancel_dialog;
bool open_controls{}, apply_controls{}, reset_controls{}, close_controls{}, cancel_capture{};
std::optional<ControlSlot> capture_key;
+69 -3
View File
@@ -174,6 +174,8 @@ class RemoteSession {
session.transport_.authority_pem = m.authority_pem;
session.transport_.authority_pem_file = m.authority_path;
session.grants_ = m.grants;
std::copy_n(m.grants.front().begin() + 32, session.account_.size(),
session.account_.begin());
session.cellular_trial_ = cellular_trial;
session.cellular_play_.initialize(static_cast<std::uint64_t>(
std::chrono::system_clock::now().time_since_epoch().count()));
@@ -187,6 +189,47 @@ class RemoteSession {
RemoteSession& operator=(RemoteSession&&) noexcept = default;
RemoteSession(const RemoteSession&) = delete;
RemoteSession& operator=(const RemoteSession&) = delete;
void manage_grants() {
managed_grants_ = true;
}
[[nodiscard]] bool needs_grant(std::uint64_t utc_now) const {
if (!managed_grants_)
return false;
if (!client_)
return exhausted_ || std::ranges::all_of(used_, [](bool used) { return used; });
const auto& welcome = client_->welcome();
return client_->state() == client::RegionSessionState::established && welcome &&
std::max(welcome->expires_at, refreshed_until_) <= utc_now + 60 && !refresh_waiting_;
}
// Grants are supplied by the authenticated broker, never written to a bundle.
[[nodiscard]] bool supply_grant(const client::ConnectMaterial& material) {
if (!managed_grants_ || material.grants.size() != 1 ||
material.authority_pem != transport_.authority_pem)
return false;
const auto& grant = material.grants.front();
const auto serial = grant_serial(grant);
if (serial < newest_serial_ || serial == UINT64_MAX ||
!std::equal(account_.begin(), account_.end(), grant.begin() + 32))
return false;
const auto region = grant_region(grant);
if (client_ && client_->state() == client::RegionSessionState::established) {
if (region != region_ || !client_->refresh(grant))
return false;
newest_serial_ = serial + 1;
refresh_waiting_ = true;
refresh_sent_at_ = Clock::now();
return true;
}
if (!learn_route({region,
material.endpoint.host + ":" + std::to_string(material.endpoint.port),
material.server_name}))
return false;
grants_.assign(1, grant);
used_.assign(1, false);
target_region_ = region;
exhausted_ = false;
return true;
}
~RemoteSession() {
if (client_)
client_->close(); // Close{client_closed}; unresolved intentions stay unknown.
@@ -350,6 +393,13 @@ class RemoteSession {
return;
auto& c = *client_;
c.poll(now);
if (const auto refreshed = c.take_refresh_result()) {
refresh_waiting_ = false;
if (refreshed->status == 0) // Accepted: the session now runs to the new expiry.
refreshed_until_ = std::max(refreshed_until_, refreshed->expires_at);
} else if (refresh_waiting_ && now - refresh_sent_at_ >= std::chrono::seconds{5}) {
refresh_waiting_ = false;
}
if (c.state() == client::RegionSessionState::closed) {
end(now, ui, launcher);
return;
@@ -472,9 +522,12 @@ class RemoteSession {
if (pick == grants_.size()) {
if (!exhausted_) {
exhausted_ = true;
launcher.observe(client::ConnectSignal::exhausted);
std::printf("session grants for region %llu exhausted; issue a new bundle\n",
static_cast<unsigned long long>(target_region_));
if (!managed_grants_) {
launcher.observe(client::ConnectSignal::exhausted);
std::printf(
"session grants for region %llu exhausted; issue a new bundle\n",
static_cast<unsigned long long>(target_region_));
}
}
return false;
}
@@ -491,6 +544,8 @@ class RemoteSession {
}
client_ = std::move(*made);
established_ = false;
refreshed_until_ = 0; // A new session starts from its own welcome.
refresh_waiting_ = false;
return true;
}
void pause(Clock::time_point now) {
@@ -860,6 +915,10 @@ class RemoteSession {
network::quic::ClientConfig transport_;
std::vector<network::WireGrant> grants_;
std::vector<bool> used_;
std::array<std::uint8_t, 16> account_{};
bool managed_grants_{}, refresh_waiting_{};
std::uint64_t refreshed_until_{}; // Expiry from the last accepted SessionRefresh.
Clock::time_point refresh_sent_at_{};
std::map<std::uint64_t, network::quic::ClientConfig> routes_; // Region -> how to dial it.
std::uint64_t target_region_{}, region_{}, handoffs_{}, newest_serial_{};
std::optional<network::WireGrant> ticket_; // A handoff ticket for the next connection.
@@ -903,6 +962,13 @@ class RemoteSession {
inline constexpr bool remote_available = false;
class RemoteSession {
public:
void manage_grants() {}
[[nodiscard]] bool needs_grant(std::uint64_t) const {
return false;
}
[[nodiscard]] bool supply_grant(const client::ConnectMaterial&) {
return false;
}
static std::optional<RemoteSession> open(const client::ConnectMaterial&, bool = false) {
return std::nullopt;
}