Added a dialog to verify the password #2283

This commit is contained in:
J-Jamet committed 2025-12-01 18:40:38 +01:00
1 parent 9c6241afc9
commit c754b6a049
21 files changed
+292 -263

No files matched your search

@@ -84,6 +84,7 @@ import com.kunzisoft.keepass.view.changeTitleColor
import com.kunzisoft.keepass.view.hideByFading
import com.kunzisoft.keepass.view.setTransparentNavigationBar
import com.kunzisoft.keepass.view.showActionErrorIfNeeded
import com.kunzisoft.keepass.view.showError
import com.kunzisoft.keepass.viewmodels.EntryViewModel
import com.kunzisoft.keepass.viewmodels.UserVerificationViewModel
import kotlinx.coroutines.launch
@@ -214,7 +215,7 @@ class EntryActivity : DatabaseLockActivity() {
mEntryViewModel.loadEntry(mDatabase, mainEntryId, historyPosition)
}
} catch (e: ClassCastException) {
} catch (_: ClassCastException) {
Log.e(TAG, "Unable to retrieve the entry key")
}
@@ -328,6 +329,7 @@ class EntryActivity : DatabaseLockActivity() {
when (uIState) {
is UserVerificationViewModel.UIState.Loading -> {}
is UserVerificationViewModel.UIState.OnUserVerificationCanceled -> {
coordinatorLayout?.showError(uIState.error)
mUserVerificationViewModel.onUserVerificationReceived()
}
is UserVerificationViewModel.UIState.OnUserVerificationSucceeded -> {
@@ -122,6 +122,7 @@ import com.kunzisoft.keepass.view.applyWindowInsets
import com.kunzisoft.keepass.view.hideByFading
import com.kunzisoft.keepass.view.setTransparentNavigationBar
import com.kunzisoft.keepass.view.showActionErrorIfNeeded
import com.kunzisoft.keepass.view.showError
import com.kunzisoft.keepass.view.toastError
import com.kunzisoft.keepass.view.updateLockPaddingStart
import com.kunzisoft.keepass.viewmodels.GroupEditViewModel
@@ -581,6 +582,7 @@ class GroupActivity : DatabaseLockActivity(),
when (uIState) {
is UserVerificationViewModel.UIState.Loading -> {}
is UserVerificationViewModel.UIState.OnUserVerificationCanceled -> {
coordinatorLayout?.showError(uIState.error)
mUserVerificationViewModel.onUserVerificationReceived()
}
is UserVerificationViewModel.UIState.OnUserVerificationSucceeded -> {
@@ -711,14 +713,6 @@ class GroupActivity : DatabaseLockActivity(),
result: ActionRunnable.Result
) {
super.onDatabaseActionFinished(database, actionTask, result)
var entry: Entry? = null
try {
entry = result.data?.getNewEntry(database)
} catch (e: Exception) {
Log.e(TAG, "Unable to retrieve entry action for selection", e)
}
when (actionTask) {
ACTION_DATABASE_UPDATE_ENTRY_TASK -> {
if (result.isSuccess) {
@@ -731,6 +725,12 @@ class GroupActivity : DatabaseLockActivity(),
// Search not used
},
selectionAction = { intentSenderMode, typeMode, searchInfo ->
var entry: Entry? = null
try {
entry = result.data?.getNewEntry(database)
} catch (e: Exception) {
Log.e(TAG, "Unable to retrieve entry action for selection", e)
}
when (typeMode) {
TypeMode.DEFAULT -> {}
TypeMode.MAGIKEYBOARD -> entry?.let {
@@ -749,14 +749,12 @@ class GroupActivity : DatabaseLockActivity(),
}
)
}
coordinatorError?.showActionErrorIfNeeded(result)
// Reload the group
loadGroup()
finishNodeAction()
}
}
coordinatorError?.showActionErrorIfNeeded(result)
// Reload the group
loadGroup()
finishNodeAction()
}
private fun manageIntent(intent: Intent?) {
@@ -0,0 +1,72 @@
/*
* Copyright 2025 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePassDX.
*
* KeePassDX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePassDX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.activities.dialogs
import android.app.Dialog
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.activityViewModels
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.utils.UriUtil.openUrl
import com.kunzisoft.keepass.viewmodels.UserVerificationViewModel
class CheckDatabaseCredentialDialogFragment : DatabaseDialogFragment() {
private val userVerificationViewModel: UserVerificationViewModel by activityViewModels()
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
activity?.let { activity ->
val builder = AlertDialog.Builder(activity)
val inflater = activity.layoutInflater
val rootView = inflater.inflate(R.layout.fragment_check_database_credential, null)
builder.setView(rootView)
.setPositiveButton(R.string.check) { _, _ ->
userVerificationViewModel.checkMainCredential(
rootView
.findViewById<TextView>(R.id.setup_check_password_edit_text)
.text.toString(),
)
}
.setNegativeButton(android.R.string.cancel) { _, _ ->
userVerificationViewModel.onUserVerificationFailed()
dismiss()
}
rootView.findViewById<View>(R.id.user_verification_information)?.setOnClickListener {
activity.openUrl(R.string.user_verification_explanation_url)
}
return builder.create()
}
return super.onCreateDialog(savedInstanceState)
}
companion object {
fun getInstance(): CheckDatabaseCredentialDialogFragment {
val fragment = CheckDatabaseCredentialDialogFragment()
val args = Bundle()
fragment.arguments = args
return fragment
}
}
}
@@ -181,14 +181,6 @@ abstract class DatabaseLockActivity : DatabaseModeActivity(),
}
}
fun checkMainCredential(mainCredential: MainCredential) {
mDatabase?.let { database ->
database.fileUri?.let { databaseUri ->
mDatabaseViewModel.checkMainCredential(databaseUri, mainCredential)
}
}
}
fun saveDatabase() {
mDatabaseViewModel.saveDatabase(save = true)
}
@@ -2,9 +2,7 @@ package com.kunzisoft.keepass.credentialprovider
import android.content.Context
import android.content.Intent
import android.provider.Settings
import android.util.Log
import androidx.appcompat.app.AlertDialog
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_WEAK
import androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL
@@ -13,6 +11,7 @@ import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.dialogs.CheckDatabaseCredentialDialogFragment
import com.kunzisoft.keepass.credentialprovider.passkey.data.UserVerificationRequirement
import com.kunzisoft.keepass.model.EntryInfo
import com.kunzisoft.keepass.utils.getEnumExtra
@@ -99,9 +98,7 @@ class UserVerificationHelper {
if (isAuthenticatorsAllowed()) {
showUserVerificationDeviceCredential(userVerificationViewModel, dataToVerify)
} else {
showUserVerificationMessage {
userVerificationViewModel.onUserVerificationFailed()
}
showUserVerificationDatabaseCredential(userVerificationViewModel, dataToVerify)
}
}
@@ -145,33 +142,22 @@ class UserVerificationHelper {
}
}).authenticate(
BiometricPrompt.PromptInfo.Builder()
.setTitle(getString(R.string.user_verification_required))
.setTitle(getString(R.string.user_verification_required_title))
.setSubtitle(getString(R.string.user_verification_required_description))
.setAllowedAuthenticators(ALLOWED_AUTHENTICATORS)
.setConfirmationRequired(false)
.build()
)
}
fun FragmentActivity.showUserVerificationMessage(
onActionPerformed: () -> Unit
fun FragmentActivity.showUserVerificationDatabaseCredential(
userVerificationViewModel: UserVerificationViewModel,
dataToVerify: UserVerificationData
) {
AlertDialog.Builder(this)
.setTitle(getString(R.string.set_up_user_verification_passkeys_title))
.setMessage(getString(R.string.set_up_user_verification_passkeys_description))
.setPositiveButton(resources.getString(R.string.set_up_user_verification)
) { _, _ ->
startActivity(Intent(Settings.ACTION_SECURITY_SETTINGS))
onActionPerformed()
}
.setNegativeButton(resources.getString(android.R.string.cancel)
) { _, _ ->
onActionPerformed()
}
.setOnDismissListener {
onActionPerformed()
}
.create()
.show()
userVerificationViewModel.dataToVerify = dataToVerify
CheckDatabaseCredentialDialogFragment
.getInstance()
.show(this.supportFragmentManager, "checkDatabaseCredentialDialog")
}
}
}
@@ -59,7 +59,6 @@ import com.kunzisoft.keepass.credentialprovider.viewmodel.PasskeyLauncherViewMod
import com.kunzisoft.keepass.database.ContextualDatabase
import com.kunzisoft.keepass.model.AppOrigin
import com.kunzisoft.keepass.model.SearchInfo
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_CHECK_CREDENTIAL_TASK
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_UPDATE_ENTRY_TASK
import com.kunzisoft.keepass.settings.PreferencesUtil.isPasskeyUserVerificationPreferred
import com.kunzisoft.keepass.tasks.ActionRunnable
@@ -189,6 +188,7 @@ class PasskeyLauncherActivity : DatabaseLockActivity() {
userVerificationViewModel.onUserVerificationReceived()
}
is UserVerificationViewModel.UIState.OnUserVerificationCanceled -> {
toastError(uiState.error)
passkeyLauncherViewModel.cancelResult()
userVerificationViewModel.onUserVerificationReceived()
}
@@ -229,17 +229,6 @@ class PasskeyLauncherActivity : DatabaseLockActivity() {
// TODO When auto save is enabled, WARNING filter by the calling activity
// passkeyLauncherViewModel.autoSelectPasskey(result, database)
}
ACTION_DATABASE_CHECK_CREDENTIAL_TASK -> {
if (result.isSuccess) {
userVerificationViewModel.onUserVerificationSucceeded(
UserVerificationData(database)
)
} else {
userVerificationViewModel.onUserVerificationFailed(
UserVerificationData(database)
)
}
}
}
}
@@ -49,7 +49,6 @@ import com.kunzisoft.keepass.model.CipherEncryptDatabase
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.ACTION_CHALLENGE_RESPONDED
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_ASSIGN_CREDENTIAL_TASK
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_CHECK_CREDENTIAL_TASK
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_COPY_NODES_TASK
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_CREATE_ENTRY_TASK
import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_CREATE_GROUP_TASK
@@ -239,7 +238,7 @@ class DatabaseTaskProvider(
try {
context.unregisterReceiver(databaseTaskBroadcastReceiver)
} catch (e: IllegalArgumentException) {
} catch (_: IllegalArgumentException) {
// If receiver not register, do nothing
}
}
@@ -326,16 +325,6 @@ class DatabaseTaskProvider(
}, ACTION_DATABASE_ASSIGN_CREDENTIAL_TASK)
}
fun startDatabaseCheckCredential(
databaseUri: Uri,
mainCredential: MainCredential
) {
start(Bundle().apply {
putParcelable(DatabaseTaskNotificationService.DATABASE_URI_KEY, databaseUri)
putParcelable(DatabaseTaskNotificationService.MAIN_CREDENTIAL_KEY, mainCredential)
}, ACTION_DATABASE_CHECK_CREDENTIAL_TASK)
}
/*
----
Nodes Actions
@@ -1,65 +0,0 @@
/*
* Copyright 2025 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePassDX.
*
* KeePassDX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePassDX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.database.action
import android.content.Context
import android.net.Uri
import com.kunzisoft.keepass.database.ContextualDatabase
import com.kunzisoft.keepass.database.MainCredential
import com.kunzisoft.keepass.database.exception.DatabaseInputException
import com.kunzisoft.keepass.database.exception.UnknownDatabaseLocationException
import com.kunzisoft.keepass.hardware.HardwareKey
import com.kunzisoft.keepass.tasks.ActionRunnable
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
import com.kunzisoft.keepass.utils.getUriInputStream
class CheckCredentialDatabaseRunnable(
private val context: Context,
private val mDatabase: ContextualDatabase,
private val mDatabaseUri: Uri,
private val mMainCredential: MainCredential,
private val mChallengeResponseRetriever: (hardwareKey: HardwareKey, seed: ByteArray?) -> ByteArray,
private val progressTaskUpdater: ProgressTaskUpdater?
) : ActionRunnable() {
var afterCheckCredential : ((Result) -> Unit)? = null
override fun onStartRun() {}
override fun onActionRun() {
try {
val contentResolver = context.contentResolver
mDatabase.fileUri = mDatabaseUri
mDatabase.checkMasterKey(
databaseStream = contentResolver.getUriInputStream(mDatabaseUri)
?: throw UnknownDatabaseLocationException(),
masterCredential = mMainCredential.toMasterCredential(contentResolver),
challengeResponseRetriever = mChallengeResponseRetriever,
progressTaskUpdater = progressTaskUpdater
)
} catch (e: DatabaseInputException) {
setError(e)
}
}
override fun onFinishRun() {
afterCheckCredential?.invoke(result)
}
}
@@ -37,7 +37,6 @@ import com.kunzisoft.keepass.credentialprovider.activity.HardwareKeyActivity
import com.kunzisoft.keepass.database.ContextualDatabase
import com.kunzisoft.keepass.database.MainCredential
import com.kunzisoft.keepass.database.ProgressMessage
import com.kunzisoft.keepass.database.action.CheckCredentialDatabaseRunnable
import com.kunzisoft.keepass.database.action.CreateDatabaseRunnable
import com.kunzisoft.keepass.database.action.LoadDatabaseRunnable
import com.kunzisoft.keepass.database.action.MergeDatabaseRunnable
@@ -349,7 +348,6 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
ACTION_DATABASE_MERGE_TASK -> buildDatabaseMergeActionTask(intent, database)
ACTION_DATABASE_RELOAD_TASK -> buildDatabaseReloadActionTask(database)
ACTION_DATABASE_ASSIGN_CREDENTIAL_TASK -> buildDatabaseAssignCredentialActionTask(intent, database)
ACTION_DATABASE_CHECK_CREDENTIAL_TASK -> buildDatabaseCheckCredentialActionTask(intent, database)
ACTION_DATABASE_CREATE_GROUP_TASK -> buildDatabaseCreateGroupActionTask(intent, database)
ACTION_DATABASE_UPDATE_GROUP_TASK -> buildDatabaseUpdateGroupActionTask(intent, database)
ACTION_DATABASE_CREATE_ENTRY_TASK -> buildDatabaseCreateEntryActionTask(intent, database)
@@ -944,37 +942,6 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
}
}
private fun buildDatabaseCheckCredentialActionTask(
intent: Intent,
database: ContextualDatabase
): ActionRunnable? {
return if (intent.hasExtra(DATABASE_URI_KEY)
&& intent.hasExtra(MAIN_CREDENTIAL_KEY)
) {
val databaseUri: Uri = intent.getParcelableExtraCompat(DATABASE_URI_KEY) ?: return null
val mainCredential: MainCredential =
intent.getParcelableExtraCompat(MAIN_CREDENTIAL_KEY) ?: MainCredential()
CheckCredentialDatabaseRunnable(
context = this,
mDatabase = database,
mDatabaseUri = databaseUri,
mMainCredential = mainCredential,
mChallengeResponseRetriever = { hardwareKey, seed ->
retrieveResponseFromChallenge(hardwareKey, seed)
},
progressTaskUpdater = this
).apply {
afterCheckCredential = {
result.data = Bundle().apply {
putParcelable(DATABASE_URI_KEY, databaseUri)
}
}
}
} else {
null
}
}
private fun eraseCredentials(databaseUri: Uri) {
// Erase the biometric
CipherDatabaseAction.getInstance(this)
@@ -1363,7 +1330,6 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
const val ACTION_DATABASE_MERGE_TASK = "ACTION_DATABASE_MERGE_TASK"
const val ACTION_DATABASE_RELOAD_TASK = "ACTION_DATABASE_RELOAD_TASK"
const val ACTION_DATABASE_ASSIGN_CREDENTIAL_TASK = "ACTION_DATABASE_ASSIGN_CREDENTIAL_TASK"
const val ACTION_DATABASE_CHECK_CREDENTIAL_TASK = "ACTION_DATABASE_CHECK_CREDENTIAL_TASK"
const val ACTION_DATABASE_CREATE_GROUP_TASK = "ACTION_DATABASE_CREATE_GROUP_TASK"
const val ACTION_DATABASE_UPDATE_GROUP_TASK = "ACTION_DATABASE_UPDATE_GROUP_TASK"
const val ACTION_DATABASE_CREATE_ENTRY_TASK = "ACTION_DATABASE_CREATE_ENTRY_TASK"
@@ -238,15 +238,13 @@ fun View.updateLockPaddingStart() {
}
}
fun Context.toastError(e: Throwable) {
Toast.makeText(
applicationContext,
if (e is LocalizedException)
e.getLocalizedMessage(resources)
else
e.localizedMessage,
Toast.LENGTH_LONG
).show()
fun Context.toastError(e: Throwable?) {
val message = if (e is LocalizedException)
e.getLocalizedMessage(resources)
else e?.localizedMessage
message?.let {
Toast.makeText(applicationContext, message, Toast.LENGTH_LONG).show()
}
}
fun Context.showActionErrorIfNeeded(result: ActionRunnable.Result) {
@@ -259,6 +257,15 @@ fun Context.showActionErrorIfNeeded(result: ActionRunnable.Result) {
}
}
fun CoordinatorLayout.showError(error: Throwable?) {
val message = if (error is LocalizedException) {
error.getLocalizedMessage(resources) ?: error.message
} else error?.message
message?.let {
Snackbar.make(this, message, Snackbar.LENGTH_LONG).asError().show()
}
}
fun CoordinatorLayout.showActionErrorIfNeeded(result: ActionRunnable.Result) {
if (!result.isSuccess) {
result.exception?.getLocalizedMessage(resources)?.let { errorMessage ->
@@ -133,13 +133,6 @@ class DatabaseViewModel(application: Application): AndroidViewModel(application)
}
}
fun checkMainCredential(
databaseUri: Uri,
mainCredential: MainCredential
) {
mDatabaseTaskProvider.startDatabaseCheckCredential(databaseUri, mainCredential)
}
fun saveDatabase(save: Boolean, saveToUri: Uri? = null) {
mDatabaseTaskProvider.startDatabaseSave(save, saveToUri)
}
@@ -2,6 +2,8 @@ package com.kunzisoft.keepass.viewmodels
import androidx.lifecycle.ViewModel
import com.kunzisoft.keepass.credentialprovider.UserVerificationData
import com.kunzisoft.keepass.database.element.MasterCredential.CREATOR.getCheckKey
import com.kunzisoft.keepass.database.exception.InvalidCredentialsDatabaseException
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -13,12 +15,28 @@ class UserVerificationViewModel: ViewModel() {
private val mUiState = MutableStateFlow<UIState>(UIState.Loading)
val uiState: StateFlow<UIState> = mUiState
var dataToVerify: UserVerificationData = UserVerificationData()
fun checkMainCredential(checkString: String) {
// Check the password part
if (dataToVerify.database?.checkKey(getCheckKey(checkString)) == true)
onUserVerificationSucceeded(dataToVerify)
else {
onUserVerificationFailed(dataToVerify, InvalidCredentialsDatabaseException())
}
dataToVerify = UserVerificationData()
}
fun onUserVerificationSucceeded(dataToVerify: UserVerificationData) {
mUiState.value = UIState.OnUserVerificationSucceeded(dataToVerify)
}
fun onUserVerificationFailed(dataToVerify: UserVerificationData = UserVerificationData()) {
mUiState.value = UIState.OnUserVerificationCanceled(dataToVerify)
fun onUserVerificationFailed(
dataToVerify: UserVerificationData = UserVerificationData(),
error: Throwable? = null
) {
this.dataToVerify = dataToVerify
mUiState.value = UIState.OnUserVerificationCanceled(dataToVerify, error)
}
fun onUserVerificationReceived() {
@@ -28,7 +46,10 @@ class UserVerificationViewModel: ViewModel() {
sealed class UIState {
object Loading: UIState()
data class OnUserVerificationSucceeded(val dataToVerify: UserVerificationData): UIState()
data class OnUserVerificationCanceled(val dataToVerify: UserVerificationData): UIState()
data class OnUserVerificationCanceled(
val dataToVerify: UserVerificationData,
val error: Throwable?
): UIState()
}
}