001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.client;
018:
019: import javax.security.auth.Subject;
020: import javax.security.auth.callback.Callback;
021: import javax.security.auth.callback.CallbackHandler;
022: import javax.security.auth.callback.NameCallback;
023: import javax.security.auth.callback.PasswordCallback;
024: import javax.security.auth.callback.UnsupportedCallbackException;
025: import javax.security.auth.login.LoginException;
026: import javax.security.auth.spi.LoginModule;
027: import java.io.IOException;
028: import java.net.URI;
029: import java.net.URISyntaxException;
030: import java.util.Map;
031: import java.util.logging.Logger;
032:
033: public class ClientLoginModule implements LoginModule {
034: private static final Logger log = Logger
035: .getLogger("OpenEJB.client");
036: private Subject subject;
037: private CallbackHandler callbackHandler;
038: private String serverUri;
039: private boolean debug;
040:
041: private String user;
042: private Object clientIdentity;
043: private ClientIdentityPrincipal principal;
044: private String realmNameSeparator;
045: private String realmName;
046:
047: public void initialize(Subject subject,
048: CallbackHandler callbackHandler, Map sharedState,
049: Map options) {
050: this .subject = subject;
051: this .callbackHandler = callbackHandler;
052:
053: // determine the server uri
054: serverUri = System.getProperty("openejb.server.uri");
055: if (serverUri == null) {
056: serverUri = (String) options.get("openejb.server.uri");
057: }
058:
059: this .debug = "true".equalsIgnoreCase((String) options
060: .get("debug"));
061: if (debug) {
062: log.config("Initialized ClientLoginModule: debug=" + debug);
063: }
064:
065: if (options.containsKey("RealmNameSeparator")) {
066: realmNameSeparator = (String) options
067: .get("RealmNameSeparator");
068: }
069:
070: if (options.containsKey("RealmName")) {
071: realmName = (String) options.get("RealmName");
072: }
073: }
074:
075: public boolean login() throws LoginException {
076: // determine the server location
077: URI location = null;
078: try {
079: location = new URI(serverUri);
080: } catch (Exception e) {
081: if (serverUri.indexOf("://") == -1) {
082: try {
083: location = new URI("foo://" + serverUri);
084: } catch (URISyntaxException giveUp) {
085: throw new LoginException(
086: "Invalid openejb.server.uri " + serverUri);
087: }
088: }
089: }
090: ServerMetaData server = new ServerMetaData(location);
091:
092: // create the callbacks
093: Callback[] callbacks = new Callback[2];
094: callbacks[0] = new NameCallback("Username: ");
095: callbacks[1] = new PasswordCallback("Password: ", false);
096:
097: // get the call back values (username and password)
098: try {
099: callbackHandler.handle(callbacks);
100: } catch (IOException ioe) {
101: throw new LoginException(ioe.getMessage());
102: } catch (UnsupportedCallbackException uce) {
103: throw new LoginException(uce.getMessage()
104: + " not available to obtain information from user");
105: }
106: user = ((NameCallback) callbacks[0]).getName();
107: char[] tmpPassword = ((PasswordCallback) callbacks[1])
108: .getPassword();
109: if (tmpPassword == null)
110: tmpPassword = new char[0];
111:
112: if (realmNameSeparator != null) {
113: String[] strings = user.split(realmNameSeparator);
114: if (strings.length == 2) {
115: realmName = strings[0];
116: user = strings[1];
117: }
118: }
119:
120: if (realmName != null) {
121: clientIdentity = ClientSecurity.directAuthentication(
122: realmName, user, new String(tmpPassword), server);
123: } else {
124: clientIdentity = ClientSecurity.directAuthentication(user,
125: new String(tmpPassword), server);
126: }
127:
128: if (debug) {
129: log.config("login " + user);
130: }
131: return true;
132: }
133:
134: public boolean commit() throws LoginException {
135: principal = new ClientIdentityPrincipal(user, clientIdentity);
136: subject.getPrincipals().add(principal);
137:
138: if (debug) {
139: log.config("commit");
140: }
141: return true;
142: }
143:
144: public boolean abort() throws LoginException {
145: clear();
146:
147: if (debug) {
148: log.config("abort");
149: }
150: return true;
151: }
152:
153: public boolean logout() throws LoginException {
154: subject.getPrincipals().remove(principal);
155:
156: if (debug) {
157: log.config("logout");
158: }
159: return true;
160: }
161:
162: private void clear() {
163: user = null;
164: clientIdentity = null;
165: principal = null;
166: }
167: }
|