Refactor Main class and implement UserModel registration and authentication methods; add Database and Model classes for ORM functionality

This commit is contained in:
Dominik Krenn 2025-09-12 14:08:39 +02:00
parent dc1b85a774
commit fc1aca80d2
5 changed files with 35 additions and 31 deletions

View File

@ -1,10 +1,8 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import src.models.UserModel;
import src.models.squirrel.Database;
import src.models.squirrel.ModelManager;
import src.squirrel.Database;
import src.squirrel.ModelManager;
public class Main {
public static void main(String[] args) throws SQLException {
@ -13,24 +11,8 @@ public class Main {
Database.getConnection();
UserModel userModel = ModelManager.get(UserModel.class);
List<UserModel> users = userModel.where(java.util.Collections.emptyMap());
for (UserModel user : users) {
user.set("name", user.get("name") + " Updated");
user.save();
System.out.println(user);
}
Connection conn = Database.getConnection();
// Example: Run a simple SQL query
try (var stmt = conn.createStatement();
var rs = stmt.executeQuery("SELECT COUNT(*) AS user_count FROM users")) {
if (rs.next()) {
int userCount = rs.getInt("user_count");
System.out.println("Total users: " + userCount);
}
} catch (SQLException e) {
e.printStackTrace();
}
UserModel user = userModel.register("Jack", "jack@example.com", "1234");
System.out.println("Registered user: " + user);
Database.close();
}

View File

@ -3,8 +3,8 @@ package src.models;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import src.models.squirrel.Model;
import src.models.squirrel.ModelManager;
import src.squirrel.Model;
import src.squirrel.ModelManager;
import java.security.SecureRandom;
import java.util.Base64;
@ -25,7 +25,7 @@ public class UserModel extends Model {
private static final int ITERATIONS = 65536;
private static final int KEY_LENGTH = 256;
public static UserModel register(String name, String email, String password) {
public UserModel register(String name, String email, String password) {
if (password == null || password.trim().isEmpty()) {
throw new IllegalArgumentException("Password cannot be empty");
}
@ -46,7 +46,29 @@ public class UserModel extends Model {
return user;
}
public static String hashPassword(String password, byte[] salt) {
public UserModel authenticate(String email, String password) {
UserModel userModel = ModelManager.get(UserModel.class);
List<UserModel> users = userModel.where(Map.of("email", email));
if (users == null || users.isEmpty()) {
return null;
}
UserModel user = users.get(0);
String storedHash = (String) user.get("password_hash");
if (storedHash == null) {
return null;
}
byte[] salt = Base64.getDecoder().decode(storedHash);
if (!validatePassword(password, storedHash, salt)) {
return null;
}
return user;
}
public String hashPassword(String password, byte[] salt) {
try {
PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, ITERATIONS, KEY_LENGTH);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
@ -57,14 +79,14 @@ public class UserModel extends Model {
}
}
public static byte[] getSalt() {
public byte[] getSalt() {
SecureRandom sr = new SecureRandom();
byte[] salt = new byte[16];
sr.nextBytes(salt);
return salt;
}
public static boolean validatePassword(String password, String storedHash, byte[] salt) {
public boolean validatePassword(String password, String storedHash, byte[] salt) {
try {
String newHash = hashPassword(password, salt);
return Arrays.equals(Base64.getDecoder().decode(storedHash), Base64.getDecoder().decode(newHash));

View File

@ -1,4 +1,4 @@
package src.models.squirrel;
package src.squirrel;
import java.sql.Connection;
import java.sql.DriverManager;

View File

@ -1,4 +1,4 @@
package src.models.squirrel;
package src.squirrel;
import java.sql.*;
import java.util.*;

View File

@ -1,4 +1,4 @@
package src.models.squirrel;
package src.squirrel;
import java.util.*;