Convert Git and SSH config factories to Kotlin

Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
This commit is contained in:
Harsh Shandilya 2019-05-31 13:23:37 +05:30
parent f05c0b37ca
commit 68d20c5f2f
No known key found for this signature in database
GPG key ID: C2E74282C2133D62
4 changed files with 76 additions and 90 deletions

View file

@ -1,22 +0,0 @@
package com.zeapo.pwdstore.git.config;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.util.FS;
public class GitConfigSessionFactory extends JschConfigSessionFactory {
protected 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;
}
}

View file

@ -0,0 +1,22 @@
package com.zeapo.pwdstore.git.config
import com.jcraft.jsch.JSch
import com.jcraft.jsch.JSchException
import com.jcraft.jsch.Session
import org.eclipse.jgit.transport.JschConfigSessionFactory
import org.eclipse.jgit.transport.OpenSshConfig
import org.eclipse.jgit.util.FS
open class GitConfigSessionFactory : JschConfigSessionFactory() {
override fun configure(hc: OpenSshConfig.Host, session: Session) {
session.setConfig("StrictHostKeyChecking", "no")
}
@Throws(JSchException::class)
override fun getJSch(hc: OpenSshConfig.Host, fs: FS): JSch {
val jsch = super.getJSch(hc, fs)
jsch.removeAllIdentity()
return jsch
}
}

View file

@ -1,68 +0,0 @@
package com.zeapo.pwdstore.git.config;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
import org.eclipse.jgit.errors.UnsupportedCredentialItem;
import org.eclipse.jgit.transport.CredentialItem;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.CredentialsProviderUserInfo;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.util.FS;
public class SshConfigSessionFactory extends GitConfigSessionFactory {
private String sshKey;
private String passphrase;
private String username;
public SshConfigSessionFactory(String sshKey, String username, String passphrase) {
this.sshKey = sshKey;
this.passphrase = passphrase;
this.username = username;
}
@Override
protected JSch
getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException {
JSch jsch = super.getJSch(hc, fs);
jsch.removeAllIdentity();
jsch.addIdentity(sshKey);
return jsch;
}
@Override
protected void configure(OpenSshConfig.Host hc, Session session) {
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PreferredAuthentications", "publickey,password");
CredentialsProvider provider = new CredentialsProvider() {
@Override
public boolean isInteractive() {
return false;
}
@Override
public boolean supports(CredentialItem... items) {
return true;
}
@Override
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
for (CredentialItem item : items) {
if (item instanceof CredentialItem.Username) {
((CredentialItem.Username) item).setValue(username);
continue;
}
if (item instanceof CredentialItem.StringType) {
((CredentialItem.StringType) item).setValue(passphrase);
}
}
return true;
}
};
UserInfo userInfo = new CredentialsProviderUserInfo(session, provider);
session.setUserInfo(userInfo);
}
}

View file

@ -0,0 +1,54 @@
package com.zeapo.pwdstore.git.config
import com.jcraft.jsch.JSch
import com.jcraft.jsch.JSchException
import com.jcraft.jsch.Session
import org.eclipse.jgit.errors.UnsupportedCredentialItem
import org.eclipse.jgit.transport.CredentialItem
import org.eclipse.jgit.transport.CredentialsProvider
import org.eclipse.jgit.transport.CredentialsProviderUserInfo
import org.eclipse.jgit.transport.OpenSshConfig
import org.eclipse.jgit.transport.URIish
import org.eclipse.jgit.util.FS
class SshConfigSessionFactory(private val sshKey: String, private val username: String, private val passphrase: String) : GitConfigSessionFactory() {
@Throws(JSchException::class)
override fun getJSch(hc: OpenSshConfig.Host, fs: FS): JSch {
val jsch = super.getJSch(hc, fs)
jsch.removeAllIdentity()
jsch.addIdentity(sshKey)
return jsch
}
override fun configure(hc: OpenSshConfig.Host, session: Session) {
session.setConfig("StrictHostKeyChecking", "no")
session.setConfig("PreferredAuthentications", "publickey,password")
val provider = object : CredentialsProvider() {
override fun isInteractive(): Boolean {
return false
}
override fun supports(vararg items: CredentialItem): Boolean {
return true
}
@Throws(UnsupportedCredentialItem::class)
override fun get(uri: URIish, vararg items: CredentialItem): Boolean {
for (item in items) {
if (item is CredentialItem.Username) {
item.value = username
continue
}
if (item is CredentialItem.StringType) {
item.value = passphrase
}
}
return true
}
}
val userInfo = CredentialsProviderUserInfo(session, provider)
session.userInfo = userInfo
}
}