Added ability to change the password, theoretically on client and server
(needs testing!)
This commit is contained in:
parent
719aa4cd4f
commit
0d77fbf831
@ -158,6 +158,12 @@ public class Client implements Closeable {
|
||||
// Process IsTyping events
|
||||
receiver.registerProcessor(IsTyping.class, eventBus::dispatch);
|
||||
|
||||
// Process PasswordChangeResults
|
||||
receiver.registerProcessor(PasswordChangeResult.class, eventBus::dispatch);
|
||||
|
||||
// Process ProfilePicChanges
|
||||
receiver.registerProcessor(ProfilePicChange.class, eventBus::dispatch);
|
||||
|
||||
// Send event
|
||||
eventBus.register(SendEvent.class, evt -> {
|
||||
try {
|
||||
|
@ -6,10 +6,10 @@ import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Cursor;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.control.Alert.AlertType;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.input.InputEvent;
|
||||
@ -35,7 +35,7 @@ public class UserSettingsPane extends SettingsPane {
|
||||
|
||||
private boolean profilePicChanged, usernameChanged, passwordChanged, validPassword;
|
||||
private byte[] currentImageBytes;
|
||||
private String newUsername, newPassword;
|
||||
private String newUsername, newPassword = "";
|
||||
|
||||
/**
|
||||
* Creates a new {@code UserSettingsPane}.
|
||||
@ -81,7 +81,8 @@ public class UserSettingsPane extends SettingsPane {
|
||||
hbox.getChildren().add(profilePic);
|
||||
|
||||
// Displaying the username change mechanism
|
||||
final var username = user.getName();
|
||||
final var username = user.getName();
|
||||
newUsername = username;
|
||||
final var usernameTextField = new TextField(username);
|
||||
final EventHandler<? super InputEvent> textChanged = e -> {
|
||||
newUsername = usernameTextField.getText();
|
||||
@ -91,6 +92,39 @@ public class UserSettingsPane extends SettingsPane {
|
||||
usernameTextField.setOnKeyTyped(textChanged);
|
||||
hbox.getChildren().add(usernameTextField);
|
||||
vbox.getChildren().add(hbox);
|
||||
|
||||
// "Displaying" the password change mechanism
|
||||
final HBox[] passwordHBoxes = { new HBox(), new HBox(), new HBox() };
|
||||
final Label[] passwordLabels = { new Label("Enter current password:"), new Label("Enter new password:"),
|
||||
new Label("Repeat new password:") };
|
||||
|
||||
final var currentPasswordField = new PasswordField();
|
||||
final var newPasswordField = new PasswordField();
|
||||
final var repeatNewPasswordField = new PasswordField();
|
||||
final PasswordField[] passwordFields = { currentPasswordField, newPasswordField, repeatNewPasswordField };
|
||||
final EventHandler<? super InputEvent> passwordEntered = e -> {
|
||||
newPassword = newPasswordField.getText();
|
||||
validPassword = newPassword.equals(repeatNewPasswordField.getText())
|
||||
&& !newPasswordField.getText().isBlank();
|
||||
};
|
||||
newPasswordField.setOnInputMethodTextChanged(passwordEntered);
|
||||
newPasswordField.setOnKeyTyped(passwordEntered);
|
||||
repeatNewPasswordField.setOnInputMethodTextChanged(passwordEntered);
|
||||
repeatNewPasswordField.setOnKeyTyped(passwordEntered);
|
||||
|
||||
for (int i = 0; i < passwordHBoxes.length; i++) {
|
||||
final var hBox2 = passwordHBoxes[i];
|
||||
hBox2.getChildren().add(passwordLabels[i]);
|
||||
hBox2.getChildren().add(passwordFields[i]);
|
||||
vbox.getChildren().add(hBox2);
|
||||
}
|
||||
|
||||
// Displaying the save button
|
||||
final var saveButton = new Button("Save");
|
||||
saveButton.setOnAction(e -> save(user.getID(), currentPasswordField.getText()));
|
||||
saveButton.setAlignment(Pos.BOTTOM_RIGHT);
|
||||
vbox.getChildren().add(saveButton);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -118,7 +152,7 @@ public class UserSettingsPane extends SettingsPane {
|
||||
|
||||
// The password was changed
|
||||
if (passwordChanged && validPassword) eventbus.dispatch(new SendEvent(new PasswordChangeRequest(newPassword, oldPassword, userID)));
|
||||
else if (!(validPassword || newPassword != null && newPassword.isBlank())) {
|
||||
else if (!(validPassword || newPassword.isBlank())) {
|
||||
final var alert = new Alert(AlertType.ERROR);
|
||||
alert.setTitle("Unequal Password");
|
||||
alert.setContentText("Repeated password is unequal to the chosen new password");
|
||||
|
44
common/src/main/java/envoy/event/PasswordChangeRequest.java
Normal file
44
common/src/main/java/envoy/event/PasswordChangeRequest.java
Normal file
@ -0,0 +1,44 @@
|
||||
package envoy.event;
|
||||
|
||||
import envoy.data.Contact;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
* File: <strong>PasswordChangeRequest.java</strong><br>
|
||||
* Created: <strong>31.07.2020</strong><br>
|
||||
*
|
||||
* @author Leon Hofmeister
|
||||
* @since Envoy Common v0.2-beta
|
||||
*/
|
||||
public class PasswordChangeRequest extends Event<String> {
|
||||
|
||||
private final long id;
|
||||
|
||||
private final String oldPassword;
|
||||
|
||||
private static final long serialVersionUID = 0L;
|
||||
|
||||
/**
|
||||
* @param newPassword the new password of that user
|
||||
* @param oldPassword the old password of that user
|
||||
* @param userID the ID of the user who wants to change his password
|
||||
* @since Envoy Common v0.2-beta
|
||||
*/
|
||||
public PasswordChangeRequest(String newPassword, String oldPassword, long userID) {
|
||||
super(newPassword);
|
||||
this.oldPassword = oldPassword;
|
||||
id = userID;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the ID of the {@link Contact} this event is related to
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public long getID() { return id; }
|
||||
|
||||
/**
|
||||
* @return the old password of the underlying user
|
||||
* @since Envoy Common v0.2-beta
|
||||
*/
|
||||
public String getOldPassword() { return oldPassword; }
|
||||
}
|
26
common/src/main/java/envoy/event/PasswordChangeResult.java
Normal file
26
common/src/main/java/envoy/event/PasswordChangeResult.java
Normal file
@ -0,0 +1,26 @@
|
||||
package envoy.event;
|
||||
|
||||
/**
|
||||
* This class acts as a notice to the user whether his
|
||||
* {@link envoy.event.PasswordChangeRequest} was successful.
|
||||
* <p>
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
* File: <strong>PasswordChangeResult.java</strong><br>
|
||||
* Created: <strong>01.08.2020</strong><br>
|
||||
*
|
||||
* @author Leon Hofmeister
|
||||
* @since Envoy Common v0.2-beta
|
||||
*/
|
||||
public class PasswordChangeResult extends Event<Boolean> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Creates an instance of {@code PasswordChangeResult}.
|
||||
*
|
||||
* @param value whether the preceding {@link envoy.event.PasswordChangeRequest}
|
||||
* was successful.
|
||||
* @since Envoy Common v0.2-beta
|
||||
*/
|
||||
public PasswordChangeResult(boolean value) { super(value); }
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package envoy.server.processors;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import envoy.event.PasswordChangeRequest;
|
||||
import envoy.event.PasswordChangeResult;
|
||||
import envoy.server.data.PersistenceManager;
|
||||
import envoy.server.net.ObjectWriteProxy;
|
||||
import envoy.server.util.PasswordUtil;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-server-standalone</strong><br>
|
||||
* File: <strong>PasswordChangeRequestProcessor.java</strong><br>
|
||||
* Created: <strong>31.07.2020</strong><br>
|
||||
*
|
||||
* @author Leon Hofmeister
|
||||
* @since Envoy Server v0.2-beta
|
||||
*/
|
||||
public class PasswordChangeRequestProcessor implements ObjectProcessor<PasswordChangeRequest> {
|
||||
|
||||
@Override
|
||||
public void process(PasswordChangeRequest event, long socketID, ObjectWriteProxy writeProxy) throws IOException {
|
||||
final var persistenceManager = PersistenceManager.getInstance();
|
||||
final var user = persistenceManager.getUserByID(event.getID());
|
||||
final var correctAuthentication = PasswordUtil.validate(event.getOldPassword(), user.getPasswordHash());
|
||||
if (correctAuthentication) user.setPasswordHash(PasswordUtil.hash(event.get()));
|
||||
writeProxy.write(socketID, new PasswordChangeResult(correctAuthentication));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user