jdk/src/share/classes/javax/security/auth/callback/CallbackHandler.java
author juh
Tue, 16 Jul 2013 12:19:41 -0700
changeset 18830 90956ead732f
parent 18156 edb590d448c5
permissions -rw-r--r--
8020557: javadoc cleanup in javax.security Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.security.auth.callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    29
 * <p> An application implements a {@code CallbackHandler} and passes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * it to underlying security services so that they may interact with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * the application to retrieve specific authentication data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * such as usernames and passwords, or to display certain information,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * such as error and warning messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p> CallbackHandlers are implemented in an application-dependent fashion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * For example, implementations for an application with a graphical user
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * interface (GUI) may pop up windows to prompt for requested information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * or to display error messages.  An implementation may also choose to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * requested information from an alternate source without asking the end user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p> Underlying security services make requests for different types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * of information by passing individual Callbacks to the
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    43
 * {@code CallbackHandler}.  The {@code CallbackHandler}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * implementation decides how to retrieve and display information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * depending on the Callbacks passed to it.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * if the underlying service needs a username and password to
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    47
 * authenticate a user, it uses a {@code NameCallback} and
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    48
 * {@code PasswordCallback}.  The {@code CallbackHandler}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * can then choose to prompt for a username and password serially,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * or to prompt for both in a single window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    52
 * <p> A default {@code CallbackHandler} class implementation
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    53
 * may be specified by setting the value of the
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    54
 * {@code auth.login.defaultCallbackHandler} security property.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p> If the security property is set to the fully qualified name of a
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    57
 * {@code CallbackHandler} implementation class,
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    58
 * then a {@code LoginContext} will load the specified
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    59
 * {@code CallbackHandler} and pass it to the underlying LoginModules.
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    60
 * The {@code LoginContext} only loads the default handler
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * if it was not provided one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p> All default handler implementations must provide a public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * zero-argument constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    66
 * @see java.security.Security security properties
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public interface CallbackHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <p> Retrieve or display the information requested in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * provided Callbacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    74
     * <p> The {@code handle} method implementation checks the
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    75
     * instance(s) of the {@code Callback} object(s) passed in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * to retrieve or display the requested information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * The following example is provided to help demonstrate what an
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    78
     * {@code handle} method implementation might look like.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * This example code is for guidance only.  Many details,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * including proper error handling, are left out for simplicity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14775
diff changeset
    82
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * public void handle(Callback[] callbacks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * throws IOException, UnsupportedCallbackException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *   for (int i = 0; i < callbacks.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *      if (callbacks[i] instanceof TextOutputCallback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *          // display the message according to the specified type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *          TextOutputCallback toc = (TextOutputCallback)callbacks[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *          switch (toc.getMessageType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *          case TextOutputCallback.INFORMATION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *              System.out.println(toc.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *          case TextOutputCallback.ERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *              System.out.println("ERROR: " + toc.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *          case TextOutputCallback.WARNING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *              System.out.println("WARNING: " + toc.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *              throw new IOException("Unsupported message type: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *                                  toc.getMessageType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *      } else if (callbacks[i] instanceof NameCallback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *          // prompt the user for a username
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *          NameCallback nc = (NameCallback)callbacks[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *          // ignore the provided defaultName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *          System.err.print(nc.getPrompt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *          System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *          nc.setName((new BufferedReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *                  (new InputStreamReader(System.in))).readLine());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *      } else if (callbacks[i] instanceof PasswordCallback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *          // prompt the user for sensitive information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *          PasswordCallback pc = (PasswordCallback)callbacks[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *          System.err.print(pc.getPrompt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *          System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *          pc.setPassword(readPassword(System.in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *          throw new UnsupportedCallbackException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *                  (callbacks[i], "Unrecognized Callback");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * // Reads user password from given input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * private char[] readPassword(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *    // insert code to read a user password from the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * }
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14775
diff changeset
   136
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   138
     * @param callbacks an array of {@code Callback} objects provided
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *          by an underlying security service which contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *          the information requested to be retrieved or displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @exception java.io.IOException if an input or output error occurs. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @exception UnsupportedCallbackException if the implementation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *          method does not support one or more of the Callbacks
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   146
     *          specified in the {@code callbacks} parameter.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    void handle(Callback[] callbacks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    throws java.io.IOException, UnsupportedCallbackException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
}