From 61b8da3775b6898eca65fb746be41bc5cd2dcfb7 Mon Sep 17 00:00:00 2001 From: AnkitJain Date: Wed, 12 Feb 2020 11:57:32 -0500 Subject: [PATCH 1/2] iOS only: Fixed isAvailable to return error code rather than generic unknown error --- src/ios/Fingerprint.swift | 68 ++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/src/ios/Fingerprint.swift b/src/ios/Fingerprint.swift index 1f01be47..27b268ba 100644 --- a/src/ios/Fingerprint.swift +++ b/src/ios/Fingerprint.swift @@ -33,8 +33,6 @@ import LocalAuthentication var pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: "Not available"); let available = authenticationContext.canEvaluatePolicy(policy, error: &error); - var results: [String : Any] - if(error != nil){ biometryType = "none"; errorResponse["code"] = error?.code; @@ -55,31 +53,45 @@ import LocalAuthentication pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: biometryType); }else{ - var code: Int; - switch(error!._code) { - case Int(kLAErrorBiometryNotAvailable): - code = PluginError.BIOMETRIC_UNAVAILABLE.rawValue; - break; - case Int(kLAErrorBiometryNotEnrolled): - code = PluginError.BIOMETRIC_NOT_ENROLLED.rawValue; - break; - - default: - code = PluginError.BIOMETRIC_UNKNOWN_ERROR.rawValue; - break; - } - results = ["code": code, "message": error!.localizedDescription]; - pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: results); + pluginResult = self.handleError(error: error); } commandDelegate.send(pluginResult, callbackId:command.callbackId); } + @objc(handleError:) + func handleError(error: Error?) -> CDVPluginResult? { + let errorResponse: [AnyHashable: Any] = [ + "message": "Something went wrong" + ]; + var pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: errorResponse); + if (error != nil) { + + var errorCodes = [Int: ErrorCodes]() + var errorResult: [String : Any] = ["code": PluginError.BIOMETRIC_UNKNOWN_ERROR.rawValue, "message": error?.localizedDescription ?? ""]; + + errorCodes[1] = ErrorCodes(code: PluginError.BIOMETRIC_AUTHENTICATION_FAILED.rawValue) + errorCodes[2] = ErrorCodes(code: PluginError.BIOMETRIC_DISMISSED.rawValue) + errorCodes[5] = ErrorCodes(code: PluginError.BIOMETRIC_SCREEN_GUARD_UNSECURED.rawValue) + errorCodes[6] = ErrorCodes(code: PluginError.BIOMETRIC_UNAVAILABLE.rawValue) + errorCodes[7] = ErrorCodes(code: PluginError.BIOMETRIC_NOT_ENROLLED.rawValue) + errorCodes[8] = ErrorCodes(code: PluginError.BIOMETRIC_LOCKED_OUT.rawValue) + + let errorCode = abs(error!._code) + if let e = errorCodes[errorCode] { + errorResult = ["code": e.code, "message": error!.localizedDescription]; + } + + pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: errorResult); + } + return pluginResult; + } + @objc(authenticate:) func authenticate(_ command: CDVInvokedUrlCommand){ let authenticationContext = LAContext(); - var errorResponse: [AnyHashable: Any] = [ + let errorResponse: [AnyHashable: Any] = [ "message": "Something went wrong" ]; var pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: errorResponse); @@ -112,25 +124,7 @@ import LocalAuthentication if( success ) { pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: "Success"); }else { - if (error != nil) { - - var errorCodes = [Int: ErrorCodes]() - var errorResult: [String : Any] = ["code": PluginError.BIOMETRIC_UNKNOWN_ERROR.rawValue, "message": error?.localizedDescription ?? ""]; - - errorCodes[1] = ErrorCodes(code: PluginError.BIOMETRIC_AUTHENTICATION_FAILED.rawValue) - errorCodes[2] = ErrorCodes(code: PluginError.BIOMETRIC_DISMISSED.rawValue) - errorCodes[5] = ErrorCodes(code: PluginError.BIOMETRIC_SCREEN_GUARD_UNSECURED.rawValue) - errorCodes[6] = ErrorCodes(code: PluginError.BIOMETRIC_UNAVAILABLE.rawValue) - errorCodes[7] = ErrorCodes(code: PluginError.BIOMETRIC_NOT_ENROLLED.rawValue) - errorCodes[8] = ErrorCodes(code: PluginError.BIOMETRIC_LOCKED_OUT.rawValue) - - let errorCode = abs(error!._code) - if let e = errorCodes[errorCode] { - errorResult = ["code": e.code, "message": error!.localizedDescription]; - } - - pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: errorResult); - } + pluginResult = self.handleError(error: error); } self.commandDelegate.send(pluginResult, callbackId:command.callbackId); } From 3fd6e46eaeeba4840b04d064c2d88a2eabaf7149 Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Sun, 1 Mar 2020 22:47:42 -0500 Subject: [PATCH 2/2] make handleError function as private --- src/ios/Fingerprint.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/Fingerprint.swift b/src/ios/Fingerprint.swift index 27b268ba..caa3b5a8 100644 --- a/src/ios/Fingerprint.swift +++ b/src/ios/Fingerprint.swift @@ -60,7 +60,7 @@ import LocalAuthentication } @objc(handleError:) - func handleError(error: Error?) -> CDVPluginResult? { + private func handleError(error: Error?) -> CDVPluginResult? { let errorResponse: [AnyHashable: Any] = [ "message": "Something went wrong" ];