diff --git a/crates/voelin-store/src/keystore.rs b/crates/voelin-store/src/keystore.rs index b36da42..34a797b 100644 --- a/crates/voelin-store/src/keystore.rs +++ b/crates/voelin-store/src/keystore.rs @@ -71,6 +71,26 @@ //! key stays registered with the account there. The release is its own //! operation, see below. //! +//! # Releasing a device key +//! +//! The release of the device key from its account (part 2 of open question 10) +//! is the account service's operation followed by a local unbind, and the unbind +//! is a sign-out: `AccountClient::release` of `voelin-account-client` calls +//! `POST /v1/devices//release` after a password check, and then runs +//! [`KeyStore::sign_out`] with [`DeviceKeyChoice::Auto`], which keeps the key, +//! unbound. So the store needs nothing of its own for it: no call that unbinds +//! one key (a [`KeyStore::put`] of the blob without its binding does that, and +//! keeps the data, as the sign-out does), and no record that the key was +//! released. The accounts design asks the client to remember nothing of the +//! kind: the service holds the registration of the key (after a release it is +//! registered with no account, after a sign-out with the account that was +//! signed out of), and the client learns which at the next sign-in or sign-up, +//! from the service's answer (`CONFLICT`, field `device`), not from a note kept +//! here. The deletion of the account (`AccountClient::delete_account`) is the +//! same: the service writes a DEVICE and a RELEASED entry for every device key +//! of the account, and the local half is this sign-out, which keeps this +//! device's key. +//! //! # Forgetting the device's identity //! //! "Forget this device's identity" is the user's explicit choice and the only diff --git a/crates/voelin-store/tests/keystore.rs b/crates/voelin-store/tests/keystore.rs index 88c63ab..8d93646 100644 --- a/crates/voelin-store/tests/keystore.rs +++ b/crates/voelin-store/tests/keystore.rs @@ -1956,6 +1956,68 @@ fn a_key_kept_by_a_sign_out_is_exported_and_then_forgotten_without_a_trace() { assert_eq!(store.get(KIND_VOELIN, &uid.to_base32()).unwrap().unwrap(), stored); } +// ------------------------------------------ releasing the device key from its account +// +// `AccountClient::release` asks the service to release the key and then runs the +// local sign-out with `Auto` (protocol 13.6, "Releasing this device's key"), so +// the store's part is the sign-out of the tests above, and the key has to be +// usable for another account afterwards. + +#[test] +fn the_local_half_of_a_release_keeps_the_key_and_it_serves_another_account() { + let Install { store, .. } = signed_in_install(false); + let uid = key().uid().to_base32(); + let before = store.get(KIND_VOELIN, &uid).unwrap().unwrap(); + let signature = key().sign(b"the same key, before and after the release"); + + // The service released the key; the device finishes with a local sign-out. + let done = store.sign_out(&binding().account, DeviceKeyChoice::Auto).unwrap(); + assert!(done.unbound.contains(&device_key_info()), "{done:?}"); + assert!(!done.removed.contains(&device_key_info()), "{done:?}"); + assert_eq!(store.get(KIND_VOELIN, &uid).unwrap().unwrap(), before, "byte for byte"); + assert_eq!(store.account_record().unwrap(), None); + + // The key is registered with no account and signs up for another one: its + // record names the same key, and a copy of the new account's material is + // bound to it. + let other_id = AccountId::from_bytes([42; 16]); + let other = AccountRecord { account_id: other_id, login: "bob".into(), ..record() }; + store.put_account_record(&other).unwrap(); + assert_eq!(store.account_record().unwrap(), Some(other.clone())); + let other_binding = AccountBinding { account: account_uid(&issuer(), &other_id), ..binding() }; + let other_copy = fresh_teamspeak(other_binding); + store.put(&other_copy).unwrap(); + + // Signing out of the new account keeps the key again, and the copy goes. + let done = store.sign_out(&other_binding.account, DeviceKeyChoice::Auto).unwrap(); + assert!(done.unbound.contains(&device_key_info()), "{done:?}"); + assert!(done.removed.contains(&info_of(&other_copy)), "{done:?}"); + let kept = store.voelin_key(&key().uid()).unwrap().expect("the key stays"); + assert_eq!(kept.sign(b"the same key, before and after the release"), signature); + assert_eq!(store.get(KIND_VOELIN, &uid).unwrap().unwrap(), before, "byte for byte"); +} + +#[test] +fn a_release_whose_local_half_was_cut_short_is_finished_by_a_sign_out() { + // The service released the device and the call was dropped before the local + // sign-out took the slot: the record and the copies are still here, and the + // key is still the record's device key. A sign-out with `Auto` finishes it + // and keeps the key; running it again finds nothing to do. + let Install { store, synced, device_only, .. } = signed_in_install(true); + let uid = key().uid().to_base32(); + let before = store.get(KIND_VOELIN, &uid).unwrap().unwrap(); + + let done = store.sign_out(&binding().account, DeviceKeyChoice::Auto).unwrap(); + assert!(done.removed.contains(&info_of(&synced)), "{done:?}"); + assert!(done.unbound.contains(&info_of(&device_only.clone().with_binding(None))), "{done:?}"); + assert_eq!(store.account_record().unwrap(), None); + assert_eq!(store.get(KIND_VOELIN, &uid).unwrap().unwrap(), before); + assert_eq!( + store.sign_out(&binding().account, DeviceKeyChoice::Auto).unwrap(), + SignOut::default() + ); +} + // -------------------------------------- the device key is never a synced copy // // `KeyStore::sign_out` deletes everything bound with `synced = true` in its