Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit d9a7c46

Browse files
committed
refactor: rework BiometricAuthenticator API
Align internal representation with the AndroidX documentation
1 parent 38f63c6 commit d9a7c46

File tree

5 files changed

+57
-30
lines changed

5 files changed

+57
-30
lines changed

app/src/main/java/app/passwordstore/ui/autofill/AutofillDecryptActivity.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class AutofillDecryptActivity : BasePGPActivity() {
8181
decrypt(filePath, clientState, action, authResult)
8282
}
8383
} else {
84-
decrypt(filePath, clientState, action, Result.Cancelled)
84+
decrypt(filePath, clientState, action, Result.CanceledByUser)
8585
}
8686
}
8787
}
@@ -98,7 +98,8 @@ class AutofillDecryptActivity : BasePGPActivity() {
9898
// Internally handled by the prompt dialog
9999
is Result.Retry -> {}
100100
// If the dialog is dismissed for any reason, prompt for passphrase
101-
is Result.Cancelled,
101+
is Result.CanceledBySystem,
102+
is Result.CanceledByUser,
102103
is Result.Failure,
103104
is Result.HardwareUnavailableOrDisabled -> askPassphrase(filePath, clientState, action)
104105
//

app/src/main/java/app/passwordstore/ui/crypto/DecryptActivity.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class DecryptActivity : BasePGPActivity() {
7878
requireKeysExist { decrypt(isError = false, authResult) }
7979
}
8080
} else {
81-
requireKeysExist { decrypt(isError = false, Result.Cancelled) }
81+
requireKeysExist { decrypt(isError = false, Result.CanceledByUser) }
8282
}
8383
}
8484

