00001
00027 package org.objectweb.jonas.security.auth.callback;
00028
00029 import java.io.IOException;
00030 import java.io.PushbackInputStream;
00031 import java.io.InputStream;
00032 import java.io.BufferedReader;
00033 import java.io.InputStreamReader;
00034 import java.util.Arrays;
00035
00036 import javax.security.auth.callback.CallbackHandler;
00037 import javax.security.auth.callback.Callback;
00038 import javax.security.auth.callback.NameCallback;
00039 import javax.security.auth.callback.PasswordCallback;
00040 import javax.security.auth.callback.TextOutputCallback;
00041 import javax.security.auth.callback.UnsupportedCallbackException;
00042
00050 public class LoginCallbackHandler implements CallbackHandler {
00051
00055 private static final int BUFFER_SIZE = 128;
00056
00067 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
00068
00069 for (int i = 0; i < callbacks.length; i++) {
00070 if (callbacks[i] instanceof TextOutputCallback) {
00071
00072
00073 TextOutputCallback toc = (TextOutputCallback) callbacks[i];
00074 switch (toc.getMessageType()) {
00075 case TextOutputCallback.INFORMATION:
00076 System.out.println(toc.getMessage());
00077 break;
00078 case TextOutputCallback.ERROR:
00079 System.out.println("ERROR: " + toc.getMessage());
00080 break;
00081 case TextOutputCallback.WARNING:
00082 System.out.println("WARNING: " + toc.getMessage());
00083 break;
00084 default:
00085 throw new IOException("Unsupported message type: " + toc.getMessageType());
00086 }
00087
00088 } else if (callbacks[i] instanceof NameCallback) {
00089
00090
00091 NameCallback nc = (NameCallback) callbacks[i];
00092
00093 System.err.print(nc.getPrompt());
00094 System.err.flush();
00095 nc.setName((new BufferedReader(new InputStreamReader(System.in))).readLine());
00096
00097 } else if (callbacks[i] instanceof PasswordCallback) {
00098
00099
00100 PasswordCallback pc = (PasswordCallback) callbacks[i];
00101 System.err.print(pc.getPrompt());
00102 System.err.flush();
00103 pc.setPassword(readPassword(System.in));
00104 } else {
00105 throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
00106 }
00107 }
00108 }
00109
00116 private char[] readPassword(InputStream in) throws IOException {
00117
00118 char[] lineBuffer;
00119 char[] buf;
00120 int i;
00121
00122 lineBuffer = new char[BUFFER_SIZE];
00123 buf = lineBuffer;
00124
00125 int room = buf.length;
00126 int offset = 0;
00127 int c;
00128
00129 loop: while (true) {
00130 switch (c = in.read()) {
00131 case -1:
00132 case '\n':
00133 break loop;
00134
00135 case '\r':
00136 int c2 = in.read();
00137 if ((c2 != '\n') && (c2 != -1)) {
00138 if (!(in instanceof PushbackInputStream)) {
00139 in = new PushbackInputStream(in);
00140 }
00141 ((PushbackInputStream) in).unread(c2);
00142 } else {
00143 break loop;
00144 }
00145 default:
00146 if (--room < 0) {
00147 buf = new char[offset + BUFFER_SIZE];
00148 room = buf.length - offset - 1;
00149 System.arraycopy(lineBuffer, 0, buf, 0, offset);
00150 Arrays.fill(lineBuffer, ' ');
00151 lineBuffer = buf;
00152 }
00153 buf[offset++] = (char) c;
00154 break;
00155 }
00156 }
00157
00158 if (offset == 0) {
00159 return null;
00160 }
00161
00162 char[] ret = new char[offset];
00163 System.arraycopy(buf, 0, ret, 0, offset);
00164 Arrays.fill(buf, ' ');
00165
00166 return ret;
00167 }
00168 }