Search in sources :

Example 1 with Credentials

use of org.apache.ivy.util.Credentials in project ant-ivy by apache.

the class AbstractSshBasedRepository method getSession.

/**
 * get a new session using the default attributes if the given String is a full uri, use the
 * data from the uri instead
 *
 * @param pathOrUri
 *            might be just a path or a full ssh or sftp uri
 * @return matching Session
 * @throws IOException if something goes wrong
 */
protected Session getSession(String pathOrUri) throws IOException {
    URI uri = parseURI(pathOrUri);
    String host = getHost();
    int port = getPort();
    String user = getUser();
    String userPassword = getUserPassword();
    String sshConfig = getSshConfig();
    File keyFile = getKeyFile();
    if (uri != null && uri.getScheme() != null) {
        if (uri.getHost() != null) {
            host = uri.getHost();
        }
        if (uri.getPort() != -1) {
            port = uri.getPort();
        }
        if (uri.getUserInfo() != null) {
            String userInfo = uri.getUserInfo();
            if (!userInfo.contains(":")) {
                user = userInfo;
            } else {
                user = userInfo.substring(0, userInfo.indexOf(":"));
                userPassword = userInfo.substring(userInfo.indexOf(":") + 1);
            }
        }
    }
    if (sshConfig != null) {
        ConfigRepository configRepository = OpenSSHConfig.parseFile(sshConfig);
        Config config = configRepository.getConfig(host);
        host = config.getHostname();
        if (user == null) {
            user = config.getUser();
        }
        String keyFilePath = config.getValue("IdentityFile");
        if (keyFilePath != null && keyFile == null) {
            keyFile = new File(keyFilePath);
        }
    }
    if (host == null) {
        throw new IllegalArgumentException("missing host information. host should be provided either " + "directly on the repository or in the connection URI " + ", or in the openssh config file specified by sshConfig");
    }
    if (user == null) {
        Credentials c = requestCredentials(host);
        if (c != null) {
            user = c.getUserName();
            userPassword = c.getPasswd();
        } else {
            Message.error("username is not set");
        }
    }
    return SshCache.getInstance().getSession(host, port, user, userPassword, keyFile, getKeyFilePassword(), getPassFile(), isAllowedAgentUse());
}
Also used : ConfigRepository(com.jcraft.jsch.ConfigRepository) Config(com.jcraft.jsch.ConfigRepository.Config) OpenSSHConfig(com.jcraft.jsch.OpenSSHConfig) URI(java.net.URI) File(java.io.File) TimeoutConstraint(org.apache.ivy.core.settings.TimeoutConstraint) Credentials(org.apache.ivy.util.Credentials)

Example 2 with Credentials

use of org.apache.ivy.util.Credentials in project ant-ivy by apache.

the class CredentialsStore method addCredentials.

public void addCredentials(String realm, String host, String userName, String passwd) {
    if (userName == null) {
        return;
    }
    Credentials c = new Credentials(realm, host, userName, passwd);
    Message.debug("credentials added: " + c);
    KEYRING.put(c.getKey(), c);
    SECURED_HOSTS.add(host);
}
Also used : Credentials(org.apache.ivy.util.Credentials)

Example 3 with Credentials

use of org.apache.ivy.util.Credentials in project ant-ivy by apache.

the class IvyAuthenticator method getPasswordAuthentication.

// API ******************************************************************
// Overriding Authenticator *********************************************
protected PasswordAuthentication getPasswordAuthentication() {
    PasswordAuthentication result = null;
    if (isProxyAuthentication()) {
        String proxyUser = System.getProperty("http.proxyUser");
        if (!isNullOrEmpty(proxyUser)) {
            String proxyPass = System.getProperty("http.proxyPassword", "");
            Message.debug("authenticating to proxy server with username [" + proxyUser + "]");
            result = new PasswordAuthentication(proxyUser, proxyPass.toCharArray());
        }
    } else {
        Credentials c = CredentialsStore.INSTANCE.getCredentials(getRequestingPrompt(), getRequestingHost());
        Message.debug("authentication: k='" + Credentials.buildKey(getRequestingPrompt(), getRequestingHost()) + "' c='" + c + "'");
        if (c != null) {
            final String password = c.getPasswd() == null ? "" : c.getPasswd();
            result = new PasswordAuthentication(c.getUserName(), password.toCharArray());
        }
    }
    if (result == null && original != null) {
        Authenticator.setDefault(original);
        try {
            result = Authenticator.requestPasswordAuthentication(getRequestingHost(), getRequestingSite(), getRequestingPort(), getRequestingProtocol(), getRequestingPrompt(), getRequestingScheme(), getRequestingURL(), getRequestorType());
        } finally {
            Authenticator.setDefault(this);
        }
    }
    return result;
}
Also used : Credentials(org.apache.ivy.util.Credentials) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

Credentials (org.apache.ivy.util.Credentials)3 ConfigRepository (com.jcraft.jsch.ConfigRepository)1 Config (com.jcraft.jsch.ConfigRepository.Config)1 OpenSSHConfig (com.jcraft.jsch.OpenSSHConfig)1 File (java.io.File)1 PasswordAuthentication (java.net.PasswordAuthentication)1 URI (java.net.URI)1 TimeoutConstraint (org.apache.ivy.core.settings.TimeoutConstraint)1