@@ -158,7 +158,8 @@ class DecryptActivity : BasePGPActivity() {
158158
// Internally handled by the prompt dialog
159159
is Result.Retry -> {}
160160
// If the dialog is dismissed for any reason, prompt for passphrase
161-
is Result.Cancelled,
161+
is Result.CanceledByUser,
162+
is Result.CanceledBySystem,
162163
is Result.Failure,
163164
is Result.HardwareUnavailableOrDisabled ->
164165
askPassphrase(isError, gpgIdentifiers, authResult)

app/src/main/java/app/passwordstore/ui/main/LaunchActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class LaunchActivity : AppCompatActivity() {
3636
startTargetActivity(false)
3737
}
3838
is Result.Failure,
39-
Result.Cancelled -> {
39+
Result.CanceledBySystem,
40+
Result.CanceledByUser -> {
4041
finish()
4142
}
4243
is Result.Retry -> {}

app/src/main/java/app/passwordstore/util/auth/BiometricAuthenticator.kt

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ object BiometricAuthenticator {
4242
/** The biometric hardware is unavailable or disabled on a software or hardware level. */
4343
data object HardwareUnavailableOrDisabled : Result()
4444

45-
/** The prompt was dismissed. */
46-
data object Cancelled : Result()
45+
/** The biometric prompt was canceled due to a user-initiated action. */
46+
data object CanceledByUser : Result()
47+
48+
/** The biometric prompt was canceled by the system. */
49+
data object CanceledBySystem : Result()
4750
}
4851

4952
fun canAuthenticate(activity: FragmentActivity): Boolean {
@@ -84,32 +87,51 @@ object BiometricAuthenticator {
8487
super.onAuthenticationError(errorCode, errString)
8588
logcat(TAG) { "onAuthenticationError(errorCode=$errorCode, msg=$errString)" }
8689
when (errorCode) {
87-
BiometricPrompt.ERROR_CANCELED,
88-
BiometricPrompt.ERROR_USER_CANCELED,
89-
BiometricPrompt.ERROR_NEGATIVE_BUTTON -> {
90-
callback(Result.Cancelled)
91-
}
92-
BiometricPrompt.ERROR_HW_NOT_PRESENT,
93-
BiometricPrompt.ERROR_HW_UNAVAILABLE,
94-
BiometricPrompt.ERROR_NO_BIOMETRICS,
95-
BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL -> {
96-
callback(Result.HardwareUnavailableOrDisabled)
97-
}
98-
BiometricPrompt.ERROR_LOCKOUT,
99-
BiometricPrompt.ERROR_LOCKOUT_PERMANENT,
100-
BiometricPrompt.ERROR_NO_SPACE,
101-
BiometricPrompt.ERROR_TIMEOUT,
102-
BiometricPrompt.ERROR_VENDOR -> {
90+
/** Keep in sync with [androidx.biometric.BiometricPrompt.AuthenticationError] */
91+
BiometricPrompt.ERROR_HW_UNAVAILABLE -> callback(Result.HardwareUnavailableOrDisabled)
92+
BiometricPrompt.ERROR_UNABLE_TO_PROCESS -> callback(Result.Retry)
93+
BiometricPrompt.ERROR_TIMEOUT ->
10394
callback(
10495
Result.Failure(
10596
errorCode,
10697
activity.getString(R.string.biometric_auth_error_reason, errString)
10798
)
10899
)
109-
}
110-
BiometricPrompt.ERROR_UNABLE_TO_PROCESS -> {
111-
callback(Result.Retry)
112-
}
100+
BiometricPrompt.ERROR_NO_SPACE ->
101+
callback(
102+
Result.Failure(
103+
errorCode,
104+
activity.getString(R.string.biometric_auth_error_reason, errString)
105+
)
106+
)
107+
BiometricPrompt.ERROR_CANCELED -> callback(Result.CanceledBySystem)
108+
BiometricPrompt.ERROR_LOCKOUT ->
109+
callback(
110+
Result.Failure(
111+
errorCode,
112+
activity.getString(R.string.biometric_auth_error_reason, errString)
113+
)
114+
)
115+
BiometricPrompt.ERROR_VENDOR ->
116+
callback(
117+
Result.Failure(
118+
errorCode,
119+
activity.getString(R.string.biometric_auth_error_reason, errString)
120+
)
121+
)
122+
BiometricPrompt.ERROR_LOCKOUT_PERMANENT ->
123+
callback(
124+
Result.Failure(
125+
errorCode,
126+
activity.getString(R.string.biometric_auth_error_reason, errString)
127+
)
128+
)
129+
BiometricPrompt.ERROR_USER_CANCELED -> callback(Result.CanceledByUser)
130+
BiometricPrompt.ERROR_NO_BIOMETRICS -> callback(Result.HardwareUnavailableOrDisabled)
131+
BiometricPrompt.ERROR_HW_NOT_PRESENT -> callback(Result.HardwareUnavailableOrDisabled)
132+
BiometricPrompt.ERROR_NEGATIVE_BUTTON -> callback(Result.CanceledByUser)
133+
BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL ->
134+
callback(Result.HardwareUnavailableOrDisabled)
113135
// We cover all guaranteed values above, but [errorCode] is still an Int
114136
// at the end of the day so a catch-all else will always be required.
115137
else -> {

app/src/main/java/app/passwordstore/util/git/operation/GitOperation.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import app.passwordstore.data.repo.PasswordRepository
1212
import app.passwordstore.ui.sshkeygen.SshKeyGenActivity
1313
import app.passwordstore.ui.sshkeygen.SshKeyImportActivity
1414
import app.passwordstore.util.auth.BiometricAuthenticator
15-
import app.passwordstore.util.auth.BiometricAuthenticator.Result.Cancelled
15+
import app.passwordstore.util.auth.BiometricAuthenticator.Result.CanceledBySystem
16+
import app.passwordstore.util.auth.BiometricAuthenticator.Result.CanceledByUser
1617
import app.passwordstore.util.auth.BiometricAuthenticator.Result.Failure
1718
import app.passwordstore.util.auth.BiometricAuthenticator.Result.Retry
1819
import app.passwordstore.util.auth.BiometricAuthenticator.Result.Success
@@ -183,10 +184,11 @@ abstract class GitOperation(protected val callingActivity: FragmentActivity) {
183184
is Success -> {
184185
registerAuthProviders(SshAuthMethod.SshKey(authActivity))
185186
}
186-
is Cancelled -> {
187+
is CanceledByUser -> {
187188
return Err(SSHException(DisconnectReason.AUTH_CANCELLED_BY_USER))
188189
}
189-
is Failure -> {
190+
is Failure,
191+
is CanceledBySystem -> {
190192
throw IllegalStateException("Biometric authentication failures should be ignored")
191193
}
192194
else -> {

0 commit comments

Comments
 (0)