Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/security/auth/callback/CallbackHandler.java
38918 views
/*1* Copyright (c) 1999, 2013, 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* @see java.security.Security security properties66*/67public interface CallbackHandler {6869/**70* <p> Retrieve or display the information requested in the71* provided Callbacks.72*73* <p> The {@code handle} method implementation checks the74* instance(s) of the {@code Callback} object(s) passed in75* to retrieve or display the requested information.76* The following example is provided to help demonstrate what an77* {@code handle} method implementation might look like.78* This example code is for guidance only. Many details,79* including proper error handling, are left out for simplicity.80*81* <pre>{@code82* public void handle(Callback[] callbacks)83* throws IOException, UnsupportedCallbackException {84*85* for (int i = 0; i < callbacks.length; i++) {86* if (callbacks[i] instanceof TextOutputCallback) {87*88* // display the message according to the specified type89* TextOutputCallback toc = (TextOutputCallback)callbacks[i];90* switch (toc.getMessageType()) {91* case TextOutputCallback.INFORMATION:92* System.out.println(toc.getMessage());93* break;94* case TextOutputCallback.ERROR:95* System.out.println("ERROR: " + toc.getMessage());96* break;97* case TextOutputCallback.WARNING:98* System.out.println("WARNING: " + toc.getMessage());99* break;100* default:101* throw new IOException("Unsupported message type: " +102* toc.getMessageType());103* }104*105* } else if (callbacks[i] instanceof NameCallback) {106*107* // prompt the user for a username108* NameCallback nc = (NameCallback)callbacks[i];109*110* // ignore the provided defaultName111* System.err.print(nc.getPrompt());112* System.err.flush();113* nc.setName((new BufferedReader114* (new InputStreamReader(System.in))).readLine());115*116* } else if (callbacks[i] instanceof PasswordCallback) {117*118* // prompt the user for sensitive information119* PasswordCallback pc = (PasswordCallback)callbacks[i];120* System.err.print(pc.getPrompt());121* System.err.flush();122* pc.setPassword(readPassword(System.in));123*124* } else {125* throw new UnsupportedCallbackException126* (callbacks[i], "Unrecognized Callback");127* }128* }129* }130*131* // Reads user password from given input stream.132* private char[] readPassword(InputStream in) throws IOException {133* // insert code to read a user password from the input stream134* }135* }</pre>136*137* @param callbacks an array of {@code Callback} objects provided138* by an underlying security service which contains139* the information requested to be retrieved or displayed.140*141* @exception java.io.IOException if an input or output error occurs. <p>142*143* @exception UnsupportedCallbackException if the implementation of this144* method does not support one or more of the Callbacks145* specified in the {@code callbacks} parameter.146*/147void handle(Callback[] callbacks)148throws java.io.IOException, UnsupportedCallbackException;149}150151152