From fd9e958d40a56a2a3b8dfbef86944d3d5f53fec2 Mon Sep 17 00:00:00 2001 From: Mohamed Zenadi Date: Sat, 12 Nov 2016 18:38:45 +0100 Subject: detect wrong ssh-key passphrase --- .../java/com/zeapo/pwdstore/git/GitOperation.java | 29 +++++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'app') 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 1ff4888e..8ec13593 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java @@ -88,7 +88,20 @@ public abstract class GitOperation { * @param sshKey the ssh-key file * @throws Exception */ - public void executeAfterAuthentication(String connectionMode, final String username, @Nullable final File sshKey) throws Exception { + public void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey) throws Exception { + executeAfterAuthentication(connectionMode, username, sshKey, false); + } + + /** + * Executes the GitCommand in an async task after creating the authentication + * + * @param connectionMode the server-connection mode + * @param username the username + * @param sshKey the ssh-key file + * @param showError show the passphrase edit text in red + * @throws Exception + */ + private void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey, final boolean showError) throws Exception { if (connectionMode.equalsIgnoreCase("ssh-key")) { if (sshKey == null || !sshKey.exists()) { new AlertDialog.Builder(callingActivity) @@ -135,8 +148,11 @@ public abstract class GitOperation { passphrase.setHint("Passphrase"); passphrase.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); passphrase.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); + if (showError) { + passphrase.setError("Wrong passphrase"); + } JSch jsch = new JSch(); - KeyPair keyPair = KeyPair.load(jsch, callingActivity.getFilesDir() + "/.ssh_key"); + 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)) @@ -145,8 +161,13 @@ public abstract class GitOperation { .setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { try { - // Authenticate using the ssh-key and then execute the command - setAuthentication(sshKey, username, passphrase.getText().toString()).execute(); + 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); + } } catch (Exception e) { e.printStackTrace(); } -- cgit v1.2.3