Skip to content

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

feat(api): support API key authentication alongside session JWTs

Steven Enamakel9c8c3180483c
diff --git a/crates/openhuman-core/src/api/rest.rs b/crates/openhuman-core/src/api/rest.rs
index 5e28eacba3..71c63f666c 100644
--- a/crates/openhuman-core/src/api/rest.rs
+++ b/crates/openhuman-core/src/api/rest.rs
@@ -564,18 +564,34 @@ impl BackendOAuthClient {
.await
}
+ /// An SDK client carrying `credential` on the wire the backend expects for
+ /// its kind: a session JWT as `Authorization: Bearer`, an API key as
+ /// `x-api-key`. See `security::credentials::api_key`.
+ fn sdk_with_credential(&self, credential: &BackendCredential) -> TinyHumansClient {
+ let secret = credential.secret().trim().to_string();
+ match credential {
+ BackendCredential::Session(_) => self.sdk.clone().with_token(Some(secret)),
+ BackendCredential::ApiKey(_) => {
+ log::trace!("[backend-api] authenticating request with x-api-key");
+ self.sdk.clone().with_api_key(Some(secret))
+ }
+ }
+ }
+
/// Generic authenticated JSON request helper for backend API routes.
+ ///
+ /// `credential` accepts a [`BackendCredential`] (from
+ /// `session_support::resolve_backend_credential`) or, for the many callers
+ /// that still hold a bare session token string, a `&str` / `&String`,
+ /// which is treated as a session JWT.
pub async fn authed_json(
&self,
- bearer_jwt: &str,
+ credential: impl Into<BackendCredential>,
method: Method,
path: &str,
body: Option<Value>,
) -> Result<Value> {
- let sdk = self
- .sdk
- .clone()
- .with_token(Some(bearer_jwt.trim().to_string()));
+ let sdk = self.sdk_with_credential(&credential.into());
let response = sdk
.raw()
.send(method.clone(), path, &[], body.as_ref(), true)
@@ -584,12 +600,12 @@ impl BackendOAuthClient {
}
/// Fetch the deployed billing summary through the SDK's authenticated raw API.
- pub async fn fetch_billing_summary(&self, bearer_jwt: &str) -> Result<Value> {
+ pub async fn fetch_billing_summary(
+ &self,
+ credential: impl Into<BackendCredential>,
+ ) -> Result<Value> {
const PATH: &str = "/payments/summary";
- let sdk = self
- .sdk
- .clone()
- .with_token(Some(bearer_jwt.trim().to_string()));
+ let sdk = self.sdk_with_credential(&credential.into());
let response = sdk.raw().send(Method::GET, PATH, &[], None, true).await;
self.finish_authed_json(Method::GET, PATH, response)
}
diff --git a/crates/openhuman-core/src/hosted/announcements/ops.rs b/crates/openhuman-core/src/hosted/announcements/ops.rs
index 17537d8db3..577eb139fb 100644
--- a/crates/openhuman-core/src/hosted/announcements/ops.rs
+++ b/crates/openhuman-core/src/hosted/announcements/ops.rs
@@ -16,8 +16,10 @@ use crate::rpc::RpcOutcome;
/// Canonical authed-session guard — rejects an expired token locally instead of
/// firing a doomed backend 401 (see `billing/ops.rs` / #3297).
-fn require_token(config: &Config) -> Result<String, String> {
- crate::security::credentials::session_support::require_live_session_token(config)
+fn require_token(
+ config: &Config,
+) -> Result<crate::security::credentials::session_support::BackendCredential, String> {
+ crate::security::credentials::session_support::resolve_backend_credential(config)
}
/// `true` when `err` is the typed `BackendApiError::AnnouncementNotFound` 404
diff --git a/crates/openhuman-core/src/hosted/billing/ops.rs b/crates/openhuman-core/src/hosted/billing/ops.rs
index 82c504081c..6049b9fdba 100644
--- a/crates/openhuman-core/src/hosted/billing/ops.rs
+++ b/crates/openhuman-core/src/hosted/billing/ops.rs
@@ -21,8 +21,10 @@ use crate::rpc::RpcOutcome;
/// Canonical authed-session guard. Delegates to `require_live_session_token`,
/// which rejects an expired token locally (publishing `SessionExpired`) instead
/// of firing a doomed backend 401 — see #3297 / `session_support`.
-fn require_token(config: &Config) -> Result<String, String> {
- crate::security::credentials::session_support::require_live_session_token(config)
+fn require_token(
+ config: &Config,
+) -> Result<crate::security::credentials::session_support::BackendCredential, String> {
+ crate::security::credentials::session_support::resolve_backend_credential(config)
}
async fn get_authed_value(
diff --git a/crates/openhuman-core/src/hosted/team/ops.rs b/crates/openhuman-core/src/hosted/team/ops.rs
index fc62f5eeb3..9f760d330b 100644
--- a/crates/openhuman-core/src/hosted/team/ops.rs
+++ b/crates/openhuman-core/src/hosted/team/ops.rs
@@ -24,8 +24,10 @@ use crate::rpc::RpcOutcome;
/// Canonical authed-session guard. Delegates to `require_live_session_token`,
/// which rejects an expired token locally (publishing `SessionExpired`) instead
/// of firing a doomed backend 401 — see #3297 / `session_support`.
-fn require_token(config: &Config) -> Result<String, String> {
- crate::security::credentials::session_support::require_live_session_token(config)
+fn require_token(
+ config: &Config,
+) -> Result<crate::security::credentials::session_support::BackendCredential, String> {
+ crate::security::credentials::session_support::resolve_backend_credential(config)
}
fn normalize_id(input: &str, field: &str) -> Result<String, String> {
diff --git a/crates/openhuman-core/src/security/credentials/session_support.rs b/crates/openhuman-core/src/security/credentials/session_support.rs
index 286b1529a8..605b089c1c 100644
--- a/crates/openhuman-core/src/security/credentials/session_support.rs
+++ b/crates/openhuman-core/src/security/credentials/session_support.rs
@@ -223,6 +223,27 @@ pub enum BackendCredential {
ApiKey(String),
}
+impl From<&str> for BackendCredential {
+ /// A bare token string is a session JWT — the historical `authed_json`
+ /// contract. API keys only ever arrive typed, from
+ /// [`resolve_backend_credential`].
+ fn from(token: &str) -> Self {
+ Self::Session(token.to_string())
+ }
+}
+
+impl From<&String> for BackendCredential {
+ fn from(token: &String) -> Self {
+ Self::Session(token.clone())
+ }
+}
+
+impl From<&BackendCredential> for BackendCredential {
+ fn from(credential: &BackendCredential) -> Self {
+ credential.clone()
+ }
+}
+
impl BackendCredential {
/// The raw secret, whichever kind it is.
pub fn into_secret(self) -> String {