001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.realm;
019:
020: import java.io.IOException;
021: import javax.security.auth.callback.Callback;
022: import javax.security.auth.callback.CallbackHandler;
023: import javax.security.auth.callback.NameCallback;
024: import javax.security.auth.callback.PasswordCallback;
025: import javax.security.auth.callback.UnsupportedCallbackException;
026:
027: import org.apache.catalina.util.StringManager;
028:
029: /**
030: * <p>Implementation of the JAAS <code>CallbackHandler</code> interface,
031: * used to negotiate delivery of the username and credentials that were
032: * specified to our constructor. No interaction with the user is required
033: * (or possible).</p>
034: *
035: * <p>This <code>CallbackHandler</code> will pre-digest the supplied
036: * password, if required by the <code><Realm></code> element in
037: * <code>server.xml</code>.</p>
038: * <p>At present, <code>JAASCallbackHandler</code> knows how to handle callbacks of
039: * type <code>javax.security.auth.callback.NameCallback</code> and
040: * <code>javax.security.auth.callback.PasswordCallback</code>.</p>
041: *
042: * @author Craig R. McClanahan
043: * @author Andrew R. Jaquith
044: * @version $Revision: 543691 $ $Date: 2007-06-02 03:37:08 +0200 (sam., 02 juin 2007) $
045: */
046:
047: public class JAASCallbackHandler implements CallbackHandler {
048:
049: // ------------------------------------------------------------ Constructor
050:
051: /**
052: * Construct a callback handler configured with the specified values.
053: * Note that if the <code>JAASRealm</code> instance specifies digested passwords,
054: * the <code>password</code> parameter will be pre-digested here.
055: *
056: * @param realm Our associated JAASRealm instance
057: * @param username Username to be authenticated with
058: * @param password Password to be authenticated with
059: */
060: public JAASCallbackHandler(JAASRealm realm, String username,
061: String password) {
062:
063: super ();
064: this .realm = realm;
065: this .username = username;
066:
067: if (realm.hasMessageDigest()) {
068: this .password = realm.digest(password);
069: } else {
070: this .password = password;
071: }
072: }
073:
074: // ----------------------------------------------------- Instance Variables
075:
076: /**
077: * The string manager for this package.
078: */
079: protected static final StringManager sm = StringManager
080: .getManager(Constants.Package);
081:
082: /**
083: * The password to be authenticated with.
084: */
085: protected String password = null;
086:
087: /**
088: * The associated <code>JAASRealm</code> instance.
089: */
090: protected JAASRealm realm = null;
091:
092: /**
093: * The username to be authenticated with.
094: */
095: protected String username = null;
096:
097: // --------------------------------------------------------- Public Methods
098:
099: /**
100: * Retrieve the information requested in the provided <code>Callbacks</code>.
101: * This implementation only recognizes <code>NameCallback</code> and
102: * <code>PasswordCallback</code> instances.
103: *
104: * @param callbacks The set of <code>Callback</code>s to be processed
105: *
106: * @exception IOException if an input/output error occurs
107: * @exception UnsupportedCallbackException if the login method requests
108: * an unsupported callback type
109: */
110: public void handle(Callback callbacks[]) throws IOException,
111: UnsupportedCallbackException {
112:
113: for (int i = 0; i < callbacks.length; i++) {
114:
115: if (callbacks[i] instanceof NameCallback) {
116: if (realm.getContainer().getLogger().isTraceEnabled())
117: realm.getContainer().getLogger().trace(
118: sm.getString("jaasCallback.username",
119: username));
120: ((NameCallback) callbacks[i]).setName(username);
121: } else if (callbacks[i] instanceof PasswordCallback) {
122: final char[] passwordcontents;
123: if (password != null) {
124: passwordcontents = password.toCharArray();
125: } else {
126: passwordcontents = new char[0];
127: }
128: ((PasswordCallback) callbacks[i])
129: .setPassword(passwordcontents);
130: } else {
131: throw new UnsupportedCallbackException(callbacks[i]);
132: }
133: }
134: }
135: }
|