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. */
38
package net.sourceforge.jnlp.security;
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;
55
import net.sourceforge.jnlp.runtime.JNLPRuntime;
58
* Modal non-minimizable dialog to request http authentication credentials
61
public class PasswordAuthenticationDialog extends JDialog {
63
private JLabel jlInfo = new JLabel("");
64
private JTextField jtfUserName = new JTextField();
65
private JPasswordField jpfPassword = new JPasswordField();
66
private boolean userCancelled;
68
public PasswordAuthenticationDialog() {
73
* Initialized the dialog components
76
public void initialize() {
78
setTitle("IcedTea Java Plugin - Authorization needed to proceed");
80
setLayout(new GridBagLayout());
82
JLabel jlUserName = new JLabel("Username: ");
83
JLabel jlPassword = new JLabel("Password: ");
84
JButton jbOK = new JButton("OK");
85
JButton jbCancel = new JButton("Cancel");
87
jtfUserName.setSize(20, 10);
88
jpfPassword.setSize(20, 10);
92
c = new GridBagConstraints();
93
c.fill = c.HORIZONTAL;
97
c.insets = new Insets(10, 5, 3, 3);
100
c = new GridBagConstraints();
103
c.insets = new Insets(10, 5, 3, 3);
106
c = new GridBagConstraints();
107
c.fill = c.HORIZONTAL;
110
c.insets = new Insets(10, 5, 3, 3);
115
c = new GridBagConstraints();
118
c.insets = new Insets(5, 5, 3, 3);
121
c = new GridBagConstraints();
122
c.fill = c.HORIZONTAL;
125
c.insets = new Insets(5, 5, 3, 3);
129
c = new GridBagConstraints();
130
c.anchor = c.SOUTHEAST;
133
c.insets = new Insets(5, 5, 3, 70);
137
c = new GridBagConstraints();
138
c.anchor = c.SOUTHEAST;
141
c.insets = new Insets(5, 5, 3, 3);
145
setMinimumSize(new Dimension(400,150));
146
setMaximumSize(new Dimension(1024,150));
147
setAlwaysOnTop(true);
150
setLocationRelativeTo(null);
152
// OK => read supplied info and pass it on
153
jbOK.addActionListener(new ActionListener() {
154
public void actionPerformed(ActionEvent e) {
155
userCancelled = false;
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;
168
// "return" key in either user or password field => OK
170
jtfUserName.addActionListener(new ActionListener() {
171
public void actionPerformed(ActionEvent e) {
172
userCancelled = false;
177
jpfPassword.addActionListener(new ActionListener() {
178
public void actionPerformed(ActionEvent e) {
179
userCancelled = false;
186
* Present a dialog to the user asking them for authentication information
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)
194
protected PasswordAuthentication askUser(String host, int port, String prompt, String type) {
195
PasswordAuthentication auth = null;
197
host += port != -1 ? ":" + port : "";
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>");
204
SwingUtilities.invokeAndWait( new Runnable() {
206
// show dialog to user
211
if (JNLPRuntime.isDebug()) {
212
System.out.println("password dialog shown");
215
// wait until dialog is gone
216
while (this.isShowing()) {
219
} catch (InterruptedException ie) {
223
if (JNLPRuntime.isDebug()) {
224
System.out.println("password dialog closed");
227
if (!userCancelled) {
228
auth = new PasswordAuthentication(jtfUserName.getText(), jpfPassword.getText().toCharArray());
230
} catch (Exception e) {
233
// Nothing else we can do. Empty auth will be returned
239
public static void main(String[] args) {
240
PasswordAuthenticationDialog frame = new PasswordAuthenticationDialog();
242
PasswordAuthentication auth = frame.askUser("127.0.0.1", 3128, "Password for local proxy", "proxy");
244
System.err.println("Auth info: " + auth.getUserName() + ":" + new String(auth.getPassword()));