Skip to content

Commit 17a82a6

Browse files
matt2eclaude
andcommitted
fix(identity): let Buzz copies that share an FI session sign in and out
Buzz copies of one debug/release flavour share each sign-in server's Keychain item, whatever their app identifier, like the human key. That sharing stays. But a running copy read the item only once, and its compare-and-swap writes then failed whenever another copy had written since. With Buzz and Buzz Staging both using the public server, or two debug worktrees: - Sign-in in the stale copy failed with "Sign-in did not finish. Try again." after a full browser trip, and kept failing until restart. - Sign-out in the stale copy showed "Couldn't sign out of ...", although the session was already gone, and its Try again could delete the other copy's newer session. A signed-out copy now reads the item again before a browser sign-in. If another copy saved a session, it restores and verifies that session instead, with no browser. An empty item or a failed read starts the browser sign-in as before. A sign-out whose removal finds the session already ended or replaced now finishes signed out. The stale record's refusal marker stays, so a record put back later cannot restore, and the newer session is left alone. No migration: service, account, index folder and the Sign out of Buzz wipe are unchanged. The store and docs/identity.md now say the session is shared across copies, and docs/buzz-help.md tells people what that means for sign-in and sign-out. Tests cover adopting another copy's session at sign-in and a stale sign-out (session emptied or replaced); both fail without this change. A store test pins that a stale removal keeps its refusal marker and the newer session. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent 33d00da commit 17a82a6

5 files changed

Lines changed: 101 additions & 2 deletions

File tree

‎docs/buzz-help.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ the interface unless an available tool actually supports that action.
7676
lists each one you're signed in to, with the communities it's used by, and
7777
each **Sign out** ends only that server's sign-in. **Sign out of Buzz** still
7878
ends them all.
79+
- Buzz apps on this computer that use the same organization server, such as
80+
Buzz and Buzz Staging, share its sign-in, like the private key. After you sign
81+
in in one, **Sign in** in the other finishes without the browser. Signing out
82+
or switching account in one also applies to the other, but an app that's
83+
already open keeps its sign-in until it restarts.
7984
- If a community doesn't accept the account you signed in with, its prompt
8085
names the server and offers **Switch account**: Buzz signs out of that server
8186
only and opens its sign-in again, so other servers stay signed in. It also

‎docs/identity.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,14 @@ replacement. Local expiry, cancellation, and sign-out fence stale work.
914914
FI records use the OS secure-store service
915915
`xyz.block.buzz.desktop.federated-session` (with `.debug` for Rust debug
916916
builds), keyed by profile and each server's deployment fingerprint, so a build's
917-
first server keeps the sessions it saved as the only server. On macOS one item per
917+
first server keeps the sessions it saved as the only server. Neither includes the
918+
app identifier: like the human key, copies in one debug/release namespace that
919+
use the same server (Buzz and Buzz Staging both use the public server) share its
920+
session. A signed-out copy reads the item again before a browser sign-in and
921+
adopts a session another copy saved. A sign-out after another copy already ended
922+
or replaced the session finishes signed out and leaves the newer session alone.
923+
A signed-in copy keeps its session until it restarts, even after another copy
924+
signs out. On macOS one item per
918925
profile and deployment holds the session and every agent grant, so Keychain
919926
prompts once; Windows and Linux keep one item per record. Older macOS items
920927
move into it on read. This is a one-way development migration: close older

