~ubuntu-branches/ubuntu/oneiric/icedtea-web/oneiric

« back to all changes in this revision

Viewing changes to netx/net/sourceforge/jnlp/security/PasswordAuthenticationDialog.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-11-24 13:23:28 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101124132328-2xb9z39vxga63vr9
Tags: 1.0~20101124-0ubuntu1
* Update to hg 20101124.
* Fix xulrunner dependencies for natty.
* Build-depend on pkg-config and libgtk2.0-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* PasswordAuthenticationDialog -- requests authentication information from users
 
2
   Copyright (C) 2009  Red Hat
 
3
 
 
4
This file is part of IcedTea.
 
5
 
 
6
IcedTea is free software; you can redistribute it and/or modify
 
7
it under the terms of the GNU General Public License as published by
 
8
the Free Software Foundation; either version 2, or (at your option)
 
9
any later version.
 
10
 
 
11
IcedTea is distributed in the hope that it will be useful, but
 
12
WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with IcedTea; see the file COPYING.  If not, write to the
 
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
19
02110-1301 USA.
 
20
 
 
21
Linking this library statically or dynamically with other modules is
 
22
making a combined work based on this library.  Thus, the terms and
 
23
conditions of the GNU General Public License cover the whole
 
24
combination.
 
25
 
 
26
As a special exception, the copyright holders of this library give you
 
27
permission to link this library with independent modules to produce an
 
28
executable, regardless of the license terms of these independent
 
29
modules, and to copy and distribute the resulting executable under
 
30
terms of your choice, provided that you also meet, for each linked
 
31
independent module, the terms and conditions of the license of that
 
32
module.  An independent module is a module which is not derived from
 
33
or based on this library.  If you modify this library, you may extend
 
34
this exception to your version of the library, but you are not
 
35
obligated to do so.  If you do not wish to do so, delete this
 
36
exception statement from your version. */
 
37
 
 
38
package net.sourceforge.jnlp.security;
 
39
 
 
40
import java.awt.Dimension;
 
41
import java.awt.GridBagConstraints;
 
42
import java.awt.GridBagLayout;
 
43
import java.awt.Insets;
 
44
import java.awt.event.ActionEvent;
 
45
import java.awt.event.ActionListener;
 
46
import java.net.PasswordAuthentication;
 
47
 
 
48
import javax.swing.JButton;
 
49
import javax.swing.JDialog;
 
50
import javax.swing.JLabel;
 
51
import javax.swing.JPasswordField;
 
52
import javax.swing.JTextField;
 
53
import javax.swing.SwingUtilities;
 
54
 
 
55
import net.sourceforge.jnlp.runtime.JNLPRuntime;
 
56
 
 
57
/**
 
58
 * Modal non-minimizable dialog to request http authentication credentials
 
59
 */
 
