James Moger
2015-11-22 ed552ba47c02779c270ffd62841d6d1048dade70
commit | author | age
924c9b 1 /*
JM 2  * Copyright 2014 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.gitblit.transport.ssh;
17
18 import java.util.Locale;
19
d41034 20 import org.apache.sshd.server.auth.password.PasswordAuthenticator;
924c9b 21 import org.apache.sshd.server.session.ServerSession;
44e2ee 22 import org.slf4j.Logger;
JM 23 import org.slf4j.LoggerFactory;
924c9b 24
b53611 25 import com.gitblit.manager.IAuthenticationManager;
924c9b 26 import com.gitblit.models.UserModel;
JM 27
28 /**
29  *
56b3f3 30  * Authenticates an SSH session with username/password credentials.
JM 31  *
924c9b 32  * @author James Moger
JM 33  *
34  */
448145 35 public class UsernamePasswordAuthenticator implements PasswordAuthenticator {
924c9b 36
44e2ee 37     protected final Logger log = LoggerFactory.getLogger(getClass());
JM 38
b53611 39     protected final IAuthenticationManager authManager;
924c9b 40
448145 41     public UsernamePasswordAuthenticator(IAuthenticationManager authManager) {
b53611 42         this.authManager = authManager;
924c9b 43     }
JM 44
45     @Override
46     public boolean authenticate(String username, String password, ServerSession session) {
a8dd37 47         SshDaemonClient client = session.getAttribute(SshDaemonClient.KEY);
JM 48         if (client.getUser() != null) {
44e2ee 49             log.info("{} has already authenticated!", username);
JM 50             return true;
51         }
52
924c9b 53         username = username.toLowerCase(Locale.US);
0d7c65 54         UserModel user = authManager.authenticate(username, password.toCharArray(), null);
924c9b 55         if (user != null) {
a8dd37 56             client.setUser(user);
924c9b 57             return true;
JM 58         }
44e2ee 59
b7fcca 60         log.warn("could not authenticate {} ({}) for SSH using the supplied password", username, client.getRemoteAddress());
924c9b 61         return false;
JM 62     }
63 }