001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.realm;
018:
019: import java.io.IOException;
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:
026: /**
027: * <p>Implementation of the JAAS <strong>CallbackHandler</code> interface,
028: * used to negotiate delivery of the username and credentials that were
029: * specified to our constructor. No interaction with the user is required
030: * (or possible).</p>
031: *
032: * @author Craig R. McClanahan
033: * @version $Revision: 1.3 $ $Date: 2004/02/29 12:38:47 $
034: */
035:
036: public class JAASCallbackHandler implements CallbackHandler {
037:
038: // ------------------------------------------------------------ Constructor
039:
040: /**
041: * Construct a callback handler configured with the specified values.
042: *
043: * @param realm Our associated JAASRealm instance
044: * @param username Username to be authenticated with
045: * @param password Password to be authenticated with
046: */
047: public JAASCallbackHandler(JAASRealm realm, String username,
048: String password) {
049:
050: super ();
051: this .realm = realm;
052: this .username = username;
053: this .password = password;
054:
055: }
056:
057: // ----------------------------------------------------- Instance Variables
058:
059: /**
060: * The password to be authenticated with.
061: */
062: protected String password = null;
063:
064: /**
065: * The associated <code>JAASRealm</code> instance.
066: */
067: protected JAASRealm realm = null;
068:
069: /**
070: * The username to be authenticated with.
071: */
072: protected String username = null;
073:
074: // --------------------------------------------------------- Public Methods
075:
076: /**
077: * Retrieve the information requested in the provided Callbacks. This
078: * implementation only recognizes <code>NameCallback</code> and
079: * <code>PasswordCallback</code> instances.
080: *
081: * @param callbacks The set of callbacks to be processed
082: *
083: * @exception IOException if an input/output error occurs
084: * @exception UnsupportedCallbackException if the login method requests
085: * an unsupported callback type
086: */
087: public void handle(Callback callbacks[]) throws IOException,
088: UnsupportedCallbackException {
089:
090: for (int i = 0; i < callbacks.length; i++) {
091:
092: if (callbacks[i] instanceof NameCallback) {
093: if (realm.getDebug() >= 3)
094: realm.log("Returning username " + username);
095: ((NameCallback) callbacks[i]).setName(username);
096: } else if (callbacks[i] instanceof PasswordCallback) {
097: if (realm.getDebug() >= 3)
098: realm.log("Returning password " + password);
099: final char[] passwordcontents;
100: if (password != null) {
101: passwordcontents = password.toCharArray();
102: } else {
103: passwordcontents = new char[0];
104: }
105: ((PasswordCallback) callbacks[i])
106: .setPassword(passwordcontents);
107: } else {
108: throw new UnsupportedCallbackException(callbacks[i]);
109: }
110:
111: }
112:
113: }
114:
115: }
|