feat #14740: settings, keyring, tray and manual recording
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
//! API keys, stored only in the OS keyring (Windows Credential Manager,
|
||||
//! Linux Secret Service) under the service `eu.euvino.callrecorder`.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
|
||||
/// Keyring service name all keys are stored under.
|
||||
pub const KEYRING_SERVICE: &str = "eu.euvino.callrecorder";
|
||||
|
||||
/// The external services whose API keys the app keeps.
|
||||
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Service {
|
||||
DeepScript,
|
||||
Anthropic,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
/// Keyring user name of this service's entry.
|
||||
pub fn user(self) -> &'static str {
|
||||
match self {
|
||||
Service::DeepScript => "deepscript",
|
||||
Service::Anthropic => "anthropic",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum SecretError {
|
||||
#[error("Schlüsselbund-Fehler: {0}")]
|
||||
Keyring(String),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, SecretError>;
|
||||
|
||||
pub trait SecretStore: Send + Sync {
|
||||
/// The stored key, or `None` if there is none (or the keyring cannot be
|
||||
/// read).
|
||||
fn get(&self, s: Service) -> Option<String>;
|
||||
fn set(&self, s: Service, v: &str) -> Result<()>;
|
||||
/// Removes the key; removing a key that does not exist is not an error.
|
||||
fn delete(&self, s: Service) -> Result<()>;
|
||||
}
|
||||
|
||||
/// The OS keyring.
|
||||
pub struct KeyringSecrets;
|
||||
|
||||
impl KeyringSecrets {
|
||||
fn entry(s: Service) -> Result<keyring::Entry> {
|
||||
keyring::Entry::new(KEYRING_SERVICE, s.user())
|
||||
.map_err(|e| SecretError::Keyring(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl SecretStore for KeyringSecrets {
|
||||
fn get(&self, s: Service) -> Option<String> {
|
||||
Self::entry(s).ok()?.get_password().ok()
|
||||
}
|
||||
|
||||
fn set(&self, s: Service, v: &str) -> Result<()> {
|
||||
Self::entry(s)?
|
||||
.set_password(v)
|
||||
.map_err(|e| SecretError::Keyring(e.to_string()))
|
||||
}
|
||||
|
||||
fn delete(&self, s: Service) -> Result<()> {
|
||||
match Self::entry(s)?.delete_credential() {
|
||||
Ok(()) | Err(keyring::Error::NoEntry) => Ok(()),
|
||||
Err(e) => Err(SecretError::Keyring(e.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// In-memory secret store for tests.
|
||||
#[derive(Default)]
|
||||
pub struct MemorySecrets {
|
||||
keys: Mutex<HashMap<Service, String>>,
|
||||
}
|
||||
|
||||
impl SecretStore for MemorySecrets {
|
||||
fn get(&self, s: Service) -> Option<String> {
|
||||
self.keys.lock().unwrap().get(&s).cloned()
|
||||
}
|
||||
|
||||
fn set(&self, s: Service, v: &str) -> Result<()> {
|
||||
self.keys.lock().unwrap().insert(s, v.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete(&self, s: Service) -> Result<()> {
|
||||
self.keys.lock().unwrap().remove(&s);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn memory_secrets_roundtrip() {
|
||||
let secrets = MemorySecrets::default();
|
||||
assert_eq!(secrets.get(Service::DeepScript), None);
|
||||
|
||||
secrets.set(Service::DeepScript, "ds-key").unwrap();
|
||||
assert_eq!(secrets.get(Service::DeepScript).as_deref(), Some("ds-key"));
|
||||
assert_eq!(secrets.get(Service::Anthropic), None);
|
||||
|
||||
secrets.delete(Service::DeepScript).unwrap();
|
||||
assert_eq!(secrets.get(Service::DeepScript), None);
|
||||
secrets.delete(Service::DeepScript).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn service_serializes_as_keyring_user() {
|
||||
for s in [Service::DeepScript, Service::Anthropic] {
|
||||
let json = serde_json::to_string(&s).unwrap();
|
||||
assert_eq!(json, format!("\"{}\"", s.user()));
|
||||
assert_eq!(serde_json::from_str::<Service>(&json).unwrap(), s);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user