From ae6158a0a839a43f9b9d1175e6c67a39ebac8c4d Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 7 Jan 2019 12:10:09 +0100 Subject: [PATCH] Git: Add a method to generate the SSH keys --- android/app/build.gradle | 3 + .../example/journal/GenerateSSHKeysTask.java | 70 ++++++++++++++ .../com/example/journal/MainActivity.java | 94 ++----------------- lib/gitapp.dart | 21 ++++- 4 files changed, 99 insertions(+), 89 deletions(-) create mode 100644 android/app/src/main/java/com/example/journal/GenerateSSHKeysTask.java diff --git a/android/app/build.gradle b/android/app/build.gradle index db08198a..faf5c85d 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -51,6 +51,9 @@ dependencies { // https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '4.4.1.201607150455-r' + + // For reading a file to string + implementation 'commons-io:commons-io:2.5' } configurations { diff --git a/android/app/src/main/java/com/example/journal/GenerateSSHKeysTask.java b/android/app/src/main/java/com/example/journal/GenerateSSHKeysTask.java new file mode 100644 index 00000000..50e4ee96 --- /dev/null +++ b/android/app/src/main/java/com/example/journal/GenerateSSHKeysTask.java @@ -0,0 +1,70 @@ +package com.example.journal; + +import android.os.AsyncTask; +import android.util.Log; + +import com.jcraft.jsch.*; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; + +import org.apache.commons.io.FileUtils; + +import io.flutter.plugin.common.MethodChannel.Result; + +public class GenerateSSHKeysTask extends AsyncTask { + private Result result; + + public GenerateSSHKeysTask(Result _result) { + result = _result; + } + + protected Void doInBackground(String... params) { + String keysDirPath = params[0]; + File keysDir = new File(keysDirPath); + if (!keysDir.exists()) { + keysDir.mkdir(); + } + + final String privateKeyPath = keysDir + "/id_rsa"; + final String publicKeyPath = keysDir + "/id_rsa.pub"; + + File privateKeyFile = new File(privateKeyPath); + if (privateKeyFile.exists()) { + Log.d("GenerateSSHKeys", "Private key already exists"); + result.error("FAILED", "Private key already exists", null); + return null; + } + + // Generate key pair + try { + JSch jsch = new JSch(); + KeyPair kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA, 1024 * 4); + + kpair.writePrivateKey(privateKeyPath); + kpair.writePublicKey(publicKeyPath, "Auto generated Key"); + kpair.dispose(); + } catch (JSchException ex) { + Log.d("GenerateSSHKeys", ex.toString()); + result.error("FAILED", ex.toString(), null); + return null; + } catch (IOException ex) { + Log.d("GenerateSSHKeys", ex.toString()); + result.error("FAILED", ex.toString(), null); + return null; + } + + String publicKey; + try { + publicKey = FileUtils.readFileToString(new File(publicKeyPath), Charset.defaultCharset()); + } catch (IOException ex) { + Log.d("GenerateSSHKeys", ex.toString()); + result.error("FAILED", "Failed to read the public key", null); + return null; + } + + result.success(publicKey); + return null; + } +} diff --git a/android/app/src/main/java/com/example/journal/MainActivity.java b/android/app/src/main/java/com/example/journal/MainActivity.java index 047f4ea2..e6510c0a 100644 --- a/android/app/src/main/java/com/example/journal/MainActivity.java +++ b/android/app/src/main/java/com/example/journal/MainActivity.java @@ -21,7 +21,7 @@ import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; public class MainActivity extends FlutterActivity { - private static final String CHANNEL = "samples.flutter.io/battery"; + private static final String CHANNEL = "gitjournal.io/git"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -56,93 +56,17 @@ public class MainActivity extends FlutterActivity { return; } - result.notImplemented(); + if (call.method.equals("generateSSHKeys")) { + String appFilesDir = PathUtils.getFilesDir(getApplicationContext()); + String sshKeysLocation = appFilesDir + "/ssh"; - // Methods to add - // git clone - // git pull - merge by taking newest - // git add - // git commit - // git push + new GenerateSSHKeysTask(result).execute(sshKeysLocation); + return; + } + + result.notImplemented(); } }); } - private boolean gitClone(String url, String filePath) { - // TODO: Progress - // TODO: Credentials - // TODO: Handle errors! - - File directory = new File("/git"); - - try { - Git git = Git.cloneRepository() - .setURI(url) - .setDirectory(directory) - .call(); - return true; - } - catch (GitAPIException e) { - System.err.println("Error Cloning repository " + url + " : "+ e.getMessage()); - return false; - } - } - - /* - private void gitAdd(String gitRootUrl, String gitFileUrl) { - File directory = new File(gitRootUrl); - - try { - Git git = Git.open(directory); - - git.add() - .addFilepattern(gitFileUrl) - .call(); - } - catch (GitAPIException e) { - System.err.println("Error Cloning repository " + url + " : "+ e.getMessage()); - } - catch (java.io.IOException e) { - System.err.println("Error Opening GitRepo " + gitRootUrl + " : "+ e.getMessage()); - } - } - - private void gitRemove(String gitRootUrl, String gitFileUrl) { - File directory = new File(gitRootUrl); - - try { - Git git = Git.open(directory); - - git.rm() - .addFilepattern(gitFileUrl) - .call(); - } - catch (GitAPIException e) { - System.err.println("Error Cloning repository " + url + " : "+ e.getMessage()); - } - catch (java.io.IOException e) { - System.err.println("Error Opening GitRepo " + gitRootUrl + " : "+ e.getMessage()); - } - } - - private void gitCommit(String gitRootUrl, String message) { - File directory = new File(gitRootUrl); - - try { - Git git = Git.open(directory); - - git.commit() - .setAuthor("JournalApp", "none@example.com") - .setMessage(message) - .call(); - } - catch (GitAPIException e) { - System.err.println("Error Cloning repository " + url + " : "+ e.getMessage()); - } - catch (java.io.IOException e) { - System.err.println("Error Opening GitRepo " + gitRootUrl + " : "+ e.getMessage()); - } - } - */ - } diff --git a/lib/gitapp.dart b/lib/gitapp.dart index 0e89b127..6d5ce62a 100644 --- a/lib/gitapp.dart +++ b/lib/gitapp.dart @@ -21,9 +21,9 @@ class GitApp extends StatelessWidget { buildGitButtons() { return [ RaisedButton( - child: Text("Remove Directory"), - onPressed: () { - print("FOO"); + child: Text("Generate Keys"), + onPressed: () async { + await generateSSHKeys(); }, ), RaisedButton( @@ -42,7 +42,7 @@ buildGitButtons() { } Future gitClone() async { - const platform = const MethodChannel('samples.flutter.io/battery'); + const platform = const MethodChannel('gitjournal.io/git'); print("Going to git clone"); await platform.invokeMethod('gitClone', { @@ -51,3 +51,16 @@ Future gitClone() async { }); print("FOO"); } + +Future generateSSHKeys() async { + print("generateSSHKeys"); + try { + const platform = const MethodChannel('gitjournal.io/git'); + String publicKey = await platform.invokeMethod('generateSSHKeys', {}); + print("Public Key " + publicKey); + } on PlatformException catch (e) { + print("Failed to generateSSHKeys: '${e.message}'."); + } catch (e) { + print("EX: '${e.message}'."); + } +}