Skip to main content

localauthentication/
la_authentication_requirement.rs

1//! `LAAuthenticationRequirement` and `LABiometryFallbackRequirement` wrappers.
2
3use crate::ffi;
4use crate::la_error::Result;
5use crate::private::{bridge_ptr, OwnedHandle};
6
7/// Authentication requirements that can be attached to a `LARight`.
8#[derive(Debug)]
9pub struct LAAuthenticationRequirement {
10    handle: OwnedHandle,
11}
12
13impl LAAuthenticationRequirement {
14    pub(crate) const fn as_ptr(&self) -> *mut core::ffi::c_void {
15        self.handle.as_ptr()
16    }
17
18    /// The framework's default requirement.
19    ///
20    /// # Errors
21    ///
22    /// Returns an error if the API is unavailable or the Swift bridge rejects the request.
23    pub fn default_requirement() -> Result<Self> {
24        let raw = bridge_ptr(|out, error_out| unsafe {
25            ffi::la_authentication_requirement::la_authentication_requirement_default(
26                out, error_out,
27            )
28        })?;
29        Ok(Self {
30            handle: OwnedHandle::new(
31                raw,
32                ffi::la_authentication_requirement::la_authentication_requirement_release,
33            ),
34        })
35    }
36
37    /// Require biometric authentication.
38    ///
39    /// # Errors
40    ///
41    /// Returns an error if the API is unavailable or the Swift bridge rejects the request.
42    pub fn biometry_requirement() -> Result<Self> {
43        let raw = bridge_ptr(|out, error_out| unsafe {
44            ffi::la_authentication_requirement::la_authentication_requirement_biometry(
45                out, error_out,
46            )
47        })?;
48        Ok(Self {
49            handle: OwnedHandle::new(
50                raw,
51                ffi::la_authentication_requirement::la_authentication_requirement_release,
52            ),
53        })
54    }
55
56    /// Require biometric authentication with the current enrolled set.
57    ///
58    /// # Errors
59    ///
60    /// Returns an error if the API is unavailable or the Swift bridge rejects the request.
61    pub fn biometry_current_set_requirement() -> Result<Self> {
62        let raw = bridge_ptr(|out, error_out| unsafe {
63            ffi::la_authentication_requirement::la_authentication_requirement_biometry_current_set(
64                out, error_out,
65            )
66        })?;
67        Ok(Self {
68            handle: OwnedHandle::new(
69                raw,
70                ffi::la_authentication_requirement::la_authentication_requirement_release,
71            ),
72        })
73    }
74
75    /// Require biometry with the supplied fallback requirement.
76    ///
77    /// # Errors
78    ///
79    /// Returns an error if the API is unavailable or the Swift bridge rejects the request.
80    pub fn biometry_requirement_with_fallback(
81        fallback: &LABiometryFallbackRequirement,
82    ) -> Result<Self> {
83        let raw = bridge_ptr(|out, error_out| unsafe {
84            ffi::la_authentication_requirement::la_authentication_requirement_biometry_with_fallback(
85                fallback.as_ptr(),
86                out,
87                error_out,
88            )
89        })?;
90        Ok(Self {
91            handle: OwnedHandle::new(
92                raw,
93                ffi::la_authentication_requirement::la_authentication_requirement_release,
94            ),
95        })
96    }
97}
98
99/// Fallback requirements usable with `LAAuthenticationRequirement::biometry_requirement_with_fallback`.
100#[derive(Debug)]
101pub struct LABiometryFallbackRequirement {
102    handle: OwnedHandle,
103}
104
105impl LABiometryFallbackRequirement {
106    pub(crate) const fn as_ptr(&self) -> *mut core::ffi::c_void {
107        self.handle.as_ptr()
108    }
109
110    /// The framework's default fallback requirement.
111    ///
112    /// # Errors
113    ///
114    /// Returns an error if the API is unavailable or the Swift bridge rejects the request.
115    pub fn default_requirement() -> Result<Self> {
116        let raw = bridge_ptr(|out, error_out| unsafe {
117            ffi::la_authentication_requirement::la_biometry_fallback_requirement_default(
118                out, error_out,
119            )
120        })?;
121        Ok(Self {
122            handle: OwnedHandle::new(
123                raw,
124                ffi::la_authentication_requirement::la_biometry_fallback_requirement_release,
125            ),
126        })
127    }
128
129    /// Require the device passcode as the biometry fallback.
130    ///
131    /// # Errors
132    ///
133    /// Returns an error if the API is unavailable or the Swift bridge rejects the request.
134    pub fn device_passcode_requirement() -> Result<Self> {
135        let raw = bridge_ptr(|out, error_out| unsafe {
136            ffi::la_authentication_requirement::la_biometry_fallback_requirement_device_passcode(
137                out, error_out,
138            )
139        })?;
140        Ok(Self {
141            handle: OwnedHandle::new(
142                raw,
143                ffi::la_authentication_requirement::la_biometry_fallback_requirement_release,
144            ),
145        })
146    }
147}