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

Commit 763fa9e

Browse files
agrahnAlexander Grahn
andauthored
enabling pgp passphrase cache with authentication (#3124)
* enabling pgp passphrase cache with authentication * clear passphrase cache on first autofill decrypt after screen off --------- Co-authored-by: Alexander Grahn <[email protected]>
1 parent 3062c92 commit 763fa9e

File tree

6 files changed

+37
-27
lines changed

6 files changed

+37
-27
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.os.Bundle
1313
import android.view.autofill.AutofillManager
1414
import androidx.fragment.app.setFragmentResultListener
1515
import androidx.lifecycle.lifecycleScope
16+
import app.passwordstore.Application.Companion.screenWasOff
1617
import app.passwordstore.R
1718
import app.passwordstore.crypto.PGPIdentifier
1819
import app.passwordstore.data.crypto.PGPPassphraseCache
@@ -28,6 +29,7 @@ import app.passwordstore.util.autofill.DirectoryStructure
2829
import app.passwordstore.util.extensions.asLog
2930
import app.passwordstore.util.features.Feature.EnablePGPPassphraseCache
3031
import app.passwordstore.util.features.Features
32+
import app.passwordstore.util.settings.PreferenceKeys
3133
import com.github.androidpasswordstore.autofillparser.AutofillAction
3234
import com.github.androidpasswordstore.autofillparser.Credentials
3335
import com.github.michaelbull.result.getOrElse
@@ -110,6 +112,12 @@ class AutofillDecryptActivity : BasePGPActivity() {
110112
askPassphrase(filePath, gpgIdentifiers, clientState, action)
111113
//
112114
is Result.Success -> {
115+
/* clear passphrase cache on first use after application startup or if screen was off;
116+
also make sure to purge a stale cache after caching has been disabled via PGP settings */
117+
if (screenWasOff && settings.getBoolean(PreferenceKeys.CLEAR_PASSPHRASE_CACHE, true)) {
118+
passphraseCache.clearAllCachedPassphrases(this@AutofillDecryptActivity)
119+
screenWasOff = false
120+
}
113121
val cachedPassphrase =
114122
passphraseCache.retrieveCachedPassphrase(
115123
this@AutofillDecryptActivity,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ class DecryptActivity : BasePGPActivity() {
168168
askPassphrase(isError, gpgIdentifiers, authResult)
169169
//
170170
is BiometricResult.Success -> {
171-
// clear passphrase cache on first use after application startup or if screen was off
172-
if (screenWasOff && settings.getBoolean(PreferenceKeys.CLEAR_PASSPHRASE_CACHE, false)) {
171+
/* clear passphrase cache on first use after application startup or if screen was off;
172+
also make sure to purge a stale cache after caching has been disabled via PGP settings */
173+
if (screenWasOff && settings.getBoolean(PreferenceKeys.CLEAR_PASSPHRASE_CACHE, true)) {
173174
passphraseCache.clearAllCachedPassphrases(this@DecryptActivity)
174175
screenWasOff = false
175176
}

app/src/main/java/app/passwordstore/ui/settings/PGPSettings.kt

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
package app.passwordstore.ui.settings
77

8+
import androidx.core.content.edit
89
import androidx.fragment.app.FragmentActivity
910
import androidx.lifecycle.lifecycleScope
1011
import app.passwordstore.R
1112
import app.passwordstore.data.crypto.PGPPassphraseCache
1213
import app.passwordstore.ui.pgp.PGPKeyListActivity
1314
import app.passwordstore.util.auth.BiometricAuthenticator
1415
import app.passwordstore.util.extensions.launchActivity
16+
import app.passwordstore.util.extensions.sharedPrefs
1517
import app.passwordstore.util.features.Feature
1618
import app.passwordstore.util.settings.PreferenceKeys
1719
import de.Maxr1998.modernpreferences.PreferenceScreen
@@ -46,16 +48,24 @@ class PGPSettings(
4648
summaryRes = R.string.pref_passphrase_cache_summary
4749
defaultValue = false
4850
onCheckedChange { checked ->
49-
if (!checked && BiometricAuthenticator.canAuthenticate(activity)) {
50-
BiometricAuthenticator.authenticate(
51-
activity,
52-
R.string.pref_passphrase_cache_authenticate_clear,
53-
) {
54-
if (it is BiometricAuthenticator.Result.Success)
55-
activity.lifecycleScope.launch {
56-
passphraseCache.clearAllCachedPassphrases(activity)
57-
}
58-
}
51+
if (checked) {
52+
if (BiometricAuthenticator.canAuthenticate(activity)) {
53+
BiometricAuthenticator.authenticate(
54+
activity,
55+
R.string.pref_passphrase_cache_authenticate_enable,
56+
) {
57+
if (!(it is BiometricAuthenticator.Result.Success))
58+
activity.sharedPrefs.edit {
59+
putBoolean(Feature.EnablePGPPassphraseCache.configKey, false)
60+
}
61+
}
62+
} else
63+
activity.sharedPrefs.edit {
64+
putBoolean(Feature.EnablePGPPassphraseCache.configKey, false)
65+
}
66+
} else {
67+
activity.sharedPrefs.edit { remove(PreferenceKeys.CLEAR_PASSPHRASE_CACHE) }
68+
activity.lifecycleScope.launch { passphraseCache.clearAllCachedPassphrases(activity) }
5969
}
6070
true
6171
}
@@ -64,21 +74,12 @@ class PGPSettings(
6474
dependency = Feature.EnablePGPPassphraseCache.configKey
6575
titleRes = R.string.pref_passphrase_cache_auto_clear_title
6676
summaryRes = R.string.pref_passphrase_cache_auto_clear_summary
67-
defaultValue = false
77+
defaultValue = true
6878
/* clear cache once when unchecking; this is to prevent a malicious user
6979
* from bypassing cache clearing via the settings */
7080
onCheckedChange { checked ->
71-
if (!checked && BiometricAuthenticator.canAuthenticate(activity)) {
72-
BiometricAuthenticator.authenticate(
73-
activity,
74-
R.string.pref_passphrase_cache_authenticate_clear,
75-
) {
76-
if (it is BiometricAuthenticator.Result.Success)
77-
activity.lifecycleScope.launch {
78-
passphraseCache.clearAllCachedPassphrases(activity)
79-
}
80-
}
81-
}
81+
if (!checked)
82+
activity.lifecycleScope.launch { passphraseCache.clearAllCachedPassphrases(activity) }
8283
true
8384
}
8485
}

app/src/main/res/values-gl/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
<string name="pref_pgp_ascii_armor_title">Cifrar co modo ASCII armor</string>
121121
<string name="pref_passphrase_cache_title">Permitir usar a caché para frase de paso</string>
122122
<string name="pref_passphrase_cache_summary">AVISO: esta característica funciona ben pero é experimental. Require que a pantalla estea bloqueada.</string>
123-
<string name="pref_passphrase_cache_authenticate_clear">Autenticarse para poder baleirar a cache</string>
123+
<string name="pref_passphrase_cache_authenticate_enable">Autenticarse para activar a caché</string>
124124
<string name="pref_passphrase_cache_auto_clear_title">Limpar automáticamente a frase de paso da caché</string>
125125
<string name="pref_passphrase_cache_auto_clear_summary">Retira automáticamente a frase de paso da caché ao apagarse a pantalla</string>
126126
<!-- PasswordGenerator fragment -->

app/src/main/res/values-pl-rPL/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
<string name="pref_pgp_key_manager_title">Menedżer kluczy</string>
117117
<string name="pref_pgp_ascii_armor_title">Szyfruj w trybie ASCII-armor</string>
118118
<string name="pref_passphrase_cache_summary">UWAGA: ta funkcjonalność powinna działać poprawnie, ale jest bardzo eksperymentalna. Wymaga aktywnej blokady ekranu.</string>
119-
<string name="pref_passphrase_cache_authenticate_clear">Uwierzytelnij aby wyczyścić pamięć podręczną</string>
119+
<string name="pref_passphrase_cache_authenticate_enable">Uwierzytelnij aby włączyć pamięć podręczną</string>
120120
<string name="pref_passphrase_cache_auto_clear_title">Automatycznie wyczyść pamięć podręczną haseł</string>
121121
<string name="pref_passphrase_cache_auto_clear_summary">Czyści pamięć podręczną haseł po wyłączeniu ekranu</string>
122122
<!-- PasswordGenerator fragment -->

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
<string name="pref_pgp_ascii_armor_title">Encrypt in ASCII armor mode</string>
138138
<string name="pref_passphrase_cache_title">Enable passphrase caching</string>
139139
<string name="pref_passphrase_cache_summary">WARNING: this feature is functional but very experimental. Requires an active screen lock.</string>
140-
<string name="pref_passphrase_cache_authenticate_clear">Authenticate to clear cache</string>
140+
<string name="pref_passphrase_cache_authenticate_enable">Authenticate to enable cache</string>
141141
<string name="pref_passphrase_cache_auto_clear_title">Automatically clear passphrase cache</string>
142142
<string name="pref_passphrase_cache_auto_clear_summary">Clears the passphrase cache when the screen is turned off</string>
143143

0 commit comments

Comments
 (0)