pub struct LACredential { /* private fields */ }Expand description
Owned credential bytes paired with their LACredentialType.
Implementations§
Source§impl LACredential
impl LACredential
Sourcepub fn new(credential_type: LACredentialType, bytes: impl Into<Vec<u8>>) -> Self
pub fn new(credential_type: LACredentialType, bytes: impl Into<Vec<u8>>) -> Self
Create a credential from raw bytes.
Sourcepub fn application_password(bytes: impl Into<Vec<u8>>) -> Self
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
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}Sourcepub fn smart_card_pin(bytes: impl Into<Vec<u8>>) -> Self
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}Sourcepub const fn credential_type(&self) -> LACredentialType
pub const fn credential_type(&self) -> LACredentialType
Return the credential kind.
Sourcepub fn bytes(&self) -> &[u8] ⓘ
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
impl Clone for LACredential
Source§fn clone(&self) -> LACredential
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for LACredential
impl Debug for LACredential
Source§impl PartialEq for LACredential
impl PartialEq for LACredential
Source§fn eq(&self, other: &LACredential) -> bool
fn eq(&self, other: &LACredential) -> bool
Tests for
self and other values to be equal, and is used by ==.impl Eq for LACredential
impl StructuralPartialEq for LACredential
Auto Trait Implementations§
impl Freeze for LACredential
impl RefUnwindSafe for LACredential
impl Send for LACredential
impl Sync for LACredential
impl Unpin for LACredential
impl UnsafeUnpin for LACredential
impl UnwindSafe for LACredential
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more