From 173e802a369313952bcaa1ef65686a40c99659a3 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 26 May 2024 01:54:15 +0530 Subject: [PATCH] feat: add option to auto clear passphrase cache Fixes #3053 --- .../java/app/passwordstore/Application.kt | 35 ++++++++++++++++++- .../passwordstore/ui/settings/PGPSettings.kt | 6 ++++ .../util/settings/PreferenceKeys.kt | 1 + app/src/main/res/values/strings.xml | 2 ++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/app/passwordstore/Application.kt b/app/src/main/java/app/passwordstore/Application.kt index b2bfa2cc..414ef54e 100644 --- a/app/src/main/java/app/passwordstore/Application.kt +++ b/app/src/main/java/app/passwordstore/Application.kt @@ -4,12 +4,18 @@ */ package app.passwordstore +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter import android.content.SharedPreferences import android.os.Build import android.os.StrictMode import androidx.appcompat.app.AppCompatDelegate.* +import app.passwordstore.data.crypto.PGPPassphraseCache import app.passwordstore.injection.context.FilesDirPath import app.passwordstore.injection.prefs.SettingsPreferences +import app.passwordstore.util.coroutines.DispatcherProvider import app.passwordstore.util.extensions.getString import app.passwordstore.util.features.Feature import app.passwordstore.util.features.Features @@ -24,6 +30,10 @@ import io.sentry.Sentry import io.sentry.protocol.User import java.util.concurrent.Executors import javax.inject.Inject +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import logcat.AndroidLogcatLogger import logcat.LogPriority.DEBUG import logcat.LogPriority.VERBOSE @@ -36,8 +46,10 @@ class Application : android.app.Application(), SharedPreferences.OnSharedPrefere @Inject @SettingsPreferences lateinit var prefs: SharedPreferences @Inject @FilesDirPath lateinit var filesDirPath: String - @Inject lateinit var proxyUtils: ProxyUtils + @Inject lateinit var dispatcherProvider: DispatcherProvider + @Inject lateinit var passphraseCache: PGPPassphraseCache @Inject lateinit var gitSettings: GitSettings + @Inject lateinit var proxyUtils: ProxyUtils @Inject lateinit var features: Features override fun onCreate() { @@ -64,6 +76,27 @@ class Application : android.app.Application(), SharedPreferences.OnSharedPrefere } scope.user = user } + setupPassphraseCacheClearAction() + } + + @OptIn(DelicateCoroutinesApi::class) + private fun setupPassphraseCacheClearAction() { + if (prefs.getBoolean(PreferenceKeys.CLEAR_PASSPHRASE_CACHE, false)) { + val screenOffReceiver: BroadcastReceiver = + object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + if (intent.action == Intent.ACTION_SCREEN_OFF) { + GlobalScope.launch { + withContext(dispatcherProvider.main()) { + passphraseCache.clearAllCachedPassphrases(context) + } + } + } + } + } + val filter = IntentFilter(Intent.ACTION_SCREEN_OFF) + registerReceiver(screenOffReceiver, filter) + } } override fun onTerminate() { diff --git a/app/src/main/java/app/passwordstore/ui/settings/PGPSettings.kt b/app/src/main/java/app/passwordstore/ui/settings/PGPSettings.kt index bb045799..09f81102 100644 --- a/app/src/main/java/app/passwordstore/ui/settings/PGPSettings.kt +++ b/app/src/main/java/app/passwordstore/ui/settings/PGPSettings.kt @@ -60,6 +60,12 @@ class PGPSettings( true } } + switch(PreferenceKeys.CLEAR_PASSPHRASE_CACHE) { + dependency = Feature.EnablePGPPassphraseCache.configKey + titleRes = R.string.pref_passphrase_cache_auto_clear_title + summaryRes = R.string.pref_passphrase_cache_auto_clear_summary + defaultValue = false + } } } } diff --git a/app/src/main/java/app/passwordstore/util/settings/PreferenceKeys.kt b/app/src/main/java/app/passwordstore/util/settings/PreferenceKeys.kt index 5e55582c..d67a4bef 100644 --- a/app/src/main/java/app/passwordstore/util/settings/PreferenceKeys.kt +++ b/app/src/main/java/app/passwordstore/util/settings/PreferenceKeys.kt @@ -91,4 +91,5 @@ object PreferenceKeys { const val DICEWARE_LENGTH = "diceware_length" const val DISABLE_SYNC_ACTION = "disable_sync_action" const val ASCII_ARMOR = "pgpainless_ascii_armor" + const val CLEAR_PASSPHRASE_CACHE = "pgpainless_auto_clear_passphrase_cache_screen_off" } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fa46776c..bafda226 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -138,6 +138,8 @@ Enable passphrase caching WARNING: this feature is functional but very experimental. Requires an active screen lock. Authenticate to clear cache + Automatically clear passphrase cache + Clears the passphrase cache when the screen is turned off Generate Password