1 26 27 package org.objectweb.jonas.security.auth.callback; 28 29 import java.io.IOException ; 30 import java.io.PushbackInputStream ; 31 import java.io.InputStream ; 32 import java.io.BufferedReader ; 33 import java.io.InputStreamReader ; 34 import java.util.Arrays ; 35 36 import javax.security.auth.callback.CallbackHandler ; 37 import javax.security.auth.callback.Callback ; 38 import javax.security.auth.callback.NameCallback ; 39 import javax.security.auth.callback.PasswordCallback ; 40 import javax.security.auth.callback.TextOutputCallback ; 41 import javax.security.auth.callback.UnsupportedCallbackException ; 42 43 50 public class LoginCallbackHandler implements CallbackHandler { 51 52 55 private static final int BUFFER_SIZE = 128; 56 57 67 public void handle(Callback [] callbacks) throws IOException , UnsupportedCallbackException { 68 69 for (int i = 0; i < callbacks.length; i++) { 70 if (callbacks[i] instanceof TextOutputCallback ) { 71 72 TextOutputCallback toc = (TextOutputCallback ) callbacks[i]; 74 switch (toc.getMessageType()) { 75 case TextOutputCallback.INFORMATION: 76 System.out.println(toc.getMessage()); 77 break; 78 case TextOutputCallback.ERROR: 79 System.out.println("ERROR: " + toc.getMessage()); 80 break; 81 case TextOutputCallback.WARNING: 82 System.out.println("WARNING: " + toc.getMessage()); 83 break; 84 default: 85 throw new IOException ("Unsupported message type: " + toc.getMessageType()); 86 } 87 88 } else if (callbacks[i] instanceof NameCallback ) { 89 90 NameCallback nc = (NameCallback ) callbacks[i]; 92 93 System.err.print(nc.getPrompt()); 94 System.err.flush(); 95 nc.setName((new BufferedReader (new InputStreamReader (System.in))).readLine()); 96 97 } else if (callbacks[i] instanceof PasswordCallback ) { 98 99 PasswordCallback pc = (PasswordCallback ) callbacks[i]; 101 System.err.print(pc.getPrompt()); 102 System.err.flush(); 103 pc.setPassword(readPassword(System.in)); 104 } else { 105 throw new UnsupportedCallbackException (callbacks[i], "Unrecognized Callback"); 106 } 107 } 108 } 109 110 116 private char[] readPassword(InputStream in) throws IOException { 117 118 char[] lineBuffer; 119 char[] buf; 120 int i; 121 122 lineBuffer = new char[BUFFER_SIZE]; 123 buf = lineBuffer; 124 125 int room = buf.length; 126 int offset = 0; 127 int c; 128 129 loop: while (true) { 130 switch (c = in.read()) { 131 case -1: 132 case '\n': 133 break loop; 134 135 case '\r': 136 int c2 = in.read(); 137 if ((c2 != '\n') && (c2 != -1)) { 138 if (!(in instanceof PushbackInputStream )) { 139 in = new PushbackInputStream (in); 140 } 141 ((PushbackInputStream ) in).unread(c2); 142 } else { 143 break loop; 144 } 145 default: 146 if (--room < 0) { 147 buf = new char[offset + BUFFER_SIZE]; 148 room = buf.length - offset - 1; 149 System.arraycopy(lineBuffer, 0, buf, 0, offset); 150 Arrays.fill(lineBuffer, ' '); 151 lineBuffer = buf; 152 } 153 buf[offset++] = (char) c; 154 break; 155 } 156 } 157 158 if (offset == 0) { 159 return null; 160 } 161 162 char[] ret = new char[offset]; 163 System.arraycopy(buf, 0, ret, 0, offset); 164 Arrays.fill(buf, ' '); 165 166 return ret; 167 } 168 } | Popular Tags |