set_internet_password.rs 900 B

123456789101112131415161718192021222324252627282930313233
  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 password = b"oxidize";
  10. let res = SecKeychain::default().unwrap().set_internet_password(
  11. hostname,
  12. None,
  13. username,
  14. "",
  15. None,
  16. SecProtocolType::HTTPS,
  17. SecAuthenticationType::HTMLForm,
  18. password,
  19. );
  20. match res {
  21. Ok(_) => {
  22. println!(
  23. "Password set for {}@{}. You can read it using find_internet_password example",
  24. username, hostname
  25. );
  26. }
  27. Err(err) => {
  28. eprintln!("Could not set password: {:?}", err);
  29. }
  30. }
  31. }}