From 757f615d207cc0be88a95abaf5841caf3c1d104a Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 10:54:40 +0100 Subject: [PATCH 1/5] Added an if statement in the action listener for the checkbox in LoginGUI so the stayLoggedIn variable gets also changed when the checkbox is unticked --- src/main/java/LoginGUI.java | 4 ++++ user.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 675ffa5..6672019 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -51,6 +51,10 @@ public class LoginGUI extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); String username = usernameField.getText(); + // Set stayLoggedIn to false if the checkbox is unchecked + if (!stayLoggedInCheckbox.isSelected()) { + stayLoggedIn = false; + } updateStayLoggedIn(username, stayLoggedIn); } }); diff --git a/user.json b/user.json index 5437c96..2dcd5ea 100644 --- a/user.json +++ b/user.json @@ -18,6 +18,6 @@ "userName": "Test User", "password": "Test", "birthday": "01.01.2000", - "stayLoggedIn": true + "stayLoggedIn": false } ] \ No newline at end of file From 1437f9f5ce043ad7e5ecc6da399bcf1710e46ac2 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 11:46:04 +0100 Subject: [PATCH 2/5] Added a signup button --- src/main/java/LoginGUI.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 6672019..08b26b5 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -10,10 +10,11 @@ public class LoginGUI extends JFrame implements ActionListener { private JPasswordField passwordField; private JButton loginButton; private JCheckBox stayLoggedInCheckbox; + private JButton signUpButton; public LoginGUI() { setTitle("Login"); - setSize(300, 180); + setSize(300, 220); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); @@ -42,6 +43,11 @@ public class LoginGUI extends JFrame implements ActionListener { loginButton.addActionListener(this); add(loginButton); + signUpButton = new JButton("Sign Up"); + signUpButton.setBounds(100, 140, 100, 25); // Adjusted position + signUpButton.addActionListener(this); + add(signUpButton); + getRootPane().setDefaultButton(loginButton); passwordField.addKeyListener(new EnterKeyListener()); From 79c72ab0fe1726df4b4f4d9d82f0e2466e5a56aa Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 11:49:14 +0100 Subject: [PATCH 3/5] Added functionality to the signup button --- src/main/java/LoginGUI.java | 4 ++++ user.json | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 08b26b5..f8dad01 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -71,6 +71,10 @@ public class LoginGUI extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { login(); + }else if (e.getSource() == signUpButton) { + SignUpGUI signUpGUI = new SignUpGUI(); + signUpGUI.setVisible(true); + } } diff --git a/user.json b/user.json index 2dcd5ea..2bef498 100644 --- a/user.json +++ b/user.json @@ -19,5 +19,12 @@ "password": "Test", "birthday": "01.01.2000", "stayLoggedIn": false + }, + { + "id": "57b7fb2e-50c1-4027-8871-58cbfc8405c8", + "userName": "New", + "password": "123456", + "birthday": "", + "stayLoggedIn": false } ] \ No newline at end of file From 522f04d359cf53dbe38836e738f64358cb3045cc Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 13:53:40 +0100 Subject: [PATCH 4/5] Added a firstname and surname field to SignUpGUI --- src/main/java/CreateUser.java | 12 ++++++++++++ src/main/java/SignUpGUI.java | 35 +++++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index c799654..59c55b1 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -17,6 +17,8 @@ class CreateUser { private String userName; private String password; private String birthday; + private String firstName; + private String surName; private boolean stayLoggedIn; @@ -26,6 +28,8 @@ class CreateUser { this.userName = name; this.password = password; this.birthday = birthday; + this.firstName = firstName; + this.surName = surName; } // Getters and Setters @@ -61,6 +65,14 @@ class CreateUser { this.birthday = birthday; } + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + public boolean isStayLoggedIn() { return stayLoggedIn; } diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index b19cf8e..0266fe4 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -5,12 +5,12 @@ import java.util.List; import java.util.UUID; public class SignUpGUI extends JFrame implements ActionListener { - private JTextField usernameField, passwordField, confirmPasswordField, birthdayField; + private JTextField usernameField, passwordField, confirmPasswordField, birthdayField, firstNameField, surnameField; private JButton signUpButton; - + public SignUpGUI() { setTitle("Sign Up"); - setSize(300, 250); + setSize(400, 300); // Adjusted size for accommodating more fields setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); @@ -19,7 +19,7 @@ public class SignUpGUI extends JFrame implements ActionListener { add(usernameLabel); usernameField = new JTextField(); - usernameField.setBounds(100, 20, 160, 25); + usernameField.setBounds(140, 20, 160, 25); add(usernameField); JLabel passwordLabel = new JLabel("Password:"); @@ -27,7 +27,7 @@ public class SignUpGUI extends JFrame implements ActionListener { add(passwordLabel); passwordField = new JPasswordField(); - passwordField.setBounds(100, 50, 160, 25); + passwordField.setBounds(140, 50, 160, 25); add(passwordField); JLabel confirmPasswordLabel = new JLabel("Confirm Password:"); @@ -35,22 +35,37 @@ public class SignUpGUI extends JFrame implements ActionListener { add(confirmPasswordLabel); confirmPasswordField = new JPasswordField(); - confirmPasswordField.setBounds(140, 80, 120, 25); + confirmPasswordField.setBounds(140, 80, 160, 25); add(confirmPasswordField); + JLabel firstNameLabel = new JLabel("First Name:"); // New field for first name + firstNameLabel.setBounds(20, 110, 80, 25); + add(firstNameLabel); + + firstNameField = new JTextField(); + firstNameField.setBounds(140, 110, 160, 25); + add(firstNameField); + + JLabel surnameLabel = new JLabel("Surname:"); // New field for surname + surnameLabel.setBounds(20, 140, 80, 25); + add(surnameLabel); + + surnameField = new JTextField(); + surnameField.setBounds(140, 140, 160, 25); + add(surnameField); + JLabel birthdayLabel = new JLabel("Birthday:"); - birthdayLabel.setBounds(20, 110, 80, 25); + birthdayLabel.setBounds(20, 170, 80, 25); add(birthdayLabel); birthdayField = new JTextField(); - birthdayField.setBounds(100, 110, 160, 25); + birthdayField.setBounds(140, 170, 160, 25); add(birthdayField); signUpButton = new JButton("Sign Up"); - signUpButton.setBounds(100, 150, 100, 25); + signUpButton.setBounds(140, 210, 100, 25); // Adjusted position signUpButton.addActionListener(this); add(signUpButton); - } @Override From 85707e32c590567b0afe53e2d4cf9114f96a7146 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 15:40:19 +0100 Subject: [PATCH 5/5] Added hashing algorithm to the user signup and login --- src/main/java/CreateUser.java | 43 +++++++++++++++++++++++++++++------ src/main/java/LoginGUI.java | 32 +++++++++++++++++++++++--- src/main/java/SignUpGUI.java | 4 +++- user.json | 32 +++++++++----------------- 4 files changed, 79 insertions(+), 32 deletions(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 59c55b1..c078749 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -10,6 +10,9 @@ import java.util.List; import java.io.FileWriter; import java.io.IOException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + import java.util.UUID; class CreateUser { @@ -18,18 +21,18 @@ class CreateUser { private String password; private String birthday; private String firstName; - private String surName; + private String surname; private boolean stayLoggedIn; // Constructor - public CreateUser(String id, String name, String password, String birthday) { + public CreateUser(String id, String name, String password, String birthday, String firstName, String surname) { this.id = id; this.userName = name; - this.password = password; + this.password = hashPassword(password); this.birthday = birthday; this.firstName = firstName; - this.surName = surName; + this.surname = surname; } // Getters and Setters @@ -72,6 +75,14 @@ class CreateUser { public void setFirstName(String firstName) { this.firstName = firstName; } + + public String surname() { + return firstName; + } + + public void surname(String firstName) { + this.firstName = firstName; + } public boolean isStayLoggedIn() { return stayLoggedIn; @@ -82,7 +93,7 @@ class CreateUser { } // Function to create user with validation - public static CreateUser createUser(String id, String userName, String password, String birthday) { + public static CreateUser createUser(String id, String userName, String password, String birthday, String firstName, String surname) { if (userName == null || userName.isEmpty()) { throw new IllegalArgumentException("Username cannot be empty"); } @@ -91,7 +102,25 @@ class CreateUser { } if (password.length() < 6) { throw new IllegalArgumentException("Password must be at least 6 characters long"); - } return new CreateUser(id, userName, password, birthday); + } return new CreateUser(id, userName, password, birthday, firstName, surname); + } + + // Function to hash the password using SHA-256 algorithm + private String hashPassword(String password) { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte[] hash = digest.digest(password.getBytes()); + StringBuilder hexString = new StringBuilder(); + for (byte b : hash) { + String hex = Integer.toHexString(0xff & b); + if (hex.length() == 1) hexString.append('0'); + hexString.append(hex); + } + return hexString.toString(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + return null; + } } // Function to save to JSON file, replace with database call later @@ -147,7 +176,7 @@ class CreateUser { try { // Example usage UUID randomUUID = UUID.randomUUID(); - CreateUser user = createUser(randomUUID.toString(), "Test User", "TestPasswort123", "01.01.2000"); + CreateUser user = createUser(randomUUID.toString(), "Hash Test", "123456", "01.01.2000", "Hans", "Wurst"); // Example of accessing properties System.out.println("UserID: " + user.getId()); diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index f8dad01..6a442b7 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -5,6 +5,9 @@ import java.util.List; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; private JPasswordField passwordField; @@ -102,15 +105,38 @@ public class LoginGUI extends JFrame implements ActionListener { private boolean authenticateUser(String username, String password) { List userList = CreateUser.readUserListFromJsonFile("user.json"); if (userList != null) { - for (CreateUser user : userList) { - if (user.getUserName().equals(username) && user.getPassword().equals(password)) { - return true; //Success + for (CreateUser user : userList) { + if (user.getUserName().equals(username)) { + // Hash the user input password + String hashedPassword = hashPassword(password); + // Compare the hashed passwords + if (user.getPassword().equals(hashedPassword)) { + return true; // Success } } } + } return false; // Fail } + private String hashPassword(String password) { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte[] hash = digest.digest(password.getBytes()); + StringBuilder hexString = new StringBuilder(); + for (byte b : hash) { + String hex = Integer.toHexString(0xff & b); + if (hex.length() == 1) { + hexString.append('0'); + } + hexString.append(hex); + } + return hexString.toString(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + return null; + } + } private class EnterKeyListener implements KeyListener { @Override diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index 0266fe4..2f31890 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -75,6 +75,8 @@ public class SignUpGUI extends JFrame implements ActionListener { String password = passwordField.getText(); String confirmPassword = confirmPasswordField.getText(); String birthday = birthdayField.getText(); + String firstName = firstNameField.getText(); + String surname = surnameField.getText(); if (!password.equals(confirmPassword)) { JOptionPane.showMessageDialog(this, "Passwords do not match!", "Sign Up Error", JOptionPane.ERROR_MESSAGE); @@ -87,7 +89,7 @@ public class SignUpGUI extends JFrame implements ActionListener { } try { UUID randomUUID = UUID.randomUUID(); - CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday); + CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday, firstName, surname); user.saveToJsonFile("user.json"); JOptionPane.showMessageDialog(this, "User signed up successfully!"); dispose(); diff --git a/user.json b/user.json index 2bef498..29df4e8 100644 --- a/user.json +++ b/user.json @@ -1,30 +1,20 @@ [ { - "id": "a2864d79-1079-4cbb-8d77-f5f84995580d", - "userName": "Another Test User", - "password": "TestPasswort123", - "birthday": "01.01.2000", - "stayLoggedIn": false - }, - { - "id": "3690702d-9c7e-48fb-8a01-ef89b3b76268", - "userName": "TestUser2", - "password": "123456", - "birthday": "01.01.2000", - "stayLoggedIn": false - }, - { - "id": "685bc3a6-e706-4214-a5e1-8443d1a5258e", - "userName": "Test User", - "password": "Test", - "birthday": "01.01.2000", + "id": "d7ae19fe-4684-4d69-a73d-4cca612962a3", + "userName": "Test", + "password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92", + "birthday": "", + "firstName": "", + "surname": "", "stayLoggedIn": false }, { - "id": "57b7fb2e-50c1-4027-8871-58cbfc8405c8", - "userName": "New", - "password": "123456", + "id": "2ec2c0c5-677c-4262-8958-fef98d11cc63", + "userName": "Test2", + "password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92", "birthday": "", + "firstName": "", + "surname": "", "stayLoggedIn": false } ] \ No newline at end of file