#8: Implement Login Logic

Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
This commit is contained in:
Dominik Säume 2024-05-21 14:48:33 +02:00
parent 375fbe4826
commit b9eae8088f
Signed by: SZUT-Dominik
GPG key ID: 67D15BB250B41E7C
3 changed files with 93 additions and 14 deletions

View file

@ -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();
}

View file

@ -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();
}
}
}

View file

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