1 26 27 package org.objectweb.jonas.security.auth.callback; 28 29 import java.awt.GridLayout ; 30 import java.io.IOException ; 31 32 import javax.security.auth.callback.Callback ; 33 import javax.security.auth.callback.CallbackHandler ; 34 import javax.security.auth.callback.NameCallback ; 35 import javax.security.auth.callback.PasswordCallback ; 36 import javax.security.auth.callback.UnsupportedCallbackException ; 37 import javax.swing.BoxLayout ; 38 import javax.swing.JLabel ; 39 import javax.swing.JOptionPane ; 40 import javax.swing.JPanel ; 41 import javax.swing.JPasswordField ; 42 import javax.swing.JTextField ; 43 44 51 public class DialogCallbackHandler implements CallbackHandler { 52 53 56 private JTextField loginField = null; 57 58 61 private JTextField passwordField = null; 62 63 66 private boolean cancelled = false; 67 68 71 private String title = "Login Dialog"; 72 73 76 private String username = "Username "; 77 78 81 private String password = "Password "; 82 83 86 private String loginButton = "Login !"; 87 88 91 private String cancelButton = "Cancel"; 92 93 96 private static final int MAX_FIELD_LENGTH = 20; 97 98 101 private int passwordLength = MAX_FIELD_LENGTH; 102 103 106 private int usernameLength = MAX_FIELD_LENGTH; 107 108 112 private char echoChar = '*'; 113 114 118 private boolean echoCharOn = false; 119 120 123 private String [] connectOptionNames; 124 125 129 public DialogCallbackHandler() { 130 int i = 0; 131 connectOptionNames = new String [2]; 132 connectOptionNames[i] = loginButton; 133 connectOptionNames[++i] = cancelButton; 134 } 135 136 141 public DialogCallbackHandler(String title) { 142 this(); 143 this.title = title; 144 } 145 146 153 public DialogCallbackHandler(String title, String username, String password) { 154 this(); 155 this.title = title; 156 this.username = username; 157 this.password = password; 158 } 159 160 173 public DialogCallbackHandler(String title, String username, String password, String loginButton, 174 String cancelButton, int usernameLength, int passwordLength, char echoChar) { 175 this(title, username, password); 176 this.echoCharOn = true; 177 this.echoChar = echoChar; 178 this.loginButton = loginButton; 179 this.cancelButton = cancelButton; 180 this.passwordLength = passwordLength; 181 this.usernameLength = usernameLength; 182 this.echoChar = echoChar; 183 } 184 185 190 private void dialogInit(boolean isEchoOn) { 191 192 echoCharOn = (isEchoOn || echoCharOn); 197 dialogInit(); 198 } 199 200 204 private void dialogInit() { 205 206 JLabel userNameLabel = new JLabel (username, JLabel.RIGHT); 208 loginField = new JTextField (""); 209 210 JLabel passwordLabel = new JLabel (password, JLabel.RIGHT); 212 213 if (!echoCharOn) { passwordField = new JPasswordField (passwordLength); 218 ((JPasswordField ) passwordField).setEchoChar(echoChar); 219 } else { 220 passwordField = new JTextField (passwordLength); 221 } 222 223 JPanel connectionPanel = new JPanel (false); 225 connectionPanel.setLayout(new BoxLayout (connectionPanel, BoxLayout.X_AXIS)); 226 227 JPanel namePanel = new JPanel (false); 229 namePanel.setLayout(new GridLayout (0, 1)); 230 namePanel.add(userNameLabel); 231 namePanel.add(passwordLabel); 232 233 JPanel fieldPanel = new JPanel (false); 235 fieldPanel.setLayout(new GridLayout (0, 1)); 236 fieldPanel.add(loginField); 237 fieldPanel.add(passwordField); 238 239 connectionPanel.add(namePanel); 241 connectionPanel.add(fieldPanel); 242 243 int choice = JOptionPane.showOptionDialog(null, connectionPanel, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, connectOptionNames, loginField); 254 if (choice == JOptionPane.OK_OPTION) { 255 cancelled = false; 256 } else if (choice == JOptionPane.CANCEL_OPTION) { 257 cancelled = true; 260 } else if (choice == JOptionPane.CLOSED_OPTION) { 261 cancelled = true; 262 } else { 263 cancelled = true; } 265 266 if (cancelled) { 267 loginField.setText("Invalid"); 268 passwordField.setText("Invalid"); 269 } 270 } 271 272 282 public void handle(Callback [] callbacks) throws IOException , UnsupportedCallbackException { 283 284 if (cancelled) { 287 return; 288 } 289 290 int i = 0; 293 boolean found = false; 294 while ((i < callbacks.length) && !found) { 295 if (callbacks[i] instanceof PasswordCallback ) { 296 found = true; 297 dialogInit(((PasswordCallback ) callbacks[i]).isEchoOn()); 298 } 299 i++; 301 } 302 if (!found) { 308 dialogInit(); 309 } 310 311 for (i = 0; i < callbacks.length; i++) { 313 314 if (callbacks[i] instanceof NameCallback ) { 315 ((NameCallback ) callbacks[i]).setName(loginField.getText()); 316 } else if (callbacks[i] instanceof PasswordCallback ) { 317 ((PasswordCallback ) callbacks[i]).setPassword((passwordField.getText()).toCharArray()); 318 } else { 319 throw new UnsupportedCallbackException (callbacks[i], "Unrecognized Callback"); 320 } 321 } 322 } 323 } | Popular Tags |