‎src-tauri/src/federated_identity/mod.rs‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,20 @@ impl FederatedIdentity {
734734
if !self.current(&observed.revision) {
735735
return Ok(self.snapshot());
736736
}
737+
// Other Buzz copies share this item: when one signed in after this
738+
// copy restored, restore its session instead of a browser sign-in.
739+
let signed_out = {
740+
let state = self.state();
741+
state.restored && state.session.is_none()
742+
};
743+
let attempt =
744+
if signed_out && matches!(self.storage(|store| store.read()).await, Ok(Some(_))) {
745+
let mut state = self.state();
746+
state.restored = false;
747+
state.restore_attempt
748+
} else {
749+
attempt
750+
};
737751
let observed = self.restore_locked(attempt).await;
738752
let (revision, cancel, renewing) = {
739753
let mut state = self.state();
@@ -1015,7 +1029,13 @@ impl FederatedIdentity {
10151029
) -> Snapshot {
10161030
let _operation = self.0.operation.lock().await;
10171031
let result = if let Some(session) = session {
1018-
self.remove(session).await
1032+
match self.remove(session).await {
1033+
// Another Buzz copy already ended or replaced this session.
1034+
// Its refusal marker stays, so it can't restore here, and
1035+
// the newer session is left alone.
1036+
Err(failure) if failure.code == "session-changed" => Ok(()),
1037+
result => result,
1038+
}
10191039
} else {
10201040
// Also retries an incomplete deletion or sign-out during restore.
10211041
self.storage(|store| {

‎src-tauri/src/federated_identity/store.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use serde::{Deserialize, Serialize};
33
use std::{collections::BTreeMap, fs, path::PathBuf, sync::Mutex};
44
use zeroize::{Zeroize, Zeroizing};
55

6+
/// Not keyed by app identifier: like the human key, every Buzz copy of this
7+
/// flavour shares each server's session per profile, and Sign out of Buzz
8+
/// erases them all.
69
pub(crate) const SERVICE: &str = if cfg!(debug_assertions) {
710
"xyz.block.buzz.desktop.federated-session.debug"
811
} else {
@@ -766,6 +769,23 @@ mod tests {
766769
drop(held);
767770
}
768771

772+
#[test]
773+
fn a_stale_copys_removal_keeps_its_refusal_and_the_newer_session() {
774+
let root = tempfile::tempdir().unwrap();
775+
// Two Buzz copies on one item: B replaces the session A still holds.
776+
let (a, b) = (secure(&root), secure(&root));
777+
let (old, newer) = (super::super::tests::record(), super::super::tests::record());
778+
a.compare(None, Some(&old.raw)).unwrap();
779+
b.compare(Some(&old.raw), Some(&newer.raw)).unwrap();
780+
assert_eq!(
781+
a.remove(&old.raw, &old.record.revision),
782+
Err("session-changed")
783+
);
784+
assert_eq!(b.read().unwrap().unwrap().as_slice(), &newer.raw[..]);
785+
// A's old record stays refused if it is ever put back.
786+
assert!(a.refused(&old.record.revision).unwrap().is_some());
787+
}
788+
769789
#[test]
770790
fn failed_removal_still_refuses_only_the_known_revision() {
771791
let root = tempfile::tempdir().unwrap();

‎src-tauri/src/federated_identity/tests.rs‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,53 @@ fn signed_in_at(
826826
(host, current)
827827
}
828828

829+
#[tokio::test]
830+
async fn signing_in_adopts_a_session_another_copy_saved() {
831+
let (base, server) = adapter(vec![
832+
serde_json::json!({"expires_at":"2099-01-01T00:00:00Z","email":"[email protected]"}),
833+
])
834+
.await;
835+
let store = Arc::new(Memory::default());
836+
let host = at_adapter(store.clone(), base);
837+
assert!(host.restore().await.status == Status::SignedOut);
838+
// Another Buzz copy sharing the item signs in after this one restored.
839+
let other = record();
840+
*store.raw.lock().unwrap() = Some(other.raw.clone());
841+
// The fixture's plain-HTTP login URL fails a browser sign-in at once.
842+
let app = tauri::test::mock_builder()
843+
.manage(crate::oauth_callback::OAuthCallbackHost::default())
844+
.build(crate::app_context())
845+
.unwrap();
846+
let snapshot = host.sign_in(app.handle()).await.unwrap();
847+
assert!(snapshot.status == Status::SignedIn);
848+
assert_eq!(snapshot.failure, None);
849+
assert_eq!(snapshot.revision, other.record.revision);
850+
assert_eq!(snapshot.email.as_deref(), Some("[email protected]"));
851+
let seen = server.await.unwrap();
852+
assert_eq!(seen[0].target, "GET /mount/v1/session HTTP/1.1");
853+
}
854+
855+
#[tokio::test]
856+
async fn signing_out_after_another_copy_ended_or_replaced_the_session_is_signed_out() {
857+
for newer in [None, Some(record())] {
858+
let store = Arc::new(Memory::default());
859+
let (host, _) = signed_in_at(
860+
store.clone(),
861+
"https://identity.example".into(),
862+
863+
);
864+
// Another Buzz copy sharing the item signed out, or in again.
865+
*store.raw.lock().unwrap() = newer.as_ref().map(|session| session.raw.clone());
866+
let snapshot = host.sign_out().await;
867+
assert!(snapshot.status == Status::SignedOut);
868+
assert_eq!(snapshot.failure, None);
869+
assert_eq!(
870+
store.raw.lock().unwrap().as_deref(),
871+
newer.as_ref().map(|session| &*session.raw)
872+
);
873+
}
874+
}
875+
829876
#[tokio::test]
830877
async fn the_expiry_watch_warns_then_ends_the_session_at_expiry() {
831878
let store = Arc::new(Memory::default());

0 commit comments

Comments
 (0)