feat: add option to auto clear passphrase cache

Fixes #3053
This commit is contained in:
Harsh Shandilya 2024-05-26 01:54:15 +05:30
parent 3a63334815
commit 173e802a36
4 changed files with 43 additions and 1 deletions

View file

@ -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() {

View file

@ -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
}
}
}
}

View file

@ -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"
}

View file

@ -138,6 +138,8 @@
<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>
<string name="pref_passphrase_cache_auto_clear_title">Automatically clear passphrase cache</string>
<string name="pref_passphrase_cache_auto_clear_summary">Clears the passphrase cache when the screen is turned off</string>
<!-- PasswordGenerator fragment -->
<string name="pwgen_title">Generate Password</string>