Subversion Repositories Programming Utils

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 rm5248 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
package org.apache.sshd.server.jaas;
20
 
21
import java.io.IOException;
22
 
23
import javax.security.auth.Subject;
24
import javax.security.auth.callback.Callback;
25
import javax.security.auth.callback.CallbackHandler;
26
import javax.security.auth.callback.NameCallback;
27
import javax.security.auth.callback.PasswordCallback;
28
import javax.security.auth.callback.UnsupportedCallbackException;
29
import javax.security.auth.login.LoginContext;
30
 
31
import org.apache.sshd.server.PasswordAuthenticator;
32
import org.apache.sshd.server.session.ServerSession;
33
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35
 
36
/**
37
 * TODO Add javadoc
38
 *
39
 * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
40
 */
41
public class JaasPasswordAuthenticator implements PasswordAuthenticator {
42
 
43
    private static final Logger LOGGER = LoggerFactory.getLogger(JaasPasswordAuthenticator.class);
44
 
45
    private String domain;
46
 
47
    public String getDomain() {
48
        return domain;
49
    }
50
 
51
    public void setDomain(String domain) {
52
        this.domain = domain;
53
    }
54
 
55
    public boolean authenticate(final String username, final String password, final ServerSession session) {
56
        return authenticate(username, password);
57
    }
58
 
59
    public boolean authenticate(final String username, final String password) {
60
        try {
61
            Subject subject = new Subject();
62
            LoginContext loginContext = new LoginContext(domain, subject, new CallbackHandler() {
63
                public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
64
                    for (int i = 0; i < callbacks.length; i++) {
65
                        if (callbacks[i] instanceof NameCallback) {
66
                            ((NameCallback) callbacks[i]).setName(username);
67
                        } else if (callbacks[i] instanceof PasswordCallback) {
68
                            ((PasswordCallback) callbacks[i]).setPassword(password.toCharArray());
69
                        } else {
70
                            throw new UnsupportedCallbackException(callbacks[i]);
71
                        }
72
                    }
73
                }
74
            });
75
            loginContext.login();
76
            loginContext.logout();
77
            return true;
78
        } catch (Exception e) {
79
            LOGGER.error("Authentication failed with error", e);
80
            return false;
81
        }
82
    }
83
 
84
}