Skip to content

Commit

Permalink
AND-4784 Improved the biometrics feature initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
iiiburnyiii committed Oct 4, 2023
1 parent 3e21a68 commit 0f449e2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.tangem.sdk.DefaultSessionViewDelegate
import com.tangem.sdk.extensions.createLogger
import com.tangem.sdk.extensions.getWordlist
import com.tangem.sdk.extensions.initBiometricManager
import com.tangem.sdk.extensions.initCryptographyManager
import com.tangem.sdk.extensions.initKeystoreManager
import com.tangem.sdk.extensions.initNfcManager
import com.tangem.sdk.storage.create
import com.tangem.tangem_demo.R
Expand Down Expand Up @@ -100,7 +100,7 @@ class DemoActivity : AppCompatActivity() {
wordlist = Wordlist.getWordlist(this),
config = config,
authenticationManager = authenticationManager,
keystoreManager = TangemSdk.initCryptographyManager(authenticationManager, secureStorage),
keystoreManager = TangemSdk.initKeystoreManager(authenticationManager, secureStorage),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import com.tangem.common.core.TangemError
import com.tangem.common.core.TangemSdkError
import com.tangem.sdk.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.sync.Mutex
Expand All @@ -37,18 +40,24 @@ internal class AndroidAuthenticationManager(
}

private val authenticationMutex = Mutex()
private val canAuthenticateInternal: MutableStateFlow<Boolean?> = MutableStateFlow(value = null)
private val needEnrollBiometricsInternal: MutableStateFlow<Boolean?> = MutableStateFlow(value = null)

override var canAuthenticate: Boolean = false
private set
override var canEnrollBiometrics: Boolean = false
private set
override val canAuthenticate: Boolean
get() = requireNotNull(canAuthenticateInternal.value) {
"`canAuthenticate` has not been initialized"
}
override val needEnrollBiometrics: Boolean
get() = requireNotNull(needEnrollBiometricsInternal.value) {
"`needEnrollBiometrics` has not been initialized"
}

override fun onResume(owner: LifecycleOwner) {
owner.lifecycleScope.launch(Dispatchers.Main) {
owner.lifecycleScope.launch {
val (available, canBeEnrolled) = checkBiometricsAvailabilityStatus()

canAuthenticate = available
canEnrollBiometrics = canBeEnrolled
canAuthenticateInternal.value = available
needEnrollBiometricsInternal.value = canBeEnrolled
}
}

Expand All @@ -60,6 +69,7 @@ internal class AndroidAuthenticationManager(
authenticationMutex.withLock {
Log.debug { "$TAG - Trying to authenticate a user" }

val canAuthenticate = canAuthenticateInternal.filterNotNull().first()
if (canAuthenticate) {
withContext(Dispatchers.Main) {
authenticateInternal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ fun TangemSdk.Companion.initWithBiometrics(activity: FragmentActivity, config: C
reader = nfcManager.reader,
viewDelegate = viewDelegate,
secureStorage = secureStorage,
authenticationManager = initBiometricManager(activity),
keystoreManager = initCryptographyManager(authenticationManager, secureStorage),
authenticationManager = authenticationManager,
keystoreManager = initKeystoreManager(authenticationManager, secureStorage),
wordlist = Wordlist.getWordlist(activity),
config = config,
)
Expand Down Expand Up @@ -122,7 +122,7 @@ fun TangemSdk.Companion.initBiometricManager(activity: FragmentActivity): Authen
}
}

fun TangemSdk.Companion.initCryptographyManager(
fun TangemSdk.Companion.initKeystoreManager(
authenticationManager: AuthenticationManager,
secureStorage: SecureStorage,
): KeystoreManager {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface AuthenticationManager {
* Checks if biometrics can be enrolled. Returns `true` if the hardware is available
* but there is no biometric data enrolled.
*/
val canEnrollBiometrics: Boolean
val needEnrollBiometrics: Boolean

/**
* Initiates the authentication process. Depending on the implementation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class DummyAuthenticationManager : AuthenticationManager {
override val canAuthenticate: Boolean
get() = false

override val canEnrollBiometrics: Boolean
override val needEnrollBiometrics: Boolean
get() = false

override suspend fun authenticate() = Unit
Expand Down

0 comments on commit 0f449e2

Please sign in to comment.