Skip to content

Commit

Permalink
Allow overriding biometric reauth logic in latte
Browse files Browse the repository at this point in the history
  • Loading branch information
tung2744 committed Aug 31, 2023
1 parent e78031f commit f2f72e6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
19 changes: 4 additions & 15 deletions sdk/src/main/java/com/oursky/authgear/latte/Latte.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,35 +178,24 @@ class Latte(
builder.setNegativeButtonText(negativeButtonText)
}
val promptInfo = builder.build()
val callback = options.biometricCallback
val prompt =
BiometricPrompt(
biometricOptions.activity,
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationFailed() {
// This callback will be invoked EVERY time the recognition failed.
// So while the prompt is still opened, this callback can be called repetitively.
// Finally, either onAuthenticationError or onAuthenticationSucceeded will be called.
// So this callback is not important to the developer.
callback.onAuthenticationFailed(resumeWith)
}

override fun onAuthenticationError(
errorCode: Int,
errString: CharSequence
) {
if (isBiometricCancelError(errorCode)) {
return
}
resumeWith(
Result.failure(
wrapException(BiometricPromptAuthenticationException(
errorCode
))
)
)
callback.onAuthenticationError(errorCode, errString, resumeWith)
}

override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
resumeWith(Result.success(true))
callback.onAuthenticationSucceeded(result, resumeWith)
}
})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.oursky.authgear.latte

import androidx.biometric.BiometricPrompt
import com.oursky.authgear.BiometricPromptAuthenticationException
import com.oursky.authgear.isBiometricCancelError
import com.oursky.authgear.wrapException

interface ReauthenticateBiometricCallback {

@JvmDefault
fun onAuthenticationFailed(complete: FlowCompletor) {
// This callback will be invoked EVERY time the recognition failed.
// So while the prompt is still opened, this callback can be called repetitively.
// Finally, either onAuthenticationError or onAuthenticationSucceeded will be called.
// So this callback is not important to the developer.
}

@JvmDefault
fun onAuthenticationError(
errorCode: Int,
errString: CharSequence,
complete: FlowCompletor
) {
if (isBiometricCancelError(errorCode)) {
return
}
complete(
Result.failure(
wrapException(
BiometricPromptAuthenticationException(
errorCode
)
)
)
)
}

@JvmDefault
fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult, complete: FlowCompletor) {
complete(Result.success(true))
}
}

typealias FlowCompletor = (Result<Boolean>) -> Unit
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ data class ReauthenticateOptions @JvmOverloads constructor(
var xSecrets: Map<String, String> = mapOf(),
var xState: Map<String, String> = mapOf(),
var uiLocales: List<String>? = null,
var biometricOptions: BiometricOptions? = null
var biometricOptions: BiometricOptions? = null,
var biometricCallback: ReauthenticateBiometricCallback = object : ReauthenticateBiometricCallback {}
)

0 comments on commit f2f72e6

Please sign in to comment.