From 6f888c140b8c506b9d53ac6b5013b2c659f8f94c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Tue, 21 May 2024 14:48:33 +0200 Subject: [PATCH] #8: Implement Login Logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dominik Säume --- src/main/java/de/hitec/nhplus/Main.java | 22 +++++-- .../hitec/nhplus/login/LoginController.java | 57 ++++++++++++++++++- .../de/hitec/nhplus/login/LoginView.fxml | 28 ++++++--- 3 files changed, 93 insertions(+), 14 deletions(-) diff --git a/src/main/java/de/hitec/nhplus/Main.java b/src/main/java/de/hitec/nhplus/Main.java index eb807a1..1ebffd0 100644 --- a/src/main/java/de/hitec/nhplus/Main.java +++ b/src/main/java/de/hitec/nhplus/Main.java @@ -1,6 +1,7 @@ package de.hitec.nhplus; import de.hitec.nhplus.datastorage.ConnectionBuilder; +import de.hitec.nhplus.login.LoginController; import javafx.application.Application; import javafx.application.Platform; import javafx.fxml.FXMLLoader; @@ -34,8 +35,8 @@ public class Main extends Application { @Override public void start(Stage primaryStage) { this.primaryStage = primaryStage; - //executePassword(); - executeMainApplication(); + executePassword(); + //executeMainApplication(); } private void executePassword() { @@ -44,10 +45,19 @@ public class Main extends Application { FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/login/LoginView.fxml")); BorderPane pane = loader.load(); Scene scene = new Scene(pane); - this.primaryStage.setTitle("NHPlus"); - this.primaryStage.setScene(scene); - this.primaryStage.setResizable(true); - this.primaryStage.show(); + Stage loginStage = new Stage(); + loginStage.setTitle("NHPlus"); + loginStage.setScene(scene); + loginStage.setResizable(false); + + LoginController controller = loader.getController(); + controller.initialize(loginStage); + + loginStage.showAndWait(); + + if(controller.loginSucessfull){ + executeMainApplication(); + } } catch (IOException exception) { exception.printStackTrace(); } diff --git a/src/main/java/de/hitec/nhplus/login/LoginController.java b/src/main/java/de/hitec/nhplus/login/LoginController.java index 1ab43b8..52d5b19 100644 --- a/src/main/java/de/hitec/nhplus/login/LoginController.java +++ b/src/main/java/de/hitec/nhplus/login/LoginController.java @@ -1,8 +1,63 @@ package de.hitec.nhplus.login; +import de.hitec.nhplus.datastorage.DaoFactory; +import de.hitec.nhplus.login.database.UserDao; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.control.PasswordField; +import javafx.scene.control.TextField; +import javafx.stage.Stage; + +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 void initialize() { + @FXML + public TextField textFieldUsername; + @FXML + public PasswordField passwordField; + private Stage stage; + public boolean loginSucessfull = false; + private int loginTries = 0; + + public void initialize(Stage stage) { + this.stage = stage; + } + + private void handleWrongPasswordOrUsername(){ + loginTries++; + if(loginTries == 3){ + stage.close(); + } + } + + @FXML + public void handleSubmit() { + 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; + } + loginSucessfull = true; + stage.close(); + } catch (SQLException | NoSuchAlgorithmException exception) { + exception.printStackTrace(); + } } } diff --git a/src/main/resources/de/hitec/nhplus/login/LoginView.fxml b/src/main/resources/de/hitec/nhplus/login/LoginView.fxml index 2a3212d..8cf444f 100644 --- a/src/main/resources/de/hitec/nhplus/login/LoginView.fxml +++ b/src/main/resources/de/hitec/nhplus/login/LoginView.fxml @@ -1,9 +1,11 @@ - - + + + + + - -
- - + +
-