Sample code for 30+ languages & platforms
Rust

Windows Credentials Manager / Apple Keychain - List Matching Secrets

See more Secrets Examples

List secrets matching one or more wildcarded names for app, service, domain, and username.

Note: This example requires Chilkat v10.1.0 or later.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let secrets = chilkat::Secrets::new();

// On Windows, this is the Windows Credentials Manager
// On MacOS/iOS, it is the Apple Keychain
secrets.set_location("local_manager");

// Set wildcarded or exact values for appName, service, domain, and username.
// Omit any members where anything is allowed to match, or alternatively specify "*" to match anything.
let json_match = chilkat::JsonObject::new();
let _ = json_match.update_string("appName", "Test*");
let _ = json_match.update_string("service", "*");
let _ = json_match.update_string("domain", "*");
let _ = json_match.update_string("username", "Starfish*");

let results = chilkat::JsonObject::new();
results.set_emit_compact(false);

if secrets.list_secrets(&json_match, &results).is_err() {
    println!("{}", secrets.last_error_text());
    return;
}

println!("{}", results.emit().unwrap_or_default());

// Sample output on Windows.
// The "targetName" is purely informational and indicates the raw TargetName of the secret (i.e. Credential) stored in the Credentials Manager.

// {
//   "secrets": [
//     {
//       "appName": "Test2",
//       "service": "Custom",
//       "domain": "Ocean",
//       "username": "Starfish20",
//       "targetName": "Test2/Custom/Ocean/Starfish20"
//     },
//     {
//       "appName": "Test2",
//       "service": "Custom",
//       "domain": "Ocean",
//       "username": "Starfish",
//       "targetName": "Test2/Custom/Ocean/Starfish"
//     }
//   ]
// }

// ---------------------------------------------------------------------------------------
// Here's sample code for parsing the JSON list of secrets.

let mut i = 0;
let count_i = results.size_of_array("secrets");
while i < count_i {
    results.set_i(i);
    let _ = results.string_of("secrets[i].appName").unwrap_or_default();
    let _ = results.string_of("secrets[i].service").unwrap_or_default();
    let _ = results.string_of("secrets[i].domain").unwrap_or_default();
    let _ = results.string_of("secrets[i].username").unwrap_or_default();
    // Information field for Windows Credentials Manager.
    let _ = results.string_of("secrets[i].targetName").unwrap_or_default();
    // Informational fields if on MacOS using the Apple Keychain.
    let _ = results.string_of("secrets[i].keyChainService").unwrap_or_default();
    let _ = results.string_of("secrets[i].keyChainAccount").unwrap_or_default();
    i = i + 1;
}