Added handling of incorrect logins

This commit is contained in:
delvh 2020-02-09 20:43:12 +01:00
parent e7b8c12cc7
commit 6582e88a02
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(Contacts.class, contacts -> { logger.info("Acquired contacts object " + contacts); this.contacts = contacts; });
receiver.registerProcessor(Message.class, receivedMessageCache);
receiver.registerProcessor(HandshakeRejectionEvent.class, EventBus.getInstance()::dispatch);
// Start receiver
new Thread(receiver).start();

View File

@ -2,14 +2,16 @@ package envoy.client.ui;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import envoy.client.event.HandshakeSuccessfulEvent;
import envoy.data.LoginCredentials;
import envoy.event.EventBus;
import envoy.event.HandshakeRejectionEvent;
/**
* Project: <strong>envoy-client</strong><br>
@ -28,8 +30,11 @@ public class LoginDialog extends JDialog {
private JTextField textField;
private JPasswordField passwordField;
private JPasswordField repeatPasswordField;
private JLabel lblRepeatPassword;
private JPasswordField repeatPasswordField;
private JLabel lblRepeatPassword;
private JLabel errorMessage;
private GridBagConstraints gbc_errorMessage;
private GridBagConstraints gbc_lblRepeatPassword;
private GridBagConstraints gbc_repeatPasswordField;
@ -97,12 +102,27 @@ public class LoginDialog extends JDialog {
gbc_lblRepeatPassword.gridy = 2;
}
{
repeatPasswordField = new JPasswordField();
repeatPasswordField = new JPasswordField();
gbc_repeatPasswordField = new GridBagConstraints();
gbc_repeatPasswordField.fill = GridBagConstraints.HORIZONTAL;
gbc_repeatPasswordField.gridx = 1;
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();
@ -114,30 +134,24 @@ public class LoginDialog extends JDialog {
JCheckBox registerCheckBox = new JCheckBox();
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
public void itemStateChanged(ItemEvent e) {
switch (e.getStateChange()) {
case ItemEvent.SELECTED:
contentPanel.add(lblRepeatPassword, gbc_lblRepeatPassword);
contentPanel.add(repeatPasswordField, gbc_repeatPasswordField);
setSize(338, 160);
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;
}
case ItemEvent.DESELECTED:
if (repeatPasswordField.getParent() == contentPanel) {
contentPanel.remove(lblRepeatPassword);
contentPanel.remove(repeatPasswordField);
setSize(338, 123);
}
break;
}
contentPanel.revalidate();
contentPanel.repaint();
});
buttonPane.add(registerCheckBox);
@ -148,18 +162,14 @@ public class LoginDialog extends JDialog {
okButton.addActionListener((evt) -> {
try {
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);
dispose();
} else {
JOptionPane.showMessageDialog(this, "The repeated password is unequal to the origional password!");
passwordField.setText(null);
repeatPasswordField.setText(null);
else {
JOptionPane.showMessageDialog(this, "The repeated password is not the origional password!");
clearPasswordFields();
}
} else {
credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), false);
dispose();
}
} else credentials = new LoginCredentials(textField.getText(), passwordField.getPassword(), false);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
@ -178,6 +188,17 @@ public class LoginDialog extends JDialog {
setModal(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 entering offline mode
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");
client.setSender(clientUser);
JOptionPane.showMessageDialog(null,