From cdf45bc323bd21eeb6252c9d9b70a990f7b39b75 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 27 Oct 2019 00:14:42 +0530 Subject: Add setting to save OpenKeychain auth keyid (#554) * Add setting to save OpenKeychain auth keyid * Hide pref not disable Co-Authored-By: Reagan Sanders Signed-off-by: Harsh Shandilya --- .../pwdstore/git/config/SshApiSessionFactory.java | 40 +++++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/git/config') 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 ba807979..39a31991 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 @@ -8,7 +8,9 @@ import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.content.IntentSender; +import android.content.SharedPreferences; import androidx.annotation.NonNull; +import androidx.preference.PreferenceManager; import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.jcraft.jsch.Identity; import com.jcraft.jsch.JSch; @@ -16,6 +18,7 @@ import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.UserInfo; import com.zeapo.pwdstore.R; +import com.zeapo.pwdstore.git.GitActivity; import java.util.List; import java.util.concurrent.CountDownLatch; import org.eclipse.jgit.errors.UnsupportedCredentialItem; @@ -104,7 +107,8 @@ public class SshApiSessionFactory extends GitConfigSessionFactory { private SshAuthenticationApi api; private String keyId, description, alg; private byte[] publicKey; - private Activity callingActivity; + private GitActivity callingActivity; + private SharedPreferences settings; /** * Construct a new IdentityBuilder @@ -112,7 +116,7 @@ public class SshApiSessionFactory extends GitConfigSessionFactory { * @param callingActivity Activity that will be used to launch pending intents and that will * receive and handle the results. */ - public IdentityBuilder(Activity callingActivity) { + public IdentityBuilder(GitActivity callingActivity) { this.callingActivity = callingActivity; List providers = @@ -124,6 +128,11 @@ public class SshApiSessionFactory extends GitConfigSessionFactory { // TODO: Handle multiple available providers? Are there actually any in practice beyond // OpenKeychain? connection = new SshAuthenticationConnection(callingActivity, providers.get(0)); + + settings = + PreferenceManager.getDefaultSharedPreferences( + callingActivity.getApplicationContext()); + keyId = settings.getString("ssh_openkeystore_keyid", null); } /** Free any resources associated with this IdentityBuilder */ @@ -146,7 +155,24 @@ public class SshApiSessionFactory extends GitConfigSessionFactory { case SshAuthenticationApi.RESULT_CODE_ERROR: SshAuthenticationApiError error = result.getParcelableExtra(SshAuthenticationApi.EXTRA_ERROR); - throw new RuntimeException(error.getMessage()); + // On an OpenKeychain SSH API error, clear out the stored keyid + settings.edit().putString("ssh_openkeystore_keyid", null).apply(); + + switch (error.getError()) { + // If the problem was just a bad keyid, reset to allow them to choose a + // different one + case (SshAuthenticationApiError.NO_SUCH_KEY): + case (SshAuthenticationApiError.NO_AUTH_KEY): + keyId = null; + publicKey = null; + description = null; + alg = null; + return executeApi(new KeySelectionRequest(), requestCode); + + // Other errors are fatal + default: + throw new RuntimeException(error.getMessage()); + } case SshAuthenticationApi.RESULT_CODE_SUCCESS: break; case SshAuthenticationApi.RESULT_CODE_USER_INTERACTION_REQUIRED: @@ -181,6 +207,7 @@ public class SshApiSessionFactory extends GitConfigSessionFactory { 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(); } if (intent.hasExtra(SshAuthenticationApi.EXTRA_SSH_PUBLIC_KEY)) { @@ -209,7 +236,8 @@ public class SshApiSessionFactory extends GitConfigSessionFactory { // We can immediately try the next phase without needing to post // back // though onActivityResult - tryBuild(requestCode); + callingActivity.onActivityResult( + requestCode, Activity.RESULT_OK, null); } @Override @@ -342,7 +370,9 @@ public class SshApiSessionFactory extends GitConfigSessionFactory { */ public void postSignature(Intent data) { try { - signature = handleSignResult(data); + if (data != null) { + signature = handleSignResult(data); + } } finally { if (latch != null) latch.countDown(); } -- cgit v1.2.3