Path: blob/master/src/java.base/share/classes/javax/security/auth/callback/CallbackHandler.java
41161 views
/*1* Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.security.auth.callback;2627/**28* <p> An application implements a {@code CallbackHandler} and passes29* it to underlying security services so that they may interact with30* the application to retrieve specific authentication data,31* such as usernames and passwords, or to display certain information,32* such as error and warning messages.33*34* <p> CallbackHandlers are implemented in an application-dependent fashion.35* For example, implementations for an application with a graphical user36* interface (GUI) may pop up windows to prompt for requested information37* or to display error messages. An implementation may also choose to obtain38* requested information from an alternate source without asking the end user.39*40* <p> Underlying security services make requests for different types41* of information by passing individual Callbacks to the42* {@code CallbackHandler}. The {@code CallbackHandler}43* implementation decides how to retrieve and display information44* depending on the Callbacks passed to it. For example,45* if the underlying service needs a username and password to46* authenticate a user, it uses a {@code NameCallback} and47* {@code PasswordCallback}. The {@code CallbackHandler}48* can then choose to prompt for a username and password serially,49* or to prompt for both in a single window.50*51* <p> A default {@code CallbackHandler} class implementation52* may be specified by setting the value of the53* {@code auth.login.defaultCallbackHandler} security property.54*55* <p> If the security property is set to the fully qualified name of a56* {@code CallbackHandler} implementation class,57* then a {@code LoginContext} will load the specified58* {@code CallbackHandler} and pass it to the underlying LoginModules.59* The {@code LoginContext} only loads the default handler60* if it was not provided one.61*62* <p> All default handler implementations must provide a public63* zero-argument constructor.64*65* @since 1.466* @see java.security.Security security properties67*/68public interface CallbackHandler {6970/**71* <p> Retrieve or display the information requested in the72* provided Callbacks.73*74* <p> The {@code handle} method implementation checks the75* instance(s) of the {@code Callback} object(s) passed in76* to retrieve or display the requested information.77* The following example is provided to help demonstrate what an78* {@code handle} method implementation might look like.79* This example code is for guidance only. Many details,80* including proper error handling, are left out for simplicity.81*82* <pre>{@code83* public void handle(Callback[] callbacks)84* throws IOException, UnsupportedCallbackException {85*86* for (int i = 0; i < callbacks.length; i++) {87* if (callbacks[i] instanceof TextOutputCallback) {88*89* // display the message according to the specified type90* TextOutputCallback toc = (TextOutputCallback)callbacks[i];91* switch (toc.getMessageType()) {92* case TextOutputCallback.INFORMATION:93* System.out.println(toc.getMessage());94* break;95* case TextOutputCallback.ERROR:96* System.out.println("ERROR: " + toc.getMessage());97* break;98* case TextOutputCallback.WARNING:99* System.out.println("WARNING: " + toc.getMessage());100* break;101* default:102* throw new IOException("Unsupported message type: " +103* toc.getMessageType());104* }105*106* } else if (callbacks[i] instanceof NameCallback) {107*108* // prompt the user for a username109* NameCallback nc = (NameCallback)callbacks[i];110*111* // ignore the provided defaultName112* System.err.print(nc.getPrompt());113* System.err.flush();114* nc.setName((new BufferedReader115* (new InputStreamReader(System.in))).readLine());116*117* } else if (callbacks[i] instanceof PasswordCallback) {118*119* // prompt the user for sensitive information120* PasswordCallback pc = (PasswordCallback)callbacks[i];121* System.err.print(pc.getPrompt());122* System.err.flush();123* pc.setPassword(readPassword(System.in));124*125* } else {126* throw new UnsupportedCallbackException127* (callbacks[i], "Unrecognized Callback");128* }129* }130* }131*132* // Reads user password from given input stream.133* private char[] readPassword(InputStream in) throws IOException {134* // insert code to read a user password from the input stream135* }136* }</pre>137*138* @param callbacks an array of {@code Callback} objects provided139* by an underlying security service which contains140* the information requested to be retrieved or displayed.141*142* @exception java.io.IOException if an input or output error occurs.143*144* @exception UnsupportedCallbackException if the implementation of this145* method does not support one or more of the Callbacks146* specified in the {@code callbacks} parameter.147*/148void handle(Callback[] callbacks)149throws java.io.IOException, UnsupportedCallbackException;150}151152153