60
 
 
61
public class PasswordAuthenticationDialog extends JDialog {
 
62
 
 
63
    private JLabel jlInfo = new JLabel("");
 
64
    private JTextField jtfUserName = new JTextField();
 
65
    private JPasswordField jpfPassword = new JPasswordField();
 
66
    private boolean userCancelled;
 
67
 
 
68
    public PasswordAuthenticationDialog() {
 
69
        initialize();
 
70
    }
 
71
 
 
72
    /**
 
73
     * Initialized the dialog components
 
74
     */
 
75
 
 
76
    public void initialize() {
 
77
 
 
78
        setTitle("IcedTea Java Plugin - Authorization needed to proceed");
 
79
 
 
80
        setLayout(new GridBagLayout());
 
81
 
 
82
        JLabel jlUserName = new JLabel("Username: ");
 
83
        JLabel jlPassword = new JLabel("Password: ");
 
84
        JButton jbOK = new JButton("OK");
 
85
        JButton jbCancel = new JButton("Cancel");
 
86
 
 
87
        jtfUserName.setSize(20, 10);
 
88
        jpfPassword.setSize(20, 10);
 
89
 
 
90
        GridBagConstraints c;
 
91
 
 
92
        c = new GridBagConstraints();
 
93
        c.fill = c.HORIZONTAL;
 
94
        c.gridx = 0;
 
95
        c.gridy = 0;
 
96
        c.gridwidth = 2;
 
97
        c.insets = new Insets(10, 5, 3, 3);
 
98
        add(jlInfo, c);
 
99
 
 
100
        c = new GridBagConstraints();
 
101
        c.gridx = 0;
 
102
        c.gridy = 1;
 
103
        c.insets = new Insets(10, 5, 3, 3);
 
104
        add(jlUserName, c);
 
105
 
 
106
        c = new GridBagConstraints();
 
107
        c.fill = c.HORIZONTAL;
 
108
        c.gridx = 1;
 
109
        c.gridy = 1;
 
110
        c.insets = new Insets(10, 5, 3, 3);
 
111
        c.weightx = 1.0;
 
112
        add(jtfUserName, c);
 
113
 
 
114
 
 
115
        c = new GridBagConstraints();
 
116
        c.gridx = 0;
 
117
        c.gridy = 2;
 
118
        c.insets = new Insets(5, 5, 3, 3);
 
119
        add(jlPassword, c);
 
120
 
 
121
        c = new GridBagConstraints();
 
122
        c.fill = c.HORIZONTAL;
 
123
        c.gridx = 1;
 
124
        c.gridy = 2;
 
125
        c.insets = new Insets(5, 5, 3, 3);
 
126
        c.weightx = 1.0;
 
127
        add(jpfPassword, c);
 
128
 
 
129
        c = new GridBagConstraints();
 
130
        c.anchor = c.SOUTHEAST;
 
131
        c.gridx = 1;
 
132
        c.gridy = 3;
 
133
        c.insets = new Insets(5, 5, 3, 70);
 
134
        c.weightx = 0.0;
 
135
        add(jbCancel, c);
 
136
 
 
137
        c = new GridBagConstraints();
 
138
        c.anchor = c.SOUTHEAST;
 
139
        c.gridx = 1;
 
140
        c.gridy = 3;
 
141
        c.insets = new Insets(5, 5, 3, 3);
 
142
        c.weightx = 0.0;
 
143
        add(jbOK, c);
 
144
 
 
145
        setMinimumSize(new Dimension(400,150));
 
146
        setMaximumSize(new Dimension(1024,150));
 
147
        setAlwaysOnTop(true);
 
148
 
 
149
        setSize(400,150);
 
150
        setLocationRelativeTo(null);
 
151
 
 
152
        // OK => read supplied info and pass it on
 
153
        jbOK.addActionListener(new ActionListener() {
 
154
            public void actionPerformed(ActionEvent e) {
 
155
                userCancelled = false;
 
156
                dispose();
 
157
            }
 
158
        });
 
159
 
 
160
        // Cancel => discard supplied info and pass on an empty auth
 
161
        jbCancel.addActionListener(new ActionListener() {
 
162
            public void actionPerformed(ActionEvent e) {
 
163
                userCancelled = true;
 
164
                dispose();
 
165
            }
 
166
        });
 
167
 
 
168
        // "return" key in either user or password field => OK
 
169
 
 
170
        jtfUserName.addActionListener(new ActionListener() {
 
171
            public void actionPerformed(ActionEvent e) {
 
172
                userCancelled = false;
 
173
                dispose();
 
174
            }
 
175
        });
 
176
 
 
177
        jpfPassword.addActionListener(new ActionListener() {
 
178
            public void actionPerformed(ActionEvent e) {
 
179
                userCancelled = false;
 
180
                dispose();
 
181
            }
 
182
        });
 
183
    }
 
184
 
 
185
    /**
 
186
     * Present a dialog to the user asking them for authentication information
 
187
     * 
 
188
     * @param hostThe host for with authentication is needed
 
189
     * @param port The port being accessed
 
190
     * @param prompt The prompt (realm) as presented by the server
 
191
     * @param type The type of server (proxy/web)
 
192
     * @return PasswordAuthentication containing the credentials (empty credentials if user cancelled)
 
193
     */
 
194
    protected PasswordAuthentication askUser(String host, int port, String prompt, String type) {
 
195
        PasswordAuthentication auth = null;
 
196
 
 
197
        host += port != -1 ? ":" + port : "";
 
198
 
 
199
        // This frame is reusable. So reset everything first.
 
200
        userCancelled = true;
 
201
        jlInfo.setText("<html>The " + type + " server at " + host + " is requesting authentication. It says \"" + prompt + "\"</html>");
 
202
 
 
203
        try {
 
204
            SwingUtilities.invokeAndWait( new Runnable() {
 
205
                public void run() {
 
206
                    // show dialog to user
 
207
                    setVisible(true);
 
208
                }
 
209
            });
 
210
 
 
211
            if (JNLPRuntime.isDebug()) {
 
212
                System.out.println("password dialog shown");
 
213
            }
 
214
 
 
215
            // wait until dialog is gone
 
216
            while (this.isShowing()) {
 
217
                try {
 
218
                    Thread.sleep(200);
 
219
                } catch (InterruptedException ie) {
 
220
                }
 
221
            }
 
222
 
 
223
            if (JNLPRuntime.isDebug()) {
 
224
                System.out.println("password dialog closed");
 
225
            }
 
226
 
 
227
            if (!userCancelled) {
 
228
                auth = new PasswordAuthentication(jtfUserName.getText(), jpfPassword.getText().toCharArray());
 
229
            }
 
230
        } catch (Exception e) {
 
231
            e.printStackTrace();
 
232
 
 
233
            // Nothing else we can do. Empty auth will be returned
 
234
        }
 
235
 
 
236
        return auth;
 
237
    }
 
238
 
 
239
    public static void main(String[] args) {
 
240
        PasswordAuthenticationDialog frame = new PasswordAuthenticationDialog();
 
241
 
 
242
        PasswordAuthentication auth = frame.askUser("127.0.0.1", 3128, "Password for local proxy", "proxy");
 
243
 
 
244
        System.err.println("Auth info: " + auth.getUserName() + ":" + new String(auth.getPassword()));
 
245
        System.exit(0);
 
246
    }
 
247
}