Skip to content

Commit

Permalink
fix: android cancel auth send promise
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
berendsliedrecht committed Aug 23, 2024
1 parent 6b7cda5 commit 228cd44
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -117,3 +118,5 @@ class SecureEnvironment {
}
}
}

// code: <CODE>, msg: <MSG>
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -30,6 +31,9 @@ export default function App() {
});

console.log(isValid);
}catch(e) {
console.error('ERRRRRR', e)
}
};

const testNoBiometrics = async () => {
Expand Down

0 comments on commit 228cd44

Please sign in to comment.