The GPUI shell repeated the Slint crate's connect, observe, gateway lookup, save and remove of a server, its default nickname, its FallbackSecrets and its settings opening and key registration. They now live once and both front ends call them: - voelin_ui_vm::servers::sequence: connect_voice(_to), observe, discover_gateway, gateway_found, select_server, save_bookmark, save_and_connect, delete_bookmark, bookmark_form and default_nickname, as transitions over the state, the Cx and a Backing (the store, the secrets, the identity a connection uses). The commands, the status line and what to keep go out through the Update as for every other transition; a new field, Update::discover, asks the front end to look a gateway up (the thread hop stays with each front end: Later in GPUI, later() in Slint). stream_peer is the one input a front end gives (Slint's codecs; GPUI passes none). - voelin_ui_vm::settings::open_settings and register_keys; a front end adds its own key (ui.window, studio.ui) through a function it passes, so Slint's settings page still lists what it listed. - voelin_store::FallbackSecrets (feature keyring) and Store::identity_for. - voelin-ui: the methods of servers/bind.rs, open_settings and default_secrets call them, so their call sites are unchanged; host.rs gets transition_with and the discover arm of perform. voelin-gpui: AppState's methods and the Backend call them. What cannot move is named in docs/ui.md. voelin-ui-vm now depends on tracing (the sequences log as the copies did): one line of Cargo.lock, THIRD_PARTY_NOTICES.md unchanged (scripts/notices.sh --check clean), cargo deny check clean. The sequences have ten tests over a fake front end (commands, status, store, secrets, gateways), open_settings and Store::identity_for one each; the GPUI and Slint suites pass unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
133 lines
3.6 KiB
Rust
133 lines
3.6 KiB
Rust
//! Storage for passwords and credentials.
|
|
|
|
use std::collections::HashMap;
|
|
use std::sync::Mutex;
|
|
|
|
use crate::Result;
|
|
|
|
/// Key-value secret storage. `key` names what the secret is for, e.g.
|
|
/// `bookmark/3/server-password` or `bookmark/3/query-password`.
|
|
pub trait Secrets: Send + Sync {
|
|
fn get(&self, key: &str) -> Result<Option<String>>;
|
|
fn set(&self, key: &str, value: &str) -> Result<()>;
|
|
fn delete(&self, key: &str) -> Result<()>;
|
|
}
|
|
|
|
/// In-memory secrets, for tests and platforms without a keyring.
|
|
#[derive(Default)]
|
|
pub struct MemorySecrets(Mutex<HashMap<String, String>>);
|
|
|
|
impl Secrets for MemorySecrets {
|
|
fn get(&self, key: &str) -> Result<Option<String>> {
|
|
Ok(self.0.lock().unwrap().get(key).cloned())
|
|
}
|
|
|
|
fn set(&self, key: &str, value: &str) -> Result<()> {
|
|
self.0.lock().unwrap().insert(key.to_string(), value.to_string());
|
|
Ok(())
|
|
}
|
|
|
|
fn delete(&self, key: &str) -> Result<()> {
|
|
self.0.lock().unwrap().remove(key);
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
/// The platform keyring: Secret Service on Linux, Credential Manager on
|
|
/// Windows, Keychain on macOS.
|
|
#[cfg(feature = "keyring")]
|
|
pub struct KeyringSecrets {
|
|
service: String,
|
|
}
|
|
|
|
#[cfg(feature = "keyring")]
|
|
impl KeyringSecrets {
|
|
/// `service` distinguishes this application's entries, e.g. the app id.
|
|
pub fn new(service: impl Into<String>) -> Self {
|
|
Self { service: service.into() }
|
|
}
|
|
|
|
fn entry(&self, key: &str) -> Result<keyring::Entry> {
|
|
keyring::Entry::new(&self.service, key).map_err(|e| crate::Error::Secrets(e.to_string()))
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "keyring")]
|
|
impl Secrets for KeyringSecrets {
|
|
fn get(&self, key: &str) -> Result<Option<String>> {
|
|
match self.entry(key)?.get_password() {
|
|
Ok(v) => Ok(Some(v)),
|
|
Err(keyring::Error::NoEntry) => Ok(None),
|
|
Err(e) => Err(crate::Error::Secrets(e.to_string())),
|
|
}
|
|
}
|
|
|
|
fn set(&self, key: &str, value: &str) -> Result<()> {
|
|
self.entry(key)?.set_password(value).map_err(|e| crate::Error::Secrets(e.to_string()))
|
|
}
|
|
|
|
fn delete(&self, key: &str) -> Result<()> {
|
|
match self.entry(key)?.delete_credential() {
|
|
Ok(()) | Err(keyring::Error::NoEntry) => Ok(()),
|
|
Err(e) => Err(crate::Error::Secrets(e.to_string())),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The keyring first, memory when the keyring cannot keep a secret (no Secret
|
|
/// Service on a headless Linux): what the desktop front ends keep the servers'
|
|
/// passwords in. A secret the keyring refused is kept for this run only.
|
|
#[cfg(feature = "keyring")]
|
|
pub struct FallbackSecrets {
|
|
keyring: KeyringSecrets,
|
|
memory: MemorySecrets,
|
|
}
|
|
|
|
#[cfg(feature = "keyring")]
|
|
impl FallbackSecrets {
|
|
/// Secrets in the keyring entries of `service` (the app's name), with
|
|
/// memory behind them.
|
|
pub fn new(service: impl Into<String>) -> Self {
|
|
Self { keyring: KeyringSecrets::new(service), memory: MemorySecrets::default() }
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "keyring")]
|
|
impl Secrets for FallbackSecrets {
|
|
fn get(&self, key: &str) -> Result<Option<String>> {
|
|
match self.memory.get(key)? {
|
|
Some(v) => Ok(Some(v)),
|
|
None => Ok(self.keyring.get(key).unwrap_or(None)),
|
|
}
|
|
}
|
|
|
|
fn set(&self, key: &str, value: &str) -> Result<()> {
|
|
if let Err(error) = self.keyring.set(key, value) {
|
|
tracing::warn!(%error, "keyring unavailable, keeping the secret for this session only");
|
|
return self.memory.set(key, value);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn delete(&self, key: &str) -> Result<()> {
|
|
let _ = self.keyring.delete(key);
|
|
self.memory.delete(key)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn memory_secrets() {
|
|
let s = MemorySecrets::default();
|
|
assert_eq!(s.get("a").unwrap(), None);
|
|
s.set("a", "1").unwrap();
|
|
assert_eq!(s.get("a").unwrap().as_deref(), Some("1"));
|
|
s.delete("a").unwrap();
|
|
s.delete("a").unwrap();
|
|
assert_eq!(s.get("a").unwrap(), None);
|
|
}
|
|
}
|