Search in sources :

Example 76 with UsernamePasswordCredentialsProvider

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);
}
Also used : JSchException(com.jcraft.jsch.JSchException) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) FS(org.eclipse.jgit.util.FS) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) Transport(org.eclipse.jgit.transport.Transport) SshTransport(org.eclipse.jgit.transport.SshTransport) File(java.io.File) SshTransport(org.eclipse.jgit.transport.SshTransport) Session(com.jcraft.jsch.Session)

Aggregations

UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)76 Git (org.eclipse.jgit.api.Git)47 File (java.io.File)34 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)23 IOException (java.io.IOException)19 Test (org.junit.Test)18 CloneCommand (org.eclipse.jgit.api.CloneCommand)17 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)15 RepositoryModel (com.gitblit.models.RepositoryModel)12 PushResult (org.eclipse.jgit.transport.PushResult)12 RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)9 FileOutputStream (java.io.FileOutputStream)8 PushCommand (org.eclipse.jgit.api.PushCommand)8 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 BufferedWriter (java.io.BufferedWriter)7 OutputStreamWriter (java.io.OutputStreamWriter)7 Date (java.util.Date)7 Ref (org.eclipse.jgit.lib.Ref)7 Repository (org.eclipse.jgit.lib.Repository)7 URIish (org.eclipse.jgit.transport.URIish)7