iT邦幫忙

2025 iThome 鐵人賽

DAY 25
0
Rust

用 Tauri 打造你的應用程式系列 第 25

[Day 25] Plugin (七):Biometric

  • 分享至 

  • xImage
  •  

在現代行動應用開發中,安全性是至關重要的考量。使用者的隱私資料需要得到妥善保護,而生物辨識技術提供了一種既安全又便利的身分驗證方式。今天我們將探討如何在 Tauri Android 應用中整合生物辨識功能。

生物辨識的重要性

傳統的密碼驗證方式存在許多缺點:容易被遺忘、可能被盜取、輸入繁瑣等。相較之下,生物辨識具有以下優勢:

  • 唯一性:每個人的生物特徵都是獨一無二的
  • 便利性:無需記憶複雜密碼,一觸即可驗證
  • 安全性:生物特徵難以偽造或竊取
  • 使用者體驗:提供流暢的認證流程

如何安裝 tauri-plugin-biometric

一如既往,只要簡單的一個指令就可以安裝了:

npm run tauri add biometric

檢查是否支援生物辨識

在 Vue 前端中,我們可以透過插件提供的 JavaScript API 來檢查是否支援生物辨識流程:

import { checkStatus } from '@tauri-apps/plugin-biometric';

const status = await checkStatus();
if (status.isAvailable) {
  console.log('Yes! Biometric Authentication is available');
} else {
  console.log(
    'No! Biometric Authentication is not available due to ' + status.error
  );
}

也可以在 Rust:

use tauri_plugin_biometric::BiometricExt;

fn check_biometric(app_handle: tauri::AppHandle) {
    let status = app_handle.biometric().status().unwrap();
    if status.is_available {
        println!("Yes! Biometric Authentication is available");
    } else {
        println!("No! Biometric Authentication is not available due to: {}", status.error.unwrap());
    }
}

開始生物辨識驗證

如果有支援,就可以開始驗證:

import { authenticate } from '@tauri-apps/plugin-biometric';

const options = {
  // Set true if you want the user to be able to authenticate using phone password
  allowDeviceCredential: false,
  cancelTitle: "Feature won't work if Canceled",

  // iOS only feature
  fallbackTitle: 'Sorry, authentication failed',

  // Android only features
  title: 'Tauri feature',
  subtitle: 'Authenticate to access the locked Tauri function',
  confirmationRequired: true,
};

try {
  await authenticate('This feature is locked', options);
  console.log(
    'Hooray! Successfully Authenticated! We can now perform the locked Tauri function!'
  );
} catch (err) {
  console.log('Oh no! Authentication failed because ' + err.message);
}
use tauri_plugin_biometric::{BiometricExt, AuthOptions};

fn bio_auth(app_handle: tauri::AppHandle) {

    let options = AuthOptions {
        // Set True if you want the user to be able to authenticate using phone password
        allow_device_credential:false,
        cancel_title: Some("Feature won't work if Canceled".to_string()),

        // iOS only feature
        fallback_title: Some("Sorry, authentication failed".to_string()),

        // Android only features
        title: Some("Tauri feature".to_string()),
        subtitle: Some("Authenticate to access the locked Tauri function".to_string()),
        confirmation_required: Some(true),
    };

    // if the authentication was successful, the function returns Result::Ok()
    // otherwise returns Result::Error()
    match app_handle.biometric().authenticate("This feature is locked".to_string(), options) {
        Ok(_) => {
            println!("Hooray! Successfully Authenticated! We can now perform the locked Tauri function!");
        }
        Err(e) => {
            println!("Oh no! Authentication failed because : {e}");
        }
    }
}

實際應用場景

生物辨識技術適用於多種應用場景:

  • 隱私保護:在筆記或日記應用中,使用生物辨識來保護敏感內容,確保只有本人能夠查看。
  • 金融安全:在涉及金錢交易的應用中,生物辨識可以作為額外的安全層級,防止未授權存取。
  • 企業應用:在企業內部應用中,生物辨識可以實現單一登入(SSO),提升工作效率。

使用者體驗考量

在整合生物辨識功能時,需要特別注意使用者體驗:

首先,應提供清晰的提示訊息,讓使用者了解為何需要進行生物辨識。其次,必須提供替代驗證方式,因為某些情況下生物辨識可能無法使用(如手指受傷、光線不足等)。

最後,應尊重使用者的選擇權,允許他們決定是否啟用生物辨識功能。

小結

透過 tauri-plugin-biometric,我們可以輕鬆在 Tauri Android 應用中實現專業級的生物辨識功能。這不僅提升了應用的安全性,也大幅改善了使用者體驗。


上一篇
[Day 24] Plugin (六):Deep Link
下一篇
[Day 26] Plugin (八):Updater
系列文
用 Tauri 打造你的應用程式26
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言