find_internet_password.rs 1.1 KB

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