Skip to content

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

feat(user_agents): add credential set and clear controllers

Steven Enamakel49833d91b454
diff --git a/Cargo.lock b/Cargo.lock
index bbcb520edc..07e5f96f47 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4020,6 +4020,7 @@ dependencies = [
"futures-util",
"hex",
"hkdf 0.12.4",
+ "hmac 0.12.1",
"hostname",
"iana-time-zone",
"keyring",
diff --git a/crates/openhuman-core/src/user_agents/host.rs b/crates/openhuman-core/src/user_agents/host.rs
index 70b531e3f5..c0b755a001 100644
--- a/crates/openhuman-core/src/user_agents/host.rs
+++ b/crates/openhuman-core/src/user_agents/host.rs
@@ -223,6 +223,7 @@ impl AgentHost {
agent_id: id.clone(),
created_at: meta.created_at,
open: self.is_open(id),
+ has_credential: super::credentials::has(&layout::agent_config(&layout, id)),
}))
}
diff --git a/crates/openhuman-core/src/user_agents/mod.rs b/crates/openhuman-core/src/user_agents/mod.rs
index 791cd215cb..ede162c128 100644
--- a/crates/openhuman-core/src/user_agents/mod.rs
+++ b/crates/openhuman-core/src/user_agents/mod.rs
@@ -14,6 +14,7 @@
//!
//! [`CoreContext`]: crate::core::runtime::CoreContext
+pub mod credentials;
pub mod gateway;
pub mod host;
pub mod layout;
diff --git a/crates/openhuman-core/src/user_agents/ops_tests.rs b/crates/openhuman-core/src/user_agents/ops_tests.rs
index 2c6b48123b..d62f1ac87e 100644
--- a/crates/openhuman-core/src/user_agents/ops_tests.rs
+++ b/crates/openhuman-core/src/user_agents/ops_tests.rs
@@ -40,3 +40,34 @@ fn status_and_deprovision_take_agent_ids_only() {
deprovision_on(&host, id.as_str()).unwrap();
assert!(status_on(&host, id.as_str()).is_err());
}
+
+#[test]
+fn credentials_are_set_and_cleared_per_agent_and_never_echoed() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp);
+ let alice = UserAgentId::for_user("alice").unwrap();
+ let bob = UserAgentId::for_user("bob").unwrap();
+ assert!(
+ set_credential_on(&host, alice.as_str(), UserCredentialKind::Session, "t", None)
+ .unwrap_err()
+ .contains("not provisioned")
+ );
+ provision_on(&host, "alice").unwrap();
+ provision_on(&host, "bob").unwrap();
+
+ let out = set_credential_on(
+ &host,
+ alice.as_str(),
+ UserCredentialKind::Session,
+ "alice-secret-jwt",
+ None,
+ )
+ .unwrap();
+ let json = out.into_cli_compatible_json().unwrap().to_string();
+ assert!(!json.contains("alice-secret-jwt"), "{json}");
+ assert!(host.summary(&alice).unwrap().unwrap().has_credential);
+ assert!(!host.summary(&bob).unwrap().unwrap().has_credential);
+
+ clear_credential_on(&host, alice.as_str()).unwrap();
+ assert!(!host.summary(&alice).unwrap().unwrap().has_credential);
+}
diff --git a/crates/openhuman-core/src/user_agents/schemas.rs b/crates/openhuman-core/src/user_agents/schemas.rs
index d673255eb3..2f0f6d5802 100644
--- a/crates/openhuman-core/src/user_agents/schemas.rs
+++ b/crates/openhuman-core/src/user_agents/schemas.rs
@@ -22,7 +22,23 @@ struct AgentParams {
agent_id: String,
}
-const FUNCTIONS: [&str; 4] = ["provision", "deprovision", "list", "status"];
+#[derive(Deserialize)]
+struct CredentialParams {
+ agent_id: String,
+ kind: super::credentials::UserCredentialKind,
+ token: String,
+ #[serde(default)]
+ expires_at: Option<String>,
+}
+
+const FUNCTIONS: [&str; 6] = [
+ "provision",
+ "deprovision",
+ "list",
+ "status",
+ "set_credential",
+ "clear_credential",
+];
pub fn all_user_agents_controller_schemas() -> Vec<ControllerSchema> {
FUNCTIONS.iter().map(|f| user_agents_schemas(f)).collect()
@@ -46,6 +62,14 @@ pub fn all_user_agents_registered_controllers() -> Vec<RegisteredController> {
schema: user_agents_schemas("status"),
handler: handle_status,
},
+ RegisteredController {
+ schema: user_agents_schemas("set_credential"),
+ handler: handle_set_credential,
+ },
+ RegisteredController {
+ schema: user_agents_schemas("clear_credential"),
+ handler: handle_clear_credential,
+ },
]
}
@@ -97,6 +121,45 @@ pub fn user_agents_schemas(function: &str) -> ControllerSchema {
required: true,
},
bool_field("open", "Whether it is loaded right now."),
+ bool_field("has_credential", "Whether a backend credential is installed."),
+ ],
+ },
+ "set_credential" => ControllerSchema {
+ namespace: "user_agents",
+ function: "set_credential",
+ description: "Install the TinyHumans credential the gateway holds for a user agent. \
+ The core stores it beside the agent's state and never validates or echoes it.",
+ inputs: vec![
+ string_field("agent_id", "The agent the credential belongs to."),
+ FieldSchema {
+ name: "kind",
+ ty: TypeSchema::Enum {
+ variants: vec!["session", "api_key"],
+ },
+ comment: "A session JWT or an API key.",
+ required: true,
+ },
+ string_field("token", "The credential."),
+ FieldSchema {
+ name: "expires_at",
+ ty: TypeSchema::Option(Box::new(TypeSchema::String)),
+ comment: "RFC 3339 expiry of a session, so an expired one is refused locally.",
+ required: false,
+ },
+ ],
+ outputs: vec![
+ string_field("agent_id", "The agent."),
+ bool_field("has_credential", "Always true on success."),
+ ],
+ },
+ "clear_credential" => ControllerSchema {
+ namespace: "user_agents",
+ function: "clear_credential",
+ description: "Remove every credential a user agent holds.",
+ inputs: vec![string_field("agent_id", "The agent.")],
+ outputs: vec![
+ string_field("agent_id", "The agent."),
+ bool_field("has_credential", "Always false on success."),
],
},
_ => ControllerSchema {
@@ -134,6 +197,25 @@ fn handle_status(params: Map<String, Value>) -> ControllerFuture {
})
}
+fn handle_set_credential(params: Map<String, Value>) -> ControllerFuture {
+ Box::pin(async move {
+ let payload = deserialize_params::<CredentialParams>(params)?;
+ to_json(super::ops::set_credential(
+ &payload.agent_id,
+ payload.kind,
+ &payload.token,
+ payload.expires_at.as_deref(),
+ )?)
+ })
+}
+
+fn handle_clear_credential(params: Map<String, Value>) -> ControllerFuture {
+ Box::pin(async move {
+ let payload = deserialize_params::<AgentParams>(params)?;
+ to_json(super::ops::clear_credential(&payload.agent_id)?)
+ })
+}
+
fn deserialize_params<T: DeserializeOwned>(params: Map<String, Value>) -> Result<T, String> {
serde_json::from_value(Value::Object(params)).map_err(|e| format!("invalid params: {e}"))
}
diff --git a/crates/openhuman-core/src/user_agents/types.rs b/crates/openhuman-core/src/user_agents/types.rs
index bc562421c7..5bb74b35cf 100644
--- a/crates/openhuman-core/src/user_agents/types.rs
+++ b/crates/openhuman-core/src/user_agents/types.rs
@@ -113,6 +113,17 @@ pub struct UserAgentSummary {
pub created_at: u64,
/// Whether it is loaded in this process right now.
pub open: bool,
+ /// Whether the gateway has installed a backend credential for it.
+ pub has_credential: bool,
+}
+
+/// What [`set_credential`](super::ops::set_credential) /
+/// [`clear_credential`](super::ops::clear_credential) did. The credential
+/// itself is never echoed.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
+pub struct CredentialResult {
+ pub agent_id: UserAgentId,
+ pub has_credential: bool,
}
#[cfg(test)]