Skip to main content

01_smoke/
01_smoke.rs

1use localauthentication::prelude::*;
2
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let context = LAContext::new()?;
5    context.set_interaction_not_allowed(true)?;
6    context.set_localized_fallback_title(Some("Use Password"))?;
7    context.set_localized_cancel_title(Some("Cancel"))?;
8    context.set_localized_reason("inspect the device owner's authentication state")?;
9    context.set_touch_id_authentication_allowable_reuse_duration(30.0)?;
10
11    let credential = LACredential::application_password(b"secret".to_vec());
12    assert!(context.set_credential(&credential)?);
13    assert!(context.is_credential_set(LACredentialType::ApplicationPassword)?);
14    assert!(context.clear_credential(LACredentialType::ApplicationPassword)?);
15
16    let preflight =
17        match context.can_evaluate_policy(LAPolicy::DeviceOwnerAuthenticationWithBiometrics) {
18            Ok(true) => "biometry available".to_owned(),
19            Ok(false) => "biometry unavailable without a framework error".to_owned(),
20            Err(error) => format!("biometry unavailable: {error}"),
21        };
22    let domain_state = context.domain_state()?;
23
24    println!("preflight: {preflight}");
25    println!("localized reason: {}", context.localized_reason()?);
26    println!("biometry type: {:?}", context.biometry_type()?);
27    println!(
28        "domain state hash bytes: {}",
29        domain_state.state_hash().map_or(0, <[u8]>::len)
30    );
31    println!(
32        "reuse max seconds: {}",
33        LAContext::touch_id_authentication_maximum_allowable_reuse_duration()
34    );
35    println!("✅ localauth context + credentials OK");
36    Ok(())
37}