Refactor Main class and implement UserModel registration and authentication methods; add Database and Model classes for ORM functionality
This commit is contained in:
parent
dc1b85a774
commit
fc1aca80d2
26
Main.java
26
Main.java
@ -1,10 +1,8 @@
|
|||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import src.models.UserModel;
|
import src.models.UserModel;
|
||||||
import src.models.squirrel.Database;
|
import src.squirrel.Database;
|
||||||
import src.models.squirrel.ModelManager;
|
import src.squirrel.ModelManager;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws SQLException {
|
public static void main(String[] args) throws SQLException {
|
||||||
@ -13,24 +11,8 @@ public class Main {
|
|||||||
Database.getConnection();
|
Database.getConnection();
|
||||||
|
|
||||||
UserModel userModel = ModelManager.get(UserModel.class);
|
UserModel userModel = ModelManager.get(UserModel.class);
|
||||||
List<UserModel> users = userModel.where(java.util.Collections.emptyMap());
|
UserModel user = userModel.register("Jack", "jack@example.com", "1234");
|
||||||
for (UserModel user : users) {
|
System.out.println("Registered user: " + user);
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
Database.close();
|
Database.close();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,8 +3,8 @@ package src.models;
|
|||||||
import javax.crypto.SecretKeyFactory;
|
import javax.crypto.SecretKeyFactory;
|
||||||
import javax.crypto.spec.PBEKeySpec;
|
import javax.crypto.spec.PBEKeySpec;
|
||||||
|
|
||||||
import src.models.squirrel.Model;
|
import src.squirrel.Model;
|
||||||
import src.models.squirrel.ModelManager;
|
import src.squirrel.ModelManager;
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
@ -25,7 +25,7 @@ public class UserModel extends Model {
|
|||||||
private static final int ITERATIONS = 65536;
|
private static final int ITERATIONS = 65536;
|
||||||
private static final int KEY_LENGTH = 256;
|
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()) {
|
if (password == null || password.trim().isEmpty()) {
|
||||||
throw new IllegalArgumentException("Password cannot be empty");
|
throw new IllegalArgumentException("Password cannot be empty");
|
||||||
}
|
}
|
||||||
@ -46,7 +46,29 @@ public class UserModel extends Model {
|
|||||||
return user;
|
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 {
|
try {
|
||||||
PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, ITERATIONS, KEY_LENGTH);
|
PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, ITERATIONS, KEY_LENGTH);
|
||||||
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
|
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();
|
SecureRandom sr = new SecureRandom();
|
||||||
byte[] salt = new byte[16];
|
byte[] salt = new byte[16];
|
||||||
sr.nextBytes(salt);
|
sr.nextBytes(salt);
|
||||||
return salt;
|
return salt;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean validatePassword(String password, String storedHash, byte[] salt) {
|
public boolean validatePassword(String password, String storedHash, byte[] salt) {
|
||||||
try {
|
try {
|
||||||
String newHash = hashPassword(password, salt);
|
String newHash = hashPassword(password, salt);
|
||||||
return Arrays.equals(Base64.getDecoder().decode(storedHash), Base64.getDecoder().decode(newHash));
|
return Arrays.equals(Base64.getDecoder().decode(storedHash), Base64.getDecoder().decode(newHash));
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
package src.models.squirrel;
|
package src.squirrel;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package src.models.squirrel;
|
package src.squirrel;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package src.models.squirrel;
|
package src.squirrel;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user