Browse Source

Merge branch 'richardBranch'

remotes/origin/server
Richard Schmidt 11 months ago
parent
commit
7ca61a897a
  1. 51
      src/main/java/CreateUser.java
  2. 48
      src/main/java/LoginGUI.java
  3. 39
      src/main/java/SignUpGUI.java
  4. 27
      user.json

51
src/main/java/CreateUser.java

@ -10,6 +10,9 @@ import java.util.List;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID; import java.util.UUID;
class CreateUser { class CreateUser {
@ -17,15 +20,19 @@ class CreateUser {
private String userName; private String userName;
private String password; private String password;
private String birthday; private String birthday;
private String firstName;
private String surname;
private boolean stayLoggedIn; private boolean stayLoggedIn;
// Constructor // 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.id = id;
this.userName = name; this.userName = name;
this.password = password;
this.password = hashPassword(password);
this.birthday = birthday; this.birthday = birthday;
this.firstName = firstName;
this.surname = surname;
} }
// Getters and Setters // Getters and Setters
@ -61,6 +68,22 @@ class CreateUser {
this.birthday = birthday; this.birthday = birthday;
} }
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String surname() {
return firstName;
}
public void surname(String firstName) {
this.firstName = firstName;
}
public boolean isStayLoggedIn() { public boolean isStayLoggedIn() {
return stayLoggedIn; return stayLoggedIn;
} }
@ -70,7 +93,7 @@ class CreateUser {
} }
// Function to create user with validation // 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()) { if (userName == null || userName.isEmpty()) {
throw new IllegalArgumentException("Username cannot be empty"); throw new IllegalArgumentException("Username cannot be empty");
} }
@ -79,7 +102,25 @@ class CreateUser {
} }
if (password.length() < 6) { if (password.length() < 6) {
throw new IllegalArgumentException("Password must be at least 6 characters long"); 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 // Function to save to JSON file, replace with database call later
@ -135,7 +176,7 @@ class CreateUser {
try { try {
// Example usage // Example usage
UUID randomUUID = UUID.randomUUID(); 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 // Example of accessing properties
System.out.println("UserID: " + user.getId()); System.out.println("UserID: " + user.getId());

48
src/main/java/LoginGUI.java

@ -5,15 +5,19 @@ import java.util.List;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class LoginGUI extends JFrame implements ActionListener { public class LoginGUI extends JFrame implements ActionListener {
private JTextField usernameField; private JTextField usernameField;
private JPasswordField passwordField; private JPasswordField passwordField;
private JButton loginButton; private JButton loginButton;
private JCheckBox stayLoggedInCheckbox; private JCheckBox stayLoggedInCheckbox;
private JButton signUpButton;
public LoginGUI() { public LoginGUI() {
setTitle("Login"); setTitle("Login");
setSize(300, 180);
setSize(300, 220);
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null); setLayout(null);
@ -42,6 +46,11 @@ public class LoginGUI extends JFrame implements ActionListener {
loginButton.addActionListener(this); loginButton.addActionListener(this);
add(loginButton); add(loginButton);
signUpButton = new JButton("Sign Up");
signUpButton.setBounds(100, 140, 100, 25); // Adjusted position
signUpButton.addActionListener(this);
add(signUpButton);
getRootPane().setDefaultButton(loginButton); getRootPane().setDefaultButton(loginButton);
passwordField.addKeyListener(new EnterKeyListener()); passwordField.addKeyListener(new EnterKeyListener());
@ -51,6 +60,10 @@ public class LoginGUI extends JFrame implements ActionListener {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); boolean stayLoggedIn = stayLoggedInCheckbox.isSelected();
String username = usernameField.getText(); String username = usernameField.getText();
// Set stayLoggedIn to false if the checkbox is unchecked
if (!stayLoggedInCheckbox.isSelected()) {
stayLoggedIn = false;
}
updateStayLoggedIn(username, stayLoggedIn); updateStayLoggedIn(username, stayLoggedIn);
} }
}); });
@ -61,6 +74,10 @@ public class LoginGUI extends JFrame implements ActionListener {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) { if (e.getSource() == loginButton) {
login(); login();
}else if (e.getSource() == signUpButton) {
SignUpGUI signUpGUI = new SignUpGUI();
signUpGUI.setVisible(true);
} }
} }
@ -88,15 +105,38 @@ public class LoginGUI extends JFrame implements ActionListener {
private boolean authenticateUser(String username, String password) { private boolean authenticateUser(String username, String password) {
List<CreateUser> userList = CreateUser.readUserListFromJsonFile("user.json"); List<CreateUser> userList = CreateUser.readUserListFromJsonFile("user.json");
if (userList != null) { 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 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 { private class EnterKeyListener implements KeyListener {
@Override @Override

39
src/main/java/SignUpGUI.java

@ -5,12 +5,12 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
public class SignUpGUI extends JFrame implements ActionListener { public class SignUpGUI extends JFrame implements ActionListener {
private JTextField usernameField, passwordField, confirmPasswordField, birthdayField;
private JTextField usernameField, passwordField, confirmPasswordField, birthdayField, firstNameField, surnameField;
private JButton signUpButton; private JButton signUpButton;
public SignUpGUI() { public SignUpGUI() {
setTitle("Sign Up"); setTitle("Sign Up");
setSize(300, 250);
setSize(400, 300); // Adjusted size for accommodating more fields
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null); setLayout(null);
@ -19,7 +19,7 @@ public class SignUpGUI extends JFrame implements ActionListener {
add(usernameLabel); add(usernameLabel);
usernameField = new JTextField(); usernameField = new JTextField();
usernameField.setBounds(100, 20, 160, 25);
usernameField.setBounds(140, 20, 160, 25);
add(usernameField); add(usernameField);
JLabel passwordLabel = new JLabel("Password:"); JLabel passwordLabel = new JLabel("Password:");
@ -27,7 +27,7 @@ public class SignUpGUI extends JFrame implements ActionListener {
add(passwordLabel); add(passwordLabel);
passwordField = new JPasswordField(); passwordField = new JPasswordField();
passwordField.setBounds(100, 50, 160, 25);
passwordField.setBounds(140, 50, 160, 25);
add(passwordField); add(passwordField);
JLabel confirmPasswordLabel = new JLabel("Confirm Password:"); JLabel confirmPasswordLabel = new JLabel("Confirm Password:");
@ -35,22 +35,37 @@ public class SignUpGUI extends JFrame implements ActionListener {
add(confirmPasswordLabel); add(confirmPasswordLabel);
confirmPasswordField = new JPasswordField(); confirmPasswordField = new JPasswordField();
confirmPasswordField.setBounds(140, 80, 120, 25);
confirmPasswordField.setBounds(140, 80, 160, 25);
add(confirmPasswordField); 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:"); JLabel birthdayLabel = new JLabel("Birthday:");
birthdayLabel.setBounds(20, 110, 80, 25);
birthdayLabel.setBounds(20, 170, 80, 25);
add(birthdayLabel); add(birthdayLabel);
birthdayField = new JTextField(); birthdayField = new JTextField();
birthdayField.setBounds(100, 110, 160, 25);
birthdayField.setBounds(140, 170, 160, 25);
add(birthdayField); add(birthdayField);
signUpButton = new JButton("Sign Up"); signUpButton = new JButton("Sign Up");
signUpButton.setBounds(100, 150, 100, 25);
signUpButton.setBounds(140, 210, 100, 25); // Adjusted position
signUpButton.addActionListener(this); signUpButton.addActionListener(this);
add(signUpButton); add(signUpButton);
} }
@Override @Override
@ -60,6 +75,8 @@ public class SignUpGUI extends JFrame implements ActionListener {
String password = passwordField.getText(); String password = passwordField.getText();
String confirmPassword = confirmPasswordField.getText(); String confirmPassword = confirmPasswordField.getText();
String birthday = birthdayField.getText(); String birthday = birthdayField.getText();
String firstName = firstNameField.getText();
String surname = surnameField.getText();
if (!password.equals(confirmPassword)) { if (!password.equals(confirmPassword)) {
JOptionPane.showMessageDialog(this, "Passwords do not match!", "Sign Up Error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(this, "Passwords do not match!", "Sign Up Error", JOptionPane.ERROR_MESSAGE);
@ -72,7 +89,7 @@ public class SignUpGUI extends JFrame implements ActionListener {
} }
try { try {
UUID randomUUID = UUID.randomUUID(); 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"); user.saveToJsonFile("user.json");
JOptionPane.showMessageDialog(this, "User signed up successfully!"); JOptionPane.showMessageDialog(this, "User signed up successfully!");
dispose(); dispose();

27
user.json

@ -1,23 +1,20 @@
[ [
{ {
"id": "a2864d79-1079-4cbb-8d77-f5f84995580d",
"userName": "Another Test User",
"password": "TestPasswort123",
"birthday": "01.01.2000",
"id": "d7ae19fe-4684-4d69-a73d-4cca612962a3",
"userName": "Test",
"password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92",
"birthday": "",
"firstName": "",
"surname": "",
"stayLoggedIn": false "stayLoggedIn": false
}, },
{ {
"id": "3690702d-9c7e-48fb-8a01-ef89b3b76268",
"userName": "TestUser2",
"password": "123456",
"birthday": "01.01.2000",
"id": "2ec2c0c5-677c-4262-8958-fef98d11cc63",
"userName": "Test2",
"password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92",
"birthday": "",
"firstName": "",
"surname": "",
"stayLoggedIn": false "stayLoggedIn": false
},
{
"id": "685bc3a6-e706-4214-a5e1-8443d1a5258e",
"userName": "Test User",
"password": "Test",
"birthday": "01.01.2000",
"stayLoggedIn": true
} }
] ]
Loading…
Cancel
Save