mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 02:07:39 +08:00
Android GitClone: Support SSH keys
Generate an SSH key if it doesn't exist and then use that one. This was just supposed to be a PoC. Sadly, it doesn't really work. I keep getting Auth Failures when cloning. Not really sure what the problem is.
This commit is contained in:
@ -1,11 +1,26 @@
|
||||
package com.example.journal;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.api.CloneCommand;
|
||||
import org.eclipse.jgit.api.TransportConfigCallback;
|
||||
import org.eclipse.jgit.api.errors.TransportException;
|
||||
import org.eclipse.jgit.transport.Transport;
|
||||
import org.eclipse.jgit.transport.SshTransport;
|
||||
|
||||
import org.eclipse.jgit.transport.JschConfigSessionFactory;
|
||||
import org.eclipse.jgit.transport.SshSessionFactory;
|
||||
import org.eclipse.jgit.transport.OpenSshConfig.Host;
|
||||
import org.eclipse.jgit.util.FS;
|
||||
|
||||
import com.jcraft.jsch.Session;
|
||||
import com.jcraft.jsch.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import io.flutter.plugin.common.MethodChannel.Result;
|
||||
|
||||
@ -21,14 +36,90 @@ public class GitCloneTask extends AsyncTask<String, Void, Void> {
|
||||
String filesDir = params[1];
|
||||
File directory = new File(filesDir + "/git");
|
||||
|
||||
try {
|
||||
Git git = Git.cloneRepository()
|
||||
.setURI(url)
|
||||
.setDirectory(directory)
|
||||
.call();
|
||||
Log.d("GitClone Directory", filesDir);
|
||||
|
||||
File keysDir = new File(filesDir + "/keys");
|
||||
if (!keysDir.exists()) {
|
||||
keysDir.mkdir();
|
||||
}
|
||||
catch (GitAPIException e) {
|
||||
final String privateKeyPath = filesDir + "/keys/id_rsa";
|
||||
final String publicKeyPath = filesDir + "/keys/id_rsa.pub";
|
||||
|
||||
try {
|
||||
// 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("GitClone", ex.toString());
|
||||
} catch (IOException ex) {
|
||||
Log.d("GitClone", ex.toString());
|
||||
}
|
||||
|
||||
final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
|
||||
protected void configure(Host host, Session session) {
|
||||
session.setConfig("StrictHostKeyChecking", "no");
|
||||
}
|
||||
|
||||
protected JSch createDefaultJSch(FS fs) throws JSchException {
|
||||
|
||||
class MyLogger implements com.jcraft.jsch.Logger {
|
||||
java.util.Hashtable name;
|
||||
|
||||
MyLogger() {
|
||||
name = new java.util.Hashtable();
|
||||
name.put(new Integer(DEBUG), "DEBUG: ");
|
||||
name.put(new Integer(INFO), "INFO: ");
|
||||
name.put(new Integer(WARN), "WARN: ");
|
||||
name.put(new Integer(ERROR), "ERROR: ");
|
||||
name.put(new Integer(FATAL), "FATAL: ");
|
||||
}
|
||||
|
||||
|
||||
public boolean isEnabled(int level) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void log(int level, String message) {
|
||||
System.err.print(name.get(new Integer(level)));
|
||||
System.err.println(message);
|
||||
}
|
||||
}
|
||||
JSch.setLogger(new MyLogger());
|
||||
|
||||
JSch defaultJSch = super.createDefaultJSch(fs);
|
||||
defaultJSch.addIdentity(privateKeyPath);
|
||||
|
||||
Log.d("identityNames", defaultJSch.getIdentityNames().toString());
|
||||
return defaultJSch;
|
||||
}
|
||||
};
|
||||
|
||||
CloneCommand cloneCommand = Git.cloneRepository()
|
||||
.setURI(url)
|
||||
.setDirectory(directory);
|
||||
|
||||
cloneCommand.setTransportConfigCallback(new TransportConfigCallback() {
|
||||
@Override
|
||||
public void configure(Transport transport) {
|
||||
SshTransport sshTransport = (SshTransport) transport;
|
||||
sshTransport.setSshSessionFactory(sshSessionFactory);
|
||||
}
|
||||
});
|
||||
|
||||
cloneCommand.call();
|
||||
} catch (TransportException e) {
|
||||
// FIXME: Return a better error message?
|
||||
System.err.println("Transport Error Cloning repository " + url + " : " + e.getMessage());
|
||||
return null;
|
||||
|
||||
} catch (GitAPIException e) {
|
||||
System.err.println("Error Cloning repository " + url + " : " + e.getMessage());
|
||||
Log.d("gitClone", e.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
Reference in New Issue
Block a user