Skip to content

Commit c91fb00

Browse files
committed
SPARK-2341 Show CAPTCHA image
1 parent c506976 commit c91fb00

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

core/src/main/java/org/jivesoftware/AccountCreationWizard.java

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
2727
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
2828
import org.jivesoftware.smack.util.DNSUtil;
29+
import org.jivesoftware.smackx.bob.element.BoBDataExtension;
2930
import org.jivesoftware.smackx.iqregister.AccountManager;
3031
import org.jivesoftware.smackx.iqregister.packet.Registration;
3132
import org.jivesoftware.smackx.xdata.FormField;
@@ -79,6 +80,8 @@ public class AccountCreationWizard extends JPanel {
7980

8081
private final JPanel formPanelFields = new JPanel();
8182

83+
private final JLabel captcha = new JLabel();
84+
8285
private final JButton createAccountButton = new JButton();
8386

8487
private JDialog dialog;
@@ -139,6 +142,11 @@ public AccountCreationWizard() {
139142
formPanel.setVisible(false);
140143
formPanelFields.setVisible(false);
141144

145+
captcha.setPreferredSize(new java.awt.Dimension(250, 80));
146+
captcha.setRequestFocusEnabled(false);
147+
captcha.setHorizontalAlignment(SwingConstants.CENTER);
148+
instructionsLabel.setVisible(false);
149+
142150
JLabel serverLabel = new JLabel();
143151
ResourceUtils.resLabel( serverLabel, serverField, Res.getString("label.server") + ":");
144152
ResourceUtils.resButton(createAccountButton, Res.getString("button.create.account"));
@@ -165,10 +173,12 @@ public AccountCreationWizard() {
165173

166174
add(formPanelFields, new GridBagConstraints(0, 4, 4, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
167175

176+
add(captcha, new GridBagConstraints(0, 5, 4, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
177+
168178
add(progressBar, new GridBagConstraints(1, 6, 4, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
169179

170-
add(createAccountButton, new GridBagConstraints(2, 6, 1, 1, 1.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
171-
add( closeButton, new GridBagConstraints(3, 6, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
180+
add(createAccountButton, new GridBagConstraints(2, 7, 1, 1, 1.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
181+
add( closeButton, new GridBagConstraints(3, 7, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
172182
}
173183

174184
/**
@@ -245,11 +255,19 @@ private void startRegistration() {
245255
formPanel.setVisible(true);
246256
createAccountButton.setEnabled(true);
247257
String instructions = null;
258+
Icon captchaIcon = null;
248259
Registration info = getRegistrationInfo();
249260
if (info != null) {
261+
// try to get the CAPTCHA image from <data>
262+
// <data type="image/png" max-age="0" cid="[email protected]" xmlns="urn:xmpp:bob">BASE64_OF_PNG_HERE</data>
263+
BoBDataExtension captchaBob = info.getExtension(BoBDataExtension.class);
264+
if (captchaBob != null && "image/png".equals(captchaBob.getBobData().getType())) {
265+
byte[] imageData = captchaBob.getBobData().getContent();
266+
captchaIcon = new ImageIcon(imageData);
267+
}
250268
DataForm regFields = info.getExtension(DataForm.class);
251269
if (regFields != null) {
252-
registrationForm = getRegistrationForm(regFields);
270+
registrationForm = getRegistrationForm(regFields, captchaIcon != null);
253271
instructions = String.join("\n", regFields.getInstructions());
254272
} else {
255273
instructions = info.getInstructions();
@@ -263,6 +281,10 @@ private void startRegistration() {
263281
instructionsLabel.setText(instructions);
264282
instructionsLabel.setVisible(true);
265283
}
284+
if (captchaIcon != null) {
285+
captcha.setIcon(captchaIcon);
286+
instructionsLabel.setVisible(true);
287+
}
266288
} else {
267289
String message = Res.getString("message.create.account.not.allowed");
268290
JOptionPane.showMessageDialog(this, message, Res.getString("title.create.problem"), JOptionPane.ERROR_MESSAGE);
@@ -279,13 +301,16 @@ private void startRegistration() {
279301
}
280302
}
281303

282-
private DataFormUI getRegistrationForm(DataForm regFields) {
283-
// Create a new form without username and password that we will render ourself
284-
DataForm extRegFields = regFields.asBuilder()
304+
private DataFormUI getRegistrationForm(DataForm regFields, boolean noCaptcha) {
305+
// Create a new form without username and password that we will render ourselves
306+
DataForm.Builder extRegFields = regFields.asBuilder()
285307
.removeField("username")
286-
.removeField("password")
287-
.build();
288-
DataFormUI dataFormUI = new DataFormUI(extRegFields);
308+
.removeField("password");
309+
if (noCaptcha) {
310+
extRegFields.removeField("captcha-fallback-url");
311+
extRegFields.removeField("captcha-fallback-text");
312+
}
313+
DataFormUI dataFormUI = new DataFormUI(extRegFields.build());
289314
return dataFormUI;
290315
}
291316

@@ -458,7 +483,7 @@ public void invoke(JFrame parent) {
458483
dialog.getContentPane().add(titlePanel, BorderLayout.NORTH);
459484
dialog.getContentPane().add(this, BorderLayout.CENTER);
460485
dialog.pack();
461-
dialog.setSize(400, 400);
486+
dialog.setSize(400, 580);
462487
dialog.setLocationRelativeTo(parent);
463488
dialog.setVisible(true);
464489
}

0 commit comments

Comments
 (0)