KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > UserAuthPubKey


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 UserAuthPubKey{
7   public static void main(String JavaDoc[] arg){
8
9     try{
10       JSch jsch=new JSch();
11
12       JFileChooser chooser = new JFileChooser();
13       chooser.setDialogTitle("Choose your privatekey(ex. ~/.ssh/id_dsa)");
14       chooser.setFileHidingEnabled(false);
15       int returnVal = chooser.showOpenDialog(null);
16       if(returnVal == JFileChooser.APPROVE_OPTION) {
17         System.out.println("You chose "+
18                chooser.getSelectedFile().getAbsolutePath()+".");
19         jsch.addIdentity(chooser.getSelectedFile().getAbsolutePath()
20 // , "passphrase"
21
);
22       }
23
24       String JavaDoc host=JOptionPane.showInputDialog("Enter username@hostname",
25                           System.getProperty("user.name")+
26                           "@localhost");
27       String JavaDoc user=host.substring(0, host.indexOf('@'));
28       host=host.substring(host.indexOf('@')+1);
29
30       Session session=jsch.getSession(user, host, 22);
31
32       // username and passphrase will be given via UserInfo interface.
33
UserInfo ui=new MyUserInfo();
34       session.setUserInfo(ui);
35       session.connect();
36
37       Channel channel=session.openChannel("shell");
38
39       channel.setInputStream(System.in);
40       channel.setOutputStream(System.out);
41
42       channel.connect();
43     }
44     catch(Exception JavaDoc e){
45       System.out.println(e);
46     }
47   }
48
49
50   public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
51     public String JavaDoc getPassword(){ return null; }
52     public boolean promptYesNo(String JavaDoc str){
53       Object JavaDoc[] options={ "yes", "no" };
54       int foo=JOptionPane.showOptionDialog(null,
55              str,
56              "Warning",
57              JOptionPane.DEFAULT_OPTION,
58              JOptionPane.WARNING_MESSAGE,
59              null, options, options[0]);
60        return foo==0;
61     }
62   
63     String JavaDoc passphrase;
64     JTextField passphraseField=(JTextField)new JPasswordField(20);
65
66     public String JavaDoc getPassphrase(){ return passphrase; }
67     public boolean promptPassphrase(String JavaDoc message){
68       Object JavaDoc[] ob={passphraseField};
69       int result=
70     JOptionPane.showConfirmDialog(null, ob, message,
71                       JOptionPane.OK_CANCEL_OPTION);
72       if(result==JOptionPane.OK_OPTION){
73         passphrase=passphraseField.getText();
74         return true;
75       }
76       else{ return false; }
77     }
78     public boolean promptPassword(String JavaDoc message){ return true; }
79     public void showMessage(String JavaDoc message){
80       JOptionPane.showMessageDialog(null, message);
81     }
82     final GridBagConstraints gbc =
83       new GridBagConstraints(0,0,1,1,1,1,
84                              GridBagConstraints.NORTHWEST,
85                              GridBagConstraints.NONE,
86                              new Insets(0,0,0,0),0,0);
87     private Container panel;
88     public String JavaDoc[] promptKeyboardInteractive(String JavaDoc destination,
89                                               String JavaDoc name,
90                                               String JavaDoc instruction,
91                                               String JavaDoc[] prompt,
92                                               boolean[] echo){
93       panel = new JPanel();
94       panel.setLayout(new GridBagLayout());
95
96       gbc.weightx = 1.0;
97       gbc.gridwidth = GridBagConstraints.REMAINDER;
98       gbc.gridx = 0;
99       panel.add(new JLabel(instruction), gbc);
100       gbc.gridy++;
101
102       gbc.gridwidth = GridBagConstraints.RELATIVE;
103
104       JTextField[] texts=new JTextField[prompt.length];
105       for(int i=0; i<prompt.length; i++){
106         gbc.fill = GridBagConstraints.NONE;
107         gbc.gridx = 0;
108         gbc.weightx = 1;
109         panel.add(new JLabel(prompt[i]),gbc);
110
111         gbc.gridx = 1;
112         gbc.fill = GridBagConstraints.HORIZONTAL;
113         gbc.weighty = 1;
114         if(echo[i]){
115           texts[i]=new JTextField(20);
116         }
117         else{
118           texts[i]=new JPasswordField(20);
119         }
120         panel.add(texts[i], gbc);
121         gbc.gridy++;
122       }
123
124       if(JOptionPane.showConfirmDialog(null, panel,
125                                        destination+": "+name,
126                                        JOptionPane.OK_CANCEL_OPTION,
127                                        JOptionPane.QUESTION_MESSAGE)
128          ==JOptionPane.OK_OPTION){
129         String JavaDoc[] response=new String JavaDoc[prompt.length];
130         for(int i=0; i<prompt.length; i++){
131           response[i]=texts[i].getText();
132         }
133     return response;
134       }
135       else{
136         return null; // cancel
137
}
138     }
139   }
140 }
141
Popular Tags