From e9a81229533d92515b19a303a87df83e273ce55c Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 20:54:38 +0100 Subject: [PATCH 1/6] refactoring: createUser method in CreateUser --- src/main/java/CreateUser.java | 20 ++++++++++++---- user.json | 45 ----------------------------------- 2 files changed, 15 insertions(+), 50 deletions(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 5a84cf6..31b1749 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -94,15 +94,25 @@ class CreateUser { // Function to create user with validation 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"); + validateUserName(userName); + validatePassword(password); + + return new CreateUser(id, userName, password, birthday, firstName, surname); + } + + private static void validateUserName(String userName) { + if (userName == null || userName.isEmpty()) { + throw new IllegalArgumentException("Username cannot be empty"); } + } + + private static void validatePassword(String password) { if (password == null || password.isEmpty()) { - throw new IllegalArgumentException("Password cannot be empty"); + throw new IllegalArgumentException("Password cannot be empty"); } if (password.length() < 6) { - throw new IllegalArgumentException("Password must be at least 6 characters long"); - } return new CreateUser(id, userName, password, birthday, firstName, surname); + throw new IllegalArgumentException("Password must be at least 6 characters long"); + } } // Function to hash the password using SHA-256 algorithm diff --git a/user.json b/user.json index d1ad3d8..32960f8 100644 --- a/user.json +++ b/user.json @@ -1,47 +1,2 @@ [ - { - "id": "d7ae19fe-4684-4d69-a73d-4cca612962a3", - "userName": "Test", - "password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92", - "birthday": "", - "firstName": "", - "surname": "", - "stayLoggedIn": false - }, - { - "id": "2ec2c0c5-677c-4262-8958-fef98d11cc63", - "userName": "Test2", - "password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92", - "birthday": "", - "firstName": "", - "surname": "", - "stayLoggedIn": false - }, - { - "id": "ccfcc294-48ad-49db-996f-d7c99a93bac9", - "userName": "testuser", - "password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", - "birthday": "2000-01-01", - "firstName": "John", - "surname": "Doe", - "stayLoggedIn": false - }, - { - "id": "27ffc70f-dd76-4765-81e3-76fde9b618e5", - "userName": "testUser", - "password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", - "birthday": "1990-01-01", - "firstName": "John", - "surname": "Doe", - "stayLoggedIn": false - }, - { - "id": "731fda82-6b51-4f3f-ae97-c9b01c9b4a1a", - "userName": "existinguser", - "password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", - "birthday": "1990-01-01", - "firstName": "John", - "surname": "Doe", - "stayLoggedIn": false - } ] \ No newline at end of file From f24fb69051d6a739b95aa645356b552b4ed6322e Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 21:47:27 +0100 Subject: [PATCH 2/6] refactoring: stayLoggedInCheckbox listener in LoginGUI --- src/main/java/LoginGUI.java | 17 +++++++---------- user.json | 3 +-- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 6731ae7..a05d9ca 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -75,17 +75,14 @@ public class LoginGUI extends JFrame implements ActionListener { passwordField.addKeyListener(new EnterKeyListener()); - stayLoggedInCheckbox.addActionListener(new ActionListener() { - @Override - 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); + stayLoggedInCheckbox.addActionListener(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 32960f8..0637a08 100644 --- a/user.json +++ b/user.json @@ -1,2 +1 @@ -[ -] \ No newline at end of file +[] \ No newline at end of file From 60a1fe794c249d8df596df1e5bfc179e7e105bd9 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 21:55:08 +0100 Subject: [PATCH 3/6] refactoring: actionPerformed in SignUpGUI --- src/main/java/LoginGUI.java | 7 +++- src/main/java/SignUpGUI.java | 74 +++++++++++++++++++++++------------- user.json | 21 +++++++++- 3 files changed, 72 insertions(+), 30 deletions(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index a05d9ca..1c4ef8f 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -9,7 +9,11 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class LoginGUI extends JFrame implements ActionListener { - private JTextField usernameField; + /** + * + */ + private static final long serialVersionUID = 1L; + private JTextField usernameField; private JPasswordField passwordField; private JButton loginButton; private JCheckBox stayLoggedInCheckbox; @@ -78,7 +82,6 @@ public class LoginGUI extends JFrame implements ActionListener { stayLoggedInCheckbox.addActionListener(e -> { boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); String username = usernameField.getText(); - // Set stayLoggedIn to false if the checkbox is unchecked if (!stayLoggedInCheckbox.isSelected()) { stayLoggedIn = false; } diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index 696786b..70aa72f 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -96,35 +96,55 @@ public class SignUpGUI extends JFrame implements ActionListener { @Override public void actionPerformed(ActionEvent e) { - if (e.getSource() == signUpButton) { - String username = usernameField.getText(); - 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); - return; - } - - if (!isUsernameAvailable("user.json", username)) { - JOptionPane.showMessageDialog(this, "Username already exists!", "Sign Up Error", JOptionPane.ERROR_MESSAGE); - return; - } - - try { - UUID randomUUID = UUID.randomUUID(); - CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday, firstName, surname); - user.saveToJsonFile("user.json"); - JOptionPane.showMessageDialog(this, "User signed up successfully!"); - dispose(); - } catch (IllegalArgumentException ex) { - JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Sign Up Error", JOptionPane.ERROR_MESSAGE); - } + if (e.getSource() == signUpButton) { + signUpUser(); + } + } + + private void signUpUser() { + String username = usernameField.getText(); + String password = passwordField.getText(); + String confirmPassword = confirmPasswordField.getText(); + String birthday = birthdayField.getText(); + String firstName = firstNameField.getText(); + String surname = surnameField.getText(); + + if (!validatePasswordConfirmation(password, confirmPassword)) { + showErrorMessage("Passwords do not match!"); + return; + } + + if (!isUsernameAvailable("user.json", username)) { + showErrorMessage("Username already exists!"); + return; + } + + try { + createUserAndSave(username, password, birthday, firstName, surname); + showSuccessMessage("User signed up successfully!"); + dispose(); + } catch (IllegalArgumentException ex) { + showErrorMessage("Error: " + ex.getMessage()); } } + + private boolean validatePasswordConfirmation(String password, String confirmPassword) { + return password.equals(confirmPassword); + } + + private void showErrorMessage(String message) { + JOptionPane.showMessageDialog(this, message, "Sign Up Error", JOptionPane.ERROR_MESSAGE); + } + + private void showSuccessMessage(String message) { + JOptionPane.showMessageDialog(this, message); + } + + private void createUserAndSave(String username, String password, String birthday, String firstName, String surname) { + UUID randomUUID = UUID.randomUUID(); + CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday, firstName, surname); + user.saveToJsonFile("user.json"); + } // Function to check if the input username doesn't already exist in the JSON file private boolean isUsernameAvailable(String filename, String username) { diff --git a/user.json b/user.json index 0637a08..cfc325c 100644 --- a/user.json +++ b/user.json @@ -1 +1,20 @@ -[] \ No newline at end of file +[ + { + "id": "e365e96f-6c6c-48e5-96e0-7b5e058e7198", + "userName": "existinguser", + "password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", + "birthday": "1990-01-01", + "firstName": "John", + "surname": "Doe", + "stayLoggedIn": false + }, + { + "id": "61ef4208-9077-42c4-983f-f1e234f7e5f4", + "userName": "testUser", + "password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", + "birthday": "1990-01-01", + "firstName": "John", + "surname": "Doe", + "stayLoggedIn": false + } +] \ No newline at end of file From 4391c0ee342fce1e6c1620dee128d9c5afa98a93 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 21:57:16 +0100 Subject: [PATCH 4/6] refactoring: isUsernameAvailable in SignUpGUI --- src/main/java/SignUpGUI.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index 70aa72f..faa360d 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -149,14 +149,7 @@ public class SignUpGUI extends JFrame implements ActionListener { // Function to check if the input username doesn't already exist in the JSON file private boolean isUsernameAvailable(String filename, String username) { List userList = CreateUser.readUserListFromJsonFile(filename); - if (userList != null) { - for (CreateUser user : userList) { - if (user.getUserName().equals(username)) { - return false; - } - } - } - return true; + return userList == null || userList.stream().noneMatch(user -> user.getUserName().equals(username)); } public static void main(String[] args) { From 85d6d2a0d3da52a971b26ece16375348a923ed7a Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 22:07:36 +0100 Subject: [PATCH 5/6] refactoring: moved the password hashing into own class PasswordHasher.java --- src/main/java/CreateUser.java | 23 +---------------------- src/main/java/PasswordHasher.java | 21 +++++++++++++++++++++ src/main/java/SignUpGUI.java | 4 ++++ user.json | 21 +-------------------- 4 files changed, 27 insertions(+), 42 deletions(-) create mode 100644 src/main/java/PasswordHasher.java diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 31b1749..01085c9 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -10,9 +10,6 @@ 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 { @@ -29,7 +26,7 @@ class CreateUser { public CreateUser(String id, String name, String password, String birthday, String firstName, String surname) { this.id = id; this.userName = name; - this.password = hashPassword(password); + this.password = PasswordHasher.hashPassword(password);; this.birthday = birthday; this.firstName = firstName; this.surname = surname; @@ -115,24 +112,6 @@ class CreateUser { } } - // 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 public void saveToJsonFile(String filename) { List userList = readUserListFromJsonFile(filename); diff --git a/src/main/java/PasswordHasher.java b/src/main/java/PasswordHasher.java new file mode 100644 index 0000000..14a1137 --- /dev/null +++ b/src/main/java/PasswordHasher.java @@ -0,0 +1,21 @@ +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class PasswordHasher { + public static 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; + } + } +} \ No newline at end of file diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index faa360d..b9f7b1c 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -5,6 +5,10 @@ import java.util.List; import java.util.UUID; public class SignUpGUI extends JFrame implements ActionListener { + /** + * + */ + private static final long serialVersionUID = 1L; private JTextField usernameField, passwordField, confirmPasswordField, birthdayField, firstNameField, surnameField; private JButton signUpButton; diff --git a/user.json b/user.json index cfc325c..0637a08 100644 --- a/user.json +++ b/user.json @@ -1,20 +1 @@ -[ - { - "id": "e365e96f-6c6c-48e5-96e0-7b5e058e7198", - "userName": "existinguser", - "password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", - "birthday": "1990-01-01", - "firstName": "John", - "surname": "Doe", - "stayLoggedIn": false - }, - { - "id": "61ef4208-9077-42c4-983f-f1e234f7e5f4", - "userName": "testUser", - "password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", - "birthday": "1990-01-01", - "firstName": "John", - "surname": "Doe", - "stayLoggedIn": false - } -] \ No newline at end of file +[] \ No newline at end of file From 5e0110a8f12727c74ede5b6861764e7727d45ba5 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 22:10:59 +0100 Subject: [PATCH 6/6] refactoring: removed the hashing function in LoginGUI and replaced it with the PasswordHasher class --- src/main/java/CreateUser.java | 2 +- src/main/java/LoginGUI.java | 21 +-------------------- user.json | 12 +++++++++++- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 01085c9..e3ce511 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -26,7 +26,7 @@ class CreateUser { public CreateUser(String id, String name, String password, String birthday, String firstName, String surname) { this.id = id; this.userName = name; - this.password = PasswordHasher.hashPassword(password);; + this.password = PasswordHasher.hashPassword(password); this.birthday = birthday; this.firstName = firstName; this.surname = surname; diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 1c4ef8f..c1f61ec 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -123,7 +123,7 @@ public class LoginGUI extends JFrame implements ActionListener { if (userList != null) { for (CreateUser user : userList) { if (user.getUserName().equals(username)) { - String hashedPassword = hashPassword(password); + String hashedPassword = PasswordHasher.hashPassword(password); if (user.getPassword().equals(hashedPassword)) { return true; } @@ -133,25 +133,6 @@ public class LoginGUI extends JFrame implements ActionListener { return false; } - 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 public void keyTyped(KeyEvent e) {} diff --git a/user.json b/user.json index 0637a08..cd6dd0d 100644 --- a/user.json +++ b/user.json @@ -1 +1,11 @@ -[] \ No newline at end of file +[ + { + "id": "c99b1061-13b7-4fa8-b1ac-814e07db3ef4", + "userName": "Test", + "password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92", + "birthday": "", + "firstName": "", + "surname": "", + "stayLoggedIn": false + } +] \ No newline at end of file