find_internet_password/
find_internet_password.rs1#[cfg(target_os = "macos")]
2use apple_security_framework::os::macos::{
3 keychain::SecKeychain,
4 passwords::{SecAuthenticationType, SecProtocolType},
5};
6
7fn main() {
8 #[cfg(target_os = "macos")]
9 {
10 let hostname = "example.com";
11 let username = "rusty";
12 let res = SecKeychain::default().unwrap().find_internet_password(
13 hostname,
14 None,
15 username,
16 "",
17 None,
18 SecProtocolType::Any,
19 SecAuthenticationType::Any,
20 );
21 match res {
22 Ok((password, _)) => {
23 println!(
24 "Password for {}@{} is {} bytes long",
25 username,
26 hostname,
27 password.len()
28 );
29 }
30 Err(err) if err.code() == -128 => {
31 eprintln!("Account was found in the Keychain, but user denied access");
32 }
33 Err(err) => {
34 eprintln!("Password not found. Open Keychain Access.app and add internet password for '{}' at 'https://{}': {:?}",
35 username, hostname, err);
36 }
37 }
38 }
39}