#8: Implement Login Logic

This commit is contained in:
Dominik Säume 2024-05-21 14:48:33 +02:00
parent fb6fc923ac
commit 069465c4ca
Signed by: SZUT-Dominik
GPG key ID: 67D15BB250B41E7C
4 changed files with 136 additions and 16 deletions

View file

@ -1,6 +1,7 @@
package de.hitec.nhplus; package de.hitec.nhplus;
import de.hitec.nhplus.datastorage.ConnectionBuilder; import de.hitec.nhplus.datastorage.ConnectionBuilder;
import de.hitec.nhplus.login.LoginController;
import javafx.application.Application; import javafx.application.Application;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
@ -34,8 +35,7 @@ public class Main extends Application {
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
this.primaryStage = primaryStage; this.primaryStage = primaryStage;
//executePassword(); executePassword();
executeMainApplication();
} }
private void executePassword() { private void executePassword() {
@ -44,10 +44,19 @@ public class Main extends Application {
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/login/LoginView.fxml")); FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/login/LoginView.fxml"));
BorderPane pane = loader.load(); BorderPane pane = loader.load();
Scene scene = new Scene(pane); Scene scene = new Scene(pane);
this.primaryStage.setTitle("NHPlus"); Stage loginStage = new Stage();
this.primaryStage.setScene(scene); loginStage.setTitle("NHPlus");
this.primaryStage.setResizable(true); loginStage.setScene(scene);
this.primaryStage.show(); loginStage.setResizable(false);
LoginController controller = loader.getController();
controller.initialize(loginStage);
loginStage.showAndWait();
if(controller.user == null){
executeMainApplication();
}
} catch (IOException exception) { } catch (IOException exception) {
exception.printStackTrace(); exception.printStackTrace();
} }

View file

@ -1,8 +1,93 @@
package de.hitec.nhplus.login; package de.hitec.nhplus.login;
import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.login.database.UserDao;
import javafx.animation.PauseTransition;
import javafx.animation.TranslateTransition;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.Arrays;
public class LoginController { public class LoginController {
public void initialize() { public User user;
@FXML
public TextField textFieldUsername;
@FXML
public PasswordField passwordField;
@FXML
public Button buttonSubmit;
private Stage stage;
private int loginTries = 0;
public void initialize(Stage stage) {
this.stage = stage;
}
private void handleWrongPasswordOrUsername() {
loginTries++;
// Shake
TranslateTransition ttUsername = new TranslateTransition(Duration.millis(50), textFieldUsername);
ttUsername.setByX(10);
ttUsername.setAutoReverse(true);
ttUsername.setCycleCount(6);
TranslateTransition ttPassword = new TranslateTransition(Duration.millis(50), passwordField);
ttPassword.setByX(10);
ttPassword.setAutoReverse(true);
ttPassword.setCycleCount(6);
ttUsername.play();
ttPassword.play();
// Timout
PauseTransition pause = new PauseTransition(Duration.seconds(3));
pause.setOnFinished(event -> {
if (loginTries == 3) {
stage.close();
}
buttonSubmit.setDisable(false);
});
pause.play();
}
@FXML
public void handleSubmit() {
buttonSubmit.setDisable(true);
UserDao dao = DaoFactory.getInstance().createUserDAO();
try {
int id = dao.readUserId(textFieldUsername.getText());
if (id == 0) {
handleWrongPasswordOrUsername();
return;
}
byte[] salt = dao.readPasswordSalt(id);
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(salt);
byte[] hash = md.digest(passwordField.getText().getBytes(StandardCharsets.UTF_8));
byte[] requiredHash = dao.readPasswordHash(id);
if (!Arrays.equals(hash, requiredHash)) {
handleWrongPasswordOrUsername();
return;
}
user = dao.read(id);
stage.close();
} catch (SQLException | NoSuchAlgorithmException exception) {
exception.printStackTrace();
}
} }
} }

View file

@ -43,7 +43,12 @@ public class UserDao implements Dao<User> {
@Override @Override
public User read(int id) throws SQLException { public User read(int id) throws SQLException {
final String SQL = """ final String SQL = """
SELECT user.username, user.passwordSalt, user.passwordHash, user__permissions.permissions, user__nurse.nurseId SELECT
user.username,
user.passwordSalt,
user.passwordHash,
user__permissions.permissions,
user__nurse.nurseId
FROM user FROM user
LEFT JOIN user__permissions ON user.id = user__permissions.userId LEFT JOIN user__permissions ON user.id = user__permissions.userId
LEFT JOIN user__nurse ON user.id = user__nurse.userId LEFT JOIN user__nurse ON user.id = user__nurse.userId
@ -174,7 +179,13 @@ public class UserDao implements Dao<User> {
@Override @Override
public List<User> readAll() throws SQLException { public List<User> readAll() throws SQLException {
final String SQL = """ final String SQL = """
SELECT user.id, user.username, user.passwordSalt, user.passwordHash, user__permissions.permissions, user__nurse.nurseId SELECT
user.id,
user.username,
user.passwordSalt,
user.passwordHash,
user__permissions.permissions,
user__nurse.nurseId
FROM user FROM user
LEFT JOIN user__permissions ON user.id = user__permissions.userId LEFT JOIN user__permissions ON user.id = user__permissions.userId
LEFT JOIN user__nurse ON user.id = user__nurse.userId LEFT JOIN user__nurse ON user.id = user__nurse.userId

View file

@ -1,9 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?> <?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.*?> <?import javafx.scene.layout.*?>
<BorderPane <BorderPane
xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns="http://javafx.com/javafx/17.0.2-ea"
xmlns:fx="http://javafx.com/fxml/1" xmlns:fx="http://javafx.com/fxml/1"
@ -22,11 +24,24 @@
<BorderPane.margin> <BorderPane.margin>
<Insets bottom="8.0" top="8.0"/> <Insets bottom="8.0" top="8.0"/>
</BorderPane.margin> </BorderPane.margin>
<TextField promptText="nutzername" VBox.vgrow="ALWAYS"/> <TextField
<PasswordField promptText="password" VBox.vgrow="ALWAYS"/> fx:id="textFieldUsername"
promptText="nutzername"
VBox.vgrow="ALWAYS"
/>
<PasswordField
fx:id="passwordField"
promptText="password"
VBox.vgrow="ALWAYS"
/>
</VBox> </VBox>
</center> </center>
<bottom> <bottom>
<Button text="Bestätigen" BorderPane.alignment="CENTER" /> <Button
fx:id="buttonSubmit"
text="Bestätigen"
BorderPane.alignment="CENTER"
onAction="#handleSubmit"
/>
</bottom> </bottom>
</BorderPane> </BorderPane>