feat: isAvailable return stength instead of specific factors#63
Conversation
WalkthroughThe pull request refactors the biometric authentication API to replace biometry type detection with authentication strength levels (NONE, STRONG, WEAK), adds support for fallback authentication via useFallback option, expands error handling with a BiometricAuthError enum, and updates platform implementations (Android and iOS) alongside example applications. Changes
Sequence Diagram(s)sequenceDiagram
participant App
participant Plugin
participant BiometricManager/LAContext
App->>Plugin: isAvailable({ useFallback })
Plugin->>BiometricManager/LAContext: Check BIOMETRIC_STRONG availability
alt Strong biometrics available
BiometricManager/LAContext-->>Plugin: Success
Plugin->>Plugin: authenticationStrength = STRONG, isAvailable = true
else Strong unavailable, useFallback enabled
BiometricManager/LAContext-->>Plugin: Unavailable
Plugin->>BiometricManager/LAContext: Check device credentials/fallback
alt Fallback available
BiometricManager/LAContext-->>Plugin: Success
Plugin->>Plugin: authenticationStrength = WEAK, isAvailable = true
else Fallback unavailable
BiometricManager/LAContext-->>Plugin: Error
Plugin->>Plugin: authenticationStrength = NONE, isAvailable = false<br/>errorCode = mapped error
end
end
Plugin-->>App: AvailableResult { isAvailable, authenticationStrength, errorCode? }
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
android/src/main/java/ee/forgr/biometric/NativeBiometric.java (1)
120-146: Consider simplifying the error selection comment.The error handling logic is correct, but the comment on lines 132-135 is quite verbose. The key point is that when neither strong nor weak biometrics succeeded, we default to BIOMETRIC_ERROR_HW_UNAVAILABLE as a catch-all.
Consider this more concise comment:
} else { - // No biometrics available at all - // BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE indicates that biometric hardware is unavailable - // or cannot be accessed. This constant value may vary across Android versions, so we explicitly - // use the constant rather than assuming its numeric value. + // Default to HW_UNAVAILABLE when no specific error from biometric checks biometricManagerErrorCode = BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
.gitignore(1 hunks)README.md(2 hunks)android/src/main/java/ee/forgr/biometric/NativeBiometric.java(3 hunks)example-app/simple-test.html(3 hunks)example-app/src/js/biometric-tester.js(5 hunks)ios/Sources/NativeBiometricPlugin/NativeBiometricPlugin.swift(1 hunks)src/definitions.ts(3 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: below43
Repo: Cap-go/capacitor-native-biometric PR: 41
File: android/src/main/java/ee/forgr/biometric/AuthActivity.java:75-82
Timestamp: 2025-02-13T20:05:24.035Z
Learning: In android/src/main/java/ee/forgr/biometric/AuthActivity.java, the negativeButtonText must be set regardless of the useFallback value to prevent Android errors when negativeButtonText is null.
📚 Learning: 2025-02-13T20:05:24.035Z
Learnt from: below43
Repo: Cap-go/capacitor-native-biometric PR: 41
File: android/src/main/java/ee/forgr/biometric/AuthActivity.java:75-82
Timestamp: 2025-02-13T20:05:24.035Z
Learning: In android/src/main/java/ee/forgr/biometric/AuthActivity.java, the negativeButtonText must be set regardless of the useFallback value to prevent Android errors when negativeButtonText is null.
Applied to files:
android/src/main/java/ee/forgr/biometric/NativeBiometric.java
🧬 Code graph analysis (2)
android/src/main/java/ee/forgr/biometric/NativeBiometric.java (1)
ios/Sources/NativeBiometricPlugin/NativeBiometricPlugin.swift (1)
isAvailable(38-83)
example-app/src/js/biometric-tester.js (1)
src/index.ts (1)
NativeBiometric(10-10)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build_android
🔇 Additional comments (17)
.gitignore (1)
71-71: Clarify .codex pattern scope.The pattern
*/.codex/*only matches.codexdirectories one level deep (e.g.,subdir/.codex/). This won't ignore.codexat the repository root or in deeply nested paths likea/b/.codex/. Depending on where codex-generated artifacts are created, you may want a more comprehensive pattern.Consider using
**/.codex/(matches at any depth) or add both.codex/(root level) and**/.codex/(any nesting).If this narrower scope is intentional, the pattern is fine. Otherwise, apply this diff:
# External native build folder generated in Android Studio 2.2 and later .externalNativeBuild -*/.codex/* +**/.codex/ios/Sources/NativeBiometricPlugin/NativeBiometricPlugin.swift (3)
44-59: LGTM: Clean separation of biometric and fallback authentication checks.The two-stage policy evaluation correctly distinguishes between biometric-only and device-credential authentication, enabling accurate strength classification.
60-70: LGTM: Authentication strength classification is correct and consistent.The STRONG/WEAK classification aligns with the Android implementation and follows security best practices where biometrics are stronger than device credentials.
71-81: LGTM: Error handling is comprehensive.The error handling correctly covers both cases: when an error object exists (convert and set errorCode) and when no error is present (default to UNKNOWN_ERROR).
src/definitions.ts (3)
18-33: LGTM: Well-documented AuthenticationStrength enum.The enum values and documentation accurately reflect the authentication strength model across platforms, with clear guidance on PIN/password always being WEAK.
47-65: LGTM: Improved type safety and API clarity.The updated
AvailableResultinterface provides better type safety with theBiometricAuthErrorenum and clearer semantics withauthenticationStrengthreplacing the more specificbiometryType.
120-192: LGTM: Comprehensive error enum with excellent documentation.The
BiometricAuthErrorenum provides clear, well-documented error codes with platform-specific notes, making it easy for developers to handle different failure scenarios.android/src/main/java/ee/forgr/biometric/NativeBiometric.java (3)
64-67: LGTM: Authentication strength constants align with TypeScript definitions.The constant values match the
AuthenticationStrengthenum in definitions.ts and the iOS implementation.
88-118: LGTM: Correct strength classification with proper priority.The logic correctly prioritizes STRONG biometrics over WEAK, with fallback credentials always classified as WEAK. This aligns with iOS implementation and security best practices.
460-477: LGTM: Clear error code conversion with appropriate mappings.The helper method correctly maps BiometricManager error codes to plugin error codes, with a sensible default to UNKNOWN_ERROR for unmapped cases.
example-app/simple-test.html (2)
97-97: LGTM: Import updated for new API.
225-232: LGTM: Helper function correctly refactored for new enum.The mapping from
AuthenticationStrengthvalues to display names is clear and correct.example-app/src/js/biometric-tester.js (3)
113-118: LGTM: Useful addition for testing fallback authentication.The checkbox provides a clear way to test both biometric-only and fallback authentication modes.
237-247: LGTM: Correct usage of new isAvailable API.The code properly demonstrates the new API with
useFallbackparameter andauthenticationStrengthresponse field.
379-386: LGTM: Helper function refactored consistently.README.md (2)
239-245: LGTM: Documentation accurately reflects new API structure.The
AvailableResultdocumentation correctly describes the new fields and their types.
318-344: LGTM: Comprehensive enum documentation.Both
AuthenticationStrengthandBiometricAuthErrorenums are thoroughly documented with clear descriptions and platform notes.
Based on #62
Summary by CodeRabbit
New Features
API Changes