diff options
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/git')
5 files changed, 41 insertions, 34 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt b/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt index c75f7ad3..ae2bbc07 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt @@ -22,6 +22,7 @@ import com.zeapo.pwdstore.git.config.ConnectionMode import com.zeapo.pwdstore.git.config.Protocol import com.zeapo.pwdstore.git.config.SshApiSessionFactory import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.getEncryptedPrefs import java.io.File import java.net.URI @@ -53,14 +54,14 @@ abstract class BaseGitActivity : AppCompatActivity() { settings = PreferenceManager.getDefaultSharedPreferences(this) encryptedSettings = getEncryptedPrefs("git_operation") - protocol = Protocol.fromString(settings.getString("git_remote_protocol", null)) - connectionMode = ConnectionMode.fromString(settings.getString("git_remote_auth", null)) - serverHostname = settings.getString("git_remote_server", null) ?: "" - serverPort = settings.getString("git_remote_port", null) ?: "" - serverUser = settings.getString("git_remote_username", null) ?: "" - serverPath = settings.getString("git_remote_location", null) ?: "" - username = settings.getString("git_config_user_name", null) ?: "" - email = settings.getString("git_config_user_email", null) ?: "" + protocol = Protocol.fromString(settings.getString(PreferenceKeys.GIT_REMOTE_PROTOCOL, null)) + connectionMode = ConnectionMode.fromString(settings.getString(PreferenceKeys.GIT_REMOTE_AUTH, null)) + serverHostname = settings.getString(PreferenceKeys.GIT_REMOTE_SERVER, null) ?: "" + serverPort = settings.getString(PreferenceKeys.GIT_REMOTE_PORT, null) ?: "" + serverUser = settings.getString(PreferenceKeys.GIT_REMOTE_USERNAME, null) ?: "" + serverPath = settings.getString(PreferenceKeys.GIT_REMOTE_LOCATION, null) ?: "" + username = settings.getString(PreferenceKeys.GIT_CONFIG_USER_NAME, null) ?: "" + email = settings.getString(PreferenceKeys.GIT_CONFIG_USER_EMAIL, null) ?: "" updateUrl() } @@ -148,7 +149,7 @@ abstract class BaseGitActivity : AppCompatActivity() { PasswordRepository.addRemote("origin", newUrl, true) // When the server changes, remote password and host key file should be deleted. if (previousUrl.isNotEmpty() && newUrl != previousUrl) { - encryptedSettings.edit { remove("https_password") } + encryptedSettings.edit { remove(PreferenceKeys.HTTPS_PASSWORD) } File("$filesDir/.host_key").delete() } url = newUrl diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt index be911a3a..35cbc68a 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt @@ -14,6 +14,7 @@ import com.google.android.material.snackbar.Snackbar import com.zeapo.pwdstore.R import com.zeapo.pwdstore.databinding.ActivityGitConfigBinding import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.viewBinding import org.eclipse.jgit.lib.Constants @@ -58,8 +59,8 @@ class GitConfigActivity : BaseGitActivity() { .show() } else { settings.edit { - putString("git_config_user_email", email) - putString("git_config_user_name", name) + putString(PreferenceKeys.GIT_CONFIG_USER_EMAIL, email) + putString(PreferenceKeys.GIT_CONFIG_USER_NAME, name) } PasswordRepository.setUserName(name) PasswordRepository.setUserEmail(email) diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt index 33a08fa4..48c28920 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt @@ -23,6 +23,7 @@ import com.zeapo.pwdstore.git.config.SshApiSessionFactory import com.zeapo.pwdstore.git.config.SshAuthData import com.zeapo.pwdstore.git.config.SshjSessionFactory import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.getEncryptedPrefs import com.zeapo.pwdstore.utils.requestInputFocusOnView import net.schmizz.sshj.userauth.password.PasswordFinder @@ -50,7 +51,7 @@ private class GitOperationCredentialFinder(val callingActivity: Activity, val co @StringRes val errorRes: Int when (connectionMode) { ConnectionMode.SshKey -> { - credentialPref = "ssh_key_local_passphrase" + credentialPref = PreferenceKeys.SSH_KEY_LOCAL_PASSPHRASE messageRes = R.string.passphrase_dialog_text hintRes = R.string.ssh_keygen_passphrase rememberRes = R.string.git_operation_remember_passphrase @@ -58,7 +59,7 @@ private class GitOperationCredentialFinder(val callingActivity: Activity, val co } ConnectionMode.Password -> { // Could be either an SSH or an HTTPS password - credentialPref = "https_password" + credentialPref = PreferenceKeys.HTTPS_PASSWORD messageRes = R.string.password_dialog_text hintRes = R.string.git_operation_hint_password rememberRes = R.string.git_operation_remember_password @@ -222,14 +223,14 @@ abstract class GitOperation(gitDir: File, internal val callingActivity: Activity when (SshSessionFactory.getInstance()) { is SshApiSessionFactory -> { PreferenceManager.getDefaultSharedPreferences(callingActivity.applicationContext) - .edit { remove("ssh_openkeystore_keyid") } + .edit { remove(PreferenceKeys.SSH_OPENKEYSTORE_KEYID) } } is SshjSessionFactory -> { callingActivity.applicationContext .getEncryptedPrefs("git_operation") .edit { - remove("ssh_key_local_passphrase") - remove("https_password") + remove(PreferenceKeys.SSH_KEY_LOCAL_PASSPHRASE) + remove(PreferenceKeys.HTTPS_PASSWORD) } } } diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt index 7a3978ba..10f44960 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt @@ -17,6 +17,7 @@ import com.zeapo.pwdstore.databinding.ActivityGitCloneBinding import com.zeapo.pwdstore.git.config.ConnectionMode import com.zeapo.pwdstore.git.config.Protocol import com.zeapo.pwdstore.utils.PasswordRepository +import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.viewBinding import java.io.IOException @@ -107,12 +108,12 @@ class GitServerConfigActivity : BaseGitActivity() { when (val result = updateUrl()) { GitUpdateUrlResult.Ok -> { settings.edit { - putString("git_remote_protocol", protocol.pref) - putString("git_remote_auth", connectionMode.pref) - putString("git_remote_server", serverHostname) - putString("git_remote_port", serverPort) - putString("git_remote_username", serverUser) - putString("git_remote_location", serverPath) + putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, protocol.pref) + putString(PreferenceKeys.GIT_REMOTE_AUTH, connectionMode.pref) + putString(PreferenceKeys.GIT_REMOTE_SERVER, serverHostname) + putString(PreferenceKeys.GIT_REMOTE_PORT, serverPort) + putString(PreferenceKeys.GIT_REMOTE_USERNAME, serverUser) + putString(PreferenceKeys.GIT_REMOTE_LOCATION, serverPath) } if (!isClone) { Snackbar.make(binding.root, getString(R.string.git_server_config_save_success), Snackbar.LENGTH_SHORT).show() diff --git a/app/src/main/java/com/zeapo/pwdstore/git/config/SshApiSessionFactory.java b/app/src/main/java/com/zeapo/pwdstore/git/config/SshApiSessionFactory.java index e5a5fd17..5ad12ef8 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/config/SshApiSessionFactory.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/config/SshApiSessionFactory.java @@ -21,6 +21,7 @@ import com.jcraft.jsch.Session; import com.jcraft.jsch.UserInfo; import com.zeapo.pwdstore.R; import com.zeapo.pwdstore.git.BaseGitActivity; +import com.zeapo.pwdstore.utils.PreferenceKeys; import org.eclipse.jgit.errors.UnsupportedCredentialItem; import org.eclipse.jgit.transport.CredentialItem; @@ -51,8 +52,8 @@ public class SshApiSessionFactory extends JschConfigSessionFactory { */ public static final int POST_SIGNATURE = 301; - private String username; - private Identity identity; + private final String username; + private final Identity identity; public SshApiSessionFactory(String username, Identity identity) { this.username = username; @@ -108,12 +109,12 @@ public class SshApiSessionFactory extends JschConfigSessionFactory { * build. */ public static class IdentityBuilder { - private SshAuthenticationConnection connection; + private final SshAuthenticationConnection connection; private SshAuthenticationApi api; private String keyId, description, alg; private byte[] publicKey; - private BaseGitActivity callingActivity; - private SharedPreferences settings; + private final BaseGitActivity callingActivity; + private final SharedPreferences settings; /** * Construct a new IdentityBuilder @@ -137,7 +138,7 @@ public class SshApiSessionFactory extends JschConfigSessionFactory { settings = PreferenceManager.getDefaultSharedPreferences( callingActivity.getApplicationContext()); - keyId = settings.getString("ssh_openkeystore_keyid", null); + keyId = settings.getString(PreferenceKeys.SSH_OPENKEYSTORE_KEYID, null); } /** @@ -163,7 +164,7 @@ public class SshApiSessionFactory extends JschConfigSessionFactory { SshAuthenticationApiError error = result.getParcelableExtra(SshAuthenticationApi.EXTRA_ERROR); // On an OpenKeychain SSH API error, clear out the stored keyid - settings.edit().putString("ssh_openkeystore_keyid", null).apply(); + settings.edit().putString(PreferenceKeys.SSH_OPENKEYSTORE_KEYID, null).apply(); switch (error.getError()) { // If the problem was just a bad keyid, reset to allow them to choose a @@ -214,7 +215,7 @@ public class SshApiSessionFactory extends JschConfigSessionFactory { if (intent.hasExtra(SshAuthenticationApi.EXTRA_KEY_ID)) { keyId = intent.getStringExtra(SshAuthenticationApi.EXTRA_KEY_ID); description = intent.getStringExtra(SshAuthenticationApi.EXTRA_KEY_DESCRIPTION); - settings.edit().putString("ssh_openkeystore_keyid", keyId).apply(); + settings.edit().putString(PreferenceKeys.SSH_OPENKEYSTORE_KEYID, keyId).apply(); } if (intent.hasExtra(SshAuthenticationApi.EXTRA_SSH_PUBLIC_KEY)) { @@ -284,10 +285,12 @@ public class SshApiSessionFactory extends JschConfigSessionFactory { * A Jsch identity that delegates key operations via the OpenKeychain SSH API */ public static class ApiIdentity implements Identity { - private String keyId, description, alg; - private byte[] publicKey; - private Activity callingActivity; - private SshAuthenticationApi api; + private final String keyId; + private final String description; + private final String alg; + private final byte[] publicKey; + private final Activity callingActivity; + private final SshAuthenticationApi api; private CountDownLatch latch; private byte[] signature; |
