01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.servicemix.jbi.jmx;
18:
19: import javax.management.remote.JMXAuthenticator;
20: import javax.security.auth.Subject;
21: import javax.security.auth.login.LoginException;
22:
23: import org.apache.servicemix.jbi.security.auth.AuthenticationService;
24: import org.apache.servicemix.jbi.security.auth.impl.JAASAuthenticationService;
25:
26: /**
27: *
28: * @author gnodet
29: * @org.apache.xbean.XBean element="jmxJaasAuthenticator"
30: */
31: public class JaasAuthenticator implements JMXAuthenticator {
32:
33: private String domain = "servicemix-domain";
34: private AuthenticationService authenticationService = new JAASAuthenticationService();
35:
36: /**
37: * The authentication service can be used to customize the authentication
38: * mechanism used by this authenticator. It defaults to a
39: * JAASAuthenticationService which delegates calls to the JAAS layer.
40: *
41: * @return the authenticationService
42: */
43: public AuthenticationService getAuthenticationService() {
44: return authenticationService;
45: }
46:
47: /**
48: * @param authenticationService the authenticationService to set
49: */
50: public void setAuthenticationService(
51: AuthenticationService authenticationService) {
52: this .authenticationService = authenticationService;
53: }
54:
55: /**
56: * @return the JAAS domain to use for authentication
57: */
58: public String getDomain() {
59: return domain;
60: }
61:
62: /**
63: * @param domain the JAAS domain to use for authentication
64: */
65: public void setDomain(String domain) {
66: this .domain = domain;
67: }
68:
69: /* (non-Javadoc)
70: * @see javax.management.remote.JMXAuthenticator#authenticate(java.lang.Object)
71: */
72: public Subject authenticate(Object credentials)
73: throws SecurityException {
74: if (!(credentials instanceof String[])) {
75: throw new IllegalArgumentException(
76: "Expected String[2], got "
77: + (credentials != null ? credentials
78: .getClass().getName() : null));
79: }
80: String[] params = (String[]) credentials;
81: if (params.length != 2) {
82: throw new IllegalArgumentException(
83: "Expected String[2] but length was "
84: + params.length);
85: }
86: Subject subject = new Subject();
87: try {
88: authenticationService.authenticate(subject, domain,
89: params[0], params[1]);
90: } catch (LoginException e) {
91: throw new SecurityException("Authentication failed", e);
92: } catch (Exception e) {
93: throw new SecurityException(
94: "Error occured while authenticating", e);
95: }
96: return subject;
97: }
98:
99: }
|