The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(credentials): add API key credential support alongside session tokens
Steven Enamakel8d2cee078b0a
diff --git a/crates/openhuman-core/src/security/credentials/mod.rs b/crates/openhuman-core/src/security/credentials/mod.rsindex f0e36b545e..b60a9998dd 100644--- a/crates/openhuman-core/src/security/credentials/mod.rs+++ b/crates/openhuman-core/src/security/credentials/mod.rs@@ -1,5 +1,6 @@//! Credential management for app session and provider auth profiles.+pub mod api_key;pub mod bus;pub mod cli;mod core;diff --git a/crates/openhuman-core/src/security/credentials/session_support.rs b/crates/openhuman-core/src/security/credentials/session_support.rsindex 25a9d48079..286b1529a8 100644--- a/crates/openhuman-core/src/security/credentials/session_support.rs+++ b/crates/openhuman-core/src/security/credentials/session_support.rs@@ -204,9 +204,59 @@ pub(crate) fn classify_session_token(/// presence-only check; the `flatten_authed_error` 401 net still covers a/// server-side revocation that precedes the recorded `exp`.pub fn require_live_session_token(config: &Config) -> Result<String, String> {+ resolve_backend_credential(config).map(BackendCredential::into_secret)+}++/// The credential a backend request authenticates with.+///+/// Two shapes because the wire differs: a session JWT rides+/// `Authorization: Bearer` everywhere, while an API key rides+/// `Authorization: Bearer` on managed inference but `x-api-key` on the SDK+/// REST routes. Callers that build SDK requests match on this; callers that+/// only need "the secret string" use [`into_secret`](Self::into_secret).+#[derive(Debug, Clone, PartialEq, Eq)]+pub enum BackendCredential {+ /// The app-session JWT stored by `auth_store_session`.+ Session(String),+ /// The TinyHumans API key stored by `auth_store_api_key`+ /// (see [`super::api_key`]).+ ApiKey(String),+}++impl BackendCredential {+ /// The raw secret, whichever kind it is.+ pub fn into_secret(self) -> String {+ match self {+ Self::Session(s) | Self::ApiKey(s) => s,+ }+ }++ /// The raw secret, borrowed.+ pub fn secret(&self) -> &str {+ match self {+ Self::Session(s) | Self::ApiKey(s) => s,+ }+ }++ pub fn is_api_key(&self) -> bool {+ matches!(self, Self::ApiKey(_))+ }+}++/// Resolve the backend credential for `config`: the API key when one is+/// stored, else the live app-session token with exactly the classification+/// [`require_live_session_token`] has always applied.+///+/// The API key is checked **first**, and a stored key short-circuits every+/// session check: a library runtime never stores a session, and a desktop that+/// somehow has both has been told explicitly to use the key.+pub fn resolve_backend_credential(config: &Config) -> Result<BackendCredential, String> {+ if let Some(key) = super::api_key::get_api_key(config).map_err(|e| e.to_string())? {+ return Ok(BackendCredential::ApiKey(key));+ }let profile = load_app_session_profile(config)?;match classify_session_token(profile.as_ref(), chrono::Utc::now()) {- SessionTokenCheck::Live(token) => Ok(token),+ SessionTokenCheck::Live(token) => Ok(BackendCredential::Session(token)),SessionTokenCheck::Absent => {Err("no backend session token; run auth_store_session first".to_string())}@@ -220,6 +270,18 @@ pub fn require_live_session_token(config: &Config) -> Result<String, String> {}}+/// Whether *some* backend credential is present — an API key or a non-empty+/// app-session token — without classifying expiry. This is the boot-time+/// "signed in?" question the scheduler gate asks: an expired session still+/// counts as signed in here because the gate's job is to notice the later+/// `SessionExpired`, not to pre-empt it.+pub fn has_backend_credential(config: &Config) -> bool {+ if super::api_key::has_api_key(config) {+ return true;+ }+ matches!(get_session_token(config), Ok(Some(_)))+}+/// Announce a locally-detected session expiry on the bus so/// `SessionExpiredSubscriber` clears credentials and the UI re-authenticates,/// exactly as it would on a real network 401.@@ -237,6 +299,18 @@ pub fn publish_local_session_expiry(operation: &'static str) {if crate::cron::scheduler_gate::is_signed_out() {return;}+ // An API-key runtime has no session to expire. Its backend calls never+ // reach here through `resolve_backend_credential` (the key short-circuits),+ // but a stale direct caller must not sign the whole process out over a+ // credential the runtime does not use.+ if ambient_config_has_api_key() {+ tracing::debug!(+ domain = "credentials",+ operation = operation,+ "[credentials] session expiry ignored — runtime authenticates with an API key"+ );+ return;+ }tracing::info!(domain = "credentials",operation = operation,@@ -298,6 +372,15 @@ pub fn session_token_from_profile(profile: Option<&AuthProfile>) -> Option<Strin.map(str::to_string)}+/// Whether the ambient runtime config carries an API key. Reads the+/// embedder-supplied config off the [`CoreContext`](crate::core::runtime::CoreContext)+/// when there is one; a host that discovers its config from disk has no API+/// key by construction (it is only ever installed by a library runtime).+pub fn ambient_config_has_api_key() -> bool {+ crate::core::runtime::CoreContext::current_embedder_config()+ .is_some_and(|config| super::api_key::has_api_key(&config))+}+#[cfg(test)]#[path = "session_support_tests.rs"]mod tests;