Added handling of incorrect logins

This commit is contained in:
delvh 2020-02-09 20:43:12 +01:00
parent ac2a1aad56
commit d37ab53275
4 changed files with 79 additions and 39 deletions

View File

@ -0,0 +1,18 @@
package envoy.client.event;
import envoy.event.Event;
/**
* This {@link Event} indicates that a handshake completed successful
*
* Project: <strong>envoy-client</strong><br>
* File: <strong>HandshakeSuccessfulEvent.java</strong><br>
* Created: <strong>8 Feb 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy v0.3-alpha
*/
public class HandshakeSuccessfulEvent implements Event<Void> {
private static final long serialVersionUID = -157972384126278855L;
}

View File

@ -74,6 +74,7 @@ public class Client implements Closeable {
receiver.registerProcessor(User.class, sender -> { logger.info("Acquired user object " + sender); this.sender = sender; }); receiver.registerProcessor(User.class, sender -> { logger.info("Acquired user object " + sender); this.sender = sender; });
receiver.registerProcessor(Contacts.class, contacts -> { logger.info("Acquired contacts object " + contacts); this.contacts = contacts; }); receiver.registerProcessor(Contacts.class, contacts -> { logger.info("Acquired contacts object " + contacts); this.contacts = contacts; });
receiver.registerProcessor(Message.class, receivedMessageCache); receiver.registerProcessor(Message.class, receivedMessageCache);
receiver.registerProcessor(HandshakeRejectionEvent.class, EventBus.getInstance()::dispatch);
// Start receiver // Start receiver
new Thread(receiver).start(); new Thread(receiver).start();

View File

@ -2,14 +2,16 @@ package envoy.client.ui;
import java.awt.*; import java.awt.*;
import java.awt.event.ItemEvent; import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.Arrays;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import envoy.client.event.HandshakeSuccessfulEvent;
import envoy.data.LoginCredentials; import envoy.data.LoginCredentials;
import envoy.event.EventBus;
import envoy.event.HandshakeRejectionEvent;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -28,8 +30,11 @@ public class LoginDialog extends JDialog {
private JTextField textField; private JTextField textField;
private JPasswordField passwordField; private JPasswordField passwordField;
private JPasswordField repeatPasswordField; private JPasswordField repeatPasswordField;
private JLabel lblRepeatPassword; private JLabel lblRepeatPassword;
private JLabel errorMessage;
private GridBagConstraints gbc_errorMessage;
private GridBagConstraints gbc_lblRepeatPassword; private GridBagConstraints gbc_lblRepeatPassword;
private GridBagConstraints gbc_repeatPasswordField; private GridBagConstraints gbc_repeatPasswordField;
@ -97,12 +102,27 @@ public class LoginDialog extends JDialog {
gbc_lblRepeatPassword.gridy = 2; gbc_lblRepeatPassword.gridy = 2;
} }
{ {
repeatPasswordField = new JPasswordField(); repeatPasswordField = new JPasswordField();
gbc_repeatPasswordField = new GridBagConstraints(); gbc_repeatPasswordField = new GridBagConstraints();
gbc_repeatPasswordField.fill = GridBagConstraints.HORIZONTAL; gbc_repeatPasswordField.fill = GridBagConstraints.HORIZONTAL;
gbc_repeatPasswordField.gridx = 1; gbc_repeatPasswordField.gridx = 1;
gbc_repeatPasswordField.gridy = 2; gbc_repeatPasswordField.gridy = 2;
} }
{
EventBus.getInstance().register(HandshakeRejectionEvent.class, evt -> {
contentPanel.remove(errorMessage);
clearPasswordFields();
// TODO delete - only for testing purposes
System.out.println("Caught HandshakeRejectionEvent with reason" + ((HandshakeRejectionEvent) evt).get());
errorMessage = new JLabel(((HandshakeRejectionEvent) evt).get());
gbc_errorMessage = new GridBagConstraints();
gbc_errorMessage.gridx = 2;
gbc_errorMessage.gridy = 0;
gbc_errorMessage.fill = GridBagConstraints.HORIZONTAL;
gbc_errorMessage.insets = new Insets(5, 5, 5, 5);
contentPanel.add(errorMessage, gbc_errorMessage);
});
}
{ {
JPanel buttonPane = new JPanel(); JPanel buttonPane = new JPanel();
@ -114,30 +134,24 @@ public class LoginDialog extends JDialog {
JCheckBox registerCheckBox = new JCheckBox(); JCheckBox registerCheckBox = new JCheckBox();
registerCheckBox.setAlignmentX(LEFT_ALIGNMENT); registerCheckBox.setAlignmentX(LEFT_ALIGNMENT);
registerCheckBox.addItemListener(new ItemListener() { registerCheckBox.addItemListener(e -> {
switch (e.getStateChange()) {
case ItemEvent.SELECTED:
contentPanel.add(lblRepeatPassword, gbc_lblRepeatPassword);
contentPanel.add(repeatPasswordField, gbc_repeatPasswordField);
setSize(338, 160);
break;
@Override case ItemEvent.DESELECTED:
public void itemStateChanged(ItemEvent e) { if (repeatPasswordField.getParent() == contentPanel) {
switch (e.getStateChange()) { contentPanel.remove(lblRepeatPassword);
case ItemEvent.SELECTED: contentPanel.remove(repeatPasswordField);
contentPanel.add(lblRepeatPassword, gbc_lblRepeatPassword); setSize(338, 123);
contentPanel.add(repeatPasswordField, gbc_repeatPasswordField); }
setSize(338, 160); break;
contentPanel.revalidate();
contentPanel.repaint();
break;
case ItemEvent.DESELECTED:
if (repeatPasswordField.getParent() == contentPanel) {
contentPanel.remove(lblRepeatPassword);
contentPanel.remove(repeatPasswordField);
setSize(338, 123);
contentPanel.revalidate();
contentPanel.repaint();
}
break;
}
} }
contentPanel.revalidate();
contentPanel.repaint();
}); });
buttonPane.add(registerCheckBox); buttonPane.add(registerCheckBox);
@ -148,18 +162,14 @@ public class LoginDialog extends JDialog {
okButton.addActionListener((evt) -> { okButton.addActionListener((evt) -> {
try { try {
if (registerCheckBox.isSelected()) { if (registerCheckBox.isSelected()) {
if (Arrays.equals(passwordField.getPassword(), repeatPasswordField.getPassword())) { // password checking
if (Arrays.equals(passwordField.getPassword(), repeatPasswordField.getPassword()))
credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), true); credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), true);
dispose(); else {
} else { JOptionPane.showMessageDialog(this, "The repeated password is not the origional password!");
JOptionPane.showMessageDialog(this, "The repeated password is unequal to the origional password!"); clearPasswordFields();
passwordField.setText(null);
repeatPasswordField.setText(null);
} }
} else { } else credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), false);
credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), false);
dispose();
}
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -178,6 +188,17 @@ public class LoginDialog extends JDialog {
setModal(true); setModal(true);
setVisible(true); setVisible(true);
EventBus.getInstance().register(HandshakeSuccessfulEvent.class, evt -> this.dispose());
}
/**
* Resets the text stored by this
*
* @since Envoy v0.3-alpha
*/
public void clearPasswordFields() {
passwordField.setText(null);
repeatPasswordField.setText(null);
} }
/** /**

View File

@ -117,7 +117,7 @@ public class Startup {
try { try {
// Try entering offline mode // Try entering offline mode
localDb.loadUsers(); localDb.loadUsers();
User clientUser = localDb.getUsers().get(credentials.getName()); User clientUser = localDb.getUsers().get(credentials.getIdentifier());
if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown"); if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown");
client.setSender(clientUser); client.setSender(clientUser);
JOptionPane.showMessageDialog(null, JOptionPane.showMessageDialog(null,