KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > GUILoginDialog


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

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 /**
36  * An implementation of a LoginDialog that presents a swing based
37  * GUI for querying username and password.
38  * @author Harish Prabandham
39  * @author Harpreet Singh
40  */

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 JavaDoc entity;
49     private PassphraseDialog passphraseDialog;
50     private CertificateDialog certDialog;
51     private static LocalStringManagerImpl localStrings =
52     new LocalStringManagerImpl(GUILoginDialog.class);
53
54     /**
55      */

56     public GUILoginDialog () {
57     this (localStrings.getLocalString("enterprise.security.defaultEntity", "user"));
58     }
59     
60     /**
61      */

62     public GUILoginDialog (String JavaDoc entity) {
63     this.entity = entity;
64     JFrame f = new JFrame();
65     String JavaDoc phrase = localStrings.getLocalString("enterprise.security.loginPhrase", "Login for ");
66     passphraseDialog = new PassphraseDialog(f, phrase + entity + ":");
67     passphraseDialog.show();
68     }
69
70     public GUILoginDialog(String JavaDoc entity, Callback[] callbacks) {
71         this.entity = entity;
72     String JavaDoc 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     /**
80      * @return The username of the user.
81      */

82     public String JavaDoc getUserName() {
83     return passphraseDialog.username;
84     }
85
86     /**
87      *@return The password of the user in plain text...
88      */

89     public String JavaDoc getPassword() {
90     return passphraseDialog.passphrase;
91     }
92
93     public static void main(String JavaDoc[] args)
94     {
95     GUILoginDialog gui = new GUILoginDialog();
96     }
97 }
98
99
100 /**
101  * Create a popup dialog box to ask for the passphrase.
102  */

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     // buttons for keystore password
115
private JButton okForKP;
116     private JButton cancelForKP;
117
118     private static LocalStringManagerImpl localStrings =
119     new LocalStringManagerImpl(PassphraseDialog.class);
120     String JavaDoc username = new String JavaDoc();
121     String JavaDoc passphrase = new String JavaDoc();
122
123     private JPasswordField keystorePassword;
124     private JLabel lbl;
125     // password for the keystore
126
private String JavaDoc passKPFromUser;
127     private String JavaDoc keystorePass;
128     // parent panel for keystore password
129
private JPanel pnl = new JPanel (new GridLayout (2, 0));
130     // panel for buttons for keystore password
131
private JPanel bpanel = new JPanel (new FlowLayout ());
132     private JPanel kpPanel = new JPanel (new FlowLayout ());
133     private final String JavaDoc pnlKeyStorePassword = "Keystore Password Box";
134     private final String JavaDoc pnlCertificateList = "Cerificate Chooser";
135     // panel for certificate list
136
private JPanel pnl2 = new JPanel ();
137     /**
138      * Create a dialog box with a frame and title.
139      *
140      * @param frame The parent frame.
141      * @param title The dialog box title.
142      */

143     protected PassphraseDialog (JFrame frame, String JavaDoc title) {
144         super(frame, title, true);
145     this.frame = frame;
146         super.dialogInit();
147     initbox();
148     }
149
150     /**
151      * Create a dialog box with a frame and title.
152      *
153      * @param frame The parent frame.
154      * @param title The dialog box title.
155      */

156     protected PassphraseDialog (JFrame frame,
157                 String JavaDoc 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     // passField.setEchoChar ('*');
206

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         /*
225          * For getting the KeyStore Password from the user
226          */

227         lbl = new JLabel
228         (localStrings.getLocalString
229          ("enterprise.security.keystore",
230           "Enter the KeyStore Password "));
231         // adding the password field
232
keystorePassword = new JPasswordField (20);
233         kpPanel.add (lbl);
234         kpPanel.add (keystorePassword);
235         /* get the keystore password */
236         keystorePass = SSLUtils.getKeyStorePass ();
237         // ok button For keystore password
238
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 JavaDoc (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 JavaDoc 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         // Adding the certificate lists.
280
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 JavaDoc[] 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     // XXX I18N
304
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 JavaDoc(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         //System.out.println("IN WINDOW CLOSING");
347
//_logger.log(Level.FINE,"IN WINDOW CLOSING");
348
// send a fail back
349
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         /* first get the password to the keystore */
382         cl.show (getContentPane (), pnlKeyStorePassword);
383     } else {
384         cl.show (getContentPane (), pnlCertificateList);
385     }
386     pack ();
387     setSize (getPreferredSize ());
388     }
389
390 }
391
392 /**
393  * Create a popup dialog box to ask for the passphrase.
394  */

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 JavaDoc username = new String JavaDoc();
405     String JavaDoc passphrase = new String JavaDoc();
406
407     /**
408      * Create a dialog box with a frame and title.
409      *
410      * @param frame The parent frame.
411      * @param title The dialog box title.
412      */

413     protected CertificateDialog (JFrame frame, String JavaDoc 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 JavaDoc[] list = null;
429     /**/
430     list = new String JavaDoc[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 ")); // XXX I18N
473
okButton.setActionCommand ("ok");
474     okButton.addActionListener (new ActionListener() {
475         public void actionPerformed(ActionEvent ae) {
476         // System.out.println("OK Action");
477
//_logger.log(Level.FINE,"OK Action");
478
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")); // XXX I18N
490
cancelButton.setActionCommand ("cancel");
491     cancelButton.addActionListener (new ActionListener() {
492         public void actionPerformed(ActionEvent ae) {
493         // System.out.println("Cancel Action");
494
// _logger.log(Level.FINE,"Cancel Action");
495
// username = null;
496
// passphrase = null;
497
// setVisible(false);
498
java.awt.Toolkit.getDefaultToolkit().beep();
499         }
500     }
501     );
502
503     super.addWindowListener(new WindowAdapter() {
504         public void windowClosing(WindowEvent we) {
505         // System.out.println("IN WINDOW CLOSING");
506
// _logger.log(Level.FINE,"IN WINDOW CLOSING");
507
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