Fix error message and better implementation

This commit is contained in:
J-Jamet
2022-05-19 11:15:28 +02:00
parent cbde96dd82
commit ba1498b0b2
6 changed files with 55 additions and 55 deletions

View File

@@ -171,7 +171,7 @@ class SetMainCredentialDialogFragment : DatabaseDialogFragment() {
hardwareKeySelectionView.error =
if (!HardwareKeyResponseHelper.isHardwareKeyAvailable(requireActivity(), hardwareKey)) {
// show hardware driver dialog if required
getString(R.string.warning_hardware_key_required)
getString(R.string.error_driver_required, hardwareKey.toString())
} else {
null
}
@@ -236,7 +236,8 @@ class SetMainCredentialDialogFragment : DatabaseDialogFragment() {
) {
// show hardware driver dialog if required
error = true
hardwareKeySelectionView.error = getString(R.string.warning_hardware_key_required)
hardwareKeySelectionView.error =
getString(R.string.error_driver_required, hardwareKey.toString())
}
if (!error) {
mListener?.onAssignKeyDialogPositiveClick(retrieveMainCredential())

View File

@@ -6,9 +6,8 @@ import androidx.activity.viewModels
import com.kunzisoft.keepass.activities.stylish.StylishActivity
import com.kunzisoft.keepass.database.action.DatabaseTaskProvider
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.model.CipherEncryptDatabase
import com.kunzisoft.keepass.database.element.MainCredential
import com.kunzisoft.keepass.hardware.HardwareKeyResponseHelper
import com.kunzisoft.keepass.model.CipherEncryptDatabase
import com.kunzisoft.keepass.tasks.ActionRunnable
import com.kunzisoft.keepass.viewmodels.DatabaseViewModel
@@ -18,12 +17,10 @@ abstract class DatabaseActivity: StylishActivity(), DatabaseRetrieval {
protected var mDatabaseTaskProvider: DatabaseTaskProvider? = null
protected var mDatabase: Database? = null
private var mHardwareKeyResponseHelper = HardwareKeyResponseHelper(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mDatabaseTaskProvider = DatabaseTaskProvider(this, mHardwareKeyResponseHelper)
mDatabaseTaskProvider = DatabaseTaskProvider(this)
mDatabaseTaskProvider?.onDatabaseRetrieved = { database ->
val databaseWasReloaded = database?.wasReloaded == true

View File

@@ -38,6 +38,7 @@ import com.kunzisoft.keepass.database.crypto.kdf.KdfEngine
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.Entry
import com.kunzisoft.keepass.database.element.Group
import com.kunzisoft.keepass.database.element.MainCredential
import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm
import com.kunzisoft.keepass.database.element.node.Node
import com.kunzisoft.keepass.database.element.node.NodeId
@@ -46,7 +47,6 @@ import com.kunzisoft.keepass.database.exception.InvalidCredentialsDatabaseExcept
import com.kunzisoft.keepass.hardware.HardwareKey
import com.kunzisoft.keepass.hardware.HardwareKeyResponseHelper
import com.kunzisoft.keepass.model.CipherEncryptDatabase
import com.kunzisoft.keepass.database.element.MainCredential
import com.kunzisoft.keepass.model.SnapFileDatabaseInfo
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_ASSIGN_PASSWORD_TASK
@@ -95,7 +95,6 @@ import java.util.*
class DatabaseTaskProvider {
private var activity: FragmentActivity? = null
private var service: Service? = null
private var context: Context
var onDatabaseRetrieved: ((database: Database?) -> Unit)? = null
@@ -114,44 +113,35 @@ class DatabaseTaskProvider {
private var progressTaskDialogFragment: ProgressTaskDialogFragment? = null
private var databaseChangedDialogFragment: DatabaseChangedDialogFragment? = null
// To manage hardware key challenge response
private var mHardwareKeyResponseHelper: HardwareKeyResponseHelper? = null
private var mChallengeResponseData: ByteArray? = null
private var mCancelChallengeResponse: Boolean = false
constructor(activity: FragmentActivity,
hardwareKeyResponseHelper: HardwareKeyResponseHelper? = null) {
constructor(activity: FragmentActivity) {
this.activity = activity
this.context = activity
this.intentDatabaseTask = Intent(activity.applicationContext,
DatabaseTaskNotificationService::class.java)
if (hardwareKeyResponseHelper != null) {
this.mHardwareKeyResponseHelper = hardwareKeyResponseHelper
this.mHardwareKeyResponseHelper?.buildHardwareKeyResponse { responseData, _ ->
// TODO Verify database
mChallengeResponseData = responseData ?: ByteArray(0)
respondToChallengeIfAllowed()
}
this.requestChallengeListener = object: DatabaseTaskNotificationService.RequestChallengeListener {
override fun onChallengeResponseRequested(hardwareKey: HardwareKey, seed: ByteArray?) {
if (HardwareKeyResponseHelper.isHardwareKeyAvailable(activity, hardwareKey)) {
mHardwareKeyResponseHelper?.launchChallengeForResponse(hardwareKey, seed)
} else {
throw InvalidCredentialsDatabaseException("Driver for $hardwareKey is required.")
}
// To manage hardware key challenge response
val hardwareKeyResponseHelper = HardwareKeyResponseHelper(activity)
hardwareKeyResponseHelper.buildHardwareKeyResponse { responseData, _ ->
// TODO Verify database
mChallengeResponseData = responseData ?: ByteArray(0)
respondToChallengeIfAllowed()
}
this.requestChallengeListener = object: DatabaseTaskNotificationService.RequestChallengeListener {
override fun onChallengeResponseRequested(hardwareKey: HardwareKey, seed: ByteArray?) {
if (HardwareKeyResponseHelper.isHardwareKeyAvailable(activity, hardwareKey)) {
hardwareKeyResponseHelper.launchChallengeForResponse(hardwareKey, seed)
} else {
throw InvalidCredentialsDatabaseException(
context.getString(R.string.error_driver_required, hardwareKey.toString())
)
}
}
} else {
// It's not a credential screen, cancel the challenge response
mCancelChallengeResponse = true
cancelChallengeResponseIfAllowed()
}
}
constructor(service: Service) {
this.service = service
this.context = service
this.intentDatabaseTask = Intent(service.applicationContext,
DatabaseTaskNotificationService::class.java)
@@ -235,12 +225,9 @@ class DatabaseTaskProvider {
}
}
private fun cancelChallengeResponseIfAllowed() {
mBinder?.let { binder ->
if (mCancelChallengeResponse) {
binder.getService().cancelChallengeResponse()
}
}
// TODO Cancel challenge response
private fun cancelChallengeResponse() {
mBinder?.getService()?.cancelChallengeResponse()
}
private fun startDialog(titleId: Int?,
@@ -299,7 +286,6 @@ class DatabaseTaskProvider {
getService().checkAction()
}
respondToChallengeIfAllowed()
cancelChallengeResponseIfAllowed()
}
override fun onServiceDisconnected(name: ComponentName?) {
@@ -398,7 +384,7 @@ class DatabaseTaskProvider {
context.startService(intentDatabaseTask)
} catch (e: Exception) {
Log.e(TAG, "Unable to perform database action", e)
Toast.makeText(activity, R.string.error_start_database_action, Toast.LENGTH_LONG).show()
Toast.makeText(context, R.string.error_start_database_action, Toast.LENGTH_LONG).show()
}
}

View File

@@ -93,14 +93,16 @@ class HardwareKeyResponseHelper {
private const val YKDROID_SEED_KEY = "challenge"
private const val EXTRA_BUNDLE_KEY = "EXTRA_BUNDLE_KEY"
fun isHardwareKeyAvailable(activity: FragmentActivity,
hardwareKey: HardwareKey,
showDialog: Boolean = true): Boolean {
fun isHardwareKeyAvailable(
activity: FragmentActivity,
hardwareKey: HardwareKey,
showDialog: Boolean = true
): Boolean {
return when (hardwareKey) {
HardwareKey.FIDO2_SECRET -> {
// TODO FIDO2
if (showDialog)
showHardwareKeyDriverNeeded(activity)
showHardwareKeyDriverNeeded(activity, hardwareKey)
false
}
HardwareKey.CHALLENGE_RESPONSE_YUBIKEY -> {
@@ -110,11 +112,17 @@ class HardwareKeyResponseHelper {
}
}
private fun showHardwareKeyDriverNeeded(activity: FragmentActivity) {
private fun showHardwareKeyDriverNeeded(
activity: FragmentActivity,
hardwareKey: HardwareKey
) {
activity.lifecycleScope.launch {
val builder = AlertDialog.Builder(activity)
builder.setMessage(R.string.warning_hardware_key_required)
.setPositiveButton(android.R.string.ok) { _, _ ->
builder
.setMessage(
activity.getString(R.string.error_driver_required, hardwareKey.toString())
)
.setPositiveButton(R.string.download) { _, _ ->
UriUtil.openExternalApp(activity, UriUtil.KEEPASSDX_PRO_PACKAGE)
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }

View File

@@ -31,7 +31,9 @@ import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
import com.kunzisoft.keepass.R
import kotlinx.coroutines.launch
open class ProgressTaskDialogFragment : DialogFragment() {
@@ -89,7 +91,7 @@ open class ProgressTaskDialogFragment : DialogFragment() {
}
private fun updateView(textView: TextView?, @StringRes resId: Int) {
activity?.runOnUiThread {
activity?.lifecycleScope?.launch {
if (resId == UNDEFINED) {
textView?.visibility = View.GONE
} else {
@@ -99,6 +101,15 @@ open class ProgressTaskDialogFragment : DialogFragment() {
}
}
private fun updateCancelable() {
activity?.lifecycleScope?.launch {
cancelButton?.isVisible = cancellable != null
cancelButton?.setOnClickListener {
cancellable?.invoke()
}
}
}
fun updateTitle(@StringRes resId: Int) {
this.title = resId
updateView(titleView, title)
@@ -116,10 +127,7 @@ open class ProgressTaskDialogFragment : DialogFragment() {
fun setCancellable(cancellable: (() -> Unit)?) {
this.cancellable = cancellable
cancelButton?.isVisible = cancellable != null
cancelButton?.setOnClickListener {
cancellable?.invoke()
}
updateCancelable()
}
companion object {

View File

@@ -201,6 +201,7 @@
<string name="error_response_already_provided">Response already provided.</string>
<string name="error_no_response_from_challenge">Unable to get the response from the challenge.</string>
<string name="error_cancel_by_user">Cancelled by user.</string>
<string name="error_driver_required">Driver for %1$s is required.</string>
<string name="field_name">Field name</string>
<string name="field_value">Field value</string>
<string name="file_not_found_content">Could not find file. Try reopening it from your file browser.</string>
@@ -364,7 +365,6 @@
<string name="warning_database_revoked">Access to the file revoked by the file manager, close the database and reopen it from its location.</string>
<string name="warning_exact_alarm">You have not allowed the app to use an exact alarm. As a result, the features requiring a timer will not be done with an exact time.</string>
<string name="warning_keyfile_integrity">The hash of the file is not guaranteed because Android can change its data on the fly. Change the file extension to .bin for correct integrity.</string>
<string name="warning_hardware_key_required">Driver for this hardware is required.</string>
<string name="permission">Permission</string>
<string name="version_label">Version %1$s</string>
<string name="build_label">Build %1$s</string>