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
+301
View File
@@ -0,0 +1,301 @@
#include "evo/serialization/json.hpp"
#include <charconv>
#include <limits>
namespace evo::serialization {
namespace {
void append_utf8(std::string& out, std::uint32_t code) {
if (code < 0x80) {
out.push_back(static_cast<char>(code));
} else if (code < 0x800) {
out.push_back(static_cast<char>(0xC0 | (code >> 6U)));
out.push_back(static_cast<char>(0x80 | (code & 0x3FU)));
} else if (code < 0x10000) {
out.push_back(static_cast<char>(0xE0 | (code >> 12U)));
out.push_back(static_cast<char>(0x80 | ((code >> 6U) & 0x3FU)));
out.push_back(static_cast<char>(0x80 | (code & 0x3FU)));
} else {
out.push_back(static_cast<char>(0xF0 | (code >> 18U)));
out.push_back(static_cast<char>(0x80 | ((code >> 12U) & 0x3FU)));
out.push_back(static_cast<char>(0x80 | ((code >> 6U) & 0x3FU)));
out.push_back(static_cast<char>(0x80 | (code & 0x3FU)));
}
}
} // namespace
class JsonParser {
public:
explicit JsonParser(std::string_view text) : text_{text} {}
std::optional<JsonValue> document() {
auto value = parse_value(0);
skip_space();
if (!value || at_ != text_.size())
return std::nullopt;
return value;
}
private:
void skip_space() {
while (at_ < text_.size() && (text_[at_] == ' ' || text_[at_] == '\t' ||
text_[at_] == '\n' || text_[at_] == '\r'))
++at_;
}
bool literal(std::string_view word) {
if (text_.substr(at_, word.size()) != word)
return false;
at_ += word.size();
return true;
}
std::optional<std::uint32_t> hex4() {
if (at_ + 4 > text_.size())
return std::nullopt;
std::uint32_t value{};
for (int i = 0; i < 4; ++i) {
const char ch = text_[at_++];
value <<= 4U;
if (ch >= '0' && ch <= '9')
value |= static_cast<std::uint32_t>(ch - '0');
else if (ch >= 'a' && ch <= 'f')
value |= static_cast<std::uint32_t>(ch - 'a' + 10);
else if (ch >= 'A' && ch <= 'F')
value |= static_cast<std::uint32_t>(ch - 'A' + 10);
else
return std::nullopt;
}
return value;
}
std::optional<std::string> parse_string() {
if (at_ >= text_.size() || text_[at_] != '"')
return std::nullopt;
++at_;
std::string out;
while (at_ < text_.size()) {
const auto ch = static_cast<unsigned char>(text_[at_++]);
if (ch == '"')
return out;
if (ch < 0x20)
return std::nullopt;
if (ch != '\\') {
out.push_back(static_cast<char>(ch));
continue;
}
if (at_ >= text_.size())
return std::nullopt;
switch (text_[at_++]) {
case '"':
out.push_back('"');
break;
case '\\':
out.push_back('\\');
break;
case '/':
out.push_back('/');
break;
case 'b':
out.push_back('\b');
break;
case 'f':
out.push_back('\f');
break;
case 'n':
out.push_back('\n');
break;
case 'r':
out.push_back('\r');
break;
case 't':
out.push_back('\t');
break;
case 'u': {
auto code = hex4();
if (!code || (*code >= 0xDC00 && *code <= 0xDFFF))
return std::nullopt;
if (*code >= 0xD800 && *code <= 0xDBFF) {
if (!literal("\\u"))
return std::nullopt;
const auto low = hex4();
if (!low || *low < 0xDC00 || *low > 0xDFFF)
return std::nullopt;
code = 0x10000 + ((*code - 0xD800) << 10U) + (*low - 0xDC00);
}
append_utf8(out, *code);
break;
}
default:
return std::nullopt;
}
}
return std::nullopt;
}
std::optional<JsonValue> parse_number() {
const auto start = at_;
if (at_ < text_.size() && text_[at_] == '-')
++at_;
if (at_ >= text_.size())
return std::nullopt;
if (text_[at_] == '0') {
++at_;
} else if (text_[at_] >= '1' && text_[at_] <= '9') {
while (at_ < text_.size() && text_[at_] >= '0' && text_[at_] <= '9')
++at_;
} else {
return std::nullopt;
}
if (at_ < text_.size() && text_[at_] == '.') {
++at_;
const auto digits = at_;
while (at_ < text_.size() && text_[at_] >= '0' && text_[at_] <= '9')
++at_;
if (at_ == digits)
return std::nullopt;
}
if (at_ < text_.size() && (text_[at_] == 'e' || text_[at_] == 'E')) {
++at_;
if (at_ < text_.size() && (text_[at_] == '+' || text_[at_] == '-'))
++at_;
const auto digits = at_;
while (at_ < text_.size() && text_[at_] >= '0' && text_[at_] <= '9')
++at_;
if (at_ == digits)
return std::nullopt;
}
JsonValue value;
value.kind_ = JsonValue::Kind::number;
value.text_ = std::string(text_.substr(start, at_ - start));
return value;
}
// `depth` counts the containers enclosing this value.
std::optional<JsonValue> parse_value(std::size_t depth) {
skip_space();
if (at_ >= text_.size())
return std::nullopt;
if ((text_[at_] == '{' || text_[at_] == '[') && depth >= max_json_depth)
return std::nullopt;
JsonValue value;
const char ch = text_[at_];
if (ch == '{') {
++at_;
value.kind_ = JsonValue::Kind::object;
skip_space();
if (at_ < text_.size() && text_[at_] == '}') {
++at_;
return value;
}
for (;;) {
skip_space();
auto name = parse_string();
skip_space();
if (!name || at_ >= text_.size() || text_[at_] != ':')
return std::nullopt;
++at_;
auto member = parse_value(depth + 1);
if (!member)
return std::nullopt;
value.members_.emplace_back(std::move(*name), std::move(*member));
skip_space();
if (at_ >= text_.size())
return std::nullopt;
if (text_[at_] == ',') {
++at_;
continue;
}
if (text_[at_++] != '}')
return std::nullopt;
return value;
}
}
if (ch == '[') {
++at_;
value.kind_ = JsonValue::Kind::array;
skip_space();
if (at_ < text_.size() && text_[at_] == ']') {
++at_;
return value;
}
for (;;) {
auto item = parse_value(depth + 1);
if (!item)
return std::nullopt;
value.items_.push_back(std::move(*item));
skip_space();
if (at_ >= text_.size())
return std::nullopt;
if (text_[at_] == ',') {
++at_;
continue;
}
if (text_[at_++] != ']')
return std::nullopt;
return value;
}
}
if (ch == '"') {
auto text = parse_string();
if (!text)
return std::nullopt;
value.kind_ = JsonValue::Kind::string;
value.text_ = std::move(*text);
return value;
}
if (literal("true")) {
value.kind_ = JsonValue::Kind::boolean;
value.boolean_ = true;
return value;
}
if (literal("false")) {
value.kind_ = JsonValue::Kind::boolean;
return value;
}
if (literal("null"))
return value;
return parse_number();
}
std::string_view text_;
std::size_t at_{};
};
std::optional<JsonValue> JsonValue::parse(std::string_view text) {
if (text.size() > max_json_bytes)
return std::nullopt;
return JsonParser{text}.document();
}
std::optional<bool> JsonValue::boolean() const {
if (kind_ != Kind::boolean)
return std::nullopt;
return boolean_;
}
std::optional<std::string_view> JsonValue::string() const {
if (kind_ != Kind::string)
return std::nullopt;
return std::string_view{text_};
}
std::optional<std::uint64_t> JsonValue::integer() const {
if (kind_ != Kind::number || text_.empty() || text_.front() == '-' ||
text_.find_first_of(".eE") != std::string::npos)
return std::nullopt;
std::uint64_t value{};
const auto [end, error] = std::from_chars(text_.data(), text_.data() + text_.size(), value);
if (error != std::errc{} || end != text_.data() + text_.size() ||
value > static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()))
return std::nullopt;
return value;
}
const JsonValue* JsonValue::member(std::string_view name) const {
if (kind_ != Kind::object)
return nullptr;
for (const auto& [key, value] : members_)
if (key == name)
return &value;
return nullptr;
}
const std::vector<JsonValue>* JsonValue::array() const {
return kind_ == Kind::array ? &items_ : nullptr;
}
} // namespace evo::serialization