1
/* PasswordAuthenticationDialog -- requests authentication information from users
2
Copyright (C) 2009 Red Hat
4
This file is part of IcedTea.
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)
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.
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
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
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. */
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;
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;
56
* Modal non-minimizable dialog to request http authentication credentials
59
public class PasswordAuthenticationDialog extends JDialog {
61
private JLabel jlInfo = new JLabel("");
62
private JTextField jtfUserName = new JTextField();
63
private JPasswordField jpfPassword = new JPasswordField();
64
private boolean userCancelled;
66
public PasswordAuthenticationDialog() {
71
* Initialized the dialog components
74
public void initialize() {
76
setTitle("IcedTea Java Plugin - Authorization needed to proceed");
78
setLayout(new GridBagLayout());
80
JLabel jlUserName = new JLabel("Username: ");
81
JLabel jlPassword = new JLabel("Password: ");
82
JButton jbOK = new JButton("OK");
83
JButton jbCancel = new JButton("Cancel");
85
jtfUserName.setSize(20, 10);
86
jpfPassword.setSize(20, 10);
90
c = new GridBagConstraints();
91
c.fill = c.HORIZONTAL;
95
c.insets = new Insets(10, 5, 3, 3);
98
c = new GridBagConstraints();
101
c.insets = new Insets(10, 5, 3, 3);
104
c = new GridBagConstraints();
105
c.fill = c.HORIZONTAL;
108
c.insets = new Insets(10, 5, 3, 3);
113
c = new GridBagConstraints();
116
c.insets = new Insets(5, 5, 3, 3);
119
c = new GridBagConstraints();
120
c.fill = c.HORIZONTAL;
123
c.insets = new Insets(5, 5, 3, 3);
127
c = new GridBagConstraints();
128
c.anchor = c.SOUTHEAST;
131
c.insets = new Insets(5, 5, 3, 70);
135
c = new GridBagConstraints();
136
c.anchor = c.SOUTHEAST;
139
c.insets = new Insets(5, 5, 3, 3);
143
setMinimumSize(new Dimension(400,150));
144
setMaximumSize(new Dimension(1024,150));
145
setAlwaysOnTop(true);
148
setLocationRelativeTo(null);
150
// OK => read supplied info and pass it on
151
jbOK.addActionListener(new ActionListener() {
152
public void actionPerformed(ActionEvent e) {
153
userCancelled = false;
158
// Cancel => discard supplied info and pass on an empty auth
159
jbCancel.addActionListener(new ActionListener() {
160
public void actionPerformed(ActionEvent e) {
161
userCancelled = true;
166
// "return" key in either user or password field => OK
168
jtfUserName.addActionListener(new ActionListener() {
169
public void actionPerformed(ActionEvent e) {
170
userCancelled = false;
175
jpfPassword.addActionListener(new ActionListener() {
176
public void actionPerformed(ActionEvent e) {
177
userCancelled = false;
184
* Present a dialog to the user asking them for authentication information
186
* @param hostThe host for with authentication is needed
187
* @param port The port being accessed
188
* @param prompt The prompt (realm) as presented by the server
189
* @param type The type of server (proxy/web)
190
* @return PasswordAuthentication containing the credentials (empty credentials if user cancelled)
192
protected PasswordAuthentication askUser(String host, int port, String prompt, String type) {
193
PasswordAuthentication auth = null;
195
host += port != -1 ? ":" + port : "";
197
// This frame is reusable. So reset everything first.
198
userCancelled = true;
199
jlInfo.setText("<html>The " + type + " server at " + host + " is requesting authentication. It says \"" + prompt + "\"</html>");
202
SwingUtilities.invokeAndWait( new Runnable() {
204
// show dialog to user
209
PluginDebug.debug("password dialog shown");
211
// wait until dialog is gone
212
while (this.isShowing()) {
215
} catch (InterruptedException ie) {
219
PluginDebug.debug("password dialog closed");
221
if (!userCancelled) {
222
auth = new PasswordAuthentication(jtfUserName.getText(), jpfPassword.getText().toCharArray());
224
} catch (Exception e) {
227
// Nothing else we can do. Empty auth will be returned
233
public static void main(String[] args) {
234
PasswordAuthenticationDialog frame = new PasswordAuthenticationDialog();
236
PasswordAuthentication auth = frame.askUser("127.0.0.1", 3128, "Password for local proxy", "proxy");
238
System.err.println("Auth info: " + auth.getUserName() + ":" + new String(auth.getPassword()));