Phase 12: realm key pinning, linked accounts stay linked, control protocol and registry protocol 2 documentation, client credential audit tool

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ДмитрийandClaude Sonnet 5.5 committed 2026-10-07 23:04:48 +05:00
1 parent 2caa687490
commit 461ffddd0d
8 files changed
+822 -81

No files matched your search

+17 -2
View File
@@ -170,8 +170,18 @@ impl PlayerControl {
Ok(self.store()?.player_realm(&realm.to_string())?.map(|r| (r.username, r.kind)))
}
/// Open a verified channel to the realm's Host. The realm's key is pinned the first time: the Registry says which key a realm id has, and a Registry (or anything between) that later
/// names another key for the same id is refused here, before anything is sent.
pub fn connect(&self, target: &RealmTarget) -> R<PlayerChannel> {
Ok(PlayerChannel::connect(&target.coordinator, &target.realm_id, &target.key, &self.identity, &clock)?)
let id = target.realm_id.to_string();
let key = coord::encode_public_key(&target.key);
match self.store()?.realm_pin(&id)? {
Some(pinned) if pinned != key => return Err(ControlFail::Link(LinkError::Auth("this realm's key is not the one it had when you first joined it".into()))),
_ => {}
}
let channel = PlayerChannel::connect(&target.coordinator, &target.realm_id, &target.key, &self.identity, &clock)?;
self.store()?.realm_pin_set(&id, &key)?;
Ok(channel)
}
pub fn welcome(&self, ch: &mut PlayerChannel) -> R<(bool, bool, Option<String>)> {
@@ -204,7 +214,12 @@ impl PlayerControl {
}
})?;
self.save_credentials(realm, &Credentials { username: username.clone(), password })?;
self.store()?.player_realm_set(&realm.to_string(), &username, AccountKind::Generated)?;
// an account the player linked stays a linked one when the realm confirms it again
let kind = match self.store()?.player_realm(&realm.to_string())? {
Some(known) if known.username == username => known.kind,
_ => AccountKind::Generated,
};
self.store()?.player_realm_set(&realm.to_string(), &username, kind)?;
Ok(AccountOutcome { username, created, password_reset: reset })
}
+15
View File
@@ -39,6 +39,11 @@ CREATE TABLE IF NOT EXISTS player_realm(
kind TEXT NOT NULL CHECK (kind IN ('generated', 'linked')),
created_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS realm_pin(
realm_id TEXT PRIMARY KEY,
public_key TEXT NOT NULL,
first_seen TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS player_claim(
realm_id TEXT NOT NULL,
character_id TEXT NOT NULL,
@@ -219,6 +224,16 @@ impl ControlStore {
Ok(())
}
/// The public key a realm had when this player first reached it. A realm id never changes its key (a new key is a new realm), so a different one is refused.
pub fn realm_pin(&self, realm_id: &str) -> Result<Option<String>> {
self.conn.query_row("SELECT public_key FROM realm_pin WHERE realm_id = ?1", [realm_id], |r| r.get(0)).optional().map_err(db)
}
pub fn realm_pin_set(&self, realm_id: &str, public_key: &str) -> Result<()> {
self.conn.execute("INSERT OR IGNORE INTO realm_pin(realm_id, public_key, first_seen) VALUES (?1, ?2, ?3)", params![realm_id, public_key, now()]).map_err(db)?;
Ok(())
}
pub fn player_claim_add(&self, realm_id: &str, character: &Uuid, name: &str) -> Result<()> {
self.conn.execute("INSERT OR IGNORE INTO player_claim(realm_id, character_id, name, claimed_at) VALUES (?1, ?2, ?3, ?4)", params![realm_id, character.to_string(), name, now()]).map_err(db)?;
Ok(())