diff --git a/src/main/java/de/hitec/nhplus/Main.java b/src/main/java/de/hitec/nhplus/Main.java index eb807a1..52813b3 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,7 @@ public class Main extends Application { @Override public void start(Stage primaryStage) { this.primaryStage = primaryStage; - //executePassword(); - executeMainApplication(); + 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")); 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.user == null){ + 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..dca1cd7 100644 --- a/src/main/java/de/hitec/nhplus/login/LoginController.java +++ b/src/main/java/de/hitec/nhplus/login/LoginController.java @@ -1,8 +1,93 @@ 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 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(); + } } } diff --git a/src/main/java/de/hitec/nhplus/login/database/UserDao.java b/src/main/java/de/hitec/nhplus/login/database/UserDao.java index 6ea36ce..8807ca1 100644 --- a/src/main/java/de/hitec/nhplus/login/database/UserDao.java +++ b/src/main/java/de/hitec/nhplus/login/database/UserDao.java @@ -43,7 +43,12 @@ public class UserDao implements Dao { @Override public User read(int id) throws SQLException { 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 LEFT JOIN user__permissions ON user.id = user__permissions.userId LEFT JOIN user__nurse ON user.id = user__nurse.userId @@ -174,7 +179,13 @@ public class UserDao implements Dao { @Override public List readAll() throws SQLException { 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 LEFT JOIN user__permissions ON user.id = user__permissions.userId LEFT JOIN user__nurse ON user.id = user__nurse.userId diff --git a/src/main/resources/de/hitec/nhplus/login/LoginView.fxml b/src/main/resources/de/hitec/nhplus/login/LoginView.fxml index 2a3212d..234ff7b 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 @@ - - + + + + + - -
- - + +
-