Add M10.3a account-bound session grants and region session control
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m47s
Foundation / checks (client) (push) Failing after 9m42s
Foundation / checks (network) (push) Successful in 8m49s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 8m13s
Foundation / checks (tsan) (push) Canceled after 7m13s
Foundation / checks (checks) (push) Canceled after 17m58s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m47s
Foundation / checks (client) (push) Failing after 9m42s
Foundation / checks (network) (push) Successful in 8m49s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 8m13s
Foundation / checks (tsan) (push) Canceled after 7m13s
Foundation / checks (checks) (push) Canceled after 17m58s
Provider-independent 128-bit AccountIds; ECDSA P-256 session grants v1 signed by issuer keys and verified by regions with public keys only, the environment byte signed so dev grants never verify in stage or World0. Per-account serials make the newest grant win: replay and older stolen grants are refused, a newer grant replaces the account's session, and refresh renews in place. Attempt rates and capacities are bounded. The evo_identity library provides the provider, account directory and grant issuer interfaces with a dev-only test provider. ADR-0091. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,192 @@
|
||||
#include "evo/network/session_control.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
namespace evo::network {
|
||||
namespace {
|
||||
// Fixed attempt windows: restart when `now` left the window (or the clock went back).
|
||||
bool window_over(std::uint64_t start, std::uint64_t now, std::uint64_t length) {
|
||||
return now < start || now - start >= length;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool AccountId::valid() const noexcept {
|
||||
return std::ranges::any_of(bytes, [](std::uint8_t b) { return b != 0; });
|
||||
}
|
||||
|
||||
std::string log_label(const AccountId& account) {
|
||||
constexpr char digits[] = "0123456789abcdef";
|
||||
std::string label{"acct-"};
|
||||
for (std::size_t i = 0; i < 4; ++i) {
|
||||
label.push_back(digits[account.bytes.at(i) >> 4U]);
|
||||
label.push_back(digits[account.bytes.at(i) & 0xFU]);
|
||||
}
|
||||
return label;
|
||||
}
|
||||
|
||||
std::expected<SessionControl, ControlError> SessionControl::create(ControlConfig config) {
|
||||
if (config.world.world == 0 || config.world.frame == 0 || config.region == 0 ||
|
||||
config.epoch == 0 || config.max_sessions == 0 || config.max_sessions > 1024 ||
|
||||
config.max_accounts < config.max_sessions || config.max_accounts > (std::size_t{1} << 20) ||
|
||||
config.max_grant_lifetime == 0 || config.max_grant_lifetime > 3600 ||
|
||||
config.clock_skew > 300 || config.max_account_attempts == 0 ||
|
||||
config.max_region_attempts == 0 || config.attempt_window == 0 ||
|
||||
config.attempt_window > 3600)
|
||||
return std::unexpected(ControlError::invalid_config);
|
||||
SessionControl control{config};
|
||||
control.accounts_.reserve(config.max_accounts);
|
||||
return control;
|
||||
}
|
||||
|
||||
std::expected<void, ControlError> SessionControl::attempt(std::uint64_t now_seconds) {
|
||||
if (window_over(region_window_start_, now_seconds, config_.attempt_window)) {
|
||||
region_window_start_ = now_seconds;
|
||||
region_attempts_ = 0;
|
||||
}
|
||||
if (region_attempts_ >= config_.max_region_attempts)
|
||||
return std::unexpected(ControlError::rate_limited);
|
||||
++region_attempts_;
|
||||
return {};
|
||||
}
|
||||
|
||||
void SessionControl::forget_idle(std::uint64_t now_seconds) {
|
||||
std::erase_if(accounts_, [&](const Account& a) {
|
||||
return a.session == 0 && a.remember_until <= now_seconds &&
|
||||
window_over(a.window_start, now_seconds, config_.attempt_window);
|
||||
});
|
||||
}
|
||||
|
||||
std::expected<SessionControl::Account*, ControlError>
|
||||
SessionControl::admit(const VerifiedSessionGrant& grant, std::uint64_t now_seconds) {
|
||||
const auto& c = grant.claims();
|
||||
if (grant.environment() != config_.environment)
|
||||
return std::unexpected(ControlError::wrong_environment);
|
||||
if (c.world != config_.world)
|
||||
return std::unexpected(ControlError::wrong_world);
|
||||
if (c.region != config_.region)
|
||||
return std::unexpected(ControlError::wrong_region);
|
||||
if (c.expires_at <= c.issued_at || c.expires_at - c.issued_at > config_.max_grant_lifetime)
|
||||
return std::unexpected(ControlError::invalid_lifetime);
|
||||
if (c.issued_at > now_seconds && c.issued_at - now_seconds > config_.clock_skew)
|
||||
return std::unexpected(ControlError::not_yet_valid);
|
||||
if (now_seconds >= c.expires_at)
|
||||
return std::unexpected(ControlError::expired);
|
||||
forget_idle(now_seconds);
|
||||
auto found =
|
||||
std::ranges::find_if(accounts_, [&](const Account& a) { return a.id == c.account; });
|
||||
if (found == accounts_.end()) {
|
||||
if (accounts_.size() >= config_.max_accounts)
|
||||
return std::unexpected(ControlError::accounts_full);
|
||||
accounts_.push_back({c.account, 0, 0, 0, 0, now_seconds, 0});
|
||||
found = std::prev(accounts_.end());
|
||||
}
|
||||
auto& a = *found;
|
||||
// Every verified attempt by the account counts, accepted or not.
|
||||
if (window_over(a.window_start, now_seconds, config_.attempt_window)) {
|
||||
a.window_start = now_seconds;
|
||||
a.attempts = 0;
|
||||
}
|
||||
if (a.attempts >= config_.max_account_attempts)
|
||||
return std::unexpected(ControlError::rate_limited);
|
||||
++a.attempts;
|
||||
if (c.serial <= a.serial)
|
||||
return std::unexpected(ControlError::stale_grant);
|
||||
return &a;
|
||||
}
|
||||
|
||||
void SessionControl::accept(Account& a, const GrantClaims& c) {
|
||||
a.serial = c.serial;
|
||||
// Issuers keep serial and issued_at non-decreasing per account, so every older grant
|
||||
// expires by the newest issued_at plus the maximum lifetime: remember until then.
|
||||
a.remember_until = std::max(a.remember_until, c.issued_at + config_.max_grant_lifetime);
|
||||
a.session_expires_at = c.expires_at;
|
||||
}
|
||||
|
||||
std::expected<OpenedSession, ControlError> SessionControl::open(const VerifiedSessionGrant& grant,
|
||||
std::uint64_t now_seconds) {
|
||||
auto admitted = admit(grant, now_seconds);
|
||||
if (!admitted)
|
||||
return std::unexpected(admitted.error());
|
||||
auto& a = **admitted;
|
||||
// A refused open leaves the grant unconsumed: its serial is not recorded.
|
||||
if (a.session == 0 && sessions() >= config_.max_sessions)
|
||||
return std::unexpected(ControlError::sessions_full);
|
||||
OpenedSession opened;
|
||||
opened.superseded = a.session; // Closed before the new session exists.
|
||||
accept(a, grant.claims());
|
||||
a.session = next_session_++;
|
||||
opened.session = a.session;
|
||||
opened.epoch = config_.epoch;
|
||||
opened.expires_at = a.session_expires_at;
|
||||
return opened;
|
||||
}
|
||||
|
||||
std::expected<std::uint64_t, ControlError>
|
||||
SessionControl::refresh(std::uint64_t session, const VerifiedSessionGrant& grant,
|
||||
std::uint64_t now_seconds) {
|
||||
const auto holder_of =
|
||||
std::ranges::find_if(accounts_, [&](const Account& a) { return a.session == session; });
|
||||
if (session == 0 || holder_of == accounts_.end())
|
||||
return std::unexpected(ControlError::unknown_session);
|
||||
if (holder_of->id != grant.claims().account)
|
||||
return std::unexpected(ControlError::wrong_account);
|
||||
auto admitted = admit(grant, now_seconds);
|
||||
if (!admitted)
|
||||
return std::unexpected(admitted.error());
|
||||
accept(**admitted, grant.claims());
|
||||
return (*admitted)->session_expires_at;
|
||||
}
|
||||
|
||||
std::expected<void, ControlError> SessionControl::close(std::uint64_t session) {
|
||||
const auto found =
|
||||
std::ranges::find_if(accounts_, [&](const Account& a) { return a.session == session; });
|
||||
if (session == 0 || found == accounts_.end())
|
||||
return std::unexpected(ControlError::unknown_session);
|
||||
found->session = 0;
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<std::uint64_t> SessionControl::expire(std::uint64_t now_seconds) {
|
||||
std::vector<std::uint64_t> closed;
|
||||
for (auto& a : accounts_)
|
||||
if (a.session != 0 && now_seconds >= a.session_expires_at) {
|
||||
closed.push_back(a.session);
|
||||
a.session = 0;
|
||||
}
|
||||
return closed;
|
||||
}
|
||||
|
||||
std::expected<std::vector<std::uint64_t>, ControlError>
|
||||
SessionControl::advance_epoch(std::uint64_t epoch) {
|
||||
if (epoch <= config_.epoch)
|
||||
return std::unexpected(ControlError::invalid_config);
|
||||
config_.epoch = epoch;
|
||||
std::vector<std::uint64_t> closed;
|
||||
for (auto& a : accounts_)
|
||||
if (a.session != 0) {
|
||||
closed.push_back(a.session);
|
||||
a.session = 0;
|
||||
}
|
||||
return closed;
|
||||
}
|
||||
|
||||
std::optional<std::uint64_t> SessionControl::holder(const AccountId& account) const {
|
||||
const auto found =
|
||||
std::ranges::find_if(accounts_, [&](const Account& a) { return a.id == account; });
|
||||
if (found == accounts_.end() || found->session == 0)
|
||||
return std::nullopt;
|
||||
return found->session;
|
||||
}
|
||||
|
||||
std::optional<AccountId> SessionControl::account_of(std::uint64_t session) const {
|
||||
const auto found =
|
||||
std::ranges::find_if(accounts_, [&](const Account& a) { return a.session == session; });
|
||||
if (session == 0 || found == accounts_.end())
|
||||
return std::nullopt;
|
||||
return found->id;
|
||||
}
|
||||
|
||||
std::size_t SessionControl::sessions() const noexcept {
|
||||
return static_cast<std::size_t>(
|
||||
std::ranges::count_if(accounts_, [](const Account& a) { return a.session != 0; }));
|
||||
}
|
||||
} // namespace evo::network
|
||||
Reference in New Issue
Block a user