@aparajita/capacitor-biometric-auth 5.2.0 → 5.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -96,6 +96,13 @@ public class BiometricAuthNative extends Plugin {
96
96
  */
97
97
  @PluginMethod
98
98
  public void checkBiometry(PluginCall call) {
99
+ call.resolve(checkDeviceBiometry());
100
+ }
101
+
102
+ /**
103
+ * Check the device's availability and type of biometric authentication.
104
+ */
105
+ private JSObject checkDeviceBiometry() {
99
106
  BiometricManager manager = BiometricManager.from(getContext());
100
107
  int biometryResult;
101
108
 
@@ -106,14 +113,14 @@ public class BiometricAuthNative extends Plugin {
106
113
  biometryResult = manager.canAuthenticate();
107
114
  }
108
115
 
109
- JSObject ret = new JSObject();
110
- ret.put(
116
+ JSObject result = new JSObject();
117
+ result.put(
111
118
  "isAvailable",
112
119
  biometryResult == BiometricManager.BIOMETRIC_SUCCESS
113
120
  );
114
121
 
115
122
  biometryTypes = getDeviceBiometryTypes();
116
- ret.put("biometryType", biometryTypes.get(0).getType());
123
+ result.put("biometryType", biometryTypes.get(0).getType());
117
124
 
118
125
  JSArray returnTypes = new JSArray();
119
126
 
@@ -121,7 +128,7 @@ public class BiometricAuthNative extends Plugin {
121
128
  returnTypes.put(type.getType());
122
129
  }
123
130
 
124
- ret.put("biometryTypes", returnTypes);
131
+ result.put("biometryTypes", returnTypes);
125
132
 
126
133
  String reason = "";
127
134
 
@@ -156,13 +163,13 @@ public class BiometricAuthNative extends Plugin {
156
163
  errorCode = "biometryNotAvailable";
157
164
  }
158
165
 
159
- ret.put("reason", reason);
160
- ret.put("code", errorCode);
161
- call.resolve(ret);
166
+ result.put("reason", reason);
167
+ result.put("code", errorCode);
168
+ return result;
162
169
  }
163
170
 
164
171
  private ArrayList<BiometryType> getDeviceBiometryTypes() {
165
- ArrayList<BiometryType> types = new ArrayList<BiometryType>();
172
+ ArrayList<BiometryType> types = new ArrayList<>();
166
173
  PackageManager manager = getContext().getPackageManager();
167
174
 
168
175
  if (manager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
@@ -189,6 +196,17 @@ public class BiometricAuthNative extends Plugin {
189
196
  */
190
197
  @PluginMethod
191
198
  public void authenticate(final PluginCall call) {
199
+ // Make sure biometry is available
200
+ JSObject checkResult = checkDeviceBiometry();
201
+
202
+ if (Boolean.FALSE.equals(checkResult.getBoolean("isAvailable", false))) {
203
+ call.reject(
204
+ checkResult.getString("reason", ""),
205
+ checkResult.getString("code", "")
206
+ );
207
+ return;
208
+ }
209
+
192
210
  // The result of an intent is supposed to have the package name as a prefix
193
211
  RESULT_EXTRA_PREFIX = getContext().getPackageName() + ".";
194
212
 
@@ -279,23 +297,19 @@ public class BiometricAuthNative extends Plugin {
279
297
  );
280
298
 
281
299
  switch (resultType) {
282
- case SUCCESS:
283
- call.resolve();
284
- break;
285
- case FAILURE:
286
- // Biometry was successfully presented but was not recognized
287
- call.reject(errorMessage, BIOMETRIC_FAILURE);
288
- break;
289
- case ERROR:
290
- // The user cancelled, the system cancelled, or some error occurred.
291
- // If the user cancelled, errorMessage is the text of the "negative" button,
292
- // which is not especially descriptive.
300
+ case SUCCESS -> call.resolve();
301
+ // Biometry was successfully presented but was not recognized
302
+ case FAILURE -> call.reject(errorMessage, BIOMETRIC_FAILURE);
303
+ // The user cancelled, the system cancelled, or some error occurred.
304
+ // If the user cancelled, errorMessage is the text of the "negative" button,
305
+ // which is not especially descriptive.
306
+ case ERROR -> {
293
307
  if (errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON) {
294
308
  errorMessage = "Cancel button was pressed";
295
309
  }
296
310
 
297
311
  call.reject(errorMessage, biometryErrorCodeMap.get(errorCode));
298
- break;
312
+ }
299
313
  }
300
314
  }
301
315
 
@@ -22,12 +22,32 @@ public class BiometricAuthNative: CAPPlugin {
22
22
  LAError.biometryNotEnrolled.rawValue: "biometryNotEnrolled"
23
23
  ]
24
24
 
25
- var canEvaluatePolicy = true
25
+ struct CheckDeviceBiometryResult {
26
+ let isAvailable: Bool
27
+ let biometryType: LABiometryType.RawValue
28
+ let biometryTypes: JSArray
29
+ let reason: String
30
+ let code: String
31
+ }
26
32
 
27
33
  /**
28
- * Check the device's availability and type of biometric authentication.
34
+ * Plugin call checkBiometry()
29
35
  */
30
36
  @objc func checkBiometry(_ call: CAPPluginCall) {
37
+ let checkResult = checkDeviceBiometry()
38
+ call.resolve([
39
+ "isAvailable": checkResult.isAvailable,
40
+ "biometryType": checkResult.biometryType,
41
+ "biometryTypes": checkResult.biometryTypes,
42
+ "reason": checkResult.reason,
43
+ "code": checkResult.code
44
+ ])
45
+ }
46
+
47
+ /**
48
+ * Check the device's availability and type of biometric authentication.
49
+ */
50
+ func checkDeviceBiometry() -> CheckDeviceBiometryResult {
31
51
  let context = LAContext()
32
52
  var error: NSError?
33
53
  var available = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)
@@ -42,7 +62,6 @@ public class BiometricAuthNative: CAPPlugin {
42
62
 
43
63
  if entry == nil {
44
64
  available = false
45
- canEvaluatePolicy = false
46
65
  reason = kMissingFaceIDUsageEntry
47
66
  errorCode = biometryErrorCodeMap[LAError.biometryNotAvailable.rawValue] ?? ""
48
67
  }
@@ -61,13 +80,13 @@ public class BiometricAuthNative: CAPPlugin {
61
80
  var types = JSArray()
62
81
  types.append(context.biometryType.rawValue)
63
82
 
64
- call.resolve([
65
- "isAvailable": available,
66
- "biometryType": context.biometryType.rawValue,
67
- "biometryTypes": types,
68
- "reason": reason,
69
- "code": errorCode
70
- ])
83
+ return CheckDeviceBiometryResult(
84
+ isAvailable: available,
85
+ biometryType: context.biometryType.rawValue,
86
+ biometryTypes: types,
87
+ reason: reason,
88
+ code: errorCode
89
+ )
71
90
  }
72
91
 
73
92
  /**
@@ -79,9 +98,11 @@ public class BiometricAuthNative: CAPPlugin {
79
98
  */
80
99
  @objc func authenticate(_ call: CAPPluginCall) {
81
100
  // Make sure the app can evaluate policy, otherwise evaluatePolicy() will crash
82
- guard canEvaluatePolicy else {
101
+ let checkResult = checkDeviceBiometry()
102
+
103
+ guard checkResult.isAvailable else {
83
104
  call.reject(
84
- kMissingFaceIDUsageEntry,
105
+ checkResult.reason,
85
106
  biometryErrorCodeMap[LAError.biometryNotAvailable.rawValue]
86
107
  )
87
108
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aparajita/capacitor-biometric-auth",
3
- "version": "5.2.0",
3
+ "version": "5.2.1",
4
4
  "description": "Provides access to the native biometric auth APIs for Capacitor apps",
5
5
  "author": "Aparajita Fishman",
6
6
  "license": "MIT",
@@ -56,21 +56,21 @@
56
56
  "@aparajita/eslint-config-base": "^1.1.6",
57
57
  "@aparajita/prettier-config": "^2.0.0",
58
58
  "@aparajita/swiftly": "^1.0.4",
59
- "@capacitor/cli": "^5.4.1",
59
+ "@capacitor/cli": "^5.4.2",
60
60
  "@commitlint/cli": "^17.7.2",
61
61
  "@commitlint/config-conventional": "^17.7.0",
62
62
  "@ionic/swiftlint-config": "^1.1.2",
63
- "@rollup/plugin-json": "^6.0.0",
64
- "@types/node": "^20.8.0",
65
- "@typescript-eslint/eslint-plugin": "^6.7.3",
66
- "@typescript-eslint/parser": "^6.7.3",
67
- "commit-and-tag-version": "^11.2.3",
68
- "eslint": "^8.50.0",
63
+ "@rollup/plugin-json": "^6.0.1",
64
+ "@types/node": "^20.8.4",
65
+ "@typescript-eslint/eslint-plugin": "^6.7.5",
66
+ "@typescript-eslint/parser": "^6.7.5",
67
+ "commit-and-tag-version": "^11.3.0",
68
+ "eslint": "^8.51.0",
69
69
  "eslint-config-prettier": "^9.0.0",
70
70
  "eslint-config-standard": "^17.1.0",
71
71
  "eslint-import-resolver-typescript": "^3.6.1",
72
72
  "eslint-plugin-import": "^2.28.1",
73
- "eslint-plugin-n": "^16.1.0",
73
+ "eslint-plugin-n": "^16.2.0",
74
74
  "eslint-plugin-promise": "^6.1.1",
75
75
  "nodemon": "^3.0.1",
76
76
  "prettier": "^3.0.3",
@@ -81,10 +81,10 @@
81
81
  "typescript": "~5.2.2"
82
82
  },
83
83
  "dependencies": {
84
- "@capacitor/android": "^5.4.1",
84
+ "@capacitor/android": "^5.4.2",
85
85
  "@capacitor/app": "^5.0.6",
86
- "@capacitor/core": "^5.4.1",
87
- "@capacitor/ios": "^5.4.1"
86
+ "@capacitor/core": "^5.4.2",
87
+ "@capacitor/ios": "^5.4.2"
88
88
  },
89
89
  "scripts": {
90
90
  "clean": "rimraf dist",