set_internet_password/
set_internet_password.rs

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