1use anyhow::{anyhow, Result};
2use keyring::Entry;
3
4pub struct Keychain {
5 service: String,
6 account: String,
7}
8
9impl Keychain {
10 pub fn new(service: &str, account: &str) -> Self {
11 Self {
12 service: service.into(),
13 account: account.into(),
14 }
15 }
16
17 fn entry(&self) -> Result<Entry> {
18 Entry::new(&self.service, &self.account).map_err(|e| anyhow!("keyring error: {e}"))
19 }
20
21 pub fn set_password(&self, secret: &str) -> Result<()> {
22 self.entry()?
23 .set_password(secret)
24 .map_err(|e| anyhow!("Failed to save credentials to keychain: {e}"))
25 }
26
27 pub fn get_password(&self) -> Result<String> {
28 self.entry()?
29 .get_password()
30 .map_err(|e| anyhow!("Failed to load credentials from keychain: {e}"))
31 }
32
33 pub fn delete_password(&self) -> Result<()> {
34 self.entry()?
35 .delete_credential()
36 .map_err(|e| anyhow!("keyring error: {e}"))
37 }
38}