1/*
2 * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.util.Map;
25
26import javax.security.auth.Subject;
27import javax.security.auth.callback.Callback;
28import javax.security.auth.callback.CallbackHandler;
29import javax.security.auth.callback.NameCallback;
30import javax.security.auth.callback.PasswordCallback;
31import javax.security.auth.login.LoginException;
32import javax.security.auth.spi.LoginModule;
33
34
35public final class TestSampleLoginModule implements LoginModule {
36
37    private Subject subject;
38    private CallbackHandler callbackHandler;
39    private Map<String, ?> sharedState;
40    private Map<String, ?> options;
41
42    public TestSampleLoginModule() {
43    }
44
45    public void initialize(Subject subject,
46            CallbackHandler callbackHandler,
47            Map<String,?> sharedState,
48            Map<String,?> options) {
49
50        this.subject = subject;
51        this.callbackHandler = callbackHandler;
52        this.sharedState = sharedState;
53        this.options = options;
54    }
55
56  /*
57   * Authenticate the user by comparing the values of the java properties
58   * (username and password) against the values of the credentials.
59   * */
60    public boolean login() throws LoginException {
61
62        String credentials_username = null;
63        String credentials_password = null;
64        String authenticated_username = System.getProperty("susername");
65        String authenticated_password = System.getProperty("spassword");
66
67        System.out.println("TestSampleLoginModule::login: Start");
68
69        // First retreive the credentials {username, password} from
70        // the callback handler
71        Callback[] callbacks = new Callback[2];
72        callbacks[0] = new NameCallback("username");
73        callbacks[1] = new PasswordCallback("password", false);
74        try {
75            callbackHandler.handle(callbacks);
76            credentials_username = ((NameCallback)callbacks[0]).getName();
77            credentials_password = new String(((PasswordCallback)callbacks[1]).
78                    getPassword());
79        } catch (Exception e) {
80            throw new LoginException(e.toString());
81        }
82
83        System.out.println("TestSampleLoginModule::login: credentials username = " +
84                credentials_username);
85        System.out.println("TestSampleLoginModule::login: credentials password = " +
86                credentials_password);
87        System.out.println("TestSampleLoginModule::login: authenticated username = " +
88                authenticated_username);
89        System.out.println("TestSampleLoginModule::login: authenticated password = " +
90                authenticated_password);
91
92        if (credentials_username.equals(authenticated_username) &&
93                credentials_password.equals(authenticated_password)) {
94            System.out.println("TestSampleLoginModule::login: " +
95                    "Authentication should succeed");
96            return true;
97        } else {
98            System.out.println("TestSampleLoginModule::login: " +
99                    "Authentication should reject");
100            throw new LoginException("TestSampleLoginModule throws EXCEPTION");
101        }
102    }
103
104    public boolean commit() throws LoginException {
105        return true;
106    }
107
108    public boolean abort() throws LoginException {
109        return true;
110    }
111
112    public boolean logout() throws LoginException {
113        return true;
114    }
115}
116