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,138 @@
|
||||
#pragma once
|
||||
#include "evo/network/admission.hpp"
|
||||
#include "evo/world/chunks.hpp"
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// M10.3 account-bound session control for one region (ADR-0091). Transport-neutral and
|
||||
// single-owner. It decides which session, if any, controls each account in this region
|
||||
// and leaves intentions to 10.4. A grant authorizes opening (or refreshing) a session;
|
||||
// per-account grant serials order grants so the newest wins: a grant is usable once and
|
||||
// never after a newer one for the same account (replay and stolen stale grants fail).
|
||||
// The M9 dev path (SessionAdmission, protocol v1) is unchanged.
|
||||
namespace evo::network {
|
||||
// Provider-independent account identity: 128 random bits assigned by the account
|
||||
// directory, never derived from a provider id (Steam, dev test or otherwise).
|
||||
struct AccountId {
|
||||
std::array<std::uint8_t, 16> bytes{};
|
||||
[[nodiscard]] bool valid() const noexcept;
|
||||
auto operator<=>(const AccountId&) const = default;
|
||||
};
|
||||
// Short non-secret label for logs and events: the first 4 bytes in hex ("acct-1a2b3c4d").
|
||||
// Grants, signatures and keys have no log form at all.
|
||||
[[nodiscard]] std::string log_label(const AccountId& account);
|
||||
|
||||
// Claims of a production session grant (schemas/network/session-grant-v1.md).
|
||||
struct GrantClaims {
|
||||
std::uint64_t grant_id{};
|
||||
AccountId account;
|
||||
std::uint64_t serial{}; // Strictly increasing per account; issued by the directory.
|
||||
world::WorldIdentity world;
|
||||
std::uint64_t region{}, issued_at{}, expires_at{}; // Server clock, seconds.
|
||||
};
|
||||
class GrantVerifier;
|
||||
// Only GrantVerifier::verify constructs this, after the signature and environment checked.
|
||||
class VerifiedSessionGrant {
|
||||
public:
|
||||
[[nodiscard]] const GrantClaims& claims() const noexcept {
|
||||
return claims_;
|
||||
}
|
||||
[[nodiscard]] DeploymentEnvironment environment() const noexcept {
|
||||
return environment_;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class GrantVerifier;
|
||||
VerifiedSessionGrant(DeploymentEnvironment environment, const GrantClaims& claims)
|
||||
: claims_{claims}, environment_{environment} {}
|
||||
GrantClaims claims_;
|
||||
DeploymentEnvironment environment_;
|
||||
};
|
||||
|
||||
struct ControlConfig {
|
||||
DeploymentEnvironment environment{DeploymentEnvironment::dev};
|
||||
world::WorldIdentity world;
|
||||
std::uint64_t region{}, epoch{};
|
||||
std::size_t max_sessions{32};
|
||||
std::size_t max_accounts{4096}; // Tracked accounts (sessions, serials, rates).
|
||||
std::uint64_t max_grant_lifetime{300}; // Seconds; grants are short-lived.
|
||||
std::uint64_t clock_skew{30}; // Seconds tolerated before issued_at.
|
||||
// Every verified open or refresh by an account counts, accepted or not.
|
||||
std::uint32_t max_account_attempts{4};
|
||||
// Every attempt before signature verification counts, region-wide.
|
||||
std::uint32_t max_region_attempts{64};
|
||||
std::uint64_t attempt_window{60}; // Seconds, for both limits.
|
||||
};
|
||||
enum class ControlError : std::uint8_t {
|
||||
invalid_config,
|
||||
wrong_environment,
|
||||
wrong_world,
|
||||
wrong_region,
|
||||
invalid_lifetime,
|
||||
not_yet_valid,
|
||||
expired,
|
||||
stale_grant, // Serial not newer than one already presented for this account.
|
||||
rate_limited,
|
||||
sessions_full,
|
||||
accounts_full, // Fail closed: never forget a serial early.
|
||||
unknown_session,
|
||||
wrong_account
|
||||
};
|
||||
struct OpenedSession {
|
||||
std::uint64_t session{};
|
||||
std::uint64_t superseded{}; // The account's previous session, now closed (0 if none).
|
||||
std::uint64_t epoch{}, expires_at{};
|
||||
};
|
||||
|
||||
class SessionControl {
|
||||
public:
|
||||
[[nodiscard]] static std::expected<SessionControl, ControlError> create(ControlConfig config);
|
||||
// Region-wide attempt limit, checked before any signature work. Counts the attempt.
|
||||
[[nodiscard]] std::expected<void, ControlError> attempt(std::uint64_t now_seconds);
|
||||
// A newer grant for an account that already holds a session takes control: the old
|
||||
// session is closed first and reported as superseded. Control is never shared.
|
||||
[[nodiscard]] std::expected<OpenedSession, ControlError> open(const VerifiedSessionGrant& grant,
|
||||
std::uint64_t now_seconds);
|
||||
// In-session renewal with a newer grant for the same account: extends the expiry
|
||||
// without a reconnect or new baseline.
|
||||
[[nodiscard]] std::expected<std::uint64_t, ControlError>
|
||||
refresh(std::uint64_t session, const VerifiedSessionGrant& grant, std::uint64_t now_seconds);
|
||||
[[nodiscard]] std::expected<void, ControlError> close(std::uint64_t session);
|
||||
// Sessions whose grant expired at `now_seconds`; they are closed by this call.
|
||||
[[nodiscard]] std::vector<std::uint64_t> expire(std::uint64_t now_seconds);
|
||||
// New ownership epoch: every session closes (ADR-0089: none survives an epoch change).
|
||||
[[nodiscard]] std::expected<std::vector<std::uint64_t>, ControlError>
|
||||
advance_epoch(std::uint64_t epoch);
|
||||
[[nodiscard]] std::optional<std::uint64_t> holder(const AccountId& account) const;
|
||||
[[nodiscard]] std::optional<AccountId> account_of(std::uint64_t session) const;
|
||||
[[nodiscard]] std::size_t sessions() const noexcept;
|
||||
[[nodiscard]] std::uint64_t epoch() const noexcept {
|
||||
return config_.epoch;
|
||||
}
|
||||
|
||||
private:
|
||||
struct Account {
|
||||
AccountId id;
|
||||
std::uint64_t serial{}; // Newest grant serial presented and accepted.
|
||||
std::uint64_t remember_until{}; // Every grant up to `serial` has expired by then.
|
||||
std::uint64_t session{}, session_expires_at{};
|
||||
std::uint64_t window_start{};
|
||||
std::uint32_t attempts{};
|
||||
};
|
||||
explicit SessionControl(ControlConfig config) : config_{config} {}
|
||||
[[nodiscard]] std::expected<Account*, ControlError> admit(const VerifiedSessionGrant& grant,
|
||||
std::uint64_t now_seconds);
|
||||
void accept(Account& account, const GrantClaims& claims);
|
||||
void forget_idle(std::uint64_t now_seconds);
|
||||
ControlConfig config_;
|
||||
std::uint64_t next_session_{1};
|
||||
std::uint64_t region_window_start_{};
|
||||
std::uint32_t region_attempts_{};
|
||||
std::vector<Account> accounts_;
|
||||
};
|
||||
} // namespace evo::network
|
||||
@@ -0,0 +1,99 @@
|
||||
#pragma once
|
||||
#include "evo/network/session_control.hpp"
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
|
||||
// Production session grants v1 (ADR-0091, schemas/network/session-grant-v1.md): a fixed
|
||||
// 160-byte record ("EVO-GRANT" marker, version, environment, issuer key id, claims)
|
||||
// signed with ECDSA P-256/SHA-256 through Mbed TLS PSA. Regions hold only public keys,
|
||||
// so a compromised region cannot mint grants. The signed environment byte must match
|
||||
// the verifier's environment: a dev-signed grant never verifies in stage or World0, and
|
||||
// M9 dev grants (a different marker and length) never verify here. Grant bytes and keys
|
||||
// have no log form.
|
||||
namespace evo::network {
|
||||
inline constexpr std::size_t session_grant_bytes = 160;
|
||||
inline constexpr std::size_t grant_private_key_bytes = 32; // P-256 scalar.
|
||||
inline constexpr std::size_t grant_public_key_bytes = 65; // Uncompressed point.
|
||||
using SessionGrant = std::array<std::uint8_t, session_grant_bytes>;
|
||||
using GrantPublicKey = std::array<std::uint8_t, grant_public_key_bytes>;
|
||||
enum class SessionGrantError : std::uint8_t {
|
||||
invalid_key,
|
||||
duplicate_key,
|
||||
too_many_keys,
|
||||
invalid_claims,
|
||||
malformed,
|
||||
wrong_marker,
|
||||
unsupported_version,
|
||||
wrong_environment,
|
||||
unknown_key,
|
||||
bad_signature,
|
||||
crypto
|
||||
};
|
||||
|
||||
// Issuer-side signing key (PSA, non-exportable private part). Move-only.
|
||||
class GrantSigningKey {
|
||||
public:
|
||||
[[nodiscard]] static std::expected<GrantSigningKey, SessionGrantError>
|
||||
generate(DeploymentEnvironment environment, std::uint32_t key_id);
|
||||
[[nodiscard]] static std::expected<GrantSigningKey, SessionGrantError>
|
||||
import(DeploymentEnvironment environment, std::uint32_t key_id,
|
||||
std::span<const std::uint8_t> private_key);
|
||||
GrantSigningKey(GrantSigningKey&& other) noexcept;
|
||||
GrantSigningKey& operator=(GrantSigningKey&& other) noexcept;
|
||||
GrantSigningKey(const GrantSigningKey&) = delete;
|
||||
GrantSigningKey& operator=(const GrantSigningKey&) = delete;
|
||||
~GrantSigningKey();
|
||||
// Structural checks only (nonzero ids, serial and scope, ordered times).
|
||||
[[nodiscard]] std::expected<SessionGrant, SessionGrantError>
|
||||
sign(const GrantClaims& claims) const;
|
||||
[[nodiscard]] std::expected<GrantPublicKey, SessionGrantError> public_key() const;
|
||||
[[nodiscard]] DeploymentEnvironment environment() const noexcept {
|
||||
return environment_;
|
||||
}
|
||||
[[nodiscard]] std::uint32_t key_id() const noexcept {
|
||||
return key_id_;
|
||||
}
|
||||
|
||||
private:
|
||||
GrantSigningKey(DeploymentEnvironment environment, std::uint32_t key_id, std::uint32_t key)
|
||||
: environment_{environment}, key_id_{key_id}, key_{key} {}
|
||||
DeploymentEnvironment environment_;
|
||||
std::uint32_t key_id_;
|
||||
std::uint32_t key_; // PSA key id; zero when moved-from.
|
||||
};
|
||||
|
||||
// Region-side verifier for one environment with up to 8 issuer public keys (rotation).
|
||||
class GrantVerifier {
|
||||
public:
|
||||
static constexpr std::size_t max_keys = 8;
|
||||
[[nodiscard]] static std::expected<GrantVerifier, SessionGrantError>
|
||||
create(DeploymentEnvironment environment);
|
||||
GrantVerifier(GrantVerifier&& other) noexcept;
|
||||
GrantVerifier& operator=(GrantVerifier&& other) noexcept;
|
||||
GrantVerifier(const GrantVerifier&) = delete;
|
||||
GrantVerifier& operator=(const GrantVerifier&) = delete;
|
||||
~GrantVerifier();
|
||||
[[nodiscard]] std::expected<void, SessionGrantError>
|
||||
add_key(std::uint32_t key_id, std::span<const std::uint8_t> public_key);
|
||||
// Signature first: no claim byte is trusted before it verifies.
|
||||
[[nodiscard]] std::expected<VerifiedSessionGrant, SessionGrantError>
|
||||
verify(std::span<const std::uint8_t> grant) const;
|
||||
|
||||
private:
|
||||
struct Key {
|
||||
std::uint32_t key_id{}, key{};
|
||||
};
|
||||
explicit GrantVerifier(DeploymentEnvironment environment) : environment_{environment} {}
|
||||
void destroy() noexcept;
|
||||
DeploymentEnvironment environment_;
|
||||
std::vector<Key> keys_;
|
||||
};
|
||||
|
||||
// Cryptographically random bytes (PSA); used for account and grant ids.
|
||||
[[nodiscard]] std::expected<void, SessionGrantError> random_bytes(std::span<std::uint8_t> out);
|
||||
[[nodiscard]] std::expected<AccountId, SessionGrantError> generate_account_id();
|
||||
} // namespace evo::network
|
||||
Reference in New Issue
Block a user