0

I am trying to fetch files from sftp server, but even I tryed many different approaches, it's still gives me an error userauth.UserAuthenticationExeption: "Exhausted availible authentication methods", and IllegalStateException: "Not authenticated"

Using putty and app PSFTP, I can easy log into server using key.ppk, username and password. In PSFTP authentication works just fine.

My key.ppk looks like:

PuTTy-User-Key-File-2: ssh-rsa
Encryption: none
Comment: rsa-key-randomInt
Private-Lines: randomInt
Some random key

And another few lines, and like I said, using PSFTP I can log in using this key so I know it works.

My code example:

var client = new SSHClient();
client.setTimeout(config.getTimeout());
client.setConnectTimeout(config.getTimeout());
client.addHostKeyVerifier(new PromiscuousVerifier());

try{
   client.connect(config.getHost, config.getPort());
} catch (IOException e){
   e.printStackTrace();
}

var passwordAuth = ObjectUtils.isEmpty(config.getPassword())
   ? List<AuthMethod>empty()
   : List.of<new AuthPassword(PasswordUtils.createOneOff(config.getPassword.toCharArray()));

List< ? extends AuthMethod> key = null;

try{
   key = ObjectUtils.isEmpty(config.getPrivateKey())
   ? List<AuthMethod>empty()
   : List.of(new AuthPublicKey(createKeyProvider()));
}

createKeyProvider KeyPairWrapper createKeyProvider() throws IOException{
   PuttyKeyFile puttyKeyFile = new PuttyKeyFile();
   putyKeyfile.init(new File(path));//it works, in debug it shows proper object with values

  return new KeyPairWrapper(new KeyPair(puttyKeyFile.getPublic(), puttyKeyFile.getPrivate))
}

var authMethods = List.of(passwordAuth).<AuthMethod>flatMap(Function.identity());

try{
   client.auth(
      config.getUsername(),
      authMethods
   );
}catch(UserAuthException e){
   e.printStackTrace();
}

If I did the typo, I am sorry, but I needed to rewrite code example instead of copy paste.

Part of this code was written by someone else(who can't help me) before I started work at it. I think that I need to log in using username, key and password. I tryed to log in using just client.auth(username, password) or client.auth(username, key), but it didn't work, also I need use these 3 things when I try to log in using PSFTP.

I think that problem is in AuthPublicKey, and I tryed to add there many different objects like: KeyPair, KeyPairWrapper, client.loadKey(fileLocation), client.loadKey(keyPair).

I also converted this ppk file to other formats, and tryed to get keys, but it still gives same error.

Another things I tryed was to not provide port, checked configs many times, when I logged into server using PSFTP I copied values to check if there is no typo.

I am starting to lose my mind searching for new options how to fix it. In perfect case, I would like to get my key from config files, and I also tryed to do it this way, but still I am here.

If you have solution how to get authentication, or just idea what may be wrong, I will appreciate it.

Thank you guys, for any help I will receive.

1 Answers1

0

Ok, I solve problem using yellow duck method. The problem wasn't in AuthPublicKey, the problem was, I created it properly, but I didn't add it to list authMethods.

//WRONG
var authMethods = List.of(passwordAuth).<AuthMethod>flatMap(Function.identity());

//OK
var authMethods = List.of(passwordAuth, key).<AuthMethod>flatMap(Function.identity());

It was missing key property. It works now, sorry for wasting your time, but I really couldn't catch it.