[6145] | 1 | /* |
---|
| 2 | * To change this template, choose Tools | Templates |
---|
| 3 | * and open the template in the editor. |
---|
| 4 | */ |
---|
| 5 | package handler; |
---|
| 6 | |
---|
| 7 | import java.io.BufferedReader; |
---|
| 8 | import java.io.IOException; |
---|
| 9 | import java.io.InputStream; |
---|
| 10 | import java.io.InputStreamReader; |
---|
| 11 | import java.io.PushbackInputStream; |
---|
| 12 | import java.util.Arrays; |
---|
| 13 | import javax.security.auth.callback.Callback; |
---|
| 14 | import javax.security.auth.callback.CallbackHandler; |
---|
| 15 | import javax.security.auth.callback.NameCallback; |
---|
| 16 | import javax.security.auth.callback.PasswordCallback; |
---|
| 17 | import javax.security.auth.callback.UnsupportedCallbackException; |
---|
| 18 | |
---|
| 19 | /** |
---|
| 20 | * Username/Password Callbackhandler |
---|
| 21 | * @author MartinH |
---|
| 22 | */ |
---|
| 23 | public class UserNameHandler implements CallbackHandler { |
---|
| 24 | |
---|
| 25 | static String user; |
---|
| 26 | static String pwd; |
---|
| 27 | |
---|
| 28 | static { |
---|
| 29 | setUser("okbtester"); |
---|
| 30 | setPassword("okbtester"); |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | public static void setUser(String userName) { |
---|
| 34 | user = userName; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | public static void setPassword(String passwd) { |
---|
| 38 | pwd = passwd; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * Invoke an array of Callbacks. |
---|
| 43 | * |
---|
| 44 | * <p> |
---|
| 45 | * |
---|
| 46 | * @param callbacks an array of <code>Callback</code> objects which contain |
---|
| 47 | * the information requested by an underlying security |
---|
| 48 | * service to be retrieved or displayed. |
---|
| 49 | * |
---|
| 50 | * @exception java.io.IOException if an input or output error occurs. <p> |
---|
| 51 | * |
---|
| 52 | * @exception UnsupportedCallbackException if the implementation of this |
---|
| 53 | * method does not support one or more of the Callbacks |
---|
| 54 | * specified in the <code>callbacks</code> parameter. |
---|
| 55 | */ |
---|
| 56 | public void handle(Callback[] callbacks) |
---|
| 57 | throws IOException, UnsupportedCallbackException { |
---|
| 58 | |
---|
| 59 | for (int i = 0; i < callbacks.length; i++) { |
---|
| 60 | if (callbacks[i] instanceof NameCallback) { |
---|
| 61 | |
---|
| 62 | // prompt the user for a username |
---|
| 63 | NameCallback nc = (NameCallback) callbacks[i]; |
---|
| 64 | |
---|
| 65 | System.err.print(nc.getPrompt()); |
---|
| 66 | System.err.flush(); |
---|
| 67 | nc.setName(user); |
---|
| 68 | //nc.setName((new BufferedReader(new InputStreamReader(System.in))).readLine()); |
---|
| 69 | |
---|
| 70 | } else if (callbacks[i] instanceof PasswordCallback) { |
---|
| 71 | |
---|
| 72 | // prompt the user for sensitive information |
---|
| 73 | PasswordCallback pc = (PasswordCallback) callbacks[i]; |
---|
| 74 | System.err.print(pc.getPrompt()); |
---|
| 75 | System.err.flush(); |
---|
| 76 | |
---|
| 77 | pc.setPassword(pwd.toCharArray()); |
---|
| 78 | //pc.setPassword(readPassword(System.in)); |
---|
| 79 | |
---|
| 80 | } else { |
---|
| 81 | throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback"); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | // Reads user password from given input stream. |
---|
| 87 | private char[] readPassword(InputStream in) throws IOException { |
---|
| 88 | |
---|
| 89 | char[] lineBuffer; |
---|
| 90 | char[] buf; |
---|
| 91 | int i; |
---|
| 92 | |
---|
| 93 | buf = lineBuffer = new char[128]; |
---|
| 94 | |
---|
| 95 | int room = buf.length; |
---|
| 96 | int offset = 0; |
---|
| 97 | int c; |
---|
| 98 | |
---|
| 99 | loop: |
---|
| 100 | while (true) { |
---|
| 101 | switch (c = in.read()) { |
---|
| 102 | case -1: |
---|
| 103 | case '\n': |
---|
| 104 | break loop; |
---|
| 105 | |
---|
| 106 | case '\r': |
---|
| 107 | int c2 = in.read(); |
---|
| 108 | if ((c2 != '\n') && (c2 != -1)) { |
---|
| 109 | if (!(in instanceof PushbackInputStream)) { |
---|
| 110 | in = new PushbackInputStream(in); |
---|
| 111 | } |
---|
| 112 | ((PushbackInputStream) in).unread(c2); |
---|
| 113 | } else { |
---|
| 114 | break loop; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | default: |
---|
| 118 | if (--room < 0) { |
---|
| 119 | buf = new char[offset + 128]; |
---|
| 120 | room = buf.length - offset - 1; |
---|
| 121 | System.arraycopy(lineBuffer, 0, buf, 0, offset); |
---|
| 122 | Arrays.fill(lineBuffer, ' '); |
---|
| 123 | lineBuffer = buf; |
---|
| 124 | } |
---|
| 125 | buf[offset++] = (char) c; |
---|
| 126 | break; |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | if (offset == 0) { |
---|
| 131 | return null; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | char[] ret = new char[offset]; |
---|
| 135 | System.arraycopy(buf, 0, ret, 0, offset); |
---|
| 136 | Arrays.fill(buf, ' '); |
---|
| 137 | |
---|
| 138 | return ret; |
---|
| 139 | } |
---|
| 140 | } |
---|