Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(security): add device storage schema

Steven Enamakeld01b59137e13
diff --git a/crates/openhuman-core/src/security/devices/storage_schema.rs b/crates/openhuman-core/src/security/devices/storage_schema.rs
new file mode 100644
index 0000000000..1f802b4e1b
--- /dev/null
+++ b/crates/openhuman-core/src/security/devices/storage_schema.rs
@@ -0,0 +1,65 @@
+//! The `devices` owner's storage schema: the paired-device collection and the
+//! step that brings the v1 `paired_devices` rows forward.
+//!
+//! | v1 | v2 | scope |
+//! |---|---|---|
+//! | `paired_devices` (`created_at`, `last_seen_at` RFC 3339) | `devices.paired` (`created_ms`, `last_seen_ms`) | tenant |
+
+use serde_json::Value;
+use tinystoragedrivers::schema::{Schema, ScopeClass, SourceRecord, Step, TargetRecord};
+use tinystoragedrivers::{CollectionSpec, IndexSpec, Result};
+
+use crate::storage::schema_support::{key_of, owner, time_ms, Body};
+
+/// The v2 collection: one document per `channel_id`.
+pub(super) const DEVICES: &str = "devices.paired";
+/// The v1 name, still written by the legacy `devices.db` importer.
+pub(super) const LEGACY_DEVICES: &str = "paired_devices";
+/// The body version of [`DEVICES`].
+const SCHEMA: u32 = 2;
+
+/// What the device store keeps: `devices.paired` and the step from the v1 rows.
+#[must_use]
+pub fn storage_schema() -> Schema {
+ Schema::new(owner("devices"))
+ .collection_with(SCHEMA, spec(), ScopeClass::Tenant)
+ .step(Step::new("paired_v2", DEVICES, 1, SCHEMA, paired_v2).source(LEGACY_DEVICES))
+}
+
+/// The v2 collection with its index.
+pub(super) fn spec() -> CollectionSpec {
+ CollectionSpec::new(DEVICES).index(IndexSpec::new("by_active", ["revoked", "created_ms"]))
+}
+
+/// The v1 collection as the importer declares it.
+pub(super) fn legacy_collections() -> Vec<CollectionSpec> {
+ vec![CollectionSpec::new(LEGACY_DEVICES)
+ .index(IndexSpec::new("by_active", ["revoked", "created_at"]))]
+}
+
+fn paired_v2(batch: Vec<SourceRecord>) -> Result<Vec<TargetRecord>> {
+ batch
+ .into_iter()
+ .map(|record| {
+ let old = &record.body;
+ let created_ms = time_ms(old, "created_at").unwrap_or(record.meta.created_ms);
+ let body = Body::new()
+ .copy(old, "label")
+ .copy(old, "device_pubkey")
+ .copy(old, "core_session_token_hash")
+ .set("created_ms", created_ms)
+ .set_some("last_seen_ms", time_ms(old, "last_seen_at"))
+ .set(
+ "revoked",
+ old.get("revoked").and_then(Value::as_bool).unwrap_or(false),
+ )
+ .build();
+ Ok(TargetRecord::new(DEVICES, key_of(&record.id)?, body)
+ .created_ms(record.meta.created_ms))
+ })
+ .collect()
+}
+
+#[cfg(test)]
+#[path = "storage_schema_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/security/devices/storage_schema_tests.rs b/crates/openhuman-core/src/security/devices/storage_schema_tests.rs
new file mode 100644
index 0000000000..d2828200f8
--- /dev/null
+++ b/crates/openhuman-core/src/security/devices/storage_schema_tests.rs
@@ -0,0 +1,59 @@
+use super::*;
+use serde_json::json;
+use tinystoragedrivers::Meta;
+
+fn source(id: &str, body: Value) -> SourceRecord {
+ SourceRecord {
+ id: id.to_string(),
+ body,
+ meta: Meta::unknown(),
+ }
+}
+
+#[test]
+fn a_v1_row_becomes_epoch_millisecond_fields() {
+ let out = paired_v2(vec![source(
+ "ch-1",
+ json!({
+ "label": "iPhone",
+ "device_pubkey": "pk",
+ "core_session_token_hash": "h",
+ "created_at": "1970-01-01T00:00:01.000+00:00",
+ "last_seen_at": "1970-01-01T00:00:02.500Z",
+ "revoked": true,
+ }),
+ )])
+ .unwrap();
+ assert_eq!(out.len(), 1);
+ assert_eq!(out[0].collection, DEVICES);
+ assert_eq!(out[0].id, "ch-1");
+ assert_eq!(
+ out[0].body,
+ json!({
+ "label": "iPhone",
+ "device_pubkey": "pk",
+ "core_session_token_hash": "h",
+ "created_ms": 1000,
+ "last_seen_ms": 2500,
+ "revoked": true,
+ })
+ );
+}
+
+#[test]
+fn a_never_seen_device_has_no_last_seen_field() {
+ let out = paired_v2(vec![source(
+ "ch-2",
+ json!({
+ "label": "a", "device_pubkey": "p", "core_session_token_hash": "h",
+ "created_at": "1970-01-01T00:00:01Z", "last_seen_at": null, "revoked": false,
+ }),
+ )])
+ .unwrap();
+ assert!(out[0].body.get("last_seen_ms").is_none());
+}
+
+#[test]
+fn the_schema_composes_under_its_owner() {
+ tinystoragedrivers::schema::Registry::compose([storage_schema()]).unwrap();
+}