Skip to main content

localauthentication/
la_policy.rs

1//! `LAPolicy` values supported by `LAContext`.
2
3use crate::ffi;
4
5/// Authentication policies supported on macOS by `LocalAuthentication.framework`.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[non_exhaustive]
8pub enum LAPolicy {
9    /// Authenticate the device owner using biometry only.
10    DeviceOwnerAuthenticationWithBiometrics,
11    /// Authenticate the device owner using biometry or the local password.
12    DeviceOwnerAuthentication,
13    /// Authenticate the device owner using a nearby companion device.
14    DeviceOwnerAuthenticationWithCompanion,
15    /// Authenticate the device owner using biometry or a nearby companion device.
16    DeviceOwnerAuthenticationWithBiometricsOrCompanion,
17    /// Deprecated alias for companion-device authentication.
18    #[deprecated(note = "Use `DeviceOwnerAuthenticationWithCompanion` instead.")]
19    DeviceOwnerAuthenticationWithWatch,
20    /// Deprecated alias for biometry-or-companion authentication.
21    #[deprecated(note = "Use `DeviceOwnerAuthenticationWithBiometricsOrCompanion` instead.")]
22    DeviceOwnerAuthenticationWithBiometricsOrWatch,
23}
24
25/// Backward-compatible alias for the v0.1.x enum name.
26pub type Policy = LAPolicy;
27
28impl LAPolicy {
29    #[allow(deprecated)]
30    #[must_use]
31    pub const fn raw_value(self) -> i32 {
32        match self {
33            Self::DeviceOwnerAuthenticationWithBiometrics => {
34                ffi::la_policy::DEVICE_OWNER_AUTHENTICATION_WITH_BIOMETRICS
35            }
36            Self::DeviceOwnerAuthentication => ffi::la_policy::DEVICE_OWNER_AUTHENTICATION,
37            Self::DeviceOwnerAuthenticationWithCompanion => {
38                ffi::la_policy::DEVICE_OWNER_AUTHENTICATION_WITH_COMPANION
39            }
40            Self::DeviceOwnerAuthenticationWithBiometricsOrCompanion => {
41                ffi::la_policy::DEVICE_OWNER_AUTHENTICATION_WITH_BIOMETRICS_OR_COMPANION
42            }
43            Self::DeviceOwnerAuthenticationWithWatch => {
44                ffi::la_policy::DEVICE_OWNER_AUTHENTICATION_WITH_WATCH
45            }
46            Self::DeviceOwnerAuthenticationWithBiometricsOrWatch => {
47                ffi::la_policy::DEVICE_OWNER_AUTHENTICATION_WITH_BIOMETRICS_OR_WATCH
48            }
49        }
50    }
51
52    pub(crate) const fn as_ffi(self) -> i32 {
53        self.raw_value()
54    }
55
56    /// A short, human-readable name for the policy.
57    #[allow(deprecated)]
58    #[must_use]
59    pub const fn description(self) -> &'static str {
60        match self {
61            Self::DeviceOwnerAuthenticationWithBiometrics => {
62                "device owner authentication with biometrics"
63            }
64            Self::DeviceOwnerAuthentication => "device owner authentication",
65            Self::DeviceOwnerAuthenticationWithCompanion => {
66                "device owner authentication with companion"
67            }
68            Self::DeviceOwnerAuthenticationWithBiometricsOrCompanion => {
69                "device owner authentication with biometrics or companion"
70            }
71            Self::DeviceOwnerAuthenticationWithWatch => "device owner authentication with watch",
72            Self::DeviceOwnerAuthenticationWithBiometricsOrWatch => {
73                "device owner authentication with biometrics or watch"
74            }
75        }
76    }
77}