use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project archi-modelrepository-plugin by archi-contribs.
the class CredentialsAuthenticator method getTransportConfigCallback.
/**
* Factory method to get the default TransportConfigCallback for authentication for repoURL
* npw can be null and is ignored if repoURL is SSH
*/
public static TransportConfigCallback getTransportConfigCallback(final String repoURL, final UsernamePassword npw) throws IOException {
// SSH
if (GraficoUtils.isSSH(repoURL)) {
return new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
// Delete remote branches that we don't have
transport.setRemoveDeletedRefs(true);
if (transport instanceof SshTransport) {
((SshTransport) transport).setSshSessionFactory(getSshSessionFactory());
}
}
protected SshSessionFactory getSshSessionFactory() {
return new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host host, Session session) {
// $NON-NLS-1$ //$NON-NLS-2$
session.setConfig("StrictHostKeyChecking", "no");
}
@Override
protected JSch createDefaultJSch(FS fs) throws JSchException {
JSch jsch = super.createDefaultJSch(fs);
// TODO - we might not need to do this as it sets default locations for rsa_pub
jsch.removeAllIdentity();
File file = null;
char[] pw = null;
try {
file = sshIdentityProvider.getIdentityFile();
pw = sshIdentityProvider.getIdentityPassword();
} catch (IOException | GeneralSecurityException ex) {
throw new JSchException(ex.getMessage());
}
if (pw != null) {
jsch.addIdentity(file.getAbsolutePath(), new String(pw));
} else {
jsch.addIdentity(file.getAbsolutePath());
}
return jsch;
}
};
}
};
}
// HTTP
if (npw != null) {
return new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
transport.setCredentialsProvider(new UsernamePasswordCredentialsProvider(npw.getUsername(), npw.getPassword()));
// Delete remote branches that we don't have
transport.setRemoveDeletedRefs(true);
}
};
}
// $NON-NLS-1$
throw new IOException(Messages.CredentialsAuthenticator_2 + " " + repoURL);
}
Aggregations