MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

EddyVerbruggen/cordova-plugin-touch-id 分析

项目背景与痛点

随着移动设备的普及,无需密码的身份验证方式变得越来越受欢迎,特别是利用生物识别技术实现的安全快捷的方式。cordova-plugin-touch-id 这个项目就提供了一种通过指纹识别进行用户身份验证的方案,让应用开发者可以通过这一插件简化与各种生物识别技术的集成过程。

指纹识别的优势是不言而喻的,更快捷安全的身份验证方式长期以来都被应用在高端智能手机上。然而,如何在Cordova应用中方便地利用这种技术,就成为了项目cordova-plugin-touch-id的主要诉求。它通过对接插件的形式,实现了统一的API,让开发这能够不费吹灰之力地在自己的混合应用程序中引入生物识别技术。

技术核心揭秘

cordova-plugin-touch-id 通过实现对不同操作系统API的桥接来提供统一的生物识别支持。对Android和iOS平台的部分涉及到它们内部的生物身份验证核心。

让我们绘制一幅简单的架构图来帮助理解:

graph TD A[Cordova 应用] --> B[touch-id 插件] B --> C{"Bio识别功能"} C -->|"iOS"| D[LocalAuthentication Framework] C -->|"Android"| E[Fingerprint API] D --> F[用户身份校验] E --> F

可以看到,应用调用插件,插件通过判断设备的操作系统,分别调用相应的系统级SDK,如 iOS 的 LocalAuthentication Framework 和 Android 的 Fingerprint API。这种方式有效封装了底层的细节,开发者不需要对每个平台的安全库有深入了解,便能实现跨平台的指纹验证功能。

功能亮点与差异

相较于同类插件,cordova-plugin-touch-id 的亮点在于其美观且一致的用户界面。插件在设计时已经充分考虑了用户体验,提供了一系列配置选项,可以自定义生物识别过程中的提示文本、用户取消操作后的反馈等。这种方式不仅提高了插件的灵活性,也使得应用能够在向用户提供一致的UI的同时,更好地融入应用的整体设计风格。

此外,通过简单的API调用,插件不仅能够实现基本的指纹识别,还提供了生物识别支持的可检测性。这意味着应用可以在使用的环境中检测支持哪种类型的生物验证。

应用场景与落地建议

在生产环境中使用 cordova-plugin-touch-id,开发者需要注意几点关键条件:

  • 首先,确保用户使用的是支持指纹识别功能的设备,并且该设备已经设置过指纹。
  • 应用所需的权限应该被申请并授权,这些权限将在应用加载时被检测到缺省的用户许可策略中。
  • 应用的总体行为在缺省情况下应当更加注重回复性。也就是说,当识别用户是错误的(如,用户未正确地运用自己的指纹时),应用首选的路线通常是以安全的方式处理并应对用户重试。

此项目非常适合金融、银行、健康保陥等涉及敏感个人数据的移动应用,通过防止密码与临时性的攻击,它增强了数据的安全性。

综合评价

综合来看,cordova-plugin-touch-id 是一个非常实用的插件,对于基于 Cordova 开发的应用来说,它提供了易用的生物识别功能。不过,它也有一些限制,比如需要用户使用支持生物识别的设备。

优点:

  • 跨平台:能够同时支持 iOS 和 Android 等多种平台。
  • 用户友好:提供简明直观的配置选项以及高度一致的用户体验。
  • 安全高效:快速且安全的身份验证。

缺点:

  • 生物识别技术的普及尚有局限,用户设备未必普遍支持指纹/面部识别。(依赖硬件)
  • 需开发者本身具备相关技术的了解,以便合理利用或处理生物识别功能的限制。

关于

:nail_care: 👱‍♂️ Forget passwords, use a fingerprint scanner!

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
342
价值分
143
活跃状态
活跃

语言占比

JavaScript
Objective-C

Release

README

Cordova TouchID Plugin

Also works with Face ID on iPhone X 🚀

Index

  1. Description
  2. Screenshot
  3. Installation
  4. Usage
  5. Security++
  6. Face ID support

Description

Scan the fingerprint of your user with the TouchID sensor (iPhone 5S).

  • Compatible with Cordova Plugman.
  • Minimum iOS version is 8 (error callbacks will be gracefully invoked on lower versions).
  • Requires a fingerprint scanner, so an iPhone 5S or newer is required.

Screenshot

Distorted a bit because I created it back when Apple had not yet released the SDK and they’re not a fan of developers posting screenshots of unreleased features.

ScreenShot

Installation

Automatically (CLI / Plugman)

Compatible with Cordova Plugman, compatible with PhoneGap 3.0 CLI, here’s how it works with the CLI (backup your project first!):

From npm:

$ cordova plugin add cordova-plugin-touch-id
$ cordova prepare

The latest, from the master repo:

$ cordova plugin add https://github.com/EddyVerbruggen/cordova-plugin-touch-id
$ cordova prepare

TouchID.js is brought in automatically. There is no need to change or add anything in your html.

Manually

1. Add the following xml to your config.xml in the root directory of your www folder:

<feature name="TouchID">
  <param name="ios-package" value="TouchID" />
