Search in sources :

Example 1 with UserAuthException

use of net.schmizz.sshj.userauth.UserAuthException in project AmazeFileManager by TeamAmaze.

the class SshAuthenticationTask method doInBackground.

@Override
protected AsyncTaskResult<SSHClient> doInBackground(Void... voids) {
    final SSHClient sshClient = new SSHClient(new CustomSshJConfig());
    sshClient.addHostKeyVerifier(hostKey);
    sshClient.setConnectTimeout(SSH_CONNECT_TIMEOUT);
    try {
        sshClient.connect(hostname, port);
        if (password != null && !"".equals(password)) {
            sshClient.authPassword(username, password);
            return new AsyncTaskResult<SSHClient>(sshClient);
        } else {
            sshClient.authPublickey(username, new KeyProvider() {

                @Override
                public PrivateKey getPrivate() throws IOException {
                    return privateKey.getPrivate();
                }

                @Override
                public PublicKey getPublic() throws IOException {
                    return privateKey.getPublic();
                }

                @Override
                public KeyType getType() throws IOException {
                    return KeyType.fromKey(getPublic());
                }
            });
            return new AsyncTaskResult<SSHClient>(sshClient);
        }
    } catch (UserAuthException e) {
        e.printStackTrace();
        return new AsyncTaskResult<SSHClient>(e);
    } catch (TransportException e) {
        e.printStackTrace();
        return new AsyncTaskResult<SSHClient>(e);
    } catch (IOException e) {
        e.printStackTrace();
        return new AsyncTaskResult<SSHClient>(e);
    }
}
Also used : CustomSshJConfig(com.amaze.filemanager.filesystem.ssh.CustomSshJConfig) KeyProvider(net.schmizz.sshj.userauth.keyprovider.KeyProvider) PrivateKey(java.security.PrivateKey) KeyType(net.schmizz.sshj.common.KeyType) PublicKey(java.security.PublicKey) SSHClient(net.schmizz.sshj.SSHClient) AsyncTaskResult(com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult) IOException(java.io.IOException) UserAuthException(net.schmizz.sshj.userauth.UserAuthException) TransportException(net.schmizz.sshj.transport.TransportException)

Aggregations

AsyncTaskResult (com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult)1 CustomSshJConfig (com.amaze.filemanager.filesystem.ssh.CustomSshJConfig)1 IOException (java.io.IOException)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 SSHClient (net.schmizz.sshj.SSHClient)1 KeyType (net.schmizz.sshj.common.KeyType)1 TransportException (net.schmizz.sshj.transport.TransportException)1 UserAuthException (net.schmizz.sshj.userauth.UserAuthException)1 KeyProvider (net.schmizz.sshj.userauth.keyprovider.KeyProvider)1