00001
00027 package org.objectweb.jonas.security.auth.callback;
00028
00029 import java.awt.GridLayout;
00030 import java.io.IOException;
00031
00032 import javax.security.auth.callback.Callback;
00033 import javax.security.auth.callback.CallbackHandler;
00034 import javax.security.auth.callback.NameCallback;
00035 import javax.security.auth.callback.PasswordCallback;
00036 import javax.security.auth.callback.UnsupportedCallbackException;
00037 import javax.swing.BoxLayout;
00038 import javax.swing.JLabel;
00039 import javax.swing.JOptionPane;
00040 import javax.swing.JPanel;
00041 import javax.swing.JPasswordField;
00042 import javax.swing.JTextField;
00043
00051 public class DialogCallbackHandler implements CallbackHandler {
00052
00056 private JTextField loginField = null;
00057
00061 private JTextField passwordField = null;
00062
00066 private boolean cancelled = false;
00067
00071 private String title = "Login Dialog";
00072
00076 private String username = "Username ";
00077
00081 private String password = "Password ";
00082
00086 private String loginButton = "Login !";
00087
00091 private String cancelButton = "Cancel";
00092
00096 private static final int MAX_FIELD_LENGTH = 20;
00097
00101 private int passwordLength = MAX_FIELD_LENGTH;
00102
00106 private int usernameLength = MAX_FIELD_LENGTH;
00107
00112 private char echoChar = '*';
00113
00118 private boolean echoCharOn = false;
00119
00123 private String[] connectOptionNames;
00124
00129 public DialogCallbackHandler() {
00130 int i = 0;
00131 connectOptionNames = new String[2];
00132 connectOptionNames[i] = loginButton;
00133 connectOptionNames[++i] = cancelButton;
00134 }
00135
00141 public DialogCallbackHandler(String title) {
00142 this();
00143 this.title = title;
00144 }
00145
00153 public DialogCallbackHandler(String title, String username, String password) {
00154 this();
00155 this.title = title;
00156 this.username = username;
00157 this.password = password;
00158 }
00159
00173 public DialogCallbackHandler(String title, String username, String password, String loginButton,
00174 String cancelButton, int usernameLength, int passwordLength, char echoChar) {
00175 this(title, username, password);
00176 this.echoCharOn = true;
00177 this.echoChar = echoChar;
00178 this.loginButton = loginButton;
00179 this.cancelButton = cancelButton;
00180 this.passwordLength = passwordLength;
00181 this.usernameLength = usernameLength;
00182 this.echoChar = echoChar;
00183 }
00184
00190 private void dialogInit(boolean isEchoOn) {
00191
00192
00193
00194
00195
00196 echoCharOn = (isEchoOn || echoCharOn);
00197 dialogInit();
00198 }
00199
00204 private void dialogInit() {
00205
00206
00207 JLabel userNameLabel = new JLabel(username, JLabel.RIGHT);
00208 loginField = new JTextField("");
00209
00210
00211 JLabel passwordLabel = new JLabel(password, JLabel.RIGHT);
00212
00213
00214
00215
00216 if (!echoCharOn) {
00217 passwordField = new JPasswordField(passwordLength);
00218 ((JPasswordField) passwordField).setEchoChar(echoChar);
00219 } else {
00220 passwordField = new JTextField(passwordLength);
00221 }
00222
00223
00224 JPanel connectionPanel = new JPanel(false);
00225 connectionPanel.setLayout(new BoxLayout(connectionPanel, BoxLayout.X_AXIS));
00226
00227
00228 JPanel namePanel = new JPanel(false);
00229 namePanel.setLayout(new GridLayout(0, 1));
00230 namePanel.add(userNameLabel);
00231 namePanel.add(passwordLabel);
00232
00233
00234 JPanel fieldPanel = new JPanel(false);
00235 fieldPanel.setLayout(new GridLayout(0, 1));
00236 fieldPanel.add(loginField);
00237 fieldPanel.add(passwordField);
00238
00239
00240 connectionPanel.add(namePanel);
00241 connectionPanel.add(fieldPanel);
00242
00243
00244
00245 int choice = JOptionPane.showOptionDialog(null,
00246 connectionPanel,
00247 title,
00248 JOptionPane.OK_CANCEL_OPTION,
00249 JOptionPane.INFORMATION_MESSAGE,
00250 null,
00251 connectOptionNames,
00252 loginField);
00253
00254 if (choice == JOptionPane.OK_OPTION) {
00255 cancelled = false;
00256 } else if (choice == JOptionPane.CANCEL_OPTION) {
00257
00258
00259 cancelled = true;
00260 } else if (choice == JOptionPane.CLOSED_OPTION) {
00261 cancelled = true;
00262 } else {
00263 cancelled = true;
00264 }
00265
00266 if (cancelled) {
00267 loginField.setText("Invalid");
00268 passwordField.setText("Invalid");
00269 }
00270 }
00271
00282 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
00283
00284
00285
00286 if (cancelled) {
00287 return;
00288 }
00289
00290
00291
00292 int i = 0;
00293 boolean found = false;
00294 while ((i < callbacks.length) && !found) {
00295 if (callbacks[i] instanceof PasswordCallback) {
00296 found = true;
00297 dialogInit(((PasswordCallback) callbacks[i]).isEchoOn());
00298 }
00299
00300 i++;
00301 }
00302
00303
00304
00305
00306
00307 if (!found) {
00308 dialogInit();
00309 }
00310
00311
00312 for (i = 0; i < callbacks.length; i++) {
00313
00314 if (callbacks[i] instanceof NameCallback) {
00315 ((NameCallback) callbacks[i]).setName(loginField.getText());
00316 } else if (callbacks[i] instanceof PasswordCallback) {
00317 ((PasswordCallback) callbacks[i]).setPassword((passwordField.getText()).toCharArray());
00318 } else {
00319 throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
00320 }
00321 }
00322 }
00323 }