Skip to main content

LACredential

Struct LACredential 

Source
pub struct LACredential { /* private fields */ }
Expand description

Owned credential bytes paired with their LACredentialType.

Implementations§

Source§

impl LACredential

Source

pub fn new(credential_type: LACredentialType, bytes: impl Into<Vec<u8>>) -> Self

Create a credential from raw bytes.

Source

pub fn application_password(bytes: impl Into<Vec<u8>>) -> Self

Create an application-password credential.

Examples found in repository?
examples/04_credentials.rs (line 5)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let context = LAContext::new()?;
5    let password = LACredential::application_password(b"p@ssword".to_vec());
6    let pin = LACredential::smart_card_pin(b"123456".to_vec());
7
8    assert!(context.set_credential(&password)?);
9    assert!(context.is_credential_set(LACredentialType::ApplicationPassword)?);
10    assert!(context.clear_credential(LACredentialType::ApplicationPassword)?);
11
12    assert!(context.set_credential(&pin)?);
13    assert!(context.is_credential_set(LACredentialType::SmartCardPin)?);
14    assert!(context.clear_credential(LACredentialType::SmartCardPin)?);
15
16    println!("password bytes: {}", password.bytes().len());
17    println!("pin bytes: {}", pin.bytes().len());
18    println!("✅ credential round-trip OK");
19    Ok(())
20}
More examples
Hide additional examples
examples/01_smoke.rs (line 11)
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}
Source

pub fn smart_card_pin(bytes: impl Into<Vec<u8>>) -> Self

Create a smart-card-PIN credential.

Examples found in repository?
examples/04_credentials.rs (line 6)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let context = LAContext::new()?;
5    let password = LACredential::application_password(b"p@ssword".to_vec());
6    let pin = LACredential::smart_card_pin(b"123456".to_vec());
7
8    assert!(context.set_credential(&password)?);
9    assert!(context.is_credential_set(LACredentialType::ApplicationPassword)?);
10    assert!(context.clear_credential(LACredentialType::ApplicationPassword)?);
11
12    assert!(context.set_credential(&pin)?);
13    assert!(context.is_credential_set(LACredentialType::SmartCardPin)?);
14    assert!(context.clear_credential(LACredentialType::SmartCardPin)?);
15
16    println!("password bytes: {}", password.bytes().len());
17    println!("pin bytes: {}", pin.bytes().len());
18    println!("✅ credential round-trip OK");
19    Ok(())
20}
Source

pub const fn credential_type(&self) -> LACredentialType

Return the credential kind.

Source

pub fn bytes(&self) -> &[u8]

Borrow the credential bytes.

Examples found in repository?
examples/04_credentials.rs (line 16)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let context = LAContext::new()?;
5    let password = LACredential::application_password(b"p@ssword".to_vec());
6    let pin = LACredential::smart_card_pin(b"123456".to_vec());
7
8    assert!(context.set_credential(&password)?);
9    assert!(context.is_credential_set(LACredentialType::ApplicationPassword)?);
10    assert!(context.clear_credential(LACredentialType::ApplicationPassword)?);
11
12    assert!(context.set_credential(&pin)?);
13    assert!(context.is_credential_set(LACredentialType::SmartCardPin)?);
14    assert!(context.clear_credential(LACredentialType::SmartCardPin)?);
15
16    println!("password bytes: {}", password.bytes().len());
17    println!("pin bytes: {}", pin.bytes().len());
18    println!("✅ credential round-trip OK");
19    Ok(())
20}

Trait Implementations§

Source§

impl Clone for LACredential

Source§

fn clone(&self) -> LACredential

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LACredential

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for LACredential

Source§

fn eq(&self, other: &LACredential) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for LACredential

Source§

impl StructuralPartialEq for LACredential

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.