Skip to content

Commit

Permalink
SPARK-2341 Show CAPTCHA image
Browse files Browse the repository at this point in the history
  • Loading branch information
stokito committed Sep 12, 2024
1 parent c506976 commit c91fb00
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions core/src/main/java/org/jivesoftware/AccountCreationWizard.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smack.util.DNSUtil;
import org.jivesoftware.smackx.bob.element.BoBDataExtension;
import org.jivesoftware.smackx.iqregister.AccountManager;
import org.jivesoftware.smackx.iqregister.packet.Registration;
import org.jivesoftware.smackx.xdata.FormField;
Expand Down Expand Up @@ -79,6 +80,8 @@ public class AccountCreationWizard extends JPanel {

private final JPanel formPanelFields = new JPanel();

private final JLabel captcha = new JLabel();

private final JButton createAccountButton = new JButton();

private JDialog dialog;
Expand Down Expand Up @@ -139,6 +142,11 @@ public AccountCreationWizard() {
formPanel.setVisible(false);
formPanelFields.setVisible(false);

captcha.setPreferredSize(new java.awt.Dimension(250, 80));
captcha.setRequestFocusEnabled(false);
captcha.setHorizontalAlignment(SwingConstants.CENTER);
instructionsLabel.setVisible(false);

JLabel serverLabel = new JLabel();
ResourceUtils.resLabel( serverLabel, serverField, Res.getString("label.server") + ":");
ResourceUtils.resButton(createAccountButton, Res.getString("button.create.account"));
Expand All @@ -165,10 +173,12 @@ public AccountCreationWizard() {

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

add(captcha, new GridBagConstraints(0, 5, 4, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));

add(progressBar, new GridBagConstraints(1, 6, 4, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

add(createAccountButton, new GridBagConstraints(2, 6, 1, 1, 1.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
add( closeButton, new GridBagConstraints(3, 6, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
add(createAccountButton, new GridBagConstraints(2, 7, 1, 1, 1.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
add( closeButton, new GridBagConstraints(3, 7, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
}

/**
Expand Down Expand Up @@ -245,11 +255,19 @@ private void startRegistration() {
formPanel.setVisible(true);
createAccountButton.setEnabled(true);
String instructions = null;
Icon captchaIcon = null;
Registration info = getRegistrationInfo();
if (info != null) {
// try to get the CAPTCHA image from <data>
// <data type="image/png" max-age="0" cid="[email protected]" xmlns="urn:xmpp:bob">BASE64_OF_PNG_HERE</data>
BoBDataExtension captchaBob = info.getExtension(BoBDataExtension.class);
if (captchaBob != null && "image/png".equals(captchaBob.getBobData().getType())) {
byte[] imageData = captchaBob.getBobData().getContent();
captchaIcon = new ImageIcon(imageData);
}
DataForm regFields = info.getExtension(DataForm.class);
if (regFields != null) {
registrationForm = getRegistrationForm(regFields);
registrationForm = getRegistrationForm(regFields, captchaIcon != null);
instructions = String.join("\n", regFields.getInstructions());
} else {
instructions = info.getInstructions();
Expand All @@ -263,6 +281,10 @@ private void startRegistration() {
instructionsLabel.setText(instructions);
instructionsLabel.setVisible(true);
}
if (captchaIcon != null) {
captcha.setIcon(captchaIcon);
instructionsLabel.setVisible(true);
}
} else {
String message = Res.getString("message.create.account.not.allowed");
JOptionPane.showMessageDialog(this, message, Res.getString("title.create.problem"), JOptionPane.ERROR_MESSAGE);
Expand All @@ -279,13 +301,16 @@ private void startRegistration() {
}
}

private DataFormUI getRegistrationForm(DataForm regFields) {
// Create a new form without username and password that we will render ourself
DataForm extRegFields = regFields.asBuilder()
private DataFormUI getRegistrationForm(DataForm regFields, boolean noCaptcha) {
// Create a new form without username and password that we will render ourselves
DataForm.Builder extRegFields = regFields.asBuilder()
.removeField("username")
.removeField("password")
.build();
DataFormUI dataFormUI = new DataFormUI(extRegFields);
.removeField("password");
if (noCaptcha) {
extRegFields.removeField("captcha-fallback-url");
extRegFields.removeField("captcha-fallback-text");
}
DataFormUI dataFormUI = new DataFormUI(extRegFields.build());
return dataFormUI;
}

Expand Down Expand Up @@ -458,7 +483,7 @@ public void invoke(JFrame parent) {
dialog.getContentPane().add(titlePanel, BorderLayout.NORTH);
dialog.getContentPane().add(this, BorderLayout.CENTER);
dialog.pack();
dialog.setSize(400, 400);
dialog.setSize(400, 580);
dialog.setLocationRelativeTo(parent);
dialog.setVisible(true);
}
Expand Down

0 comments on commit c91fb00

Please sign in to comment.