001    /* CallbackHandler.java -- base interface for callback handlers.
002       Copyright (C) 2003, Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package javax.security.auth.callback;
040    
041    import java.io.IOException;
042    
043    /**
044     * <p>An application implements a <code>CallbackHandler</code> and passes it to
045     * underlying security services so that they may interact with the application
046     * to retrieve specific authentication data, such as usernames and passwords, or
047     * to display certain information, such as error and warning messages.</p>
048     *
049     * <p><code>CallbackHandler</code>s are implemented in an application-dependent
050     * fashion. For example, implementations for an application with a graphical
051     * user interface (GUI) may pop up windows to prompt for requested information
052     * or to display error messages. An implementation may also choose to obtain
053     * requested information from an alternate source without asking the end user.</p>
054     *
055     * <p>Underlying security services make requests for different types of
056     * information by passing individual Callbacks to the <code>CallbackHandler</code>.
057     * The <code>CallbackHandler</code> implementation decides how to retrieve and
058     * display information depending on the {@link Callback}s passed to it. For
059     * example, if the underlying service needs a username and password to
060     * authenticate a user, it uses a {@link NameCallback} and
061     * {@link PasswordCallback}. The <code>CallbackHandler</code> can then choose
062     * to prompt for a username and password serially, or to prompt for both in a
063     * single window.</p>
064     *
065     * <p>A default <code>CallbackHandler</code> class implementation may be
066     * specified in the <code>auth.login.defaultCallbackHandler</code> security
067     * property. The security property can be set in the Java security properties
068     * file located in the file named
069     * <code>&lt;JAVA_HOME>/lib/security/java.security</code>, where
070     * <code>&lt;JAVA_HOME></code> refers to the directory where the SDK was
071     * installed.</p>
072     *
073     * <p>If the security property is set to the fully qualified name of a
074     * <code>CallbackHandler</code> implementation class, then a
075     * <code>LoginContext</code>will load the specified <code>CallbackHandler</code>
076     * and pass it to the underlying <code>LoginModules</code>. The
077     * <code>LoginContext</code> only loads the default handler if one was not
078     * provided.</p>
079     *
080     * <p>All default handler implementations must provide a public zero-argument
081     * constructor.</p>
082     *
083     */
084    public interface CallbackHandler
085    {
086    
087      /**
088       * <p>Retrieve or display the information requested in the provided
089       * {@link Callback}s.</p>
090       *
091       * <p>The <code>handle()</code> method implementation checks the instance(s)
092       * of the {@link Callback} object(s) passed in to retrieve or display the
093       * requested information. The following example is provided to help
094       * demonstrate what an <code>handle()</code> method implementation might look
095       * like. This example code is for guidance only. Many details, including
096       * proper error handling, are left out for simplicity.</p>
097       *
098       * <pre>
099       *public void handle(Callback[] callbacks)
100       *throws IOException, UnsupportedCallbackException {
101       *   for (int i = 0; i < callbacks.length; i++) {
102       *      if (callbacks[i] instanceof TextOutputCallback) {
103       *         // display the message according to the specified type
104       *         TextOutputCallback toc = (TextOutputCallback)callbacks[i];
105       *         switch (toc.getMessageType()) {
106       *         case TextOutputCallback.INFORMATION:
107       *            System.out.println(toc.getMessage());
108       *            break;
109       *         case TextOutputCallback.ERROR:
110       *            System.out.println("ERROR: " + toc.getMessage());
111       *            break;
112       *         case TextOutputCallback.WARNING:
113       *            System.out.println("WARNING: " + toc.getMessage());
114       *            break;
115       *         default:
116       *            throw new IOException("Unsupported message type: "
117       *                  + toc.getMessageType());
118       *         }
119       *      } else if (callbacks[i] instanceof NameCallback) {
120       *         // prompt the user for a username
121       *         NameCallback nc = (NameCallback)callbacks[i];
122       *         // ignore the provided defaultName
123       *         System.err.print(nc.getPrompt());
124       *         System.err.flush();
125       *         nc.setName((new BufferedReader(
126       *               new InputStreamReader(System.in))).readLine());
127       *      } else if (callbacks[i] instanceof PasswordCallback) {
128       *         // prompt the user for sensitive information
129       *         PasswordCallback pc = (PasswordCallback)callbacks[i];
130       *         System.err.print(pc.getPrompt());
131       *         System.err.flush();
132       *         pc.setPassword(readPassword(System.in));
133       *      } else {
134       *         throw new UnsupportedCallbackException(
135       *               callbacks[i], "Unrecognized Callback");
136       *      }
137       *   }
138       *}
139       *
140       * // Reads user password from given input stream.
141       *private char[] readPassword(InputStream in) throws IOException {
142       *   // insert code to read a user password from the input stream
143       *}
144       * </pre>
145       *
146       * @param callbacks an array of {@link Callback} objects provided by an
147       * underlying security service which contains the information requested to
148       * be retrieved or displayed.
149       * @throws IOException if an input or output error occurs.
150       * @throws UnsupportedCallbackException if the implementation of this method
151       * does not support one or more of the Callbacks specified in the
152       * <code>callbacks</code> parameter.
153       */
154      void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException;
155    }