Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SFTP server multiple user authentication

I am trying to extend the user authentication example, which is also presented here, so that multiple users can login to the server. I would also like to assign a different home directory for each user. So far, I haven't been able to find any such utility provided by the Apache SSHD API, so I have tried the following workaround, by using the utilities provided by the Apache FtpServer.

What I attempt to do is:

  1. create a UserManager object to manage a list of users and store their information in a property file, in a way similar to this example
  2. create a PasswordAuthenticator that makes use of the UserManager in its authenticate method, as follows:

    public class MyPasswordAuthenticator implements PasswordAuthenticator {
    
        private UserManager userManager;
    
        public MyPasswordAuthenticator(){
            this.userManager=null;
        }
    
        public MyPasswordAuthenticator(UserManager manager) {
            this.userManager=manager;
        }
    
        @Override
        public boolean authenticate(String username, String password, ServerSession session) throws PasswordChangeRequiredException {
            if (this.userManager==null) return false;
            User usr=null;
            try {
                usr = userManager.getUserByName(username);
            } catch (FtpException e) {
                e.printStackTrace();
            }
            if (usr==null) return false;
            else{       
                String pass=usr.getPassword();
                return password.equals(pass);
            }
        }
    
    
    }
    

However, the usr.getPassword() returns null, even though a) the password fields in the property file do have values b) I have checked the functions getName() and getHomeDirectory() and they return their respective String values.

My question is, why does this happen and what should be done to fix this?

like image 861
fyts Avatar asked Nov 30 '25 23:11

fyts


1 Answers

I have found a way to make it work, it is:

usr = this.userManager.authenticate(new UsernamePasswordAuthentication(username, password));
like image 179
fyts Avatar answered Dec 03 '25 11:12

fyts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!