01: /*
02: * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.wso2.esb.jmx;
17:
18: import org.wso2.esb.ServiceBusManager;
19:
20: import javax.management.remote.JMXAuthenticator;
21: import javax.management.remote.JMXPrincipal;
22: import javax.security.auth.Subject;
23: import java.util.Collections;
24:
25: /**
26: *
27: */
28:
29: public class ESBJMXAuthenticator implements JMXAuthenticator {
30:
31: public Subject authenticate(Object credentials) {
32:
33: // if the credentials are null
34: if (credentials == null) {
35: throw new SecurityException("Credentials required");
36: }
37:
38: // credentials must be type of String[]
39: if (!(credentials instanceof String[])) {
40: throw new SecurityException(
41: "Credentials should be String[]");
42: }
43:
44: // Only expect username/password, therefore the credentials should have two entries
45: final String[] aCredentials = (String[]) credentials;
46: if (aCredentials.length < 2) {
47: throw new SecurityException(
48: "Credentials should have at least username & password");
49: }
50:
51: // Perform authentication
52: String username = aCredentials[0];
53: String password = aCredentials[1];
54:
55: // perform authentication
56: boolean authenticated = ServiceBusManager.getInstance()
57: .authenticate(username, password);
58:
59: if (authenticated) {
60: return new Subject(true, Collections
61: .singleton(new JMXPrincipal(username)),
62: Collections.EMPTY_SET, Collections.EMPTY_SET);
63: } else {
64: throw new SecurityException(
65: "Username and/or password are incorrect, "
66: + "or you do not have the necessary access rights.");
67: }
68: }
69: }
|