Skip to content

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

fix(credentials): preserve API key runtime state

Steven Enamakelf593e6ae8d35
diff --git a/crates/openhuman-core/src/security/credentials/ops/credential.rs b/crates/openhuman-core/src/security/credentials/ops/credential.rs
index 69487ee065..0eddb6c0d4 100644
--- a/crates/openhuman-core/src/security/credentials/ops/credential.rs
+++ b/crates/openhuman-core/src/security/credentials/ops/credential.rs
@@ -183,10 +183,16 @@ pub async fn set_credential(
let _mutation = CREDENTIAL_MUTATION_LOCK.lock().await;
if resolved.kind == CredentialKind::ApiKey {
+ let was_authenticated =
+ crate::security::credentials::session_support::has_backend_credential(config);
api_key::store_api_key(config, &resolved.token).map_err(|e| e.to_string())?;
// API-key-backed runs must not retain a previous session identity in
// prompt composition or observability scope.
identity::clear_current_user();
+ sentry_scope::clear();
+ if !was_authenticated {
+ start_credential_gated_services(config).await;
+ }
crate::cron::scheduler_gate::set_signed_out(false);
tracing::info!(
domain = "credentials",
@@ -363,6 +369,17 @@ pub async fn clear_credential(
}
}
+ // A session-only clear may leave an API key behind. The session teardown
+ // deliberately stops the gated services and closes the scheduler gate, so
+ // restore the surviving key's runtime after that teardown has completed.
+ if removed_session && api_key::has_api_key(config) {
+ start_credential_gated_services(config).await;
+ crate::cron::scheduler_gate::set_signed_out(false);
+ sentry_scope::clear();
+ identity::clear_current_user();
+ logs.push("credential-gated services restarted for api key".to_string());
+ }
+
Ok(RpcOutcome::new(
json!({
"removed": removed_session || removed_api_key,
diff --git a/crates/openhuman-session/src/manager.rs b/crates/openhuman-session/src/manager.rs
index d1e17f3ef9..a49aef0e40 100644
--- a/crates/openhuman-session/src/manager.rs
+++ b/crates/openhuman-session/src/manager.rs
@@ -398,22 +398,26 @@ impl<L: CoreLink> SessionManager<L> {
/// Sign out: clear the session (or local) credential in the core and
/// forget the current user.
pub async fn logout(&self) -> Result<SessionState, SessionError> {
- let _guard = self.mutation.lock().await;
- self.cancel_revalidation();
- link::clear_credential(self.link.as_ref(), Some(CredentialKind::Session))
- .await
- .map_err(SessionError::Core)?;
- self.cache.forget();
- identity::clear();
+ {
+ let _guard = self.mutation.lock().await;
+ self.cancel_revalidation();
+ link::clear_credential(self.link.as_ref(), Some(CredentialKind::Session))
+ .await
+ .map_err(SessionError::Core)?;
+ self.cache.forget();
+ identity::clear();
+ }
self.changed().await
}
/// Remove a stored API key.
pub async fn clear_api_key(&self) -> Result<SessionState, SessionError> {
- let _guard = self.mutation.lock().await;
- link::clear_credential(self.link.as_ref(), Some(CredentialKind::ApiKey))
- .await
- .map_err(SessionError::Core)?;
+ {
+ let _guard = self.mutation.lock().await;
+ link::clear_credential(self.link.as_ref(), Some(CredentialKind::ApiKey))
+ .await
+ .map_err(SessionError::Core)?;
+ }
self.changed().await
}