feat(tui): fix keyring import path, add init_keyring() with platform feature flags
This commit is contained in:
@@ -6,7 +6,7 @@ edition = "2024"
|
|||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
macos = ["dep:apple-native-keyring-store"]
|
macos = ["dep:apple-native-keyring-store"]
|
||||||
linux-zbus = ["dep:zbus-secret-service-keyring-store"]
|
linux-zbus = ["dep:zbus-secret-service-keyring-store", "zbus-secret-service-keyring-store/rt-tokio-crypto-rust"]
|
||||||
windows = ["dep:windows-native-keyring-store"]
|
windows = ["dep:windows-native-keyring-store"]
|
||||||
sqlite = ["dep:db-keystore"]
|
sqlite = ["dep:db-keystore"]
|
||||||
|
|
||||||
@@ -20,7 +20,7 @@ csv = "1"
|
|||||||
apple-native-keyring-store = { version = "1.0.0", optional = true, features = ["keychain"] }
|
apple-native-keyring-store = { version = "1.0.0", optional = true, features = ["keychain"] }
|
||||||
zbus-secret-service-keyring-store = { version = "1.0.0", optional = true }
|
zbus-secret-service-keyring-store = { version = "1.0.0", optional = true }
|
||||||
windows-native-keyring-store = { version = "1.0.0", optional = true }
|
windows-native-keyring-store = { version = "1.0.0", optional = true }
|
||||||
db-keystore = { version = "0.4.1", optional = true }
|
db-keystore = { version = "0.4.2-pre.2", optional = true }
|
||||||
|
|
||||||
reqwest = { workspace = true }
|
reqwest = { workspace = true }
|
||||||
serde = { workspace = true }
|
serde = { workspace = true }
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use directories::ProjectDirs;
|
use directories::ProjectDirs;
|
||||||
|
use keyring_core::Entry;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
@@ -31,21 +32,59 @@ impl Config {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn init_keyring() -> Result<()> {
|
||||||
|
#[cfg(feature = "macos")]
|
||||||
|
{
|
||||||
|
use apple_native_keyring_store::keychain::Store;
|
||||||
|
keyring_core::set_default_store(Store::new()?);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "linux-zbus")]
|
||||||
|
{
|
||||||
|
keyring_core::set_default_store(zbus_secret_service_keyring_store::Store::new()?);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "windows")]
|
||||||
|
{
|
||||||
|
keyring_core::set_default_store(windows_native_keyring_store::Store::new()?);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "sqlite")]
|
||||||
|
{
|
||||||
|
let path = ProjectDirs::from("com", "movies", "movie-tui")
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("cannot find data dir for sqlite keystore"))?
|
||||||
|
.data_dir()
|
||||||
|
.join("keystore.db");
|
||||||
|
std::fs::create_dir_all(path.parent().unwrap())?;
|
||||||
|
let config = db_keystore::DbKeyStoreConfig { path, ..Default::default() };
|
||||||
|
keyring_core::set_default_store(db_keystore::DbKeyStore::new(config)?);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unreachable_code)]
|
||||||
|
anyhow::bail!(
|
||||||
|
"no keyring backend compiled in — build with --features macos|linux-zbus|windows|sqlite"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load_token() -> Option<String> {
|
pub fn load_token() -> Option<String> {
|
||||||
keyring::Entry::new(KEYRING_SERVICE, KEYRING_USER)
|
Entry::new(KEYRING_SERVICE, KEYRING_USER)
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|e| e.get_password().ok())
|
.and_then(|e| e.get_password().ok())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save_token(token: &str) -> Result<()> {
|
pub fn save_token(token: &str) -> Result<()> {
|
||||||
let entry = keyring::Entry::new(KEYRING_SERVICE, KEYRING_USER)?;
|
let entry = Entry::new(KEYRING_SERVICE, KEYRING_USER)?;
|
||||||
entry.set_password(token)?;
|
entry.set_password(token)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear_token() -> Result<()> {
|
pub fn clear_token() -> Result<()> {
|
||||||
let entry = keyring::Entry::new(KEYRING_SERVICE, KEYRING_USER)?;
|
let entry = Entry::new(KEYRING_SERVICE, KEYRING_USER)?;
|
||||||
let _ = entry.delete_credential(); // ignore NotFound
|
let _ = entry.delete_credential();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,7 +103,6 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn load_returns_none_when_no_file() {
|
fn load_returns_none_when_no_file() {
|
||||||
// Tests that load() doesn't panic — may return Some or None depending on system state
|
|
||||||
let _ = Config::load();
|
let _ = Config::load();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user