Added Database class and Account record
This commit is contained in:
parent
95363e90d0
commit
08af5df15b
18
.idea/dataSources.xml
Normal file
18
.idea/dataSources.xml
Normal file
@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="chat_rsa@localhost" uuid="8cab1bef-0c27-42d9-ae74-3ac57094560d">
|
||||
<driver-ref>mariadb</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<jdbc-driver>org.mariadb.jdbc.Driver</jdbc-driver>
|
||||
<jdbc-url>jdbc:mariadb://localhost:3306/chat_rsa</jdbc-url>
|
||||
<jdbc-additional-properties>
|
||||
<property name="com.intellij.clouds.kubernetes.db.host.port" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.resource.type" value="Deployment" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.container.port" />
|
||||
</jdbc-additional-properties>
|
||||
<working-dir>$ProjectFileDir$</working-dir>
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
6
.idea/sqldialects.xml
Normal file
6
.idea/sqldialects.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="SqlDialectMappings">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java/controllers/Database.java" dialect="GenericSQL" />
|
||||
</component>
|
||||
</project>
|
24
pom.xml
24
pom.xml
@ -8,6 +8,30 @@
|
||||
<artifactId>chat-rsa</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mariadb.jdbc</groupId>
|
||||
<artifactId>mariadb-java-client</artifactId>
|
||||
<version>LATEST</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-core</artifactId>
|
||||
<version>6.2.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>2.0.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-jdk14</artifactId>
|
||||
<version>2.0.12</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
|
79
src/main/java/controllers/Database.java
Normal file
79
src/main/java/controllers/Database.java
Normal file
@ -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")
|
||||
);
|
||||
}
|
||||
}
|
6
src/main/java/models/Account.java
Normal file
6
src/main/java/models/Account.java
Normal file
@ -0,0 +1,6 @@
|
||||
package models;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public record Account(int id, String username, BigInteger e, BigInteger d, BigInteger n) {
|
||||
}
|
Loading…
Reference in New Issue
Block a user