diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..5255ffe --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,18 @@ + + + + + mariadb + true + org.mariadb.jdbc.Driver + jdbc:mariadb://localhost:3306/chat_rsa + + + + + + + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..cecf189 --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 25e8b84..f8e3987 100644 --- a/pom.xml +++ b/pom.xml @@ -8,6 +8,30 @@ chat-rsa 1.0-SNAPSHOT + + + org.mariadb.jdbc + mariadb-java-client + LATEST + + + org.springframework.security + spring-security-core + 6.2.3 + + + org.slf4j + slf4j-api + 2.0.12 + + + org.slf4j + slf4j-jdk14 + 2.0.12 + runtime + + + 21 21 diff --git a/src/main/java/controllers/Database.java b/src/main/java/controllers/Database.java new file mode 100644 index 0000000..55e0337 --- /dev/null +++ b/src/main/java/controllers/Database.java @@ -0,0 +1,79 @@ +package controllers; + +import models.Account; +import org.mariadb.jdbc.MariaDbBlob; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +import java.math.BigInteger; +import java.sql.*; + +public class Database { + public static boolean register(String username, String password, BigInteger e, BigInteger d, BigInteger n) { + try (Connection connection = getConnection()) { + if (connection == null) return false; + + PasswordEncoder encoder = new BCryptPasswordEncoder(); + String hashedPass = encoder.encode(password); + + try (PreparedStatement statement = connection.prepareStatement(""" + INSERT INTO accounts(username, password, e, d, n) + VALUES (?, ?, ?, ?, ?) + """)) { + statement.setString(1, username); + statement.setString(2, hashedPass); + statement.setBlob(3, new MariaDbBlob(e.toString().getBytes())); + statement.setBlob(4, new MariaDbBlob(d.toString().getBytes())); + statement.setBlob(5, new MariaDbBlob(n.toString().getBytes())); + + int rowsInserted = statement.executeUpdate(); + return rowsInserted == 1; + } catch (SQLException ex) { + System.err.println("Error while trying to create a new account: " + ex); + return false; + } + } catch (SQLException ex) { + System.err.println("Error while trying to open a connection in: " + ex); + return false; + } + } + + public static Account login(String username, CharSequence password) { + try (Connection connection = getConnection()) { + if (connection == null) return null; + + try (PreparedStatement statement = connection.prepareStatement(""" + SELECT id, password, e, d, n + FROM accounts + WHERE username = ? + """)) { + statement.setString(1, username); + ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + int id = resultSet.getInt("id"); + String hash_password = resultSet.getString("password"); + BigInteger e = new BigInteger(resultSet.getString("e")); + BigInteger d = new BigInteger(resultSet.getString("d")); + BigInteger n = new BigInteger(resultSet.getString("n")); + PasswordEncoder encoder = new BCryptPasswordEncoder(); + if (encoder.matches(password, hash_password)) + return new Account(id, username, e, d, n); + } + return null; + } catch (SQLException e) { + System.err.println("Error while trying to logging in: " + e); + return null; + } + } catch (SQLException e) { + System.err.println("Error while trying to open a connection in: " + e); + throw new RuntimeException(e); + } + } + + private static Connection getConnection() throws SQLException { + return DriverManager.getConnection( + System.getenv("db_url"), + System.getenv("db_user"), System.getenv("db_pass") + ); + } +} diff --git a/src/main/java/models/Account.java b/src/main/java/models/Account.java new file mode 100644 index 0000000..f9389c3 --- /dev/null +++ b/src/main/java/models/Account.java @@ -0,0 +1,6 @@ +package models; + +import java.math.BigInteger; + +public record Account(int id, String username, BigInteger e, BigInteger d, BigInteger n) { +}