KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > jndi > JXCallbackHandler


1 /*
2  * Provides a call back handler for the GSSAPI/Kerberos code.
3  */

4 package com.ca.commons.jndi;
5
6 import java.io.IOException JavaDoc;
7 import java.awt.*;
8
9 import javax.security.auth.callback.Callback JavaDoc;
10 import javax.security.auth.callback.CallbackHandler JavaDoc;
11 import javax.security.auth.callback.NameCallback JavaDoc;
12 import javax.security.auth.callback.PasswordCallback JavaDoc;
13 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
14 import javax.swing.*;
15
16 /**
17  * Provides a call back handler for the GSSAPI/Kerberos code. It allows
18  * the user to enter username / password details if the kerberos repository
19  * isn't already set up.
20   * @author vadim, chris betts
21  */

22 public class JXCallbackHandler implements CallbackHandler JavaDoc
23 {
24     // nb - this may appear to be a security problem, since storing password
25
// strings is generally unwelcome; however in fact we are simply copying
26
// the string from jndi which already has it - so we're no worse off.
27
// (we could just read them straight from the jndi environment I guess)
28
private static Frame owner = null;
29 // private static String userNamePrompt;
30
// private static String pwdPrompt;
31
private static String JavaDoc promptHeader = "Require Kerberos Credentials";
32
33     /**
34      * We optionally pass these things externally so we can smoothly fit into the calling
35      * application... otherwise english only defaults are used.
36      * @param ownerFrame - the base gui we will use to show dialogs
37      * @param userNamePromptString - the (translated) text to show the user for a user name prompt - NOT CURRENTLY USED
38      * @param pwdPromptString - the (translated) text to show the user for a pwd prompt - NOT CURRENTLY USED
39      * @param promptHeaderString - the (translated) text to adorn the prompt dialogue window.
40      */

41     public static void setupGUI(Frame ownerFrame, String JavaDoc userNamePromptString, String JavaDoc pwdPromptString, String JavaDoc promptHeaderString)
42     {
43         owner = ownerFrame;
44 // userNamePrompt = userNamePromptString;
45
// pwdPrompt = pwdPromptString;
46
promptHeader = promptHeaderString;
47     }
48
49     public JXCallbackHandler()
50     {
51     }
52
53     /**
54      * This method passes user credentials back in the event that they have not been
55      * set up in a global kerberos cache.
56      * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
57      */

58     public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc,
59             UnsupportedCallbackException JavaDoc
60     {
61         for (int i = 0; i < callbacks.length; i++)
62         {
63
64             if (callbacks[i] instanceof NameCallback JavaDoc)
65             {
66
67                 NameCallback JavaDoc cb = (NameCallback JavaDoc) callbacks[i];
68                 cb.setName(getUserName(cb.getPrompt()));
69             }
70             else if (callbacks[i] instanceof PasswordCallback JavaDoc)
71             {
72                 PasswordCallback JavaDoc cb = (PasswordCallback JavaDoc) callbacks[i];
73                 cb.setPassword(getPassword(cb.getPrompt()));
74             }
75             else
76             {
77                 throw new UnsupportedCallbackException JavaDoc(callbacks[i]);
78             }
79         }
80     }
81
82     /**
83      * This should raise a swing dialogue prompt and get a response from the user.
84      * It relies on Callback.getPrompt() to produce a sensible text message to prompt the user with.
85      * @param prompt
86      * @return
87      */

88     private String JavaDoc getUserName(String JavaDoc prompt)
89     {
90         String JavaDoc result = (String JavaDoc)JOptionPane.showInputDialog(owner, prompt, promptHeader, JOptionPane.QUESTION_MESSAGE);
91         return result;
92     }
93
94
95     /**
96      * This should raise a swing dialogue prompt and get a response from the user.
97      * It relies on Callback.getPrompt() to produce a sensible text message to prompt the user with.
98      * @param prompt
99      * @return
100      */

101     private char[] getPassword(String JavaDoc prompt)
102     {
103         JPasswordField pwd = new JPasswordField();
104         JPanel panel = new JPanel(new BorderLayout());
105         panel.add(new JLabel(prompt), BorderLayout.NORTH);
106         panel.add(pwd);
107
108         JOptionPane.showMessageDialog(owner, panel, promptHeader, JOptionPane.QUESTION_MESSAGE);
109         return pwd.getPassword();
110     }
111
112     /**
113      * A quicky test method to make sure the gui dialogues are working o.k. ...
114      * @param args
115      */

116     public static void main(String JavaDoc [] args)
117     {
118         JXCallbackHandler test = new JXCallbackHandler();
119         System.out.println("user name = " + test.getUserName("a nice shiny user name"));
120         System.out.println("pwd = " + new String JavaDoc(test.getPassword("a nice shiny password")));
121         System.exit(0);
122     }
123 }
124
Popular Tags