~ubuntu-branches/ubuntu/natty/icedtea-web/natty-security

« 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: 2011-02-24 12:57:25 UTC
  • mto: (18.1.1 experimental) (19.1.1 natty-security)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: james.westby@ubuntu.com-20110224125725-8zq5v35r6o27w8ku
Tags: upstream-1.1~20110320
ImportĀ upstreamĀ versionĀ 1.1~20110320

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
 
        c = new GridBagConstraints();
115
 
        c.gridx = 0;
116
 
        c.gridy = 2;
117
 
        c.insets = new Insets(5, 5, 3, 3);
118
 
        add(jlPassword, c);
119
 
 
120
 
        c = new GridBagConstraints();
121
 
        c.fill = c.HORIZONTAL;
122
 
        c.gridx = 1;
123
 
        c.gridy = 2;
124
 
        c.insets = new Insets(5, 5, 3, 3);
125
 
        c.weightx = 1.0;
126
 
        add(jpfPassword, c);
127
 
 
128
 
        c = new GridBagConstraints();
129
 
        c.anchor = c.SOUTHEAST;
130
 
        c.gridx = 1;
131
 
        c.gridy = 3;
132
 
        c.insets = new Insets(5, 5, 3, 70);
133
 
        c.weightx = 0.0;
134
 
        add(jbCancel, c);
135
 
 
136
 
        c = new GridBagConstraints();
137
 
        c.anchor = c.SOUTHEAST;
138
 
        c.gridx = 1;
139
 
        c.gridy = 3;
140
 
        c.insets = new Insets(5, 5, 3, 3);
141
 
        c.weightx = 0.0;
142
 
        add(jbOK, c);
143
 
 
144
 
        setMinimumSize(new Dimension(400, 150));
145
 
        setMaximumSize(new Dimension(1024, 150));
146
 
        setAlwaysOnTop(true);
147
 
 
148
 
        setSize(400, 150);
149
 
        setLocationRelativeTo(null);
150
 
 
151
 
        // OK => read supplied info and pass it on
152
 
        jbOK.addActionListener(new ActionListener() {
153
 
            public void actionPerformed(ActionEvent e) {
154
 
                userCancelled = false;
155
 
                dispose();
156
 
            }
157
 
        });
158
 
 
159
 
        // Cancel => discard supplied info and pass on an empty auth
160
 
        jbCancel.addActionListener(new ActionListener() {
161
 
            public void actionPerformed(ActionEvent e) {
162
 
                userCancelled = true;
163
 
                dispose();
164
 
            }
165
 
        });
166
 
 
167
 
        // "return" key in either user or password field => OK
168
 
 
169
 
        jtfUserName.addActionListener(new ActionListener() {
170
 
            public void actionPerformed(ActionEvent e) {
171
 
                userCancelled = false;
172
 
                dispose();
173
 
            }
174
 
        });
175
 
 
176
 
        jpfPassword.addActionListener(new ActionListener() {
177
 
            public void actionPerformed(ActionEvent e) {
178
 
                userCancelled = false;
179
 
                dispose();
180
 
            }
181
 
        });
182
 
    }
183
 
 
184
 
    /**
185
 
     * Present a dialog to the user asking them for authentication information
186
 
     *
187
 
     * @param host The host for with authentication is needed
188
 
     * @param port The port being accessed
189
 
     * @param prompt The prompt (realm) as presented by the server
190
 
     * @param type The type of server (proxy/web)
191
 
     * @return PasswordAuthentication containing the credentials (empty credentials if user cancelled)
192
 
     */
193
 
    protected PasswordAuthentication askUser(String host, int port, String prompt, String type) {
194
 
        PasswordAuthentication auth = null;
195
 
 
196
 
        host += port != -1 ? ":" + port : "";
197
 
 
198
 
        // This frame is reusable. So reset everything first.
199
 
        userCancelled = true;
200
 
        jlInfo.setText("<html>The " + type + " server at " + host +
201
 
                       " 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.getPassword());
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
 
}