Implement voelin-identity's KeyStore on the SQLite identities table

SqliteKeyStore is the identities table as a KeyStore: every kind (TeamSpeak,
a Voelin key, the account record, an unknown kind that passes through) with
the account binding of the columns that ADD_ACCOUNT_COLUMNS added. It owns a
Store behind a mutex, because a KeyStore is Sync and a connection is not;
with_store runs the TeamSpeak methods on the same connection. The trait's
provided methods (bound_to, account_record, put_account_record, sign_out)
are used as they are.

- put is one upsert; a replaced row keeps its name, origin, default flag and
  creation time, and a blob without a binding unbinds the row. The uid is
  UNIQUE across kinds, so a uid another kind holds is a backend error that
  replaces nothing; get and remove are addressed by kind and uid.
- The TeamSpeak level stays computed from the key and counter, as before; the
  level of a blob that is put is not stored. A TeamSpeak row whose JSON was
  damaged outside this crate lists and reads with level 0.
- A TeamSpeak blob is stored only if its JSON is a tsproto identity and its
  key has the uid the blob names. Without the first one bad row breaks the
  list of TeamSpeak identities; without the second a blob could replace, by
  its uid, the key of a different identity.

Tests copy the exercise() cases of voelin-identity's tests/storage.rs into
the client (with the two deviations of a SQLite store, marked), round-trip
bound and synced entries through the table and a reopen with their raw
columns, and load a client.db migrated from schema versions 5 and 6 through
the key store: kind teamspeak, no binding, JSON byte for byte, origin and
default flag kept.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Sonnet 5.5 committed 2026-10-20 05:48:51 +03:00
1 parent 0137d03d5b
commit 2898281d35
4 files changed
+936

No files matched your search

+72
View File
@@ -799,6 +799,78 @@ mod tests {
std::fs::remove_dir_all(dir).unwrap();
}
/// The acceptance of the migration for the key store: a `client.db` at the
/// version before the identity migrations (5), and at the one between them
/// (6), opens, and every identity loads through the key store unchanged:
/// kind `teamspeak`, no binding, its JSON byte for byte, the level computed
/// as before, and the origin and default flag the old API reports kept.
#[test]
fn a_migrated_database_loads_every_identity_unchanged_through_the_key_store() {
use crate::SqliteKeyStore;
use voelin_identity::{IdentityBlob, IdentityInfo, KIND_TEAMSPEAK, KeyStore};
for version in [5, 6] {
let dir = scratch_dir(&format!("keys-v{version}"));
let path = dir.join("client.db");
let other = Identity::create();
let other_json = serde_json::to_string(&other).unwrap();
let other_uid = other.key().to_pub().get_uid();
database_at(&path, version);
{
let db = rusqlite::Connection::open(&path).unwrap();
for (name, uid, json, origin, default) in [
("Main", TEAMSPEAK_UID, TEAMSPEAK_JSON, "imported", 1),
("Other", other_uid.as_str(), other_json.as_str(), "created", 0),
] {
db.execute(
"INSERT INTO identities (name, uid, data, origin, is_default)
VALUES (?1, ?2, ?3, ?4, ?5)",
params![name, uid, json, origin, default],
)
.unwrap();
}
}
let keys = SqliteKeyStore::open(&path).unwrap();
assert_eq!(keys.with_store(|s| s.schema_version().unwrap()), 7);
let want = [
(TEAMSPEAK_UID.to_owned(), TEAMSPEAK_JSON.to_owned(), TEAMSPEAK_LEVEL),
(other_uid.clone(), other_json.clone(), other.level()),
];
let mut infos: Vec<_> = want
.iter()
.map(|(uid, _, level)| IdentityInfo {
kind: KIND_TEAMSPEAK.into(),
uid: uid.clone(),
level: *level,
binding: None,
})
.collect();
infos.sort_by(|a, b| a.uid.cmp(&b.uid));
assert_eq!(keys.list().unwrap(), infos, "version {version}");
for (uid, json, level) in want {
let blob = keys.get(KIND_TEAMSPEAK, &uid).unwrap().unwrap();
assert_eq!(blob, IdentityBlob::teamspeak(&uid, level, &json), "version {version}");
assert_eq!(blob.json, json, "byte for byte");
assert_eq!(blob.binding, None);
}
// What the TeamSpeak methods report is kept as well.
let entries = keys.with_store(|s| s.identities().unwrap());
let origin = |uid: &str| entries.iter().find(|e| e.uid == uid).unwrap();
assert_eq!(
(origin(TEAMSPEAK_UID).origin, origin(TEAMSPEAK_UID).is_default),
(IdentityOrigin::Imported, true)
);
assert_eq!(
(origin(&other_uid).origin, origin(&other_uid).is_default),
(IdentityOrigin::Created, false)
);
drop(keys);
std::fs::remove_dir_all(dir).unwrap();
}
}
/// A database of version 6 (the `kind` column, no account columns yet) takes
/// the second step alone, and its rows of other kinds stay what they are.
#[test]