mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-06-06 13:53:54 +02:00
feat(mobile): add biometric plugin (#829)
* chore: update deps, make mobile script paths relative * feat(biometric): setup plugin folder * feat: implement iOS * add api * android * fix plugin name * also check empty info.plist entry * add example * fix android * supress * lint * better explanation * add partners & contributed by * change ext * license headers * update vite * add covector setup * tauri/dox removed * add example * docs --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
if("__TAURI__"in window){var __TAURI_PLUGIN_BIOMETRIC__=function(e){"use strict";async function n(e,n={},r){return window.__TAURI_INTERNALS__.invoke(e,n,r)}var r;return"function"==typeof SuppressedError&&SuppressedError,e.BiometryType=void 0,(r=e.BiometryType||(e.BiometryType={}))[r.None=0]="None",r[r.TouchID=1]="TouchID",r[r.FaceID=2]="FaceID",r[r.Iris=3]="Iris",e.authenticate=async function(e,r){return n("plugin:biometric|authenticate",{reason:e,...r})},e.checkStatus=async function(){return n("plugin:biometric|status")},e}({});Object.defineProperty(window.__TAURI__,"biometric",{value:__TAURI_PLUGIN_BIOMETRIC__})}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::{ser::Serializer, Serialize};
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error(transparent)]
|
||||
Io(#[from] std::io::Error),
|
||||
#[cfg(mobile)]
|
||||
#[error(transparent)]
|
||||
PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
|
||||
}
|
||||
|
||||
impl Serialize for Error {
|
||||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(self.to_string().as_ref())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#![cfg(mobile)]
|
||||
|
||||
use serde::Serialize;
|
||||
use tauri::{
|
||||
plugin::{Builder, PluginHandle, TauriPlugin},
|
||||
Manager, Runtime,
|
||||
};
|
||||
|
||||
pub use models::*;
|
||||
|
||||
mod error;
|
||||
mod models;
|
||||
|
||||
pub use error::{Error, Result};
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
const PLUGIN_IDENTIFIER: &str = "app.tauri.biometric";
|
||||
|
||||
#[cfg(target_os = "ios")]
|
||||
tauri::ios_plugin_binding!(init_plugin_biometric);
|
||||
|
||||
/// Access to the biometric APIs.
|
||||
pub struct Biometric<R: Runtime>(PluginHandle<R>);
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct AuthenticatePayload {
|
||||
reason: String,
|
||||
#[serde(flatten)]
|
||||
options: AuthOptions,
|
||||
}
|
||||
|
||||
impl<R: Runtime> Biometric<R> {
|
||||
pub fn status(&self) -> crate::Result<Status> {
|
||||
self.0.run_mobile_plugin("status", ()).map_err(Into::into)
|
||||
}
|
||||
|
||||
pub fn authenticate(&self, reason: String, options: AuthOptions) -> crate::Result<()> {
|
||||
self.0
|
||||
.run_mobile_plugin("authenticate", AuthenticatePayload { reason, options })
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the biometric APIs.
|
||||
pub trait BiometricExt<R: Runtime> {
|
||||
fn biometric(&self) -> &Biometric<R>;
|
||||
}
|
||||
|
||||
impl<R: Runtime, T: Manager<R>> crate::BiometricExt<R> for T {
|
||||
fn biometric(&self) -> &Biometric<R> {
|
||||
self.state::<Biometric<R>>().inner()
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes the plugin.
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
Builder::new("biometric")
|
||||
.js_init_script(include_str!("api-iife.js").to_string())
|
||||
.setup(|app, api| {
|
||||
#[cfg(target_os = "android")]
|
||||
let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "BiometricPlugin")?;
|
||||
#[cfg(target_os = "ios")]
|
||||
let handle = api.register_ios_plugin(init_plugin_biometric)?;
|
||||
app.manage(Biometric(handle));
|
||||
Ok(())
|
||||
})
|
||||
.build()
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Default, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AuthOptions {
|
||||
pub allow_device_credential: bool,
|
||||
/// iOS only.
|
||||
pub fallback_title: Option<String>,
|
||||
/// iOS only.
|
||||
pub cancel_title: Option<String>,
|
||||
/// Android only.
|
||||
pub title: Option<String>,
|
||||
/// Android only.
|
||||
pub subtitle: Option<String>,
|
||||
/// Android only.
|
||||
pub confirmation_required: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde_repr::Deserialize_repr)]
|
||||
#[repr(u8)]
|
||||
pub enum BiometryType {
|
||||
None = 0,
|
||||
TouchID = 1,
|
||||
FaceID = 2,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Status {
|
||||
pub is_available: bool,
|
||||
pub biometry_type: BiometryType,
|
||||
pub error: Option<String>,
|
||||
pub error_code: Option<String>,
|
||||
}
|
||||
Reference in New Issue
Block a user