</feature>

You’ll need to add the LocalAuthentication.framework and Security.framework to your project. Click your project, Build Phases, Link Binary With Libraries, search for and add the frameworks.

2. Grab a copy of TouchID.js, add it to your project and reference it in index.html:

<script type="text/javascript" src="js/TouchID.js"></script>

3. Download the source files and copy them to your project.

iOS: Copy the two .h and two .m files to platforms/ios/<ProjectName>/Plugins

Usage

First you’ll want to check whether or not the user has a configured fingerprint scanner. You can use this to show a ‘log in with your fingerprint’ button next to a username/password login form.

window.plugins.touchid.isAvailable(
  function(type) {alert(type)}, // type returned to success callback: 'face' on iPhone X, 'touch' on other devices
  function(msg) {alert('not available, message: ' + msg)} // error handler: no TouchID available
);

If the onSuccess handler was called, you can scan the fingerprint. There are two options: verifyFingerprint and verifyFingerprintWithCustomPasswordFallback. The first method will offer a fallback option called ‘enter passcode’ which shows the default passcode UI when pressed. The second method will offer a fallback option called ‘enter password’ (not passcode) which allows you to provide your own password dialog.

window.plugins.touchid.verifyFingerprint(
  'Scan your fingerprint please', // this will be shown in the native scanner popup
   function(msg) {alert('ok: ' + msg)}, // success handler: fingerprint accepted
   function(msg) {alert('not ok: ' + JSON.stringify(msg))} // error handler with errorcode and localised reason
);

The errorhandler of the method above can receive an error code of -2 which means the user pressed the ‘enter password’ fallback.

window.plugins.touchid.verifyFingerprintWithCustomPasswordFallback(
  'Scan your fingerprint please', // this will be shown in the native scanner popup
   function(msg) {alert('ok: ' + msg)}, // success handler: fingerprint accepted
   function(msg) {alert('not ok: ' + JSON.stringify(msg))} // error handler with errorcode and localised reason
);

This will render a button labelled ‘Enter password’ in case the fingerprint is not recognized. If you want to provide your own label (‘Enter PIN’ perhaps), you can use awkwardly named function (added in version 3.1.0):

window.plugins.touchid.verifyFingerprintWithCustomPasswordFallbackAndEnterPasswordLabel(
  'Scan your fingerprint please', // this will be shown in the native scanner popup
  'Enter PIN', // this will become the 'Enter password' button label
   function(msg) {alert('ok: ' + msg)}, // success handler: fingerprint accepted
   function(msg) {alert('not ok: ' + JSON.stringify(msg))} // error handler with errorcode and localised reason
);

You can copy-paste these lines of code for a quick test:

<button onclick="window.plugins.touchid.isAvailable(function(msg) {alert('ok: ' + msg)}, function(msg) {alert('not ok: ' + msg)})">Touch ID available?</button>
<button onclick="window.plugins.touchid.verifyFingerprint('Scan your fingerprint please', function(msg) {alert('ok: ' + msg)}, function(msg) {alert('not ok: ' + JSON.stringify(msg))})">Scan fingerprint</button>

“Biometry is locked out” (code: -8)

window.plugins.touchid.askPassword(
   'Enter password', // this will become the 'Enter password' label
   function(msg) {alert('ok: ' + msg)}, // success handler: fingerprint reactivated
   function(msg) {alert('not ok: ' + JSON.stringify(msg))} // error reason as string
);

Security++

Since iOS9 it’s possible to check whether or not the list of enrolled fingerprints changed since the last time you checked it. It’s recommended you add this check so you can counter hacker attacks to your app. See this article for more details.

So instead of checking the fingerprint after isAvailable add another check. In case didFingerprintDatabaseChange returns true you probably want to re-authenticate your user before accepting valid fingerprints again.

window.plugins.touchid.isAvailable(
    // success handler; available
    function() {
      window.plugins.touchid.didFingerprintDatabaseChange(
          function(changed) {
            if (changed) {
              // re-auth the user by asking for his credentials before allowing a fingerprint scan again
            } else {
              // call the fingerprint scanner
            }
          }
      );
    },
    // error handler; not available
    function(msg) {
      // use a more traditional auth mechanism
    }
);

Face ID Support

Since iOS 11, LocalAuthentication also supports Face ID for biometrics. This is a drop-in replacement for Touch ID and any existing apps using Touch ID will work identically on devices that use Face ID.

Since plugin version 3.3.0 the success callback of isAvailable receives the type of biometric ID, which is either touch or face.

You can use this to display “Face ID” or “Touch ID” as appropriate in your app.

window.plugins.touchid.isAvailable(
  function(type) {alert(type)}, // type returned to success callback: 'face' on iPhone X, 'touch' on other devices
  function(msg) {alert('not available, message: ' + msg)} // error handler: no TouchID available
);

If you want to alter the usage description in the consent popup, then override the default empty adds an empty NSFaceIDUsageDescription. To do so, pass the following variable when installing the plugin:

cordova plugin add cordova-plugin-touch-id --variable FACEID_USAGE_DESCRIPTION="For easy authentication"

评论

暂无评论