Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to implement PublicKey Authentication for SFTP test server using Apache Mina. However Authenticate method is not being called

I am using Apache Mina SSHD to implement a test SFTPServer. I have been able to get things working for simple Password authentication, however I am not able to configure things for PublicKey Authentication. I have a implemented the PublickeyAuthenticator interface as follows,

public class SimpleKeyAuthenticator implements PublickeyAuthenticator {

    @Override
    public boolean authenticate(String username, PublicKey key, ServerSession session) {
        System.out.println("In authenticate");
        return false;
    }

}

My server implementation is as follows,

...
sshd = SshServer.setUpDefaultServer();


sshd.setPort(2222);
//sshd.setPort(config.getSFTPPort());

//sshd.setKeyPairProvider(new 
sshd.setKeyPairProvider(new PEMGeneratorHostKeyProvider("hostkey.pem"));
//sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());

sshd.setPublickeyAuthenticator(new SimpleKeyAuthenticator());
sshd.setFileSystemFactory(new SimpleFileSystemFactory());

List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
userAuthFactories.add(new UserAuthNone.Factory());
sshd.setUserAuthFactories(userAuthFactories);

sshd.setCommandFactory(new ScpCommandFactory());

List<NamedFactory<Command>> namedFactoryList = new ArrayList<NamedFactory<Command>>();

namedFactoryList.add(new SftpSubsystem.Factory());
sshd.setSubsystemFactories(namedFactoryList);

sshd.setSessionFactory(new SimpleSessionFactory(handler));
try {
    sshd.start();
} catch (Exception e) {
    e.printStackTrace();
}

However when I try to get a file using my SFTP client everything works. I would expect the authenticate method to fail given that it always returns false. I have tried setting the KeyPairProvider to use both the PEMGeneratorHostKeyProvider and the SimpleGeneratorHostKeyProvider. I have also set the PublicKeyAuthenticator to use my SimpleKeyAuthenticator class. Note, when I look at the console output I never see 'In authenticate' so I know that Authenticate is never being called. Could someone please point me to what I have missed? Any help is appreciated.

Regards, Mark

like image 334
Mark Avatar asked Nov 02 '22 23:11

Mark


1 Answers

// below line will make client login without any validation.

userAuthFactories.add(new UserAuthNone.Factory());

You should change it like this:

userAuthFactories.add(new UserAuthPublicKey.Factory());

like image 174
cza55007 Avatar answered Jan 04 '23 15:01

cza55007