Skip to content

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

fix(auth): carry keychain-only profiles through while the keychain is unavailable

M3gA-Mind1541e8569159
diff --git a/crates/openhuman-core/src/security/credentials/profiles/migration.rs b/crates/openhuman-core/src/security/credentials/profiles/migration.rs
index 4376bc2bfb..74c0533095 100644
--- a/crates/openhuman-core/src/security/credentials/profiles/migration.rs
+++ b/crates/openhuman-core/src/security/credentials/profiles/migration.rs
@@ -307,6 +307,15 @@ impl AuthProfilesStore {
AuthProfileKind::OAuth => {
let access = match access_token {
Some(a) => a,
+ None if !self.use_keychain && keychain_held_oauth(p) => {
+ log::warn!(
+ "[auth] keeping OAuth profile whose secrets are only in the \
+ unavailable OS keychain: provider={}. It resolves once the \
+ keychain reads again.",
+ p.provider
+ );
+ continue;
+ }
None => {
log::warn!(
"[auth] dropping OAuth profile with missing access_token: \
@@ -475,6 +484,10 @@ impl AuthProfilesStore {
new_profiles.insert(final_id.clone(), ap);
new_persisted_profiles.insert(final_id, p);
}
+ } else {
+ // Neither resolved nor dropped: carried untouched because its
+ // secrets are only in the unavailable keychain.
+ new_persisted_profiles.insert(id, p);
}
}
for profile_id in persisted.active_profiles.values_mut() {
@@ -513,3 +526,14 @@ impl AuthProfilesStore {
})
}
}
+
+/// An OAuth entry whose secrets are only in the OS keychain: its JSON holds
+/// none of them. While the keychain is unavailable it cannot be resolved, so
+/// it is carried untouched (neither handed out nor purged) until it reads again.
+pub(super) fn keychain_held_oauth(p: &PersistedAuthProfile) -> bool {
+ p.access_token.is_none()
+ && p.refresh_token.is_none()
+ && p.id_token.is_none()
+ && p.token.is_none()
+ && matches!(parse_profile_kind(&p.kind), Ok(AuthProfileKind::OAuth))
+}
diff --git a/crates/openhuman-core/src/security/credentials/profiles/persistence.rs b/crates/openhuman-core/src/security/credentials/profiles/persistence.rs
index 2cbf0f9d23..773ae89249 100644
--- a/crates/openhuman-core/src/security/credentials/profiles/persistence.rs
+++ b/crates/openhuman-core/src/security/credentials/profiles/persistence.rs
@@ -89,6 +89,18 @@ impl AuthProfilesStore {
);
}
+ if !self.use_keychain {
+ // Entries whose secrets are only in the unavailable keychain never
+ // reach `data`; keep them so they resolve once it reads again.
+ for (id, held) in self.read_persisted_locked()?.profiles {
+ if !persisted.profiles.contains_key(&id)
+ && super::migration::keychain_held_oauth(&held)
+ {
+ persisted.profiles.insert(id, held);
+ }
+ }
+ }
+
self.write_persisted_locked(&persisted)
}
diff --git a/crates/openhuman-core/src/security/credentials/profiles_keychain_error_tests.rs b/crates/openhuman-core/src/security/credentials/profiles_keychain_error_tests.rs
index 30b38b8890..861227783d 100644
--- a/crates/openhuman-core/src/security/credentials/profiles_keychain_error_tests.rs
+++ b/crates/openhuman-core/src/security/credentials/profiles_keychain_error_tests.rs
@@ -163,3 +163,48 @@ fn keychain_read_error_with_undecryptable_json_keeps_the_profile() {
json_before
);
}
+
+fn persisted_entry(store: &AuthProfilesStore, id: &str) -> serde_json::Value {
+ let json: serde_json::Value =
+ serde_json::from_str(&std::fs::read_to_string(store.path()).unwrap()).unwrap();
+ json["profiles"][id].clone()
+}
+
+#[test]
+fn an_unavailable_keychain_carries_keychain_only_profiles_untouched() {
+ // A keychain that is locked when the store is built turns the keychain off
+ // for that store (`AuthProfilesStore::new`'s availability probe), so its
+ // reads never reach the keychain and a keychain-only OAuth profile resolves
+ // to no access token. It is carried, not purged, through loads and writes.
+ let mut f = locked_keychain_store();
+ f.store.use_keychain = false;
+ let entry_before = persisted_entry(&f.store, &f.oauth_id);
+
+ let data = f.store.load().expect("load without the keychain");
+ assert!(
+ !data.profiles.contains_key(&f.oauth_id),
+ "an unresolved profile is not handed out"
+ );
+ f.store
+ .upsert_profile(
+ AuthProfile::new_token("openrouter", "default", "new-token".into()),
+ false,
+ )
+ .expect("a write without the keychain");
+ assert_eq!(
+ persisted_entry(&f.store, &f.oauth_id),
+ entry_before,
+ "the keychain-only profile was dropped or rewritten"
+ );
+
+ f.store.use_keychain = true;
+ f.keychain.fail_reads.store(false, Ordering::SeqCst);
+ let data = f.store.load().expect("load once the keychain reads");
+ assert_eq!(
+ data.profiles[&f.oauth_id]
+ .token_set
+ .as_ref()
+ .map(|t| t.access_token.as_str()),
+ Some("kc-access")
+ );
+}