diff options
author | zeapo <mohamed@zenadi.com> | 2017-08-10 15:13:58 +0200 |
---|---|---|
committer | zeapo <mohamed@zenadi.com> | 2017-08-10 15:13:58 +0200 |
commit | bf1b67ea6a255866be9765f3096c91e2236f2eaf (patch) | |
tree | c77c56290463e3d549d4fa9eef86d1e97e6ba312 /app | |
parent | 24a77b90284ee6a0832245c7f8d1d432b9db3710 (diff) |
add remember passphrase and clearing it from settings
Diffstat (limited to 'app')
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/UserPreference.kt | 6 | ||||
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java | 67 | ||||
-rw-r--r-- | app/src/main/res/layout/git_passphrase_layout.xml | 34 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 3 | ||||
-rw-r--r-- | app/src/main/res/xml/preference.xml | 3 |
5 files changed, 90 insertions, 23 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt index f3c33dd3..39d6fe48 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt @@ -74,6 +74,11 @@ class UserPreference : AppCompatActivity() { true } + findPreference("ssh_key_clear_passphrase").onPreferenceClickListener = Preference.OnPreferenceClickListener { + sharedPreferences.edit().putString("ssh_key_passphrase", null).apply() + it.isEnabled = false + true + } findPreference("git_server_info").onPreferenceClickListener = Preference.OnPreferenceClickListener { val intent = Intent(callingActivity, GitActivity::class.java) @@ -155,6 +160,7 @@ class UserPreference : AppCompatActivity() { findPreference("pref_select_external").summary = preferenceManager.sharedPreferences.getString("git_external_repo", getString(R.string.no_repo_selected)) findPreference("ssh_see_key").isEnabled = sharedPreferences.getBoolean("use_generated_key", false) findPreference("git_delete_repo").isEnabled = !sharedPreferences.getBoolean("git_external", false) + findPreference("ssh_key_clear_passphrase").isEnabled = sharedPreferences.getString("ssh_key_passphrase", null)?.isNotEmpty() ?: false val keyPref = findPreference("openpgp_key_id_pref") val selectedKeys: Array<String> = ArrayList<String>(sharedPreferences.getStringSet("openpgp_key_ids_set", HashSet<String>())).toTypedArray() if (selectedKeys.isEmpty()) { diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java index fe8a083c..45189652 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java @@ -1,11 +1,17 @@ package com.zeapo.pwdstore.git; +import android.annotation.SuppressLint; import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; import android.text.InputType; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.CheckBox; import android.widget.EditText; import android.widget.LinearLayout; @@ -19,6 +25,7 @@ import com.zeapo.pwdstore.git.config.SshConfigSessionFactory; import com.zeapo.pwdstore.utils.PasswordRepository; import org.eclipse.jgit.api.GitCommand; +import org.eclipse.jgit.diff.Edit; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.transport.JschConfigSessionFactory; import org.eclipse.jgit.transport.SshSessionFactory; @@ -140,10 +147,11 @@ public abstract class GitOperation { } }).show(); } else { - final EditText passphrase = new EditText(callingActivity); - passphrase.setHint("Passphrase"); - passphrase.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); - passphrase.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); + LayoutInflater layoutInflater = LayoutInflater.from(callingActivity.getApplicationContext()); + @SuppressLint("InflateParams") final View dialogView = layoutInflater.inflate(R.layout.git_passphrase_layout, null); + final EditText passphrase = (EditText) dialogView.findViewById(R.id.sshkey_passphrase); + final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(callingActivity.getApplicationContext()); + final String sshKeyPassphrase = settings.getString("ssh_key_passphrase", null); if (showError) { passphrase.setError("Wrong passphrase"); } @@ -152,25 +160,40 @@ public abstract class GitOperation { final KeyPair keyPair = KeyPair.load(jsch, callingActivity.getFilesDir() + "/.ssh_key"); if (keyPair.isEncrypted()) { - new AlertDialog.Builder(callingActivity) - .setTitle(callingActivity.getResources().getString(R.string.passphrase_dialog_title)) - .setMessage(callingActivity.getResources().getString(R.string.passphrase_dialog_text)) - .setView(passphrase) - .setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - if (keyPair.decrypt(passphrase.getText().toString())) { - // Authenticate using the ssh-key and then execute the command - setAuthentication(sshKey, username, passphrase.getText().toString()).execute(); - } else { - // call back the method - executeAfterAuthentication(connectionMode, username, sshKey, true); - } - } - }).setNegativeButton(callingActivity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - // Do nothing. + if (sshKeyPassphrase != null && !sshKeyPassphrase.isEmpty()) { + if (keyPair.decrypt(sshKeyPassphrase)) { + // Authenticate using the ssh-key and then execute the command + setAuthentication(sshKey, username, sshKeyPassphrase).execute(); + } else { + // call back the method + executeAfterAuthentication(connectionMode, username, sshKey, true); } - }).show(); + } else { + new AlertDialog.Builder(callingActivity) + .setTitle(callingActivity.getResources().getString(R.string.passphrase_dialog_title)) + .setMessage(callingActivity.getResources().getString(R.string.passphrase_dialog_text)) + .setView(dialogView) + .setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + if (keyPair.decrypt(passphrase.getText().toString())) { + boolean rememberPassphrase = ((CheckBox) dialogView.findViewById(R.id.sshkey_remember_passphrase)).isChecked(); + if (rememberPassphrase) { + settings.edit().putString("ssh_key_passphrase", passphrase.getText().toString()).apply(); + } + // Authenticate using the ssh-key and then execute the command + setAuthentication(sshKey, username, passphrase.getText().toString()).execute(); + } else { + settings.edit().putString("ssh_key_passphrase", null).apply(); + // call back the method + executeAfterAuthentication(connectionMode, username, sshKey, true); + } + } + }).setNegativeButton(callingActivity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + // Do nothing. + } + }).show(); + } } else { setAuthentication(sshKey, username, "").execute(); } diff --git a/app/src/main/res/layout/git_passphrase_layout.xml b/app/src/main/res/layout/git_passphrase_layout.xml new file mode 100644 index 00000000..d82a0b55 --- /dev/null +++ b/app/src/main/res/layout/git_passphrase_layout.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="utf-8"?> +<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + android:id="@+id/linearLayout" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <EditText + android:id="@+id/sshkey_passphrase" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_marginEnd="16dp" + android:layout_marginStart="16dp" + android:layout_marginTop="8dp" + android:ems="10" + android:inputType="textPassword" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + + <CheckBox + android:id="@+id/sshkey_remember_passphrase" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_marginEnd="16dp" + android:layout_marginRight="16dp" + android:layout_marginStart="16dp" + android:layout_marginTop="8dp" + android:text="Remember until the application is closed" + app:layout_constraintRight_toRightOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@+id/sshkey_passphrase" /> +</android.support.constraint.ConstraintLayout>
\ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7805ad08..e39cb6f5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -46,7 +46,7 @@ <string name="ssh_preferences_dialog_import">Import</string> <string name="ssh_preferences_dialog_generate">Generate</string> <string name="passphrase_dialog_title">Authenticate</string> - <string name="passphrase_dialog_text">Please provide the passphrase for your SSH key. Leave it empty if there is no passphrase.</string> + <string name="passphrase_dialog_text">Please provide the passphrase for your SSH key</string> <string name="password_dialog_text">Please provide the password for this repository</string> <!-- Clone fragment --> @@ -208,4 +208,5 @@ <string name="git_push_generic_error">Push was rejected by remote, reason:</string> <string name="git_push_other_error">Remote rejected non-fast-forward push. Check receive.denyNonFastForwards variable in config file of destination repository.</string> <string name="jgit_error_push_dialog_text">Error occurred during the push operation:</string> + <string name="ssh_key_clear_passphrase">Clear ssh-key saved passphrase</string> </resources> diff --git a/app/src/main/res/xml/preference.xml b/app/src/main/res/xml/preference.xml index 87e77aae..23e68577 100644 --- a/app/src/main/res/xml/preference.xml +++ b/app/src/main/res/xml/preference.xml @@ -14,6 +14,9 @@ android:key="ssh_keygen" android:title="@string/pref_ssh_keygen_title" /> <Preference + android:key="ssh_key_clear_passphrase" + android:title="@string/ssh_key_clear_passphrase" /> + <Preference android:key="ssh_see_key" android:title="@string/pref_ssh_see_key_title" /> <Preference |