fix: clear passphrase cache when disabling

This commit is contained in:
Harsh Shandilya 2024-03-12 13:04:32 +05:30
parent 0d7d6eae79
commit 5082df2f93
3 changed files with 30 additions and 2 deletions

View file

@ -6,18 +6,25 @@
package app.passwordstore.ui.settings
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import app.passwordstore.R
import app.passwordstore.data.crypto.PGPPassphraseCache
import app.passwordstore.ui.pgp.PGPKeyListActivity
import app.passwordstore.util.auth.BiometricAuthenticator
import app.passwordstore.util.extensions.launchActivity
import app.passwordstore.util.features.Feature
import app.passwordstore.util.settings.PreferenceKeys
import de.Maxr1998.modernpreferences.PreferenceScreen
import de.Maxr1998.modernpreferences.helpers.onCheckedChange
import de.Maxr1998.modernpreferences.helpers.onClick
import de.Maxr1998.modernpreferences.helpers.pref
import de.Maxr1998.modernpreferences.helpers.switch
import kotlinx.coroutines.launch
class PGPSettings(private val activity: FragmentActivity) : SettingsProvider {
class PGPSettings(
private val activity: FragmentActivity,
private val passphraseCache: PGPPassphraseCache,
) : SettingsProvider {
override fun provideSettings(builder: PreferenceScreen.Builder) {
builder.apply {
@ -38,6 +45,20 @@ class PGPSettings(private val activity: FragmentActivity) : SettingsProvider {
titleRes = R.string.pref_passphrase_cache_title
summaryRes = R.string.pref_passphrase_cache_summary
defaultValue = false
onCheckedChange { checked ->
if (!checked && BiometricAuthenticator.canAuthenticate(activity)) {
BiometricAuthenticator.authenticate(
activity,
R.string.pref_passphrase_cache_authenticate_clear,
) {
if (it is BiometricAuthenticator.Result.Success)
activity.lifecycleScope.launch {
passphraseCache.clearAllCachedPassphrases(activity)
}
}
}
true
}
}
}
}

View file

@ -11,22 +11,27 @@ import androidx.activity.addCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.BundleCompat
import app.passwordstore.R
import app.passwordstore.data.crypto.PGPPassphraseCache
import app.passwordstore.databinding.ActivityPreferenceRecyclerviewBinding
import app.passwordstore.util.extensions.viewBinding
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dagger.hilt.android.AndroidEntryPoint
import de.Maxr1998.modernpreferences.Preference
import de.Maxr1998.modernpreferences.PreferencesAdapter
import de.Maxr1998.modernpreferences.helpers.screen
import de.Maxr1998.modernpreferences.helpers.subScreen
import javax.inject.Inject
@AndroidEntryPoint
class SettingsActivity : AppCompatActivity() {
@Inject lateinit var passphraseCache: PGPPassphraseCache
private val miscSettings = MiscSettings(this)
private val autofillSettings = AutofillSettings(this)
private val passwordSettings = PasswordSettings(this)
private val repositorySettings = RepositorySettings(this)
private val generalSettings = GeneralSettings(this)
private val pgpSettings = PGPSettings(this)
private lateinit var pgpSettings: PGPSettings
private val binding by viewBinding(ActivityPreferenceRecyclerviewBinding::inflate)
private val preferencesAdapter: PreferencesAdapter
@ -36,6 +41,7 @@ class SettingsActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(binding.root)
Preference.Config.dialogBuilderFactory = { context -> MaterialAlertDialogBuilder(context) }
pgpSettings = PGPSettings(this, passphraseCache)
val screen =
screen(this) {
subScreen {

View file

@ -137,6 +137,7 @@
<string name="pref_pgp_ascii_armor_title">Encrypt in ASCII armor mode</string>
<string name="pref_passphrase_cache_title">Enable passphrase caching</string>
<string name="pref_passphrase_cache_summary">WARNING: this feature is functional but very experimental. Requires an active screen lock.</string>
<string name="pref_passphrase_cache_authenticate_clear">Authenticate to clear cache</string>
<!-- PasswordGenerator fragment -->
<string name="pwgen_title">Generate Password</string>