1 23 package com.sun.enterprise.security; 24 25 26 import java.awt.*; 27 import java.awt.event.*; 28 import javax.swing.*; 29 import javax.security.auth.callback.*; 30 import com.sun.enterprise.util.LocalStringManagerImpl; 31 import java.util.logging.*; 32 import com.sun.logging.*; 33 34 35 41 public final class GUILoginDialog implements LoginDialog { 42 43 private static Logger _logger=null; 44 static { 45 _logger=LogDomains.getLogger(LogDomains.SECURITY_LOGGER); 46 } 47 48 private String entity; 49 private PassphraseDialog passphraseDialog; 50 private CertificateDialog certDialog; 51 private static LocalStringManagerImpl localStrings = 52 new LocalStringManagerImpl(GUILoginDialog.class); 53 54 56 public GUILoginDialog () { 57 this (localStrings.getLocalString("enterprise.security.defaultEntity", "user")); 58 } 59 60 62 public GUILoginDialog (String entity) { 63 this.entity = entity; 64 JFrame f = new JFrame(); 65 String phrase = localStrings.getLocalString("enterprise.security.loginPhrase", "Login for "); 66 passphraseDialog = new PassphraseDialog(f, phrase + entity + ":"); 67 passphraseDialog.show(); 68 } 69 70 public GUILoginDialog(String entity, Callback[] callbacks) { 71 this.entity = entity; 72 String phrase = localStrings.getLocalString("enterprise.security.loginPhrase", "Login for "); 73 JFrame f = new JFrame(); 74 passphraseDialog = new PassphraseDialog(f, phrase + entity + ":", callbacks); 75 passphraseDialog.show(); 76 } 77 78 79 82 public String getUserName() { 83 return passphraseDialog.username; 84 } 85 86 89 public String getPassword() { 90 return passphraseDialog.passphrase; 91 } 92 93 public static void main(String [] args) 94 { 95 GUILoginDialog gui = new GUILoginDialog(); 96 } 97 } 98 99 100 103 class PassphraseDialog extends JDialog 104 { 105 private NameCallback nameCallback = null;; 106 private PasswordCallback passwordCallback = null; 107 private ChoiceCallback choiceCallback = null; 108 private JTextField userField; 109 private JPasswordField passField; 110 private JList choiceList; 111 private JFrame frame; 112 private JButton okButton; 113 private JButton cancelButton; 114 private JButton okForKP; 116 private JButton cancelForKP; 117 118 private static LocalStringManagerImpl localStrings = 119 new LocalStringManagerImpl(PassphraseDialog.class); 120 String username = new String (); 121 String passphrase = new String (); 122 123 private JPasswordField keystorePassword; 124 private JLabel lbl; 125 private String passKPFromUser; 127 private String keystorePass; 128 private JPanel pnl = new JPanel (new GridLayout (2, 0)); 130 private JPanel bpanel = new JPanel (new FlowLayout ()); 132 private JPanel kpPanel = new JPanel (new FlowLayout ()); 133 private final String pnlKeyStorePassword = "Keystore Password Box"; 134 private final String pnlCertificateList = "Cerificate Chooser"; 135 private JPanel pnl2 = new JPanel (); 137 143 protected PassphraseDialog (JFrame frame, String title) { 144 super(frame, title, true); 145 this.frame = frame; 146 super.dialogInit(); 147 initbox(); 148 } 149 150 156 protected PassphraseDialog (JFrame frame, 157 String title, 158 Callback[] callbacks) { 159 160 super(frame, title, true); 161 this.frame = frame; 162 super.dialogInit(); 163 164 for(int i = 0; i < callbacks.length; i++) { 165 if(callbacks[i] instanceof NameCallback) { 166 nameCallback = (NameCallback) callbacks[i]; 167 } else if(callbacks[i] instanceof PasswordCallback) { 168 passwordCallback = (PasswordCallback) callbacks[i]; 169 } else if(callbacks[i] instanceof ChoiceCallback) { 170 choiceCallback = (ChoiceCallback) callbacks[i]; 171 } 172 } 173 initbox(); 174 } 175 176 private void initbox() { 177 GridBagLayout gridbag = new GridBagLayout(); 178 GridBagConstraints c = new GridBagConstraints(); 179 pnl2.setLayout(gridbag); 180 getContentPane ().setLayout (new CardLayout ()); 181 int gridx = 0; 182 int gridy = 0; 183 184 passField = new JPasswordField(20); 185 userField = new JTextField(20); 186 choiceList = new JList(); 187 188 189 if(nameCallback != null) { 190 c.gridx = gridx++; 191 c.gridy = gridy; 192 c.anchor = GridBagConstraints.CENTER; 193 c.insets = new Insets(20, 10, 10, 2); 194 JLabel jl = new JLabel(nameCallback.getPrompt()); 195 gridbag.setConstraints(jl, c); 196 pnl2.add(jl); 197 c.gridx = gridx++; 198 c.gridy = gridy++; 199 c.fill = GridBagConstraints.HORIZONTAL; 200 c.insets = new Insets(20, 3, 10, 10); 201 gridbag.setConstraints(userField, c); 202 pnl2.add(userField); 203 } 204 205 207 if(passwordCallback != null) { 208 gridx = 0; 209 c.gridx = gridx++; 210 c.gridy = gridy; 211 c.anchor = GridBagConstraints.CENTER; 212 c.insets = new Insets(20, 10, 10, 2); 213 JLabel l = new JLabel(passwordCallback.getPrompt()); 214 gridbag.setConstraints(l, c); 215 pnl2.add(l); 216 c.gridx = gridx++; 217 c.gridy = gridy++; 218 c.fill = GridBagConstraints.HORIZONTAL; 219 c.insets = new Insets(20, 3, 10, 10); 220 gridbag.setConstraints(passField, c); 221 pnl2.add(passField); 222 } 223 if(choiceCallback != null) { 224 227 lbl = new JLabel 228 (localStrings.getLocalString 229 ("enterprise.security.keystore", 230 "Enter the KeyStore Password ")); 231 keystorePassword = new JPasswordField (20); 233 kpPanel.add (lbl); 234 kpPanel.add (keystorePassword); 235 236 keystorePass = SSLUtils.getKeyStorePass (); 237 okForKP = new 239 JButton(localStrings.getLocalString 240 ( "enterprise.security.ok", " OK ")); 241 okForKP.setActionCommand ("ok"); 242 243 okForKP.addActionListener (new ActionListener() { 244 public void actionPerformed(ActionEvent ae) { 245 passKPFromUser = 246 new String (keystorePassword.getPassword ()); 247 if (keystorePass.equals (passKPFromUser)){ 248 okForKP.setEnabled (false); 249 cancelForKP.setEnabled (false); 250 keystorePassword.setEditable (false); 251 CardLayout cl = (CardLayout) (getContentPane ()).getLayout (); 252 cl.show (getContentPane (), pnlCertificateList); 253 } else{ 254 String errmessage = localStrings.getLocalString("enterprise.security.IncorrectKeystorePassword","Incorrect Keystore Password"); 255 GUIErrorDialog guierr = new GUIErrorDialog(errmessage); 256 guierr.show(); 257 } 258 } 259 } 260 ); 261 262 cancelForKP = new 263 JButton (localStrings.getLocalString 264 ( "enterprise.security.cancel", "Cancel")); 265 266 cancelForKP.setActionCommand ("cancel"); 267 cancelForKP.addActionListener (new ActionListener() { 268 public void actionPerformed(ActionEvent ae) { 269 if (choiceCallback != null) 270 choiceCallback.setSelectedIndex (-1); 271 frame.dispose(); 272 } 273 } 274 ); 275 bpanel.add (okForKP); 276 bpanel.add (cancelForKP); 277 pnl.add (kpPanel); 278 pnl.add (bpanel); 279 gridx = 0; 281 c.gridx = gridx++; 282 c.gridy = gridy; 283 c.anchor = GridBagConstraints.CENTER; 284 c.insets = new Insets(20, 10, 10, 2); 285 JLabel l = new JLabel(choiceCallback.getPrompt()); 286 gridbag.setConstraints(l, c); 287 pnl2.add(l); 288 c.gridx = gridx++; 289 c.gridy = gridy++; 290 c.fill = GridBagConstraints.HORIZONTAL; 291 c.insets = new Insets(20, 3, 10, 10); 292 293 String [] choices = choiceCallback.getChoices(); 294 choiceList.setListData(choices); 295 296 gridbag.setConstraints(choiceList, c); 297 pnl2.add(choiceList); 298 } 299 300 okButton = new 301 JButton(localStrings.getLocalString 302 ( "enterprise.security.ok", " OK ")); 303 okButton.setActionCommand ("ok"); 305 okButton.addActionListener (new ActionListener() { 306 public void actionPerformed(ActionEvent ae) { 307 username = userField.getText(); 308 if(username.trim().length() > 0) 309 nameCallback.setName(username); 310 if(passwordCallback != null) { 311 passphrase = new String (passField.getPassword()); 312 if(passphrase.trim().length() > 0) { 313 passwordCallback.setPassword(passphrase.toCharArray()); 314 } 315 } 316 if(choiceCallback != null) { 317 int idx = choiceList.getSelectedIndex(); 318 if(idx != -1) 319 choiceCallback.setSelectedIndex(idx); 320 } 321 frame.dispose(); 322 } 323 } 324 325 ); 326 327 cancelButton = new JButton 328 (localStrings.getLocalString 329 ( "enterprise.security.cancel", "Cancel")); 330 cancelButton.setActionCommand ("cancel"); 331 cancelButton.addActionListener (new ActionListener() { 332 public void actionPerformed(ActionEvent ae) { 333 if (choiceCallback!=null) { 334 choiceCallback.setSelectedIndex (-1); 335 } else { 336 username = null; 337 passphrase = null; 338 frame.dispose(); 339 } 340 } 341 } 342 ); 343 344 super.addWindowListener(new WindowAdapter() { 345 public void windowClosing(WindowEvent we) { 346 if (choiceCallback != null) 350 choiceCallback.setSelectedIndex (-1); 351 frame.dispose(); 352 } 353 }); 354 355 JPanel buttonPanel = new JPanel(); 356 buttonPanel.setLayout(gridbag); 357 c.insets = new Insets(5,0,5,15); 358 c.gridx = 0; 359 c.gridy = 0; 360 c.anchor = GridBagConstraints.CENTER; 361 c.fill = GridBagConstraints.NONE; 362 gridbag.setConstraints(okButton, c); 363 buttonPanel.add(okButton); 364 c.gridx = 2; 365 c.insets = new Insets(5,15,5,0); 366 gridbag.setConstraints(cancelButton, c); 367 buttonPanel.add(cancelButton); 368 369 c.gridx = 0; 370 c.gridy = gridy++; 371 c.gridwidth = 2; 372 c.insets = new Insets(0,0,5,0); 373 c.fill = GridBagConstraints.HORIZONTAL; 374 c.anchor = GridBagConstraints.WEST; 375 gridbag.setConstraints(buttonPanel, c); 376 pnl2.add(buttonPanel); 377 getContentPane ().add (pnl, pnlKeyStorePassword); 378 getContentPane ().add (pnl2, pnlCertificateList); 379 CardLayout cl = (CardLayout) (getContentPane ()).getLayout (); 380 if (choiceCallback != null){ 381 382 cl.show (getContentPane (), pnlKeyStorePassword); 383 } else { 384 cl.show (getContentPane (), pnlCertificateList); 385 } 386 pack (); 387 setSize (getPreferredSize ()); 388 } 389 390 } 391 392 395 class CertificateDialog extends JDialog 396 { 397 private JTextField userField; 398 private JList certList; 399 private JFrame frame; 400 private JButton okButton; 401 private JButton cancelButton; 402 private static LocalStringManagerImpl localStrings = 403 new LocalStringManagerImpl(CertificateDialog.class); 404 String username = new String (); 405 String passphrase = new String (); 406 407 413 protected CertificateDialog (JFrame frame, String title) { 414 super(frame, title, true); 415 this.frame = frame; 416 super.dialogInit(); 417 initbox(); 418 } 419 420 private void initbox() { 421 GridBagLayout gridbag = new GridBagLayout(); 422 GridBagConstraints c = new GridBagConstraints(); 423 getContentPane().setLayout(gridbag); 424 425 int gridx = 0; 426 int gridy = 0; 427 428 String [] list = null; 429 430 list = new String [5]; 431 list[0] = "foo"; 432 list[1] = "bar"; 433 list[2] = "abc"; 434 list[3] = "def"; 435 list[4] = "ghi"; 436 437 438 certList = new JList(list); 439 userField = new JTextField(20); 440 441 c.gridx = gridx++; 442 c.gridy = gridy; 443 c.anchor = GridBagConstraints.CENTER; 444 c.insets = new Insets(20, 10, 10, 2); 445 JLabel jl = new JLabel(localStrings.getLocalString("enterprise.security.login.username", 446 "Enter username:")); 447 gridbag.setConstraints(jl, c); 448 getContentPane().add(jl); 449 c.gridx = gridx++; 450 c.gridy = gridy++; 451 c.fill = GridBagConstraints.HORIZONTAL; 452 c.insets = new Insets(20, 3, 10, 10); 453 gridbag.setConstraints(userField, c); 454 getContentPane().add(userField); 455 456 gridx = 0; 457 c.gridx = gridx++; 458 c.gridy = gridy; 459 c.anchor = GridBagConstraints.CENTER; 460 c.insets = new Insets(20, 10, 10, 2); 461 JLabel l = new JLabel(localStrings.getLocalString("enterprise.security.login.password", 462 "Select a certificate:")); 463 gridbag.setConstraints(l, c); 464 getContentPane().add(l); 465 c.gridx = gridx++; 466 c.gridy = gridy++; 467 c.fill = GridBagConstraints.HORIZONTAL; 468 c.insets = new Insets(20, 3, 10, 10); 469 gridbag.setConstraints(certList, c); 470 getContentPane().add(certList); 471 472 okButton = new JButton(localStrings.getLocalString( "enterprise.security.ok", " OK ")); okButton.setActionCommand ("ok"); 474 okButton.addActionListener (new ActionListener() { 475 public void actionPerformed(ActionEvent ae) { 476 username = userField.getText(); 479 int index = certList.getSelectedIndex(); 480 481 if((username.trim().length() > 0) && 482 (passphrase.trim().length() > 0)) { 483 setVisible(false); 484 } 485 } 486 } 487 ); 488 489 cancelButton = new JButton(localStrings.getLocalString( "enterprise.security.cancel", "Cancel")); cancelButton.setActionCommand ("cancel"); 491 cancelButton.addActionListener (new ActionListener() { 492 public void actionPerformed(ActionEvent ae) { 493 java.awt.Toolkit.getDefaultToolkit().beep(); 499 } 500 } 501 ); 502 503 super.addWindowListener(new WindowAdapter() { 504 public void windowClosing(WindowEvent we) { 505 frame.dispose(); 508 } 509 }); 510 511 JPanel buttonPanel = new JPanel(); 512 buttonPanel.setLayout(gridbag); 513 c.insets = new Insets(5,0,5,15); 514 c.gridx = 0; 515 c.gridy = 0; 516 c.anchor = GridBagConstraints.CENTER; 517 c.fill = GridBagConstraints.NONE; 518 gridbag.setConstraints(okButton, c); 519 buttonPanel.add(okButton); 520 c.gridx = 2; 521 c.insets = new Insets(5,15,5,0); 522 gridbag.setConstraints(cancelButton, c); 523 buttonPanel.add(cancelButton); 524 525 c.gridx = 0; 526 c.gridy = gridy++; 527 c.gridwidth = 2; 528 c.insets = new Insets(0,0,5,0); 529 c.fill = GridBagConstraints.HORIZONTAL; 530 c.anchor = GridBagConstraints.WEST; 531 gridbag.setConstraints(buttonPanel, c); 532 getContentPane().add(buttonPanel); 533 534 pack (); 535 setSize (getPreferredSize ()); 536 } 537 538 } 539 | Popular Tags |