1/* 2 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import javax.security.auth.callback.*; 25import java.util.*; 26import java.io.*; 27import javax.security.sasl.RealmCallback; 28import javax.security.sasl.RealmChoiceCallback; 29import com.sun.security.auth.callback.TextCallbackHandler; 30 31public final class ClientCallbackHandler extends TextCallbackHandler { 32 private String username = "john"; 33 private String password = "test123"; 34 private boolean auto; 35 36 public ClientCallbackHandler(boolean auto) { 37 super(); 38 this.auto = auto; 39 } 40 41 public void handle(Callback[] callbacks) throws UnsupportedCallbackException, 42 IOException { 43 NameCallback ncb = null; 44 PasswordCallback pcb = null; 45 RealmChoiceCallback rccb = null; 46 47 List namePw = new ArrayList(3); 48 49 for (int i = 0; i < callbacks.length; i++) { 50 if (callbacks[i] instanceof NameCallback) { 51 if (auto) { 52 ((NameCallback)callbacks[i]).setName(username); 53 } else { 54 // To be processed by TextCallbackHandler 55 namePw.add(callbacks[i]); 56 } 57 } else if (callbacks[i] instanceof PasswordCallback) { 58 if (auto) { 59 ((PasswordCallback)callbacks[i]).setPassword( 60 password.toCharArray()); 61 } else { 62 // To be processed by TextCallbackHandler 63 namePw.add(callbacks[i]); 64 } 65 } else if (callbacks[i] instanceof RealmChoiceCallback) { 66 RealmChoiceCallback rcb = (RealmChoiceCallback) callbacks[i]; 67 if (!auto) { 68 System.err.println(rcb.getPrompt()); 69 } 70 71 String[] choices = rcb.getChoices(); 72 73 if (!auto) { 74 for (int j=0; j < choices.length; j++) { 75 System.err.println(j + ":" + choices[j]); 76 } 77 } 78 79 int selection; 80 if (auto) { 81 selection = 0; 82 } else { 83 System.err.print("Enter choice number: "); 84 String result = readLine(); 85 if (result.equals("")) { 86 selection = rcb.getDefaultChoice(); 87 } else { 88 selection = Integer.parseInt(result); 89 } 90 } 91 rcb.setSelectedIndex(selection); 92 93 } else if (callbacks[i] instanceof RealmCallback) { 94 RealmCallback rcb = (RealmCallback) callbacks[i]; 95 String realm = rcb.getDefaultText(); 96 97 if (auto) { 98 if (realm != null) { 99 rcb.setText(realm); 100 } 101 } else { 102 if (realm == null) { 103 System.err.print(rcb.getPrompt()); 104 } else { 105 System.err.print(rcb.getPrompt() + " [" + realm + "] "); 106 } 107 108 System.err.flush(); 109 110 String result = readLine(); 111 if (result.equals("")) { 112 result = realm; 113 } 114 rcb.setText(result); 115 } 116 } else { 117 throw new UnsupportedCallbackException(callbacks[i]); 118 } 119 } 120 121 // Process name/password callbacks using superclass 122 if (namePw.size() > 0) { 123 Callback[] np = new Callback[namePw.size()]; 124 namePw.toArray(np); 125 126 super.handle(np); 127 } 128 } 129 130 /* Reads a line of input */ 131 private String readLine() throws IOException { 132 return new BufferedReader 133 (new InputStreamReader(System.in)).readLine(); 134 } 135} 136