pub struct Keychain;Expand description
Stateless entry point for generic-password keychain operations.
Implementations§
Source§impl Keychain
impl Keychain
Sourcepub fn entry(
account: impl Into<String>,
service: impl Into<String>,
) -> KeychainEntry
pub fn entry( account: impl Into<String>, service: impl Into<String>, ) -> KeychainEntry
Build a typed keychain entry for (account, service).
Sourcepub fn set(account: &str, service: &str, password: &str) -> Result<()>
pub fn set(account: &str, service: &str, password: &str) -> Result<()>
Upsert a generic-password keychain item.
§Errors
Returns an error if Security.framework rejects the item or the strings contain NUL bytes.
Examples found in repository?
examples/01_smoke.rs (line 8)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let account = "doom-fish-smoke";
5 let service = format!("doom-fish-smoke-test-{}", std::process::id());
6
7 let _ = Keychain::delete(account, &service);
8 Keychain::set(account, &service, "hunter2")?;
9
10 let value = Keychain::get(account, &service)?;
11 assert_eq!(value, "hunter2");
12
13 let accounts = Keychain::list_accounts(&service)?;
14 assert!(accounts.iter().any(|candidate| candidate == account));
15
16 Keychain::delete(account, &service)?;
17
18 let random = SecureRandom::bytes(32)?;
19 assert!(random.iter().any(|&byte| byte != 0));
20
21 println!("✅ security keychain + RNG OK");
22 Ok(())
23}Sourcepub fn get(account: &str, service: &str) -> Result<String>
pub fn get(account: &str, service: &str) -> Result<String>
Fetch a generic-password keychain item as UTF-8 text.
§Errors
Returns an error if the item does not exist, the stored bytes are not UTF-8, or Security.framework rejects the query.
Examples found in repository?
examples/01_smoke.rs (line 10)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let account = "doom-fish-smoke";
5 let service = format!("doom-fish-smoke-test-{}", std::process::id());
6
7 let _ = Keychain::delete(account, &service);
8 Keychain::set(account, &service, "hunter2")?;
9
10 let value = Keychain::get(account, &service)?;
11 assert_eq!(value, "hunter2");
12
13 let accounts = Keychain::list_accounts(&service)?;
14 assert!(accounts.iter().any(|candidate| candidate == account));
15
16 Keychain::delete(account, &service)?;
17
18 let random = SecureRandom::bytes(32)?;
19 assert!(random.iter().any(|&byte| byte != 0));
20
21 println!("✅ security keychain + RNG OK");
22 Ok(())
23}Sourcepub fn delete(account: &str, service: &str) -> Result<()>
pub fn delete(account: &str, service: &str) -> Result<()>
Delete a generic-password keychain item.
Missing items are treated as success to make cleanup ergonomic.
§Errors
Returns an error if Security.framework rejects the delete request for another reason.
Examples found in repository?
examples/01_smoke.rs (line 7)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let account = "doom-fish-smoke";
5 let service = format!("doom-fish-smoke-test-{}", std::process::id());
6
7 let _ = Keychain::delete(account, &service);
8 Keychain::set(account, &service, "hunter2")?;
9
10 let value = Keychain::get(account, &service)?;
11 assert_eq!(value, "hunter2");
12
13 let accounts = Keychain::list_accounts(&service)?;
14 assert!(accounts.iter().any(|candidate| candidate == account));
15
16 Keychain::delete(account, &service)?;
17
18 let random = SecureRandom::bytes(32)?;
19 assert!(random.iter().any(|&byte| byte != 0));
20
21 println!("✅ security keychain + RNG OK");
22 Ok(())
23}Sourcepub fn list_accounts(service: &str) -> Result<Vec<String>>
pub fn list_accounts(service: &str) -> Result<Vec<String>>
List all account names for the given generic-password service.
§Errors
Returns an error if Security.framework rejects the query.
Examples found in repository?
examples/01_smoke.rs (line 13)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let account = "doom-fish-smoke";
5 let service = format!("doom-fish-smoke-test-{}", std::process::id());
6
7 let _ = Keychain::delete(account, &service);
8 Keychain::set(account, &service, "hunter2")?;
9
10 let value = Keychain::get(account, &service)?;
11 assert_eq!(value, "hunter2");
12
13 let accounts = Keychain::list_accounts(&service)?;
14 assert!(accounts.iter().any(|candidate| candidate == account));
15
16 Keychain::delete(account, &service)?;
17
18 let random = SecureRandom::bytes(32)?;
19 assert!(random.iter().any(|&byte| byte != 0));
20
21 println!("✅ security keychain + RNG OK");
22 Ok(())
23}Auto Trait Implementations§
impl Freeze for Keychain
impl RefUnwindSafe for Keychain
impl Send for Keychain
impl Sync for Keychain
impl Unpin for Keychain
impl UnsafeUnpin for Keychain
impl UnwindSafe for Keychain
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more