Skip to main content

localauthentication/
la_credential.rs

1//! Application-provided credential helpers for `LAContext`.
2
3use crate::ffi;
4
5/// Credential kinds accepted by `LAContext::set_credential`.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[non_exhaustive]
8pub enum LACredentialType {
9    /// Application-provided password data.
10    ApplicationPassword,
11    /// Application-provided smart-card PIN data.
12    SmartCardPin,
13}
14
15impl LACredentialType {
16    #[must_use]
17    pub const fn raw_value(self) -> i32 {
18        match self {
19            Self::ApplicationPassword => ffi::la_credential::APPLICATION_PASSWORD,
20            Self::SmartCardPin => ffi::la_credential::SMART_CARD_PIN,
21        }
22    }
23
24    pub(crate) const fn as_ffi(self) -> i32 {
25        self.raw_value()
26    }
27}
28
29/// Owned credential bytes paired with their `LACredentialType`.
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct LACredential {
32    credential_type: LACredentialType,
33    bytes: Vec<u8>,
34}
35
36impl LACredential {
37    /// Create a credential from raw bytes.
38    #[must_use]
39    pub fn new(credential_type: LACredentialType, bytes: impl Into<Vec<u8>>) -> Self {
40        Self {
41            credential_type,
42            bytes: bytes.into(),
43        }
44    }
45
46    /// Create an application-password credential.
47    #[must_use]
48    pub fn application_password(bytes: impl Into<Vec<u8>>) -> Self {
49        Self::new(LACredentialType::ApplicationPassword, bytes)
50    }
51
52    /// Create a smart-card-PIN credential.
53    #[must_use]
54    pub fn smart_card_pin(bytes: impl Into<Vec<u8>>) -> Self {
55        Self::new(LACredentialType::SmartCardPin, bytes)
56    }
57
58    /// Return the credential kind.
59    #[must_use]
60    pub const fn credential_type(&self) -> LACredentialType {
61        self.credential_type
62    }
63
64    /// Borrow the credential bytes.
65    #[must_use]
66    pub fn bytes(&self) -> &[u8] {
67        &self.bytes
68    }
69}