From 42f1abfa76f06ee6d0a0e3bd5cc13b4afb1922a4 Mon Sep 17 00:00:00 2001 From: Zeapo Date: Fri, 8 Aug 2014 18:46:35 +0100 Subject: more refactoring and ability to pull from server --- app/src/main/AndroidManifest.xml | 2 +- .../main/java/com/zeapo/pwdstore/GitAsyncTask.java | 52 +++ app/src/main/java/com/zeapo/pwdstore/GitClone.java | 385 ------------------- .../main/java/com/zeapo/pwdstore/GitHandler.java | 417 +++++++++++++++++++++ .../java/com/zeapo/pwdstore/PasswordStore.java | 52 ++- .../java/com/zeapo/pwdstore/crypto/PgpHandler.java | 24 +- app/src/main/res/layout/activity_git_clone.xml | 2 +- app/src/main/res/menu/git_clone.xml | 2 +- app/src/main/res/menu/pwdstore.xml | 7 +- app/src/main/res/xml/preference.xml | 6 + 10 files changed, 532 insertions(+), 417 deletions(-) create mode 100644 app/src/main/java/com/zeapo/pwdstore/GitAsyncTask.java delete mode 100644 app/src/main/java/com/zeapo/pwdstore/GitClone.java create mode 100644 app/src/main/java/com/zeapo/pwdstore/GitHandler.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 822ad8cd..815febe3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,7 +17,7 @@ { + private Activity activity; + private boolean finishOnEnd; + public GitAsyncTask(Activity activity, boolean finishOnEnd) { + this.activity = activity; + this.finishOnEnd = finishOnEnd; + } + @Override + protected Integer doInBackground(GitCommand... cmd) { + int count = cmd.length; + Integer totalSize = 0; + for (int i = 0; i < count; i++) { + try { + cmd[i].call(); + } catch (JGitInternalException e) { + e.printStackTrace(); + return -99; + } catch (InvalidRemoteException e) { + e.printStackTrace(); + return -1; + } catch (TransportException e) { + e.printStackTrace(); + return -2; + } catch (Exception e) { + e.printStackTrace(); + } + totalSize++; + } + return totalSize; + } + + protected void onPostExecute(Integer result) { + Log.i("GIT_ASYNC", result + ""); + if (finishOnEnd) { + this.activity.finish(); + } + } +} diff --git a/app/src/main/java/com/zeapo/pwdstore/GitClone.java b/app/src/main/java/com/zeapo/pwdstore/GitClone.java deleted file mode 100644 index 7e192e25..00000000 --- a/app/src/main/java/com/zeapo/pwdstore/GitClone.java +++ /dev/null @@ -1,385 +0,0 @@ -package com.zeapo.pwdstore; - -import android.app.Activity; -import android.app.AlertDialog; -import android.app.ProgressDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.os.AsyncTask; -import android.os.Bundle; -import android.text.InputType; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.Button; -import android.widget.EditText; -import android.widget.LinearLayout; -import android.widget.Spinner; -import android.widget.TextView; - -import com.jcraft.jsch.JSch; -import com.jcraft.jsch.JSchException; -import com.jcraft.jsch.Session; - -import org.eclipse.jgit.api.CloneCommand; -import org.eclipse.jgit.api.Git; - -import org.apache.commons.io.FileUtils; -import org.eclipse.jgit.api.errors.InvalidRemoteException; -import org.eclipse.jgit.api.errors.JGitInternalException; -import org.eclipse.jgit.api.errors.TransportException; -import org.eclipse.jgit.errors.NotSupportedException; -import org.eclipse.jgit.internal.storage.file.FileRepository; -import org.eclipse.jgit.lib.Constants; -import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.storage.file.FileRepositoryBuilder; -import org.eclipse.jgit.transport.JschConfigSessionFactory; -import org.eclipse.jgit.transport.OpenSshConfig; -import org.eclipse.jgit.transport.RefSpec; -import org.eclipse.jgit.transport.RemoteConfig; -import org.eclipse.jgit.transport.SshSessionFactory; -import org.eclipse.jgit.transport.Transport; -import org.eclipse.jgit.transport.URIish; -import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; -import org.eclipse.jgit.util.FS; - -import java.io.File; -import java.io.IOException; -import java.net.InetAddress; -import java.net.URI; -import java.net.URL; -import java.net.UnknownHostException; - -// TODO move the messages to strings.xml - -public class GitClone extends Activity { - - private Activity activity; - private Context context; - - private String protocol; - private String connectionMode; - - private File localDir; - private String hostname; - private String username; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_git_clone); - - context = getApplicationContext(); - activity = this; - - // init the spinner for protocols - Spinner protcol_spinner = (Spinner) findViewById(R.id.clone_protocol); - ArrayAdapter protocol_adapter = ArrayAdapter.createFromResource(this, - R.array.clone_protocols, android.R.layout.simple_spinner_item); - protocol_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - protcol_spinner.setAdapter(protocol_adapter); - protcol_spinner.setOnItemSelectedListener( - new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView adapterView, View view, int i, long l) { - protocol = ((Spinner)findViewById(R.id.clone_protocol)).getSelectedItem().toString(); - if (protocol.equals("ssh://")) { - ((EditText)findViewById(R.id.clone_uri)).setHint("user@hostname:path"); - } else { - ((EditText)findViewById(R.id.clone_uri)).setHint("hostname/path"); - new AlertDialog.Builder(activity). - setMessage("You are about to use a read-only repository, you will not be able to push to it"). - setCancelable(true). - setPositiveButton("OK", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - - } - }).show(); - } - } - - @Override - public void onNothingSelected(AdapterView adapterView) { - - } - } - ); - - // init the spinner for connection modes - Spinner connection_mode_spinner = (Spinner) findViewById(R.id.connection_mode); - ArrayAdapter connection_mode_adapter = ArrayAdapter.createFromResource(this, - R.array.connection_modes, android.R.layout.simple_spinner_item); - connection_mode_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - connection_mode_spinner.setAdapter(connection_mode_adapter); - connection_mode_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView adapterView, View view, int i, long l) { - String selection = ((Spinner) findViewById(R.id.connection_mode)).getSelectedItem().toString(); - - if (selection.equalsIgnoreCase("ssh-key")) { - new AlertDialog.Builder(activity) - .setMessage("Authentication method not implemented yet") - .setPositiveButton("OK", - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - } - } - ).show(); - ((Button) findViewById(R.id.clone_button)).setEnabled(false); - } else { - ((Button) findViewById(R.id.clone_button)).setEnabled(true); - } - connectionMode = selection; - } - - @Override - public void onNothingSelected(AdapterView adapterView) { - - } - }); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.git_clone, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - if (id == R.id.action_settings) { - return true; - } - return super.onOptionsItemSelected(item); - } - - /* The clone process has to be on a different thread than the main one */ - private class CloneTask extends AsyncTask { - private ProgressDialog dialog; - - public CloneTask(Activity activity) { - context = activity; - dialog = new ProgressDialog(context); - } - - protected void onPreExecute() { - this.dialog.setMessage("Cloning..."); - this.dialog.setCancelable(false); - this.dialog.show(); - } - - protected void onPostExecute(Integer result) { - switch (result) { - case -1: - new AlertDialog.Builder(activity). - setTitle("Please check that the repository path is correct."). - setMessage("Did you forget to specify the path after the hostname?"). - setPositiveButton("OK", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - - } - }).show(); - break; - case -2: - new AlertDialog.Builder(activity). - setTitle("Communication error"). - setMessage("JGit said that the server didn't like our request. Either an authentication issue or the host is not reachable. Check the debug messages."). - setPositiveButton("OK", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - - } - }).show(); - break; - case -99: - new AlertDialog.Builder(activity). - setTitle("JGit raised an internal exception"). - setMessage("OUPS, JGit didn't like what you did... Check that you provided it with a correct URI. Check also debug messages."). - setPositiveButton("OK", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - - } - }).show(); - default: - this.dialog.dismiss(); - setResult(RESULT_OK); - finish(); - return; - } - this.dialog.dismiss(); - } - - - protected Integer doInBackground(CloneCommand... cmd) { - int count = cmd.length; - Integer totalSize = 0; - for (int i = 0; i < count; i++) { - try { - cmd[i].call(); - } catch (JGitInternalException e) { - return -99; - } catch (InvalidRemoteException e) { - return -1; - } catch (TransportException e) { - return -2; - } catch (Exception e) { - e.printStackTrace(); - } - totalSize++; - } - return totalSize; - } - } - - protected class GitConfigSessionFactory extends JschConfigSessionFactory { - - public void configure(OpenSshConfig.Host hc, Session session) { - session.setConfig("StrictHostKeyChecking", "no"); - } - - @Override - protected JSch - getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException { - JSch jsch = super.getJSch(hc, fs); - jsch.removeAllIdentity(); - return jsch; - } - } - - - public void cloneRepository(View view) { - localDir = new File(getApplicationContext().getFilesDir().getAbsoluteFile() + "/store"); - - hostname = ((TextView) findViewById(R.id.clone_uri)).getText().toString(); - // don't ask the user, take off the protocol that he puts in - hostname = hostname.replaceFirst("^.+://", ""); - ((TextView) findViewById(R.id.clone_uri)).setText(hostname); - - // now cheat a little and prepend the real protocol - // jGit does not accept a ssh:// but requires https:// - if (!protocol.equals("ssh://")) { - hostname = protocol + hostname; - } else { - // did he forget the username? - if (!hostname.matches("^.+@.+")) { - new AlertDialog.Builder(this). - setMessage("Did you forget to specify a username?"). - setPositiveButton("Oups...", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - - } - }). - show(); - return; - } - - username = hostname.split("@")[0]; - } - - - if (localDir.exists()) { - new AlertDialog.Builder(this). - setTitle(R.string.dialog_delete_title). - setMessage(R.string.dialog_delete_msg). - setCancelable(false). - setPositiveButton(R.string.dialog_delete, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - try { - FileUtils.deleteDirectory(localDir); - authenticateThenClone(localDir); - } catch (IOException e) { - //TODO Handle the exception correctly if we are unable to delete the directory... - e.printStackTrace(); - } catch (Exception e) { - //This is what happens when jgit fails :( - //TODO Handle the diffent cases of exceptions - } - - dialog.cancel(); - } - } - ). - setNegativeButton(R.string.dialog_do_not_delete, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - } - } - ). - show(); - } else { - try { - authenticateThenClone(localDir); - } catch (Exception e) { - //This is what happens when jgit fails :( - //TODO Handle the diffent cases of exceptions - e.printStackTrace(); - } - } - } - - - private void authenticateThenClone(final File localDir) { - String connectionMode = ((Spinner) findViewById(R.id.connection_mode)).getSelectedItem().toString(); - - if (connectionMode.equalsIgnoreCase("ssh-key")) { - - } else { - if (protocol.equals("ssh://")) { - - final EditText password = new EditText(activity); - password.setHint("Password"); - password.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); - password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); - - new AlertDialog.Builder(activity) - .setTitle("Authenticate") - .setMessage("Please provide the password for this repository") - .setView(password) - .setPositiveButton("OK", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - - SshSessionFactory.setInstance(new GitConfigSessionFactory()); - - CloneCommand cmd = Git.cloneRepository(). - setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password.getText().toString())). - setCloneAllBranches(true). - setDirectory(localDir). - setURI(hostname); - - new CloneTask(activity).execute(cmd); - } - }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - // Do nothing. - } - }).show(); - } else { - CloneCommand cmd = Git.cloneRepository() - .setDirectory(localDir) - .setURI(hostname) - .setBare(false) - .setNoCheckout(false) - .setCloneAllBranches(true); - - new CloneTask(activity).execute(cmd); - } - } - } - - - -} diff --git a/app/src/main/java/com/zeapo/pwdstore/GitHandler.java b/app/src/main/java/com/zeapo/pwdstore/GitHandler.java new file mode 100644 index 00000000..459a8c39 --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/GitHandler.java @@ -0,0 +1,417 @@ +package com.zeapo.pwdstore; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.ProgressDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.InputType; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.Spinner; +import android.widget.TextView; + +import com.jcraft.jsch.JSch; +import com.jcraft.jsch.JSchException; +import com.jcraft.jsch.Session; +import com.zeapo.pwdstore.utils.PasswordRepository; + +import org.eclipse.jgit.api.CloneCommand; +import org.eclipse.jgit.api.Git; + +import org.apache.commons.io.FileUtils; +import org.eclipse.jgit.api.errors.InvalidRemoteException; +import org.eclipse.jgit.api.errors.JGitInternalException; +import org.eclipse.jgit.api.errors.TransportException; +import org.eclipse.jgit.transport.JschConfigSessionFactory; +import org.eclipse.jgit.transport.OpenSshConfig; +import org.eclipse.jgit.transport.SshSessionFactory; +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; +import org.eclipse.jgit.util.FS; + +import java.io.File; +import java.io.IOException; + +// TODO move the messages to strings.xml + +public class GitHandler extends Activity { + + private Activity activity; + private Context context; + + private String protocol; + private String connectionMode; + + private File localDir; + private String hostname; + private String username; + + public static final int REQUEST_PULL = 101; + public static final int REQUEST_PUSH = 102; + public static final int REQUEST_CLONE = 103; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + context = getApplicationContext(); + activity = this; + + switch (getIntent().getExtras().getInt("Operation")) { + case REQUEST_CLONE: + setContentView(R.layout.activity_git_clone); + + // init the spinner for protocols + Spinner protcol_spinner = (Spinner) findViewById(R.id.clone_protocol); + ArrayAdapter protocol_adapter = ArrayAdapter.createFromResource(this, + R.array.clone_protocols, android.R.layout.simple_spinner_item); + protocol_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + protcol_spinner.setAdapter(protocol_adapter); + protcol_spinner.setOnItemSelectedListener( + new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView adapterView, View view, int i, long l) { + protocol = ((Spinner)findViewById(R.id.clone_protocol)).getSelectedItem().toString(); + if (protocol.equals("ssh://")) { + ((EditText)findViewById(R.id.clone_uri)).setHint("user@hostname:path"); + } else { + ((EditText)findViewById(R.id.clone_uri)).setHint("hostname/path"); + new AlertDialog.Builder(activity). + setMessage("You are about to use a read-only repository, you will not be able to push to it"). + setCancelable(true). + setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + + } + }).show(); + } + } + + @Override + public void onNothingSelected(AdapterView adapterView) { + + } + } + ); + + // init the spinner for connection modes + Spinner connection_mode_spinner = (Spinner) findViewById(R.id.connection_mode); + ArrayAdapter connection_mode_adapter = ArrayAdapter.createFromResource(this, + R.array.connection_modes, android.R.layout.simple_spinner_item); + connection_mode_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + connection_mode_spinner.setAdapter(connection_mode_adapter); + connection_mode_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView adapterView, View view, int i, long l) { + String selection = ((Spinner) findViewById(R.id.connection_mode)).getSelectedItem().toString(); + + if (selection.equalsIgnoreCase("ssh-key")) { + new AlertDialog.Builder(activity) + .setMessage("Authentication method not implemented yet") + .setPositiveButton("OK", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + } + } + ).show(); + ((Button) findViewById(R.id.clone_button)).setEnabled(false); + } else { + ((Button) findViewById(R.id.clone_button)).setEnabled(true); + } + connectionMode = selection; + } + + @Override + public void onNothingSelected(AdapterView adapterView) { + + } + }); + break; + case REQUEST_PULL: + authenticateThenPull(this); + break; + } + + + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.git_clone, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + if (id == R.id.action_settings) { + return true; + } + return super.onOptionsItemSelected(item); + } + + /* The clone process has to be on a different thread than the main one */ + private class CloneTask extends AsyncTask { + private ProgressDialog dialog; + + public CloneTask(Activity activity) { + context = activity; + dialog = new ProgressDialog(context); + } + + protected void onPreExecute() { + this.dialog.setMessage("Cloning..."); + this.dialog.setCancelable(false); + this.dialog.show(); + } + + protected void onPostExecute(Integer result) { + switch (result) { + case -1: + new AlertDialog.Builder(activity). + setTitle("Please check that the repository path is correct."). + setMessage("Did you forget to specify the path after the hostname?"). + setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + + } + }).show(); + break; + case -2: + new AlertDialog.Builder(activity). + setTitle("Communication error"). + setMessage("JGit said that the server didn't like our request. Either an authentication issue or the host is not reachable. Check the debug messages."). + setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + + } + }).show(); + break; + case -99: + new AlertDialog.Builder(activity). + setTitle("JGit raised an internal exception"). + setMessage("OUPS, JGit didn't like what you did... Check that you provided it with a correct URI. Check also debug messages."). + setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + + } + }).show(); + default: + this.dialog.dismiss(); + setResult(RESULT_OK); + finish(); + return; + } + this.dialog.dismiss(); + } + + + protected Integer doInBackground(CloneCommand... cmd) { + int count = cmd.length; + Integer totalSize = 0; + for (int i = 0; i < count; i++) { + try { + cmd[i].call(); + } catch (JGitInternalException e) { + return -99; + } catch (InvalidRemoteException e) { + return -1; + } catch (TransportException e) { + return -2; + } catch (Exception e) { + e.printStackTrace(); + } + totalSize++; + } + return totalSize; + } + } + + protected class GitConfigSessionFactory extends JschConfigSessionFactory { + + public void configure(OpenSshConfig.Host hc, Session session) { + session.setConfig("StrictHostKeyChecking", "no"); + } + + @Override + protected JSch + getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException { + JSch jsch = super.getJSch(hc, fs); + jsch.removeAllIdentity(); + return jsch; + } + } + + + public void cloneRepository(View view) { + localDir = new File(getApplicationContext().getFilesDir().getAbsoluteFile() + "/store"); + + hostname = ((TextView) findViewById(R.id.clone_uri)).getText().toString(); + // don't ask the user, take off the protocol that he puts in + hostname = hostname.replaceFirst("^.+://", ""); + ((TextView) findViewById(R.id.clone_uri)).setText(hostname); + + // now cheat a little and prepend the real protocol + // jGit does not accept a ssh:// but requires https:// + if (!protocol.equals("ssh://")) { + hostname = protocol + hostname; + } else { + // did he forget the username? + if (!hostname.matches("^.+@.+")) { + new AlertDialog.Builder(this). + setMessage("Did you forget to specify a username?"). + setPositiveButton("Oups...", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + + } + }). + show(); + return; + } + + username = hostname.split("@")[0]; + } + + + if (localDir.exists()) { + new AlertDialog.Builder(this). + setTitle(R.string.dialog_delete_title). + setMessage(R.string.dialog_delete_msg). + setCancelable(false). + setPositiveButton(R.string.dialog_delete, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + try { + FileUtils.deleteDirectory(localDir); + authenticateThenClone(localDir); + } catch (IOException e) { + //TODO Handle the exception correctly if we are unable to delete the directory... + e.printStackTrace(); + } catch (Exception e) { + //This is what happens when jgit fails :( + //TODO Handle the diffent cases of exceptions + } + + dialog.cancel(); + } + } + ). + setNegativeButton(R.string.dialog_do_not_delete, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + } + } + ). + show(); + } else { + try { + authenticateThenClone(localDir); + } catch (Exception e) { + //This is what happens when jgit fails :( + //TODO Handle the diffent cases of exceptions + e.printStackTrace(); + } + } + } + + + private void authenticateThenClone(final File localDir) { + String connectionMode = ((Spinner) findViewById(R.id.connection_mode)).getSelectedItem().toString(); + + if (connectionMode.equalsIgnoreCase("ssh-key")) { + + } else { + if (protocol.equals("ssh://")) { + + final EditText password = new EditText(activity); + password.setHint("Password"); + password.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); + password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); + + new AlertDialog.Builder(activity) + .setTitle("Authenticate") + .setMessage("Please provide the password for this repository") + .setView(password) + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + + SshSessionFactory.setInstance(new GitConfigSessionFactory()); + + CloneCommand cmd = Git.cloneRepository(). + setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password.getText().toString())). + setCloneAllBranches(true). + setDirectory(localDir). + setURI(hostname); + + new CloneTask(activity).execute(cmd); + } + }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + // Do nothing. + } + }).show(); + } else { + CloneCommand cmd = Git.cloneRepository() + .setDirectory(localDir) + .setURI(hostname) + .setBare(false) + .setNoCheckout(false) + .setCloneAllBranches(true); + + new CloneTask(activity).execute(cmd); + } + } + } + + private void authenticateThenPull(final Activity activity) { + //TODO recall the username + //TODO offer the choice ssh and user/pwd + final EditText password = new EditText(activity); + password.setHint("Password"); + password.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); + password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); + + new AlertDialog.Builder(activity) + .setTitle("Authenticate") + .setMessage("Please provide the password for this repository") + .setView(password) + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + + SshSessionFactory.setInstance(new GitConfigSessionFactory()); + + new GitAsyncTask(activity, true).execute(new Git(PasswordRepository.getRepository(new File(""))) + .pull() + .setRebase(true) + .setCredentialsProvider(new UsernamePasswordCredentialsProvider("git", password.getText().toString()))); + + + } + }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + // Do nothing. + } + }).show(); + } + + +} diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java index 75437fa6..4868c9a5 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java @@ -1,26 +1,35 @@ package com.zeapo.pwdstore; -import android.app.ActionBar; import android.app.Activity; +import android.app.AlertDialog; import android.app.FragmentManager; import android.app.FragmentTransaction; -import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; -import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.os.Bundle; +import android.text.InputType; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.view.WindowManager; -import android.widget.PopupWindow; +import android.widget.EditText; +import android.widget.LinearLayout; +import com.jcraft.jsch.JSch; +import com.jcraft.jsch.JSchException; +import com.jcraft.jsch.Session; import com.zeapo.pwdstore.crypto.PgpHandler; import com.zeapo.pwdstore.utils.PasswordItem; import com.zeapo.pwdstore.utils.PasswordRepository; import org.apache.commons.io.FileUtils; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.transport.JschConfigSessionFactory; +import org.eclipse.jgit.transport.OpenSshConfig; +import org.eclipse.jgit.transport.SshSessionFactory; +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; +import org.eclipse.jgit.util.FS; import java.io.File; import java.io.IOException; @@ -85,9 +94,19 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI createPassword(getCurrentFocus()); break; - case R.id.menu_add_category: +// case R.id.menu_add_category: +// break; + + case R.id.git_push: break; + case R.id.git_pull: + Intent intent = new Intent(this, GitHandler.class); + intent.putExtra("Operation", GitHandler.REQUEST_PULL); + startActivity(intent); + this.leftActivity = true; + return true; + case R.id.referesh: refreshListAdapter(); return true; @@ -100,7 +119,8 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI } public void getClone(View view){ - Intent intent = new Intent(this, GitClone.class); + Intent intent = new Intent(this, GitHandler.class); + intent.putExtra("Operation", GitHandler.REQUEST_CLONE); startActivity(intent); } @@ -183,7 +203,7 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI intent.putExtra("NAME", item.toString()); intent.putExtra("FILE_PATH", item.getFile().getAbsolutePath()); intent.putExtra("Operation", "DECRYPT"); - startActivityForResult(intent, 0); + startActivityForResult(intent, PgpHandler.REQUEST_CODE_DECRYPT_AND_VERIFY); } catch (IOException e) { e.printStackTrace(); @@ -207,8 +227,7 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI intent.putExtra("PGP-ID", FileUtils.readFileToString(PasswordRepository.getFile("/.gpg-id"))); intent.putExtra("FILE_PATH", this.currentDir.getAbsolutePath()); intent.putExtra("Operation", "ENCRYPT"); - // TODO Define different operations here - startActivityForResult(intent, 0); + startActivityForResult(intent, PgpHandler.REQUEST_CODE_ENCRYPT); } catch (Exception e) { e.printStackTrace(); } @@ -231,10 +250,17 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI if (resultCode == RESULT_OK) { refreshListAdapter(); - // do not froget to commit the file - if (requestCode == PgpHandler.REQUEST_CODE_ENCRYPT) { - + switch (requestCode) { + case PgpHandler.REQUEST_CODE_ENCRYPT : + Git git = new Git(PasswordRepository.getRepository(new File(""))); + GitAsyncTask tasks = new GitAsyncTask(this, false); + tasks.execute( + git.add().addFilepattern(data.getExtras().getString("CREATED_FILE")), + git.commit().setMessage("Added " + data.getExtras().getString("NAME")) + ); + break; } + } } } diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java index b3caea49..4cde1453 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java @@ -294,20 +294,15 @@ public class PgpHandler extends Activity { // encrypt if (requestCode == REQUEST_CODE_ENCRYPT && os != null) { try { - Log.d(OpenPgpApi.TAG, "result: " + os.toByteArray().length - + " str=" + os.toString("UTF-8")); - - if (returnToCiphertextField) { - String path = getIntent().getExtras().getString("FILE_PATH") - + "/" + ((EditText) findViewById(R.id.crypto_password_file_edit)).getText().toString() - + ".gpg"; - OutputStream outputStream = FileUtils.openOutputStream(new File(path)); - outputStream.write(os.toByteArray()); - } else { - showToast(os.toString()); - } - - setResult(RESULT_OK); + String path = getIntent().getExtras().getString("FILE_PATH") + + "/" + ((EditText) findViewById(R.id.crypto_password_file_edit)).getText().toString() + + ".gpg"; + OutputStream outputStream = FileUtils.openOutputStream(new File(path)); + outputStream.write(os.toByteArray()); + Intent data = new Intent(); + data.putExtra("CREATED_FILE", path); + data.putExtra("NAME", ((EditText) findViewById(R.id.crypto_password_file_edit)).getText().toString()); + setResult(RESULT_OK, data); finish(); } catch (Exception e) { Log.e(Constants.TAG, "UnsupportedEncodingException", e); @@ -406,7 +401,6 @@ public class PgpHandler extends Activity { data.setAction(OpenPgpApi.ACTION_ENCRYPT); data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[]{accountName}); data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); - Log.i("BABABOU", settings.getString("openpgpg_key_ids", "") + ""); String name = ((EditText) findViewById(R.id.crypto_password_file_edit)).getText().toString(); String pass = ((EditText) findViewById(R.id.crypto_password_edit)).getText().toString(); diff --git a/app/src/main/res/layout/activity_git_clone.xml b/app/src/main/res/layout/activity_git_clone.xml index 8c0a3336..d4ccbc8a 100644 --- a/app/src/main/res/layout/activity_git_clone.xml +++ b/app/src/main/res/layout/activity_git_clone.xml @@ -6,7 +6,7 @@ android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" - tools:context="com.zeapo.pwdstore.GitClone"> + tools:context="com.zeapo.pwdstore.GitHandler"> + tools:context="com.zeapo.pwdstore.GitHandler" > + + + diff --git a/app/src/main/res/xml/preference.xml b/app/src/main/res/xml/preference.xml index d73dd5ec..45497dbe 100644 --- a/app/src/main/res/xml/preference.xml +++ b/app/src/main/res/xml/preference.xml @@ -1,5 +1,11 @@ + + + +