MIT License

技术解读

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

深度技术分析:BiometricAuthentication 项目解析

项目背景与痛点

在移动应用中,安全性与易用性一直是开发人员关注的核心问题。“rushisangani/BiometricAuthentication” 项目提供了一种基于 Apple 的 Face ID 和 Touch ID 的生物识别认证机制,用以提升应用的安全性与用户使用的便捷性。通过集成这一库,开发者能够在无需复杂二次开发的情况下,为应用引入生物识别认证功能。这不仅解决了用户数据保护的问题,还使得用户无需频繁输入密码,提高了应用的用户体验。

核心技术揭秘

架构与实现

BiometricAuthentication 利用 Apple 系统提供的生物识别认证 API 实现对 Face ID 和 Touch ID 的调用。核心代码遵循简单且直接的设计原则,封装了与生物识别认证相关的全部逻辑,使得开发者只需调用库中的方法即可完成认证流程。

具体实现过程如下:

  1. 用户发起身份认证请求。
  2. 库封装并处理 UI 请求,向用户展示认证界面。
  3. 库检查设备是否支持指定的生物识别方式。
  4. 根据用户的生物识别信息,执行相应的认证逻辑。

在此过程中,项目采用了 依赖注入 和 责任链模式,使得不同模块之间的耦合度保持在较低水平,从而提高了扩展性。同时,为了保证安全性和稳定性,库中内置了针对错误处理和用户反馈机制。

技术架构图
graph TD A[用户请求认证] --> B[检查设备支持] B -->|"支持认证"| C[展示识别UI] B -->|"不支持"| D[返回错误信息] C --> E{"识别成功?"} E -->|"成功"| F[认证通过] E -->|"失败"| G[返回错误]

功能亮点与差异

相较于同类项目,BiometricAuthentication 的亮点在于其专注于精简和高效。API 接口设计简洁,使用文档详细明了,方便开发者快速上手。同时,项目针对常见问题进行了优化,确保认证过程的可靠性和反馈的及时性。此外,该项目通过封装设备检查与认证流程,提供了开发人员和最终用户的无缝体验。

场景与部署建议

在实际生产环境中使用 BiometricAuthentication 时,有几个方面需要特别注意:

  • 设备兼容性:确保目标设备支持 Face ID 或 Touch ID 身份认证。
  • 用户体验:应考虑在某些场景下,可能需要让用户选择替代认证方式。
  • 安全性:保留旧的身份验证逻辑,在生物识别技术无法使用时提供回退选项,以防止认证失败。

适合应用于金融、医疗等对用户验证要求较高的应用场景,能够有效增强应用的安全措施并提高用户体验。

综合评价

BiometricAuthentication 基于现有的 Apple 生物识别认证 API 构建,提供了一个高效且易于集成的解决方案。项目文档详实,涵盖了必要的配置和使用步骤,并在实际中表现出色。尽管它主要面向目标明确(集成 Face ID/Touch ID 验证),但确实提升了应用的安全性,对于开发者来说是一个不错的选择。

优点

  • 直接调用系统级 API 提供高效的生物识别认证功能。
  • 易于集成,仅需简短代码即可完成认证配置。
  • 简洁明了的文档说明利于开发人员快速上手。

缺点

  • 仅支持 Apple 生态系统内的生物识别认证,限制了平台多样性。
  • 可能因设备兼容性问题导致部分用户无法使用。

总的来说,BiometricAuthentication 为移动应用开发带来了一个便捷而安全的生物识别解决方案,适合应用于对用户认证安全性有较高要求的应用中。

关于

GitHub 原始描述(英文)

Use Apple FaceID or TouchID authentication in your app using BiometricAuthentication.

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

语言占比

Objective-C
Ruby
Swift

README

BiometricAuthentication

Use Apple FaceID or TouchID authentication in your app using BiometricAuthentication. It’s very simple and easy to use that handles Touch ID and Face ID authentication based on the device.

Note: - Face ID authentication requires user’s persmission to be add in info.plist.

<key>NSFaceIDUsageDescription</key>
<string>This app requires Face ID permission to authenticate using Face recognition.</string>

What’s new in version 3.1

  • Updated to Swift 5.0
  • Implemented Result type as completion callback

Version 2.2

  • Set AllowableReuseDuration (in seconds) to auto authenticate when user has just unlocked the device with biometric.
  • This is pretty useful when app comes to foreground or device is just unlocked by the user and you want to authenticate with biometrics.
  • If you don’t want to reuse the recently used authentication then simply skip this step.

// set this before calling authenticateWithBioMetrics method (optional)
BioMetricAuthenticator.shared.allowableReuseDuration = 60

Version 2.1

  • Check if TouchID or FaceID authentication is available for iOS device.

Alt text Alt text Alt text

