strata-ui-platform 0.1.5

Native Hardware Abstraction Layer for Android, iOS, macOS, Windows, Linux, and Web
Documentation
//! Cross-platform native OS biometric authentication (`fprintd` D-Bus on Linux,
//! `LocalAuthentication` Touch ID on macOS, Windows Hello on Windows) + deterministic mock override.

use std::sync::{Mutex, OnceLock};

/// Kind of biometric hardware authenticator available on the host OS.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BiometricKind {
    Fingerprint,
    FaceId,
    Iris,
    SystemPin,
    Unavailable,
}

/// Outcome of a biometric user verification prompt.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BiometricAuthResult {
    Authenticated,
    UserCancelled,
    Lockout,
    NotEnrolled,
    Unavailable,
}

/// Reported host biometric capability metadata.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BiometricCapability {
    pub available: bool,
    pub kind: BiometricKind,
    pub backend: String,
}

static MOCK_BIOMETRIC: OnceLock<Mutex<Option<BiometricAuthResult>>> = OnceLock::new();

fn mock_slot() -> &'static Mutex<Option<BiometricAuthResult>> {
    MOCK_BIOMETRIC.get_or_init(|| Mutex::new(None))
}

/// Installs or clears a deterministic mock biometric authentication outcome for headless CI/tests.
pub fn set_mock_biometric_result(result: Option<BiometricAuthResult>) {
    if let Ok(mut guard) = mock_slot().lock() {
        *guard = result;
    }
}

/// Probes host OS biometric hardware availability without triggering a user prompt.
pub fn probe_biometric_capability() -> BiometricCapability {
    if let Ok(guard) = mock_slot().lock() {
        if let Some(res) = *guard {
            let available = res != BiometricAuthResult::Unavailable;
            return BiometricCapability {
                available,
                kind: if available {
                    BiometricKind::Fingerprint
                } else {
                    BiometricKind::Unavailable
                },
                backend: "MockBiometricProvider".into(),
            };
        }
    }

    #[cfg(target_os = "linux")]
    {
        // Check whether system D-Bus socket & fprintd service are present.
        if std::path::Path::new("/run/dbus/system_bus_socket").exists()
            && std::path::Path::new("/usr/libexec/fprintd").exists()
        {
            return BiometricCapability {
                available: true,
                kind: BiometricKind::Fingerprint,
                backend: "Linux fprintd (D-Bus)".into(),
            };
        }
    }

    #[cfg(target_os = "macos")]
    {
        return BiometricCapability {
            available: true,
            kind: BiometricKind::Fingerprint,
            backend: "macOS LocalAuthentication (Touch ID)".into(),
        };
    }

    #[cfg(windows)]
    {
        return BiometricCapability {
            available: true,
            kind: BiometricKind::SystemPin,
            backend: "Windows Hello (WinRT UserConsentVerifier)".into(),
        };
    }

    #[cfg(not(any(target_os = "macos", windows)))]
    {
        BiometricCapability {
            available: false,
            kind: BiometricKind::Unavailable,
            backend: "Unavailable".into(),
        }
    }
}

/// Requests interactive biometric user verification with a localized `reason` message.
pub fn authenticate_biometric(reason: &str) -> BiometricAuthResult {
    let _ = reason;
    if let Ok(guard) = mock_slot().lock() {
        if let Some(res) = *guard {
            return res;
        }
    }

    #[cfg(target_os = "linux")]
    {
        // Attempt fprintd D-Bus check via pure-Rust system bus socket when available.
        if let Ok(mut client) = crate::dbus::DbusClient::connect_system() {
            let reply = client.call_method(
                "net.reactivated.Fprint",
                "/net/reactivated/Fprint/Manager",
                "net.reactivated.Fprint.Manager",
                "GetDefaultDevice",
                None,
                &[],
            );
            if reply.is_err() {
                return BiometricAuthResult::Unavailable;
            }
            // Device exists, but interactive scan requires enrolled fingers + polkit session.
            return BiometricAuthResult::NotEnrolled;
        }
    }

    BiometricAuthResult::Unavailable
}