James Moger
2014-03-14 030fd739b3151162c4e84e9c63ce57532af45219
src/main/java/com/gitblit/transport/ssh/SshPasswordAuthenticator.java
@@ -19,8 +19,10 @@
import org.apache.sshd.server.PasswordAuthenticator;
import org.apache.sshd.server.session.ServerSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.gitblit.manager.IGitblit;
import com.gitblit.manager.IAuthenticationManager;
import com.gitblit.models.UserModel;
/**
@@ -30,21 +32,30 @@
 */
public class SshPasswordAuthenticator implements PasswordAuthenticator {
   protected final IGitblit gitblit;
   protected final Logger log = LoggerFactory.getLogger(getClass());
   public SshPasswordAuthenticator(IGitblit gitblit) {
      this.gitblit = gitblit;
   protected final IAuthenticationManager authManager;
   public SshPasswordAuthenticator(IAuthenticationManager authManager) {
      this.authManager = authManager;
   }
   @Override
   public boolean authenticate(String username, String password, ServerSession session) {
      username = username.toLowerCase(Locale.US);
      UserModel user = gitblit.authenticate(username, password.toCharArray());
      if (user != null) {
         SshSession sd = session.getAttribute(SshSession.KEY);
         sd.authenticationSuccess(username);
      SshDaemonClient client = session.getAttribute(SshDaemonClient.KEY);
      if (client.getUser() != null) {
         log.info("{} has already authenticated!", username);
         return true;
      }
      username = username.toLowerCase(Locale.US);
      UserModel user = authManager.authenticate(username, password.toCharArray());
      if (user != null) {
         client.setUser(user);
         return true;
      }
      log.warn("could not authenticate {} for SSH using the supplied password", username);
      return false;
   }
}