mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-12-05 16:22:31 -08:00
Fix error message and better implementation
This commit is contained in:
1 parent
cbde96dd82
commit
ba1498b0b2
6 files changed
+55
-55
No files matched your search
+3
-2
@@ -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())
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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) { _, _ -> }
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user