From a376365ca4865d60f16bd8c26f47f88b6f1889e8 Mon Sep 17 00:00:00 2001 From: Berend Sliedrecht Date: Fri, 23 Aug 2024 18:41:11 +0200 Subject: [PATCH] fix: android cancel auth send promise Signed-off-by: Berend Sliedrecht --- .../secure/environment/SecureEnvironment.kt | 3 +++ .../SecureEnvironmentBiometrics.kt | 23 +++++++------------ example/App.tsx | 4 ++++ package.json | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/android/src/main/java/id/animo/secure/environment/SecureEnvironment.kt b/android/src/main/java/id/animo/secure/environment/SecureEnvironment.kt index 8f6ea40..900df1a 100644 --- a/android/src/main/java/id/animo/secure/environment/SecureEnvironment.kt +++ b/android/src/main/java/id/animo/secure/environment/SecureEnvironment.kt @@ -105,6 +105,7 @@ class SecureEnvironment { SecureEnvironmentBiometrics( context, { sig: ByteArray -> promise.resolve(sig) }, + { code: Number, msg: String -> promise.reject(CodedException("code: $code, msg: $msg")) }, message ).authenticate(signature) } else { @@ -117,3 +118,5 @@ class SecureEnvironment { } } } + +// code: , msg: \ No newline at end of file diff --git a/android/src/main/java/id/animo/secure/environment/SecureEnvironmentBiometrics.kt b/android/src/main/java/id/animo/secure/environment/SecureEnvironmentBiometrics.kt index 508b4d0..fc20f96 100644 --- a/android/src/main/java/id/animo/secure/environment/SecureEnvironmentBiometrics.kt +++ b/android/src/main/java/id/animo/secure/environment/SecureEnvironmentBiometrics.kt @@ -11,6 +11,7 @@ import java.security.Signature class SecureEnvironmentBiometrics( private val appContext: AppContext, private val signedCb: (sig: ByteArray) -> Unit, + private val errorCb: (code: Number, message: String) -> Unit, private val toBeSigned: ByteArray, private val activity: FragmentActivity = appContext.currentActivity as FragmentActivity, private val promptInfo: PromptInfo = PromptInfo.Builder() @@ -25,30 +26,22 @@ class SecureEnvironmentBiometrics( sig.update(toBeSigned) val signature = sig.sign() signedCb(signature) - } ?: throw SecureEnvironmentExceptions.NoSignatureOnCryptoObject() + } ?: errorCb(2323, "No CryptoObjectFound") + } + + override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { + super.onAuthenticationError(errorCode, errString) + errorCb(errorCode, errString.toString()) } private fun authenticateWithPrompt(signature: Signature) { val prompt = BiometricPrompt(activity, this) prompt.authenticate(promptInfo, BiometricPrompt.CryptoObject(signature)) } - - private fun waitResult() { - if (Thread.currentThread() === Looper.getMainLooper().thread) { - throw SecureEnvironmentExceptions.NotExecutedFromMainThread() - } - - try { - synchronized(this) { (this as Object).wait() } - } catch (ignored: InterruptedException) { - /* shutdown sequence */ - } - } - + fun authenticate(signature: Signature) { if (Thread.currentThread() != Looper.getMainLooper().thread) { activity.runOnUiThread { this@SecureEnvironmentBiometrics.authenticate(signature) } - waitResult() return } diff --git a/example/App.tsx b/example/App.tsx index d0020db..5fca13c 100644 --- a/example/App.tsx +++ b/example/App.tsx @@ -13,6 +13,7 @@ import { StyleSheet, View } from "react-native"; export default function App() { const testBiometrics = async () => { + try{ const id = new Date().toString(); generateKeypair(id, true); const publicKey = getPublicBytesForKeyId(id); @@ -30,6 +31,9 @@ export default function App() { }); console.log(isValid); + }catch(e) { + console.error('ERRRRRR', e) + } }; const testNoBiometrics = async () => { diff --git a/package.json b/package.json index 5ed1831..a4efab1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@animo-id/expo-secure-environment", - "version": "0.1.0-alpha.2", + "version": "0.1.0-alpha.3", "description": "Expo Secure Environment", "main": "build/index.js", "types": "build/index.d.ts",