KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > UserAuthKI


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 import com.jcraft.jsch.*;
3 import java.awt.*;
4 import javax.swing.*;
5
6 public class UserAuthKI{
7   public static void main(String JavaDoc[] arg){
8
9     try{
10       JSch jsch=new JSch();
11
12       String JavaDoc host=JOptionPane.showInputDialog("Enter username@hostname",
13                                               System.getProperty("user.name")+
14                                               "@localhost");
15       String JavaDoc user=host.substring(0, host.indexOf('@'));
16       host=host.substring(host.indexOf('@')+1);
17
18       Session session=jsch.getSession(user, host, 22);
19
20       // username and passphrase will be given via UserInfo interface.
21
UserInfo ui=new MyUserInfo();
22       session.setUserInfo(ui);
23       session.connect();
24
25       Channel channel=session.openChannel("shell");
26
27       channel.setInputStream(System.in);
28       channel.setOutputStream(System.out);
29
30       channel.connect();
31     }
32     catch(Exception JavaDoc e){
33       System.out.println(e);
34     }
35   }
36
37   public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
38     public String JavaDoc getPassword(){ return passwd; }
39     public boolean promptYesNo(String JavaDoc str){
40       Object JavaDoc[] options={ "yes", "no" };
41       int foo=JOptionPane.showOptionDialog(null,
42              str,
43              "Warning",
44              JOptionPane.DEFAULT_OPTION,
45              JOptionPane.WARNING_MESSAGE,
46              null, options, options[0]);
47        return foo==0;
48     }
49
50     String JavaDoc passwd;
51     JTextField passwordField=(JTextField)new JPasswordField(20);
52   
53     public String JavaDoc getPassphrase(){ return null; }
54     public boolean promptPassphrase(String JavaDoc message){ return false; }
55     public boolean promptPassword(String JavaDoc message){
56       Object JavaDoc[] ob={passwordField};
57       int result=
58         JOptionPane.showConfirmDialog(null, ob, message,
59                                       JOptionPane.OK_CANCEL_OPTION);
60       if(result==JOptionPane.OK_OPTION){
61         passwd=passwordField.getText();
62         return true;
63       }
64       else{
65         return false;
66       }
67     }
68     public void showMessage(String JavaDoc message){
69       JOptionPane.showMessageDialog(null, message);
70     }
71
72     final GridBagConstraints gbc =
73       new GridBagConstraints(0,0,1,1,1,1,
74                              GridBagConstraints.NORTHWEST,
75                              GridBagConstraints.NONE,
76                              new Insets(0,0,0,0),0,0);
77     private Container panel;
78     public String JavaDoc[] promptKeyboardInteractive(String JavaDoc destination,
79                                               String JavaDoc name,
80                                               String JavaDoc instruction,
81                                               String JavaDoc[] prompt,
82                                               boolean[] echo){
83 /*
84 //System.out.println("promptKeyboardInteractive");
85 System.out.println("destination: "+destination);
86 System.out.println("name: "+name);
87 System.out.println("instruction: "+instruction);
88 System.out.println("prompt.length: "+prompt.length);
89 System.out.println("prompt: "+prompt[0]);
90 */

91       panel = new JPanel();
92       panel.setLayout(new GridBagLayout());
93
94       gbc.weightx = 1.0;
95       gbc.gridwidth = GridBagConstraints.REMAINDER;
96       gbc.gridx = 0;
97       panel.add(new JLabel(instruction), gbc);
98       gbc.gridy++;
99
100       gbc.gridwidth = GridBagConstraints.RELATIVE;
101
102       JTextField[] texts=new JTextField[prompt.length];
103       for(int i=0; i<prompt.length; i++){
104         gbc.fill = GridBagConstraints.NONE;
105         gbc.gridx = 0;
106         gbc.weightx = 1;
107         panel.add(new JLabel(prompt[i]),gbc);
108
109         gbc.gridx = 1;
110         gbc.fill = GridBagConstraints.HORIZONTAL;
111         gbc.weighty = 1;
112         if(echo[i]){
113           texts[i]=new JTextField(20);
114         }
115         else{
116           texts[i]=new JPasswordField(20);
117         }
118         panel.add(texts[i], gbc);
119         gbc.gridy++;
120       }
121
122       if(JOptionPane.showConfirmDialog(null, panel,
123                                        destination+": "+name,
124                                        JOptionPane.OK_CANCEL_OPTION,
125                                        JOptionPane.QUESTION_MESSAGE)
126          ==JOptionPane.OK_OPTION){
127         String JavaDoc[] response=new String JavaDoc[prompt.length];
128         for(int i=0; i<prompt.length; i++){
129           response[i]=texts[i].getText();
130         }
131     return response;
132       }
133       else{
134         return null; // cancel
135
}
136     }
137   }
138 }
139
Popular Tags