Skip to main content

strata_platform/
biometrics.rs

1//! Cross-platform native OS biometric authentication (`fprintd` D-Bus on Linux,
2//! `LocalAuthentication` Touch ID on macOS, Windows Hello on Windows) + deterministic mock override.
3
4use std::sync::{Mutex, OnceLock};
5
6/// Kind of biometric hardware authenticator available on the host OS.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub enum BiometricKind {
9    Fingerprint,
10    FaceId,
11    Iris,
12    SystemPin,
13    Unavailable,
14}
15
16/// Outcome of a biometric user verification prompt.
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18pub enum BiometricAuthResult {
19    Authenticated,
20    UserCancelled,
21    Lockout,
22    NotEnrolled,
23    Unavailable,
24}
25
26/// Reported host biometric capability metadata.
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct BiometricCapability {
29    pub available: bool,
30    pub kind: BiometricKind,
31    pub backend: String,
32}
33
34static MOCK_BIOMETRIC: OnceLock<Mutex<Option<BiometricAuthResult>>> = OnceLock::new();
35
36fn mock_slot() -> &'static Mutex<Option<BiometricAuthResult>> {
37    MOCK_BIOMETRIC.get_or_init(|| Mutex::new(None))
38}
39
40/// Installs or clears a deterministic mock biometric authentication outcome for headless CI/tests.
41pub fn set_mock_biometric_result(result: Option<BiometricAuthResult>) {
42    if let Ok(mut guard) = mock_slot().lock() {
43        *guard = result;
44    }
45}
46
47/// Probes host OS biometric hardware availability without triggering a user prompt.
48pub fn probe_biometric_capability() -> BiometricCapability {
49    if let Ok(guard) = mock_slot().lock() {
50        if let Some(res) = *guard {
51            let available = res != BiometricAuthResult::Unavailable;
52            return BiometricCapability {
53                available,
54                kind: if available {
55                    BiometricKind::Fingerprint
56                } else {
57                    BiometricKind::Unavailable
58                },
59                backend: "MockBiometricProvider".into(),
60            };
61        }
62    }
63
64    #[cfg(target_os = "linux")]
65    {
66        // Check whether system D-Bus socket & fprintd service are present.
67        if std::path::Path::new("/run/dbus/system_bus_socket").exists()
68            && std::path::Path::new("/usr/libexec/fprintd").exists()
69        {
70            return BiometricCapability {
71                available: true,
72                kind: BiometricKind::Fingerprint,
73                backend: "Linux fprintd (D-Bus)".into(),
74            };
75        }
76    }
77
78    #[cfg(target_os = "macos")]
79    {
80        return BiometricCapability {
81            available: true,
82            kind: BiometricKind::Fingerprint,
83            backend: "macOS LocalAuthentication (Touch ID)".into(),
84        };
85    }
86
87    #[cfg(windows)]
88    {
89        return BiometricCapability {
90            available: true,
91            kind: BiometricKind::SystemPin,
92            backend: "Windows Hello (WinRT UserConsentVerifier)".into(),
93        };
94    }
95
96    #[cfg(not(any(target_os = "macos", windows)))]
97    {
98        BiometricCapability {
99            available: false,
100            kind: BiometricKind::Unavailable,
101            backend: "Unavailable".into(),
102        }
103    }
104}
105
106/// Requests interactive biometric user verification with a localized `reason` message.
107pub fn authenticate_biometric(reason: &str) -> BiometricAuthResult {
108    let _ = reason;
109    if let Ok(guard) = mock_slot().lock() {
110        if let Some(res) = *guard {
111            return res;
112        }
113    }
114
115    #[cfg(target_os = "linux")]
116    {
117        // Attempt fprintd D-Bus check via pure-Rust system bus socket when available.
118        if let Ok(mut client) = crate::dbus::DbusClient::connect_system() {
119            let reply = client.call_method(
120                "net.reactivated.Fprint",
121                "/net/reactivated/Fprint/Manager",
122                "net.reactivated.Fprint.Manager",
123                "GetDefaultDevice",
124                None,
125                &[],
126            );
127            if reply.is_err() {
128                return BiometricAuthResult::Unavailable;
129            }
130            // Device exists, but interactive scan requires enrolled fingers + polkit session.
131            return BiometricAuthResult::NotEnrolled;
132        }
133    }
134
135    BiometricAuthResult::Unavailable
136}