Skip to main content

Crate robius_authentication

Crate robius_authentication 

Source
Expand description

Abstractions for multi-platform native authentication.

This crate supports:

  • Apple: TouchID, FaceID, and regular username/password on macOS and iOS.
  • Android: See below for additional steps.
    • Requires the USE_BIOMETRIC permission in your app’s manifest.
    • Requires API level 28 (Android 9) for biometrics, or API level 29 (Android 10) if you use the password / device-credential fallback.
  • Windows: Windows Hello (face recognition, fingerprint, PIN) via WinRT, plus a Win32 credential-UI fallback for username/password.
  • Linux: polkit-based authentication using the desktop environment’s prompt.
    • Note: Linux support is currently incomplete.

§Example

use robius_authentication::{
    AndroidText, BiometricStrength, Context, Policy, PolicyBuilder, Text, WindowsText,
};

let policy: Policy = PolicyBuilder::new()
    .biometrics(Some(BiometricStrength::Strong))
    .password(true)
    .companion(true)
    // Required on Linux (polkit action IDs); a no-op on other platforms.
    .action_ids(["org.robius.authentication"])
    .build()
    .unwrap();

let text = Text {
    android: AndroidText {
        title: "Title",
        subtitle: None,
        description: None,
    },
    apple: "authenticate",
    windows: WindowsText::new_truncated("Title", "Description"),
};

let callback = |auth_result| {
    match auth_result {
        Ok(_)  => println!("Authentication success!"),
        Err(_) => eprintln!("Authentication failed!"),
    }
};

Context::new(())
    .authenticate(text, &policy, callback)
    .expect("failed to display the authentication prompt");

The Text struct can also be constructed at compile-time to avoid run-time unwraps:

use robius_authentication::{AndroidText, Text, WindowsText};

const TEXT: Text = Text {
    android: AndroidText {
        title: "Title",
        subtitle: None,
        description: None,
    },
    apple: "authenticate",
    windows: match WindowsText::new("Title", "Description") {
        Some(text) => text,
        None => panic!("Windows text too long"),
    },
};

For more details about the prompt text see the Text struct which allows you to customize the prompt for each platform.

§Usage on Android

For authentication to work, the following must be added to your app’s AndroidManifest.xml:

<uses-permission android:name="android.permission.USE_BIOMETRIC" />

§Minimum API level

This crate uses android.hardware.biometrics.BiometricPrompt, so the minimum supported API level is 28 (Android 9). The password / device-credential fallback needs API level 29 (Android 10); requesting a credential-only policy on a lower level returns Error::Unavailable. Set minSdk accordingly in your app.

Structs§

AndroidText
The text of the authentication prompt on Android.
Context
Holds platform-specific contextual state required to display an authentication prompt.
Policy
An authentication policy.
PolicyBuilder
A builder for conveniently defining a policy.
Text
The text contents displayed by an authentication prompt.
WindowsText
The text of the authentication prompt on Windows, including a title (“caption”) and description (“message”).

Enums§

BiometricStrength
A biometric strength class.
Error
An error produced during authentication.

Type Aliases§

RawContext
A “raw” context that can be used to create a Context.
Result
The result of an authentication operation.