From beaec159e8ee76c9cb598ec3a1a6cff8c46c355b Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Fri, 17 Apr 2020 23:42:48 +0530 Subject: Update SSH clear passphrase preference to handle HTTPS (#716) * Update SSH clear passphrase preference to handle HTTPS Signed-off-by: Harsh Shandilya * Address review comments Signed-off-by: Harsh Shandilya * Address review comments Signed-off-by: Harsh Shandilya * Explicitly set preference to visible if it can do anything * Address review comments Co-authored-by: Fabian Henneke Co-authored-by: Fabian Henneke --- .../main/java/com/zeapo/pwdstore/UserPreference.kt | 40 ++++++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt index eabc35f4..5756d477 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt @@ -8,6 +8,7 @@ import android.accessibilityservice.AccessibilityServiceInfo import android.annotation.SuppressLint import android.app.Activity import android.content.Intent +import android.content.SharedPreferences import android.content.pm.ShortcutManager import android.net.Uri import android.os.Build @@ -23,6 +24,7 @@ import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.AppCompatTextView import androidx.biometric.BiometricManager +import androidx.core.content.edit import androidx.core.content.getSystemService import androidx.documentfile.provider.DocumentFile import androidx.preference.CheckBoxPreference @@ -67,15 +69,17 @@ class UserPreference : AppCompatActivity() { class PrefsFragment : PreferenceFragmentCompat() { private var autoFillEnablePreference: SwitchPreferenceCompat? = null + private var clearSavedPassPreference: Preference? = null private lateinit var autofillDependencies: List private lateinit var oreoAutofillDependencies: List private lateinit var callingActivity: UserPreference + private lateinit var encryptedPreferences: SharedPreferences override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { callingActivity = requireActivity() as UserPreference val context = requireContext() val sharedPreferences = preferenceManager.sharedPreferences - val encryptedPreferences = requireActivity().applicationContext.getEncryptedPrefs("git_operation") + encryptedPreferences = requireActivity().applicationContext.getEncryptedPrefs("git_operation") addPreferencesFromResource(R.xml.preference) @@ -85,7 +89,7 @@ class UserPreference : AppCompatActivity() { val gitConfigPreference = findPreference("git_config") val sshKeyPreference = findPreference("ssh_key") val sshKeygenPreference = findPreference("ssh_keygen") - val sshClearPassphrasePreference = findPreference("ssh_key_clear_passphrase") + clearSavedPassPreference = findPreference("clear_saved_pass") val clearHotpIncrementPreference = findPreference("hotp_remember_clear_choice") val viewSshKeyPreference = findPreference("ssh_see_key") val deleteRepoPreference = findPreference("git_delete_repo") @@ -124,8 +128,6 @@ class UserPreference : AppCompatActivity() { selectExternalGitRepositoryPreference?.summary = sharedPreferences.getString("git_external_repo", getString(R.string.no_repo_selected)) viewSshKeyPreference?.isVisible = sharedPreferences.getBoolean("use_generated_key", false) deleteRepoPreference?.isVisible = !sharedPreferences.getBoolean("git_external", false) - sshClearPassphrasePreference?.isVisible = encryptedPreferences.getString("ssh_key_local_passphrase", null)?.isNotEmpty() - ?: false clearHotpIncrementPreference?.isVisible = sharedPreferences.getBoolean("hotp_remember_check", false) clearAfterCopyPreference?.isVisible = sharedPreferences.getString("general_show_time", "45")?.toInt() != 0 clearClipboard20xPreference?.isVisible = sharedPreferences.getString("general_show_time", "45")?.toInt() != 0 @@ -142,6 +144,7 @@ class UserPreference : AppCompatActivity() { ?: false updateAutofillSettings() + updateClearSavedPassphrasePrefs() appVersionPreference?.summary = "Version: ${BuildConfig.VERSION_NAME}" @@ -174,9 +177,14 @@ class UserPreference : AppCompatActivity() { true } - sshClearPassphrasePreference?.onPreferenceClickListener = ClickListener { - encryptedPreferences.edit().putString("ssh_key_local_passphrase", null).apply() - it.isVisible = false + clearSavedPassPreference?.onPreferenceClickListener = ClickListener { + encryptedPreferences.edit { + if (encryptedPreferences.getString("https_password", null) != null) + remove("https_password") + else if (encryptedPreferences.getString("ssh_key_local_passphrase", null) != null) + remove("ssh_key_local_passphrase") + } + updateClearSavedPassphrasePrefs() true } @@ -374,6 +382,23 @@ class UserPreference : AppCompatActivity() { } } + private fun updateClearSavedPassphrasePrefs() { + clearSavedPassPreference?.apply { + val sshPass = encryptedPreferences.getString("ssh_key_local_passphrase", null) + val httpsPass = encryptedPreferences.getString("https_password", null) + if (sshPass == null && httpsPass == null) { + isVisible = false + return@apply + } + title = when { + httpsPass != null -> getString(R.string.clear_saved_passphrase_https) + sshPass != null -> getString(R.string.clear_saved_passphrase_ssh) + else -> null + } + isVisible = true + } + } + private fun onEnableAutofillClick() { if (callingActivity.isAccessibilityServiceEnabled) { startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)) @@ -431,6 +456,7 @@ class UserPreference : AppCompatActivity() { override fun onResume() { super.onResume() updateAutofillSettings() + updateClearSavedPassphrasePrefs() } } -- cgit v1.2.3