-
Notifications
You must be signed in to change notification settings - Fork 10
signin: add cross-platform biometric auth flow #715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package com.softartdev.notedelight.interactor | ||
|
|
||
| import android.content.Context | ||
| import androidx.biometric.BiometricManager | ||
| import androidx.biometric.BiometricPrompt | ||
| import androidx.fragment.app.FragmentActivity | ||
| import androidx.core.content.ContextCompat | ||
| import kotlinx.coroutines.suspendCancellableCoroutine | ||
| import kotlin.coroutines.resume | ||
|
|
||
| class AndroidBiometricAuthService(private val context: Context) : BiometricAuthService { | ||
|
|
||
| override suspend fun isBiometricAvailable(): Boolean { | ||
| val biometricManager = BiometricManager.from(context) | ||
| val authenticators = BiometricManager.Authenticators.BIOMETRIC_STRONG | ||
| return biometricManager.canAuthenticate(authenticators) == BiometricManager.BIOMETRIC_SUCCESS | ||
| } | ||
|
|
||
| override suspend fun authenticate(): BiometricAuthResult = suspendCancellableCoroutine { continuation -> | ||
| val activity = context.findActivity() as? FragmentActivity | ||
| if (activity == null) { | ||
| continuation.resume(BiometricAuthResult.FallbackToPassword) | ||
| return@suspendCancellableCoroutine | ||
| } | ||
| val promptInfo = BiometricPrompt.PromptInfo.Builder() | ||
| .setTitle("Biometric authentication") | ||
| .setSubtitle("Sign in to NoteDelight") | ||
| .setNegativeButtonText("Use password") | ||
| .build() | ||
| val biometricPrompt = BiometricPrompt( | ||
| activity, | ||
| ContextCompat.getMainExecutor(activity), | ||
| object : BiometricPrompt.AuthenticationCallback() { | ||
| override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { | ||
| if (continuation.isActive) { | ||
| continuation.resume(BiometricAuthResult.Success) | ||
| } | ||
| } | ||
|
|
||
| override fun onAuthenticationFailed() { | ||
| if (continuation.isActive) { | ||
| continuation.resume(BiometricAuthResult.Failed) | ||
| } | ||
| } | ||
|
|
||
| override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { | ||
| if (!continuation.isActive) return | ||
| val authResult = when (errorCode) { | ||
| BiometricPrompt.ERROR_NEGATIVE_BUTTON, | ||
| BiometricPrompt.ERROR_USER_CANCELED, | ||
| BiometricPrompt.ERROR_CANCELED -> BiometricAuthResult.FallbackToPassword | ||
| else -> BiometricAuthResult.Failed | ||
| } | ||
| continuation.resume(authResult) | ||
| } | ||
| } | ||
| ) | ||
| biometricPrompt.authenticate(promptInfo) | ||
| } | ||
| } | ||
|
|
||
| private tailrec fun Context.findActivity(): android.app.Activity? = when (this) { | ||
| is android.app.Activity -> this | ||
| is android.content.ContextWrapper -> baseContext.findActivity() | ||
| else -> null | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.softartdev.notedelight.interactor | ||
|
|
||
| interface BiometricAuthService { | ||
| suspend fun isBiometricAvailable(): Boolean | ||
| suspend fun authenticate(): BiometricAuthResult | ||
| } | ||
|
|
||
| sealed interface BiometricAuthResult { | ||
| data object Success : BiometricAuthResult | ||
| data object Failed : BiometricAuthResult | ||
| data object FallbackToPassword : BiometricAuthResult | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package com.softartdev.notedelight.interactor | ||
|
|
||
| import kotlinx.cinterop.ExperimentalForeignApi | ||
| import kotlinx.cinterop.ObjCObjectVar | ||
| import kotlinx.cinterop.alloc | ||
| import kotlinx.cinterop.memScoped | ||
| import kotlinx.coroutines.suspendCancellableCoroutine | ||
| import platform.LocalAuthentication.LAContext | ||
| import platform.LocalAuthentication.LAPolicyDeviceOwnerAuthenticationWithBiometrics | ||
| import platform.LocalAuthentication.LAErrorUserCancel | ||
| import platform.LocalAuthentication.LAErrorUserFallback | ||
| import kotlin.coroutines.resume | ||
|
|
||
| class IosBiometricAuthService : BiometricAuthService { | ||
|
|
||
| @OptIn(ExperimentalForeignApi::class) | ||
| override suspend fun isBiometricAvailable(): Boolean = memScoped { | ||
| val authContext = LAContext() | ||
| val errorPtr = alloc<ObjCObjectVar<platform.Foundation.NSError?>>() | ||
| authContext.canEvaluatePolicy( | ||
| policy = LAPolicyDeviceOwnerAuthenticationWithBiometrics, | ||
| error = errorPtr.ptr | ||
| ) | ||
| } | ||
|
|
||
| override suspend fun authenticate(): BiometricAuthResult = suspendCancellableCoroutine { continuation -> | ||
| val authContext = LAContext() | ||
| authContext.evaluatePolicy( | ||
| policy = LAPolicyDeviceOwnerAuthenticationWithBiometrics, | ||
| localizedReason = "Authenticate to sign in" | ||
| ) { success, error -> | ||
| if (!continuation.isActive) return@evaluatePolicy | ||
| val result = when { | ||
| success -> BiometricAuthResult.Success | ||
| error?.code?.toInt() == LAErrorUserFallback || error?.code?.toInt() == LAErrorUserCancel -> | ||
| BiometricAuthResult.FallbackToPassword | ||
| else -> BiometricAuthResult.Failed | ||
| } | ||
| continuation.resume(result) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.softartdev.notedelight.interactor | ||
|
|
||
| class JvmBiometricAuthService : BiometricAuthService { | ||
| override suspend fun isBiometricAvailable(): Boolean = false | ||
|
|
||
| override suspend fun authenticate(): BiometricAuthResult = BiometricAuthResult.FallbackToPassword | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.softartdev.notedelight.interactor | ||
|
|
||
| class WasmJsBiometricAuthService : BiometricAuthService { | ||
| override suspend fun isBiometricAvailable(): Boolean = false | ||
|
|
||
| override suspend fun authenticate(): BiometricAuthResult = BiometricAuthResult.FallbackToPassword | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| package com.softartdev.notedelight.di | ||
|
|
||
| import android.content.Context | ||
| import com.softartdev.notedelight.interactor.AndroidBiometricAuthService | ||
| import com.softartdev.notedelight.interactor.BiometricAuthService | ||
| import com.softartdev.notedelight.repository.AndroidFileRepo | ||
| import com.softartdev.notedelight.repository.AndroidSafeRepo | ||
| import com.softartdev.notedelight.repository.FileRepo | ||
|
|
@@ -20,3 +22,6 @@ actual val repoModule: Module = module { | |
|
|
||
| actual fun Module.factoryOfAppVersionUseCase(): KoinDefinition<AppVersionUseCase> = | ||
| factoryOf<AppVersionUseCase, Context>(constructor = ::AppVersionUseCase) | ||
|
|
||
| actual fun Module.singleOfBiometricAuthService(): KoinDefinition<BiometricAuthService> = | ||
| factoryOf<BiometricAuthService, Context>(constructor = ::AndroidBiometricAuthService) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This binding creates Useful? React with 👍 / 👎. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
onAuthenticationFailed()is a non-terminal callback (e.g., one bad fingerprint while the prompt remains open), but this code resumes the coroutine asFailedimmediately. That ends the sign-in flow on the first mismatch and can ignore a later successful scan from the same prompt session, producing incorrect login failures.Useful? React with 👍 / 👎.