/
AuthorizePopup.java
249 lines (208 loc) · 6.67 KB
/
AuthorizePopup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Donated code for Gallery Project
* Copyright abandonded by Stanley Knutson (Stanley@stanleyKnutson.com)
*/
package com.gallery.GalleryRemote;
import HTTPClient.NVPair;
import com.gallery.GalleryRemote.util.GRI18n;
import com.gallery.GalleryRemote.util.DialogUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* Do the authorization popup in a swing-compatible way. Built from the code in
* HTTPClient.DefaultAuthHandler
*
* @author Stanley Knutson
* @created Januray 3, 2003
*/
public class AuthorizePopup implements HTTPClient.AuthorizationPrompter {
private static BasicAuthBox inp = null;
public static final String MODULE = "AuthorizePopup";
public static String hackUsername = null;
public static String hackPassword = null;
/**
* Engage this class to be the authorization popup
*/
public static void enable() {
HTTPClient.DefaultAuthHandler.setAuthorizationPrompter
(new AuthorizePopup());
}
/**
* the method called by DefaultAuthHandler.
*
* @param challenge Description of Parameter
* @param forProxy Description of Parameter
* @return the username/password pair
*/
public NVPair getUsernamePassword(HTTPClient.AuthorizationInfo challenge,
boolean forProxy) {
// The HTTPClient library doesn't correctly accept setting auth params
// ahead of time (because it can't accept a realm of *
// so we have to do it this hackish way...
if (challenge.getScheme().equalsIgnoreCase("Basic") && hackUsername != null) {
return new NVPair(hackUsername, hackPassword);
}
String line1;
String line2;
String line3;
if (challenge.getScheme().equalsIgnoreCase("SOCKS5")) {
line1 = GRI18n.getString(MODULE, "enterUsrPwd");
line2 = challenge.getHost();
line3 = GRI18n.getString(MODULE, "authMthd");
} else {
line1 = GRI18n.getString(MODULE, "enterUsrPwdRealm", new Object[] { challenge.getRealm() });
line2 = GRI18n.getString(MODULE, "onHost", new Object[] { challenge.getHost() + ":" +
challenge.getPort()});
line3 = GRI18n.getString(MODULE, "authScheme", new Object[] { challenge.getScheme() });
}
synchronized (getClass()) {
if (inp == null) {
inp = new BasicAuthBox(GalleryRemote._().getMainFrame());
}
}
return inp.getInput(line1, line2, line3, challenge.getScheme());
}
/**
* This class implements a simple popup that request username and password
* used for the "basic" and "digest" authentication schemes.
*
* @author Ronald Tschalar
* @version 0.3-3 06/05/2001
* @created February 2, 2003
*/
private static class BasicAuthBox extends JDialog implements ActionListener {
private final static String title = GRI18n.getString(MODULE, "authreq");
private JLabel line1, line2, line3;
private JTextField user, pass;
private int done;
private final static int OK = 1, CANCEL = 0;
/**
* Constructs the popup with two lines of text above the input fields
*
* @param container Description of Parameter
*/
BasicAuthBox(Frame container) {
super(container, title, true);
addNotify();
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
JPanel p = new JPanel(new GridLayout(3, 1));
p.add(line1 = new JLabel());
p.add(line2 = new JLabel());
p.add(line3 = new JLabel());
getContentPane().add("North", p);
p = new JPanel(new GridLayout(2, 1));
p.add(new JLabel(GRI18n.getString(MODULE, "username")));
p.add(new JLabel(GRI18n.getString(MODULE, "passwd")));
getContentPane().add("West", p);
p = new JPanel(new GridLayout(2, 1));
p.add(user = new JTextField(30));
p.add(pass = new JPasswordField(30));
pass.addActionListener(this);
getContentPane().add("East", p);
GridBagLayout gb = new GridBagLayout();
p = new JPanel(gb);
GridBagConstraints constr = new GridBagConstraints();
JPanel pp = new JPanel();
p.add(pp);
constr.gridwidth = GridBagConstraints.REMAINDER;
gb.setConstraints(pp, constr);
constr.gridwidth = 1;
constr.weightx = 1.0;
JButton b;
p.add(b = new JButton(GRI18n.getString("Common", "OK")));
b.addActionListener(this);
b.setActionCommand("ok");
getRootPane().setDefaultButton(b);
constr.weightx = 1.0;
gb.setConstraints(b, constr);
p.add(b = new JButton(GRI18n.getString(MODULE, "clear")));
b.addActionListener(this);
b.setActionCommand("clear");
constr.weightx = 2.0;
gb.setConstraints(b, constr);
p.add(b = new JButton(GRI18n.getString("Common", "Cancel")));
b.addActionListener(this);
b.setActionCommand("cancel");
constr.weightx = 1.0;
gb.setConstraints(b, constr);
getContentPane().add("South", p);
pack();
}
/**
* the method called by SimpleAuthPopup.
*
* @param l1 Description of Parameter
* @param l2 Description of Parameter
* @param l3 Description of Parameter
* @param scheme Description of Parameter
* @return the username/password pair
*/
NVPair getInput(String l1, String l2, String l3,
String scheme) {
line1.setText(l1);
line2.setText(l2);
line3.setText(l3);
line1.invalidate();
line2.invalidate();
line3.invalidate();
setResizable(true);
pack();
setResizable(false);
// put popup at upper right of the parent frame (assuming we have one)
DialogUtil.center(this, getOwner());
boolean user_focus = true;
if (scheme.equalsIgnoreCase("NTLM")) {
// prefill the user field with the username
try {
user.setText(System.getProperty("user.name", ""));
user_focus = false;
} catch (SecurityException se) {
}
}
// This call to request focus probably does not do anything, but leave it in.
if (user_focus) {
user.requestFocus();
} else {
pass.requestFocus();
}
// This is a modal dialog: the "show" method does not return
// until "hide" is called by close, cancel or ok action
show();
NVPair result = new NVPair(user.getText(), pass.getText());
user.setText("");
pass.setText("");
if (done == CANCEL) {
return null;
} else {
return result;
}
}
/**
* Invoked when an action occurs.
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == pass) {
done = OK;
hide();
} else if ("ok".equals(e.getActionCommand())) {
user.setText("");
pass.setText("");
user.requestFocus();
} else if ("cancel".equals(e.getActionCommand())) {
done = CANCEL;
hide();
} else if ("close".equals(e.getActionCommand())) {
}
}
/*private class Close extends WindowAdapter {
public void windowClosing(WindowEvent we) {
new Cancel().actionPerformed(null);
}
}*/
}
}