Git: Add a method to generate the SSH keys

This commit is contained in:
Vishesh Handa
2019-01-07 12:10:09 +01:00
parent 0ae2dccda5
commit ae6158a0a8
4 changed files with 99 additions and 89 deletions

View File

@ -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 {

View File

@ -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<String, Void, Void> {
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;
}
}

View File

@ -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());
}
}
*/
}

View File

@ -21,9 +21,9 @@ class GitApp extends StatelessWidget {
buildGitButtons() {
return <Widget>[
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}'.");
}
}