com.sshtools.j2ssh.authentication.AuthenticationProtocolState Java Examples

The following examples show how to use com.sshtools.j2ssh.authentication.AuthenticationProtocolState. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SshUtil.java    From qaf with MIT License 6 votes vote down vote up
/**
 * connect to host
 * 
 * @return
 * @throws Exception
 */
private static SshClient connectSsh() throws Exception {
	SshClient ssh = new SshClient();

	HostKeyVerification host = new IgnoreHostKeyVerification();
	String hostStr = getBundle().getString("ssh.host");
	ssh.connect(hostStr, host);
	PasswordAuthenticationClient auth = new PasswordAuthenticationClient();
	auth.setUsername(getBundle().getString("ssh.user"));
	auth.setPassword(getBundle().getString("ssh.pwd"));
	int result = ssh.authenticate(auth);

	System.out.println("Status " + result);
	if ((result == AuthenticationProtocolState.CANCELLED) || (result == AuthenticationProtocolState.FAILED)) {
		throw new Exception("Authentication Error.");
	}
	return ssh;
}
 
Example #2
Source File: SSHUtils.java    From stevia with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Connect. This action simulates all the actions required for a SSH connection
 *
 * @param host  the hostname of the host you want to connect to
 * @param port the port of the host you want to connect to
 * @param username the username required for authentication
 * @param password the password required for authentication
 * @return the ssh client you connected to
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static SshClient connect(String host, int port,String username,String password) throws IOException {
	SSH_LOG.info("Connecting to " + host);
	SshClient ssh = new SshClient();
	ssh.connect(host, port, new IgnoreHostKeyVerification());
	PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient();
	passwordAuthenticationClient.setUsername(username);
	passwordAuthenticationClient.setPassword(password);
	int result = ssh.authenticate(passwordAuthenticationClient);
	if (result != AuthenticationProtocolState.COMPLETE) {
		throw new IOException("Login to " + host + ":" + port + " "+ username + "/" + password + " failed");
	}
	SSH_LOG.info("Connected " + host);
	return ssh;
}
 
Example #3
Source File: Ssh.java    From swift-k with Apache License 2.0 4 votes vote down vote up
private boolean authenticateWithKBI(boolean force)
        throws InvalidSecurityContextException, IOException {
    final KBIAuthenticationClient kbi = new KBIAuthenticationClient();
    kbi.setKBIRequestHandler(new KBIRequestHandler() {
        public void showPrompts(String name, String instruction,
                KBIPrompt[] prompts) {
            if (prompts == null) {
                return;
            }
            Prompt[] p = new Prompt[prompts.length];
            for (int i = 0; i < prompts.length; i++) {
                p[i] = new Prompt(prompts[i].getPrompt(),
                    Prompt.TYPE_HIDDEN_TEXT);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Displaying credential dialog");
            }
            char[][] results = CredentialsDialog.showCredentialsDialog(host
                    + (empty(name) ? "" : " - " + name) 
                    + (empty(instruction) ? "" : " - " + instruction), p);
            if (results != null) {
                for (int i = 0; i < prompts.length; i++) {
                    prompts[i].setResponse(new String(results[i]));
                }
            }
        }
    });

    if (logger.isDebugEnabled()) {
        logger.debug("Performing keyboard-interactive authentication");
    }
    if (username == null) {
        promptForUsername();
    }
    kbi.setUsername(username);
    int result = client.authenticate(kbi);
    if (result == AuthenticationProtocolState.COMPLETE) {
        if (logger.isDebugEnabled()) {
            logger.debug("Authentication complete");
        }
        return true;
    }
    else if (result == AuthenticationProtocolState.PARTIAL) {
        if (logger.isDebugEnabled()) {
            logger.error("Keyboard Interactive Authentication succeeded "
                    + "but further authentication required!");
        }
        if (force) {
            throw new InvalidSecurityContextException(
                "The server requested additional authentication, but none is possible.");
        }
        else {
            return false;
        }
    }
    else {
        if (force) {
            throw new InvalidSecurityContextException(
                "Keyboard Interactive Authentication failed");
        }
        else {
            return false;
        }
    }
}
 
Example #4
Source File: FtpSession.java    From iaf with Apache License 2.0 4 votes vote down vote up
private void openSftpClient(String remoteDirectory) throws FtpConnectException {
	try {
		// Set the connection properties and if necessary the proxy properties
		SshConnectionProperties sshProp = new SshConnectionProperties();
		sshProp.setHost(host);
		sshProp.setPort(port);
		if (StringUtils.isNotEmpty(prefCSEncryption))
			sshProp.setPrefCSEncryption(prefCSEncryption);
		if (StringUtils.isNotEmpty(prefSCEncryption))
			sshProp.setPrefCSEncryption(prefSCEncryption);

		if (! StringUtils.isEmpty(proxyHost)) {
			sshProp.setTransportProvider(proxyTransportType);
			sshProp.setProxyHost(proxyHost);
			sshProp.setProxyPort(proxyPort);
			CredentialFactory pcf = new CredentialFactory(getProxyAuthAlias(), proxyUsername, proxyPassword);

			if (! StringUtils.isEmpty(pcf.getUsername())) {
				sshProp.setProxyUsername(pcf.getUsername());
				sshProp.setProxyPassword(pcf.getPassword());
			}
		}

		// make a secure connection with the remote host 
		sshClient = new SshClient();
		if (StringUtils.isNotEmpty(knownHostsPath)) {
			AbstractKnownHostsKeyVerification hv = null;
			if (consoleKnownHostsVerifier) { 
				hv = new ConsoleKnownHostsKeyVerification(knownHostsPath); 
			}
			else {
				hv = new SftpHostVerification(knownHostsPath);
			}
			sshClient.connect(sshProp, hv);
		}
		else {
			sshClient.connect(sshProp, new IgnoreHostKeyVerification());
		}
		
		SshAuthenticationClient sac;
		if (!isKeyboardInteractive()) {
			// pass the authentication information
			sac = getSshAuthentication();
		} else {
			// TODO: detecteren dat sshClient.getAvailableAuthMethods("ftpmsg")
			// wel keyboard-interactive terug geeft, maar geen password en dan deze methode
			// gebruiken
			final CredentialFactory credentialFactory = new CredentialFactory(getAuthAlias(), getUsername(), getPassword());
			KBIAuthenticationClient kbiAuthenticationClient = new KBIAuthenticationClient();
			kbiAuthenticationClient.setUsername(credentialFactory.getUsername());
			kbiAuthenticationClient.setKBIRequestHandler(
				new KBIRequestHandler() {
					@Override
					public void showPrompts(String name, String instruction, KBIPrompt[] prompts) {
						//deze 3 regels in x.zip naar Zenz gemaild, hielp ook niet
						if(prompts==null) {
							return;
						}
						for(int i=0; i<prompts.length; i++) {
							prompts[i].setResponse(credentialFactory.getPassword());
						}
					}
				}
			);
			sac=kbiAuthenticationClient;
		}
		int result = sshClient.authenticate(sac);
		
		if (result != AuthenticationProtocolState.COMPLETE) {
			closeSftpClient();
			throw new IOException("Could not authenticate to sftp server " + result);
		}
		
		// use the connection for sftp
		sftpClient = sshClient.openSftpClient();
		
		if (! StringUtils.isEmpty(remoteDirectory)) {
			sftpClient.cd(remoteDirectory);
		}
	}
	catch(Exception e) {
		closeSftpClient();
		throw new FtpConnectException(e);
	}
}