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>
@ -30,6 +32,9 @@ public class LoginDialog extends JDialog {
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;
@ -103,6 +108,21 @@ public class LoginDialog extends JDialog {
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,17 +134,12 @@ 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 -> {
@Override
public void itemStateChanged(ItemEvent e) {
switch (e.getStateChange()) { switch (e.getStateChange()) {
case ItemEvent.SELECTED: case ItemEvent.SELECTED:
contentPanel.add(lblRepeatPassword, gbc_lblRepeatPassword); contentPanel.add(lblRepeatPassword, gbc_lblRepeatPassword);
contentPanel.add(repeatPasswordField, gbc_repeatPasswordField); contentPanel.add(repeatPasswordField, gbc_repeatPasswordField);
setSize(338, 160); setSize(338, 160);
contentPanel.revalidate();
contentPanel.repaint();
break; break;
case ItemEvent.DESELECTED: case ItemEvent.DESELECTED:
@ -132,12 +147,11 @@ public class LoginDialog extends JDialog {
contentPanel.remove(lblRepeatPassword); contentPanel.remove(lblRepeatPassword);
contentPanel.remove(repeatPasswordField); contentPanel.remove(repeatPasswordField);
setSize(338, 123); setSize(338, 123);
contentPanel.revalidate();
contentPanel.repaint();
} }
break; 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 {
credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), false);
dispose();
} }
} else credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), false);
} 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,