fix #14740: fixed backend url per build, key bound to its server

- the backend URL is no longer a setting: one constant per build
  (release/dev) in src-tauri/src/backend.rs; Settings shows the server
  read-only; an old backend_url in settings.json is ignored
- the stored key records the backend it was created for; a key of
  another backend is discarded before any use and never stored
- a refused key is discarded only if it still is the stored one
- the keyring is never called while the settings are locked
- one upload per call at a time, also visible after a view switch
- opener plugin without its link-click script (it also ran in the
  remote login page); a login error says to reload the login page

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-06 15:18:05 +01:00
co-authored by Claude Opus 5.5
parent f045424e97
commit 3358de7888
10 changed files with 399 additions and 217 deletions
+216 -46
View File
@@ -157,6 +157,26 @@ pub struct AppState {
/// Set while a recording starts or the devices are tested, so neither
/// runs twice and `current` is never locked for longer than a moment.
devices_busy: AtomicBool,
/// The backend this build talks to ([`crate::backend::BACKEND_URL`]).
backend_url: String,
/// Held while the stored backend key and its login change, so a
/// compare-and-delete never removes a key stored meanwhile.
backend_key_lock: Mutex<()>,
/// Calls whose transcript is being uploaded; one upload per call.
uploading: Mutex<HashSet<CallId>>,
}
/// An upload reserved with [`AppState::claim_upload`]; released when
/// dropped, also if the upload fails or panics.
pub struct UploadClaim<'a> {
state: &'a AppState,
id: CallId,
}
impl Drop for UploadClaim<'_> {
fn drop(&mut self) {
lock(&self.state.uploading).remove(&self.id);
}
}
/// Releases [`AppState::devices_busy`] when dropped.
@@ -189,6 +209,9 @@ impl AppState {
pending_prompts: Mutex::new(VecDeque::new()),
processing: Mutex::new(HashSet::new()),
devices_busy: AtomicBool::new(false),
backend_url: crate::backend::BACKEND_URL.to_string(),
backend_key_lock: Mutex::new(()),
uploading: Mutex::new(HashSet::new()),
}
}
@@ -198,50 +221,78 @@ impl AppState {
/// Validates, saves and applies the settings the user edited; the
/// detector picks up the new allowlist and delays immediately. The
/// backend login is kept, unless the backend URL changed: then its key
/// is discarded (it belongs to the old backend) and `true` returned.
/// Deletes from the keyring, so call it off the UI thread.
pub fn save_settings(&self, edited: Settings) -> Result<bool, String> {
/// backend login (managed by the app) is kept.
pub fn save_settings(&self, edited: Settings) -> Result<(), String> {
let mut current = lock(&self.settings);
let (settings, backend_changed) = current.edited(edited)?;
if backend_changed {
self.secrets
.delete(Service::Backend)
.map_err(|e| format!("Backend-Schlüssel konnte nicht entfernt werden: {e}"))?;
}
let settings = current.edited(edited)?;
save(&settings, &self.settings_path)?;
lock(&self.detector).set_config(detector_config(&settings));
*current = settings;
Ok(backend_changed)
Ok(())
}
/// Client of the configured backend.
/// The backend this build talks to.
pub fn backend_url(&self) -> &str {
&self.backend_url
}
/// Client of this build's backend.
pub fn backend_client(&self) -> BackendClient {
BackendClient::new(self.transport.clone(), &self.settings().backend_url)
BackendClient::new(self.transport.clone(), &self.backend_url)
}
/// The stored backend key with its expiry. Reads the keyring, so call
/// it off the UI thread.
/// The stored backend key with its expiry, if it was created for this
/// build's backend. A key of another backend is discarded instead (as
/// if expired), so it is never sent anywhere else. Reads the keyring,
/// so call it off the UI thread.
pub fn backend_key(&self) -> Option<StoredKey> {
let _key_lock = lock(&self.backend_key_lock);
let key = self
.secrets
.get(Service::Backend)
.filter(|key| !key.trim().is_empty())?;
let settings = self.settings();
if settings.backend_key_base_url.as_deref() != Some(self.backend_url.as_str()) {
log::info!(
"backend: the stored key belongs to {:?}, not {:?}: discarding it",
settings.backend_key_base_url,
self.backend_url
);
if let Err(e) = self.forget_backend_key() {
log::error!("backend: {e}");
}
return None;
}
Some(StoredKey {
key,
expires_at: self.settings().backend_key_expires_at,
expires_at: settings.backend_key_expires_at,
})
}
/// Stores a key the login created (in the keyring) and whose it is and
/// when it expires (in the settings).
pub fn store_backend_key(&self, key: &NewKey, username: Option<String>) -> Result<(), String> {
/// Stores a key the login created for the backend at `base_url` (in
/// the keyring) with whose it is, when it expires and its backend (in
/// the settings). A key of another backend than this build's is
/// refused and nothing stored.
pub fn store_backend_key(
&self,
key: &NewKey,
base_url: &str,
username: Option<String>,
) -> Result<(), String> {
let base_url = base_url.trim_end_matches('/');
if base_url != self.backend_url {
return Err(format!(
"Der Schlüssel gehört zu einem anderen Server ({base_url}) und wurde nicht gespeichert."
));
}
let _key_lock = lock(&self.backend_key_lock);
self.secrets
.set(Service::Backend, &key.key)
.map_err(|e| format!("Backend-Schlüssel konnte nicht gespeichert werden: {e}"))?;
self.update_settings(|s| {
s.backend_username = username;
s.backend_key_expires_at = Some(key.expires_at);
s.backend_key_base_url = Some(base_url.to_string());
})
}
@@ -254,17 +305,51 @@ impl AppState {
})
}
/// Forgets the backend key (keyring) and its login (settings).
/// "Abmelden": forgets the backend key (keyring) and its login
/// (settings).
pub fn discard_backend_key(&self) -> Result<(), String> {
let _key_lock = lock(&self.backend_key_lock);
self.forget_backend_key()
}
/// Forgets the stored backend key only if it still is `refused`, the
/// key the backend just refused; a key stored meanwhile (by a login)
/// stays. Returns whether it was forgotten.
pub fn discard_refused_backend_key(&self, refused: &str) -> Result<bool, String> {
let _key_lock = lock(&self.backend_key_lock);
if self.secrets.get(Service::Backend).as_deref() != Some(refused) {
return Ok(false);
}
self.forget_backend_key().map(|()| true)
}
/// Deletes the key and its login; the caller holds `backend_key_lock`.
/// The keyring is never called while the settings are locked.
fn forget_backend_key(&self) -> Result<(), String> {
self.secrets
.delete(Service::Backend)
.map_err(|e| format!("Backend-Schlüssel konnte nicht entfernt werden: {e}"))?;
self.update_settings(|s| {
s.backend_username = None;
s.backend_key_expires_at = None;
s.backend_key_base_url = None;
})
}
/// Reserves call `id` for one upload; released when the claim is
/// dropped. A second upload of the same call is refused meanwhile.
pub fn claim_upload(&self, id: CallId) -> Result<UploadClaim<'_>, String> {
if !lock(&self.uploading).insert(id) {
return Err("Wird bereits hochgeladen".to_string());
}
Ok(UploadClaim { state: self, id })
}
/// Whether call `id`'s transcript is being uploaded.
pub fn is_uploading(&self, id: CallId) -> bool {
lock(&self.uploading).contains(&id)
}
/// Changes and saves settings the app manages itself; saves only if
/// something changed.
fn update_settings(&self, change: impl FnOnce(&mut Settings)) -> Result<(), String> {
@@ -511,14 +596,16 @@ impl AppState {
Ok(calls.iter().map(CallListItem::of).collect())
}
/// The selected call with its transcript (speaker merges applied) and
/// speaker names.
/// The selected call with its transcript (speaker merges applied),
/// speaker names and whether it is being uploaded.
pub fn call_detail(&self, id: CallId) -> Result<CallDetail, String> {
let call = self.find_call(id)?;
let segments = self.store.segments(id).map_err(|e| e.to_string())?;
let names = self.store.speaker_names(id).map_err(|e| e.to_string())?;
let merges = self.store.speaker_merges(id).map_err(|e| e.to_string())?;
Ok(CallDetail::of(&call, &segments, names, &merges))
let mut detail = CallDetail::of(&call, &segments, names, &merges);
detail.uploading = self.is_uploading(id);
Ok(detail)
}
/// Renames a speaker of call `id`; a blank `name` restores the default
@@ -1337,7 +1424,7 @@ mod tests {
end_after_secs: 30,
..Settings::default()
};
assert_eq!(f.state.save_settings(valid.clone()), Ok(false));
assert_eq!(f.state.save_settings(valid.clone()), Ok(()));
assert_eq!(f.state.settings(), valid);
}
@@ -1348,13 +1435,19 @@ mod tests {
}
}
const BASE: &str = crate::backend::BACKEND_URL;
#[test]
fn a_stored_backend_key_comes_with_its_login() {
fn a_stored_backend_key_comes_with_its_login_and_server() {
let f = fixture();
assert_eq!(f.state.backend_key(), None);
f.state
.store_backend_key(&new_key("euv_k"), Some("hinrichs".into()))
.store_backend_key(
&new_key("euv_k"),
&format!("{BASE}/"),
Some("hinrichs".into()),
)
.unwrap();
let expires_at = Local.with_ymd_and_hms(2027, 11, 6, 10, 0, 0).unwrap();
@@ -1368,16 +1461,55 @@ mod tests {
let saved = Settings::load(&f.dir.path().join("settings.json"));
assert_eq!(saved.backend_username.as_deref(), Some("hinrichs"));
assert_eq!(saved.backend_key_expires_at, Some(expires_at));
assert_eq!(saved.backend_key_base_url.as_deref(), Some(BASE));
assert!(!fs::read_to_string(f.dir.path().join("settings.json"))
.unwrap()
.contains("euv_k"));
assert_eq!(f.state.settings(), saved);
}
#[test]
fn a_key_of_another_server_is_not_stored() {
let f = fixture();
let err = f
.state
.store_backend_key(&new_key("euv_k"), "https://other.example", None)
.unwrap_err();
assert!(err.contains("anderen Server"), "{err}");
assert_eq!(f.state.secrets.get(Service::Backend), None);
assert_eq!(f.state.settings(), Settings::default());
}
#[test]
fn a_stored_key_of_another_server_is_discarded_before_use() {
for recorded in [Some("https://other.example"), None] {
let f = fixture();
f.state
.store_backend_key(&new_key("euv_k"), BASE, Some("hinrichs".into()))
.unwrap();
// As if stored by a build for another backend.
f.state
.update_settings(|s| s.backend_key_base_url = recorded.map(str::to_string))
.unwrap();
assert_eq!(f.state.backend_key(), None, "{recorded:?}");
assert_eq!(f.state.secrets.get(Service::Backend), None);
let settings = f.state.settings();
assert_eq!(settings.backend_username, None);
assert_eq!(settings.backend_key_expires_at, None);
assert_eq!(settings.backend_key_base_url, None);
}
}
#[test]
fn the_backend_login_is_refreshed_from_the_key_info() {
let f = fixture();
f.state.store_backend_key(&new_key("euv_k"), None).unwrap();
f.state
.store_backend_key(&new_key("euv_k"), BASE, None)
.unwrap();
let later = Local.with_ymd_and_hms(2028, 1, 1, 0, 0, 0).unwrap();
f.state
@@ -1393,13 +1525,14 @@ mod tests {
let settings = f.state.settings();
assert_eq!(settings.backend_username.as_deref(), Some("hinrichs"));
assert_eq!(settings.backend_key_expires_at, Some(later));
assert_eq!(settings.backend_key_base_url.as_deref(), Some(BASE));
}
#[test]
fn a_discarded_backend_key_is_gone_with_its_login() {
let f = fixture();
f.state
.store_backend_key(&new_key("euv_k"), Some("hinrichs".into()))
.store_backend_key(&new_key("euv_k"), BASE, Some("hinrichs".into()))
.unwrap();
f.state.discard_backend_key().unwrap();
@@ -1409,13 +1542,33 @@ mod tests {
let settings = f.state.settings();
assert_eq!(settings.backend_username, None);
assert_eq!(settings.backend_key_expires_at, None);
assert_eq!(settings.backend_key_base_url, None);
}
#[test]
fn saving_settings_keeps_the_backend_login_unless_the_url_changes() {
fn only_the_refused_key_is_discarded() {
let f = fixture();
f.state
.store_backend_key(&new_key("euv_k"), Some("hinrichs".into()))
.store_backend_key(&new_key("euv_new"), BASE, None)
.unwrap();
// A login stored a new key after the old one was refused.
assert_eq!(f.state.discard_refused_backend_key("euv_old"), Ok(false));
assert_eq!(
f.state.backend_key().map(|k| k.key).as_deref(),
Some("euv_new")
);
assert_eq!(f.state.discard_refused_backend_key("euv_new"), Ok(true));
assert_eq!(f.state.backend_key(), None);
assert_eq!(f.state.settings().backend_key_base_url, None);
}
#[test]
fn saving_settings_keeps_the_backend_login() {
let f = fixture();
f.state
.store_backend_key(&new_key("euv_k"), BASE, Some("hinrichs".into()))
.unwrap();
// The settings page sends what it loaded, maybe before the login.
let edited = Settings {
@@ -1423,28 +1576,45 @@ mod tests {
..Settings::default()
};
assert_eq!(f.state.save_settings(edited.clone()), Ok(false));
assert!(f.state.backend_key().is_some());
assert_eq!(
f.state.settings().backend_username.as_deref(),
Some("hinrichs")
);
assert_eq!(f.state.settings().retention_days, 14);
assert_eq!(f.state.save_settings(edited), Ok(()));
let other_backend = Settings {
backend_url: "https://other.example/".into(),
..edited
};
assert_eq!(f.state.save_settings(other_backend), Ok(true));
assert_eq!(f.state.backend_key(), None);
assert_eq!(f.state.settings().backend_url, "https://other.example");
assert_eq!(f.state.settings().backend_username, None);
assert!(f.state.backend_key().is_some());
let settings = f.state.settings();
assert_eq!(settings.backend_username.as_deref(), Some("hinrichs"));
assert_eq!(settings.backend_key_base_url.as_deref(), Some(BASE));
assert_eq!(settings.retention_days, 14);
}
#[test]
fn the_backend_is_fixed_per_build() {
let f = fixture();
assert_eq!(f.state.backend_url(), "https://test.euvinopro.eu");
assert_eq!(
f.state.backend_client().login_url(),
"https://other.example/euvadm"
"https://test.euvinopro.eu/euvadm"
);
}
#[test]
fn a_call_is_uploaded_once_at_a_time() {
let f = fixture();
let (id, _, _) = finished_call(&f, "a");
assert!(!f.state.call_detail(id).unwrap().uploading);
let claim = f.state.claim_upload(id).unwrap();
assert!(f.state.is_uploading(id));
assert!(f.state.call_detail(id).unwrap().uploading);
assert_eq!(
f.state.claim_upload(id).err(),
Some("Wird bereits hochgeladen".to_string())
);
assert!(f.state.claim_upload(id + 1).is_ok(), "other calls are free");
drop(claim);
assert!(!f.state.is_uploading(id));
assert!(f.state.claim_upload(id).is_ok());
}
#[test]
fn an_upload_is_recorded_on_the_call() {
let f = fixture();