diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 5a84cf6..e3ce511 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; @@ -94,32 +91,24 @@ 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); - } - - // 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; + throw new IllegalArgumentException("Password must be at least 6 characters long"); } } diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 6731ae7..c1f61ec 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; @@ -75,17 +79,13 @@ 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(); + if (!stayLoggedInCheckbox.isSelected()) { + stayLoggedIn = false; } + updateStayLoggedIn(username, stayLoggedIn); }); } @@ -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/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 696786b..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; @@ -96,47 +100,60 @@ 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) { 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) { diff --git a/user.json b/user.json index d1ad3d8..cd6dd0d 100644 --- a/user.json +++ b/user.json @@ -1,47 +1,11 @@ [ { - "id": "d7ae19fe-4684-4d69-a73d-4cca612962a3", + "id": "c99b1061-13b7-4fa8-b1ac-814e07db3ef4", "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