Example usage for org.eclipse.jgit.api TransportCommand setCredentialsProvider

List of usage examples for org.eclipse.jgit.api TransportCommand setCredentialsProvider

Introduction

In this page you can find the example usage for org.eclipse.jgit.api TransportCommand setCredentialsProvider.

Prototype

public C setCredentialsProvider(final CredentialsProvider credentialsProvider) 

Source Link

Document

Set the credentialsProvider.

Usage

From source file:bluej.groupwork.git.GitCommand.java

License:Open Source License

/**
 * Prepare a TransportCommand to be executed later.
 * This method checks if the command is using ssh. if it is,
 * then it will disable SSH's fingerprint detection and use
 * username and password authentication.
 * @param command The command to be configured.
 *//*from  ww w.  ja v  a2  s .c om*/
public void disableFingerprintCheck(TransportCommand command) {
    //check if the command is not null and conects via ssh.
    if (command != null && repository.getReposUrl().startsWith("ssh")) {
        //disable ssh host fingerprint check.
        SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
            @Override
            protected void configure(OpenSshConfig.Host host, Session sn) {
                java.util.Properties config = new java.util.Properties();
                config.put("StrictHostKeyChecking", "no");
                sn.setConfig(config);
            }

            @Override
            protected JSch createDefaultJSch(FS fs) throws JSchException {
                return super.createDefaultJSch(fs);
            }
        };

        command.setTransportConfigCallback((Transport t) -> {
            SshTransport sshTransport = (SshTransport) t;
            sshTransport.setSshSessionFactory(sshSessionFactory);
        });
        command.setCredentialsProvider(getRepository().getCredentialsProvider());
    }
}

From source file:com.ibm.liberty.starter.GitHubConnector.java

License:Apache License

private void addAuth(TransportCommand command) {
    command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(oAuthToken, ""));
}

From source file:com.rimerosolutions.ant.git.AbstractGitTask.java

License:Apache License

/**
 * Setups the Git credentials if specified and needed
 *
 * @param command The git command to configure
 *//* w  w w  .  j ava2s .co m*/
@SuppressWarnings("rawtypes")
protected void setupCredentials(GitCommand<?> command) {
    GitSettings settings = lookupSettings();

    if (settings != null && command instanceof TransportCommand) {
        TransportCommand cmd = (TransportCommand) command;
        cmd.setCredentialsProvider(settings.getCredentials());
    }
}

From source file:io.fabric8.collector.git.GitHelpers.java

License:Apache License

/**
 * Configures the transport of the command to deal with things like SSH
 *//*from  www  . j  a v  a  2  s  .co  m*/
public static <C extends GitCommand> void configureCommand(TransportCommand<C, ?> command,
        UserDetails userDetails) {
    configureCommand(command, userDetails.createCredentialsProvider(), userDetails.getSshPrivateKey(),
            userDetails.getSshPublicKey());
    command.setCredentialsProvider(userDetails.createCredentialsProvider());
}

From source file:org.craftercms.commons.git.auth.BasicUsernamePasswordAuthConfigurator.java

License:Open Source License

@Override
public void configureAuthentication(TransportCommand command) {
    command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

/**
 * Execute remote jgit command./*from   w  w  w  .ja  v a2  s  .com*/
 *
 * @param remoteUrl
 *         remote url
 * @param command
 *         command to execute
 * @return executed command
 * @throws GitException
 * @throws GitAPIException
 * @throws UnauthorizedException
 */
private Object executeRemoteCommand(String remoteUrl, TransportCommand command)
        throws GitException, GitAPIException, UnauthorizedException {
    String sshKeyDirectoryPath = "";
    try {
        if (GitUrlUtils.isSSH(remoteUrl)) {
            File keyDirectory = Files.createTempDir();
            sshKeyDirectoryPath = keyDirectory.getPath();
            File sshKey = writePrivateKeyFile(remoteUrl, keyDirectory);

            SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
                @Override
                protected void configure(OpenSshConfig.Host host, 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();
                    jsch.addIdentity(sshKey.getAbsolutePath());
                    return jsch;
                }
            };
            command.setTransportConfigCallback(new TransportConfigCallback() {
                @Override
                public void configure(Transport transport) {
                    SshTransport sshTransport = (SshTransport) transport;
                    sshTransport.setSshSessionFactory(sshSessionFactory);
                }
            });
        } else {
            UserCredential credentials = credentialsLoader.getUserCredential(remoteUrl);
            if (credentials != null) {
                command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(
                        credentials.getUserName(), credentials.getPassword()));
            }
        }
        return command.call();
    } catch (GitException | TransportException exception) {
        if ("Unable get private ssh key".equals(exception.getMessage())
                || exception.getMessage().contains(ERROR_AUTHENTICATION_REQUIRED)) {
            throw new UnauthorizedException(exception.getMessage());
        } else {
            throw exception;
        }
    } finally {
        if (!sshKeyDirectoryPath.isEmpty()) {
            try {
                FileUtils.delete(new File(sshKeyDirectoryPath), FileUtils.RECURSIVE);
            } catch (IOException exception) {
                throw new GitException("Can't remove SSH key directory", exception);
            }
        }
    }
}

From source file:org.springframework.cloud.config.server.environment.JGitEnvironmentRepository.java

License:Apache License

private void setCredentialsProvider(TransportCommand<?, ?> cmd) {
    if (gitCredentialsProvider != null) {
        cmd.setCredentialsProvider(gitCredentialsProvider);
    } else if (hasText(getUsername())) {
        cmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(getUsername(), getPassword()));
    } else if (hasText(getPassphrase())) {
        cmd.setCredentialsProvider(new PassphraseCredentialsProvider(getPassphrase()));
    }//  w  w w.  j  a v a  2  s  .  c om
}

From source file:org.springframework.cloud.config.server.JGitEnvironmentRepository.java

License:Apache License

private void setCredentialsProvider(TransportCommand<?, ?> cmd) {
    cmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(getUsername(), getPassword()));
}