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.security.Principal;
25
26import javax.management.Attribute;
27import javax.management.MBeanServer;
28import javax.management.ObjectName;
29import javax.management.remote.JMXAuthenticator;
30import javax.management.remote.JMXPrincipal;
31import javax.security.auth.Subject;
32
33public final class TestJMXAuthenticator implements JMXAuthenticator {
34
35    private String protocol = "";
36    private MBeanServer mbs = null;
37
38    public TestJMXAuthenticator() {
39    }
40
41    public TestJMXAuthenticator(String protocol) {
42        this.protocol = protocol;
43    }
44
45    public TestJMXAuthenticator(String protocol, MBeanServer mbs) {
46        this.protocol = protocol;
47        this.mbs = mbs;
48    }
49
50    public Subject authenticate(Object credentials) {
51
52        String credentials_username = "";
53        String credentials_password = "";
54        Principal aPrincipal = null;
55
56        credentials_username = ((String[]) credentials)[0];
57        credentials_password = ((String[]) credentials)[1];
58
59        String authenticated_username = System.getProperty("susername");
60        String authenticated_password = System.getProperty("spassword");
61        String principal = System.getProperty("principal");
62
63        System.out.println("TestJMXAuthenticator::authenticate: Start");
64        System.out.println("TestJMXAuthenticator::authenticate: credentials username = " +
65                credentials_username);
66        System.out.println("TestJMXAuthenticator::authenticate: credentials password = " +
67                credentials_password);
68        System.out.println("TestJMXAuthenticator::authenticate: authenticated username = " +
69                authenticated_username);
70        System.out.println("TestJMXAuthenticator::authenticate: authenticated password = " +
71                authenticated_password);
72        System.out.println("TestJMXAuthenticator::authenticate: principal used for " +
73                "authorization = " + principal);
74
75        if (credentials_username.equals(authenticated_username) &&
76                credentials_password.equals(authenticated_password)) {
77            System.out.println("TestJMXAuthenticator::authenticate: " +
78                    "Authenticator should succeed");
79        } else {
80            System.out.println("TestJMXAuthenticator::authenticate: " +
81                    "Authenticator should reject");
82            throw new SecurityException("TestJMXAuthenticator throws EXCEPTION");
83        }
84
85        // At this point, authentication has succeeded
86        // (no SecurityException thrown).
87        //
88        // If no authorization is required, the returned subject (empty or not)
89        // is useless.
90        // Otherwise, the returned subject must define a principal
91        // and authorization will be performed against this principal.
92        //
93        // Note that this custom JMXAuthenticator is used for test purpose and
94        // the username used to perform authentication may be different from the
95        // username used to perform authorization.
96        //
97        Subject subject = new Subject();
98
99        if (principal != null) {
100            System.out.println("TestJMXAuthenticator::authenticate: " +
101                    "Add " + principal + " principal to the returned subject");
102            subject.getPrincipals().add(new JMXPrincipal(principal));
103        }
104
105        return subject;
106    }
107}
108