Features

  • Works with Apple Face ID (iPhone X, Xs, XR, XsMax) and other Touch ID having devices.
  • Predefined error handling when recognition fails.
  • Automatic authentication with device passcode on multiple failed attempts.

Requirements

  • iOS 12.0+
  • Xcode 10+
  • Swift 3.0+

Installation

CocoaPods

pod 'BiometricAuthentication'

Carthage

github "rushisangani/BiometricAuthentication"

Usage

Authenticate with biometric

BioMetricAuthenticator.authenticateWithBioMetrics(reason: "") { (result) in

    switch result {
    case .success( _):
        print("Authentication Successful")
    case .failure(let error):
        print("Authentication Failed")
    }
}
  • When reason specified as empty - default will be used based on the device. Ex. for iPhone X - “Confirm your face to authenticate.”, For other devices - “Confirm your fingerprint to authenticate.”

Can Authenticate with biometric

  • Alternatively you can check before authentication by following. This will check that if device supports Touch ID or Face ID authentication and your app can use that as of now.
if BioMetricAuthenticator.canAuthenticate() {

    BioMetricAuthenticator.authenticateWithBioMetrics(reason: "") { (result) in
        // check result -> success or failure
    }
}

Check for Face ID

  • Check if device supports face recognition or not.
if BioMetricAuthenticator.shared.faceIDAvailable() {
    // device supports face id recognition.
}

Check for Touch ID

  • Check if device supports touch id authentication or not.
if BioMetricAuthenticator.shared.touchIDAvailable() {
    // device supports touch id authentication
}

Fallback Reason

  • Fallback reason title will be shown when first authentication attempt is failed.
  • You can give alternate authentication options like enter ‘username & password’ when user clicks on fallback button.
  • Default reason is empty, which will hide fallback button.
BioMetricAuthenticator.authenticateWithBioMetrics(reason: "Biometric Authentication", fallbackTitle: "Enter Credentials") { (result) in

    switch result {
    case .success( _):
        // proceed further

    case .failure(let error):

        switch error {
        case .fallback:

            print("Authentication Failed")

            // show alternatives on fallback button clicked
            // for ex. - enter username/email and password

        default:
            break
        }
    }
}

BiometryLockedout

  • When biometry authentication is locked out after multiple failed attempts. You can unlock it by passcode authentication.
  • Provide your own passcode authentication reason here, default will be used if not provided.
BioMetricAuthenticator.authenticateWithPasscode(reason: message) { (result) in
    switch result {
    case .success( _):
        // passcode authentication success
    case .failure(let error):
        print(error.message())
    }
}

Error Handling

  • There are various cases when biometric authentication can be failed.
  1. fallback
    • Called when user clicks on provided fallback button.
  2. biometryNotEnrolled
    • Called when no fingerprints or face is registered with the device.
    • You can show message to register a new face or fingerprint here.
    • Default message will be shown if not provided.
  3. canceledByUser
    • Called when authentication canceled by user.
  4. canceledBySystem
    • Called when authentication canceled by system when app goes into background or any other reason.
  5. passcodeNotSet
    • Called when device passcode is not set and trying to use biometry authentication.
    • We can ask user to set device passcode here by opening Settings Application.
  6. failed
    • Called when multiple failed attempts made by user.
    • You can show error message to user here.
    • Default message can be shown if not provided.
  7. biometryLockedout
    • Called when more than 5 failed attempts made using biometric authentication. This will locked your biometry system.
    • You’ll restrict user when this error is occured.
    • Aleternatively you can ask user to enter device passcode to unlock biometry.
  8. biometryNotAvailable
    • Called when device does not support Face ID or Touch ID authentication.

Example

BioMetricAuthenticator.authenticateWithBioMetrics(reason: "") { [weak self] (result) in

    switch result {
    case .success( _):

        // authentication successful
        self?.showLoginSucessAlert()

    case .failure(let error):

        switch error {

        // device does not support biometric (face id or touch id) authentication
        case .biometryNotAvailable:
            self?.showErrorAlert(message: error.message())

        // No biometry enrolled in this device, ask user to register fingerprint or face
        case .biometryNotEnrolled:
            self?.showGotoSettingsAlert(message: error.message())

        // show alternatives on fallback button clicked
        case .fallback:
            self?.txtUsername.becomeFirstResponder() // enter username password manually

        // Biometry is locked out now, because there were too many failed attempts.
        // Need to enter device passcode to unlock.
        case .biometryLockedout:
            self?.showPasscodeAuthentication(message: error.message())

        // do nothing on canceled by system or user
        case .canceledBySystem, .canceledByUser:
            break

        // show error for any other reason
        default:
            self?.showErrorAlert(message: error.message())
        }
    }
}

See Example for more details.

License

BiometricAuthentication is released under the MIT license. See LICENSE for details.

评论

暂无评论