sos_platform_authenticator/
local_auth.rs

1//! Interface to the platform authenticator.
2
3#[cfg(not(target_os = "linux"))]
4mod supported {
5    use robius_authentication::{
6        AndroidText, BiometricStrength, Context, Policy, PolicyBuilder, Text,
7        WindowsText,
8    };
9
10    /// Options for platform authentication.
11    pub struct AuthenticateOptions {
12        /// Biometrics strength.
13        pub biometrics: BiometricStrength,
14        /// Password fallback.
15        pub password: bool,
16        /// Text for android.
17        pub android: AndroidText<'static, 'static, 'static>,
18        /// Text for apple.
19        pub apple: &'static str,
20        /// Text for windows.
21        pub windows: WindowsText<'static, 'static>,
22    }
23
24    impl Default for AuthenticateOptions {
25        fn default() -> Self {
26            Self {
27                biometrics: BiometricStrength::Strong,
28                password: true,
29                android: AndroidText {
30                    title: "Authenticate",
31                    subtitle: None,
32                    description: None,
33                },
34                apple: "authenticate",
35                windows: WindowsText::new(
36                    "Save Our Secrets",
37                    "Verify your identity to authenticate",
38                )
39                .unwrap(),
40            }
41        }
42    }
43
44    /// Authenticate using the platform authenticator.
45    pub fn authenticate(options: AuthenticateOptions) -> bool {
46        let policy: Policy = PolicyBuilder::new()
47            .biometrics(Some(options.biometrics))
48            .password(options.password)
49            .watch(true)
50            .build()
51            .unwrap();
52
53        let text: Text = Text {
54            android: options.android,
55            apple: options.apple,
56            windows: options.windows,
57        };
58
59        let context = Context::new(());
60        context.blocking_authenticate(text, &policy).is_ok()
61    }
62
63    /// Determine if local platform authentication is supported.
64    pub fn supported() -> bool {
65        true
66    }
67}
68#[cfg(not(target_os = "linux"))]
69pub use supported::*;
70
71#[cfg(target_os = "linux")]
72mod unsupported {
73    /// Options for platform authentication.
74    #[derive(Default)]
75    pub struct AuthenticateOptions {}
76
77    /// Authenticate using the platform authenticator.
78    pub fn authenticate(_options: AuthenticateOptions) -> bool {
79        false
80    }
81
82    /// Determine if local platform authentication is supported.
83    pub fn supported() -> bool {
84        false
85    }
86}
87
88#[cfg(target_os = "linux")]
89pub use unsupported::*;