Skip to content

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

feat(auth): add API key credential support to auth state

Steven Enamakel23abd58b919a
diff --git a/crates/openhuman-embed/src/auth.rs b/crates/openhuman-embed/src/auth.rs
index 4c6f9ecf58..4367925aaa 100644
--- a/crates/openhuman-embed/src/auth.rs
+++ b/crates/openhuman-embed/src/auth.rs
@@ -96,6 +96,19 @@ pub struct AuthState {
/// The signed-in user id, when there is one.
#[serde(default, rename = "userId")]
pub user_id: Option<String>,
+ /// Which credential backs `is_authenticated`: `"session"` for an app
+ /// session, `"api-key"` for a TinyHumans API key (no user), absent when
+ /// signed out.
+ #[serde(default)]
+ pub credential: Option<String>,
+}
+
+impl AuthState {
+ /// Whether the runtime authenticates with a TinyHumans API key rather
+ /// than a user session.
+ pub fn is_api_key(&self) -> bool {
+ self.credential.as_deref() == Some("api-key")
+ }
}
/// Typed access to the session store.
@@ -157,6 +170,35 @@ impl Auth<'_> {
Ok(payload.token.filter(|token| !token.trim().is_empty()))
}
+ /// Store a TinyHumans API key as the runtime's backend credential.
+ ///
+ /// [`RuntimeBuilder::api_key`](crate::RuntimeBuilder::api_key) is the
+ /// usual path — it installs the key before the core boots. This is the
+ /// same operation on a running core, for a host that obtains the key
+ /// later.
+ pub async fn store_api_key(&self, key: impl Into<crate::ApiKey>) -> Result<(), CoreError> {
+ let key = key.into();
+ log::debug!("[embed][auth] storing api key blank={}", key.is_blank());
+ let _: serde_json::Value = call(
+ self.0,
+ "openhuman.auth_store_api_key",
+ serde_json::json!({ "key": key.expose() }),
+ )
+ .await?;
+ Ok(())
+ }
+
+ /// Remove the stored TinyHumans API key.
+ pub async fn clear_api_key(&self) -> Result<(), CoreError> {
+ let _: serde_json::Value = call(
+ self.0,
+ "openhuman.auth_clear_api_key",
+ serde_json::json!({}),
+ )
+ .await?;
+ Ok(())
+ }
+
/// Remove the stored session.
pub async fn clear(&self) -> Result<(), CoreError> {
let _: serde_json::Value = call(
diff --git a/crates/openhuman-embed/tests/runtime_agents.rs b/crates/openhuman-embed/tests/runtime_agents.rs
index adc32bd4f5..47aa9e512e 100644
--- a/crates/openhuman-embed/tests/runtime_agents.rs
+++ b/crates/openhuman-embed/tests/runtime_agents.rs
@@ -120,6 +120,7 @@ fn one_runtime_hosts_independently_configured_agents() {
let state = runtime.core().auth().state().await.expect("auth state");
assert!(state.is_authenticated, "api key must count as signed in");
assert!(state.user_id.is_none(), "an api key carries no user");
+ assert!(state.is_api_key(), "state names the credential: {state:?}");
assert!(
runtime
.core()