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

Commit 173e802

Browse files
committed
feat: add option to auto clear passphrase cache
Fixes #3053
1 parent 3a63334 commit 173e802

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

app/src/main/java/app/passwordstore/Application.kt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44
*/
55
package app.passwordstore
66

7+
import android.content.BroadcastReceiver
8+
import android.content.Context
9+
import android.content.Intent
10+
import android.content.IntentFilter
711
import android.content.SharedPreferences
812
import android.os.Build
913
import android.os.StrictMode
1014
import androidx.appcompat.app.AppCompatDelegate.*
15+
import app.passwordstore.data.crypto.PGPPassphraseCache
1116
import app.passwordstore.injection.context.FilesDirPath
1217
import app.passwordstore.injection.prefs.SettingsPreferences
18+
import app.passwordstore.util.coroutines.DispatcherProvider
1319
import app.passwordstore.util.extensions.getString
1420
import app.passwordstore.util.features.Feature
1521
import app.passwordstore.util.features.Features
@@ -24,6 +30,10 @@ import io.sentry.Sentry
2430
import io.sentry.protocol.User
2531
import java.util.concurrent.Executors
2632
import javax.inject.Inject
33+
import kotlinx.coroutines.DelicateCoroutinesApi
34+
import kotlinx.coroutines.GlobalScope
35+
import kotlinx.coroutines.launch
36+
import kotlinx.coroutines.withContext
2737
import logcat.AndroidLogcatLogger
2838
import logcat.LogPriority.DEBUG
2939
import logcat.LogPriority.VERBOSE
@@ -36,8 +46,10 @@ class Application : android.app.Application(), SharedPreferences.OnSharedPrefere
3646

3747
@Inject @SettingsPreferences lateinit var prefs: SharedPreferences
3848
@Inject @FilesDirPath lateinit var filesDirPath: String
39-
@Inject lateinit var proxyUtils: ProxyUtils
49+
@Inject lateinit var dispatcherProvider: DispatcherProvider
50+
@Inject lateinit var passphraseCache: PGPPassphraseCache
4051
@Inject lateinit var gitSettings: GitSettings
52+
@Inject lateinit var proxyUtils: ProxyUtils
4153
@Inject lateinit var features: Features
4254

4355
override fun onCreate() {
@@ -64,6 +76,27 @@ class Application : android.app.Application(), SharedPreferences.OnSharedPrefere
6476
}
6577
scope.user = user
6678
}
79+
setupPassphraseCacheClearAction()
80+
}
81+
82+
@OptIn(DelicateCoroutinesApi::class)
83+
private fun setupPassphraseCacheClearAction() {
84+
if (prefs.getBoolean(PreferenceKeys.CLEAR_PASSPHRASE_CACHE, false)) {
85+
val screenOffReceiver: BroadcastReceiver =
86+
object : BroadcastReceiver() {
87+
override fun onReceive(context: Context, intent: Intent) {
88+
if (intent.action == Intent.ACTION_SCREEN_OFF) {
89+
GlobalScope.launch {
90+
withContext(dispatcherProvider.main()) {
91+
passphraseCache.clearAllCachedPassphrases(context)
92+
}
93+
}
94+
}
95+
}
96+
}
97+
val filter = IntentFilter(Intent.ACTION_SCREEN_OFF)
98+
registerReceiver(screenOffReceiver, filter)
99+
}
67100
}
68101

69102
override fun onTerminate() {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ class PGPSettings(
6060
true
6161
}
6262
}
63+
switch(PreferenceKeys.CLEAR_PASSPHRASE_CACHE) {
64+
dependency = Feature.EnablePGPPassphraseCache.configKey
65+
titleRes = R.string.pref_passphrase_cache_auto_clear_title
66+
summaryRes = R.string.pref_passphrase_cache_auto_clear_summary
67+
defaultValue = false
68+
}
6369
}
6470
}
6571
}

app/src/main/java/app/passwordstore/util/settings/PreferenceKeys.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,5 @@ object PreferenceKeys {
9191
const val DICEWARE_LENGTH = "diceware_length"
9292
const val DISABLE_SYNC_ACTION = "disable_sync_action"
9393
const val ASCII_ARMOR = "pgpainless_ascii_armor"
94+
const val CLEAR_PASSPHRASE_CACHE = "pgpainless_auto_clear_passphrase_cache_screen_off"
9495
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@
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>
140140
<string name="pref_passphrase_cache_authenticate_clear">Authenticate to clear cache</string>
141+
<string name="pref_passphrase_cache_auto_clear_title">Automatically clear passphrase cache</string>
142+
<string name="pref_passphrase_cache_auto_clear_summary">Clears the passphrase cache when the screen is turned off</string>
141143

142144
<!-- PasswordGenerator fragment -->
143145
<string name="pwgen_title">Generate Password</string>

0 commit comments

Comments
 (0)