From e0f3d7e9149e7e44a22973bd786dba67a0ecfcea Mon Sep 17 00:00:00 2001 From: lily Date: Fri, 25 Apr 2025 20:06:37 -0400 Subject: [PATCH] feat: sqlite database support and generic database class --- .../oauthfabric/database/Database.java | 36 ++++ .../lilyvex/oauthfabric/database/SQLite.java | 200 ++++++++++++++++++ 2 files changed, 236 insertions(+) create mode 100644 src/main/java/dev/lilyvex/oauthfabric/database/Database.java create mode 100644 src/main/java/dev/lilyvex/oauthfabric/database/SQLite.java diff --git a/src/main/java/dev/lilyvex/oauthfabric/database/Database.java b/src/main/java/dev/lilyvex/oauthfabric/database/Database.java new file mode 100644 index 0000000..6b348f8 --- /dev/null +++ b/src/main/java/dev/lilyvex/oauthfabric/database/Database.java @@ -0,0 +1,36 @@ +package dev.lilyvex.oauthfabric.database; + +public class Database implements IDatabase { + private IDatabase databaseClass; + + public void setDatabase(String database) { + switch (database) { + case "sqlite": + databaseClass = new SQLite(); + } + } + + public void addUser(String uuid, String oauthId, String sessionToken) { + databaseClass.addUser(uuid, oauthId, sessionToken); + } + + public void updateUser(String uuid, String oauthId, String sessionToken) { + databaseClass.updateUser(uuid, oauthId, sessionToken); + } + + public void removeUser(String uuid) { + databaseClass.removeUser(uuid); + } + + public boolean isRegisteredUser(String uuid) { + return databaseClass.isRegisteredUser(uuid); + } + + public String getOauthId(String uuid) { + return databaseClass.getOauthId(uuid); + } + + public String getSessionToken(String uuid) { + return databaseClass.getSessionToken(uuid); + } +} diff --git a/src/main/java/dev/lilyvex/oauthfabric/database/SQLite.java b/src/main/java/dev/lilyvex/oauthfabric/database/SQLite.java new file mode 100644 index 0000000..340f1c0 --- /dev/null +++ b/src/main/java/dev/lilyvex/oauthfabric/database/SQLite.java @@ -0,0 +1,200 @@ +package dev.lilyvex.oauthfabric.database; + +import java.nio.file.Path; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import net.fabricmc.loader.api.FabricLoader; + +public class SQLite implements IDatabase { + private static final Logger LOGGER = LoggerFactory.getLogger("oauth-fabric"); + private static final Path DATABASE_PATH = FabricLoader.getInstance().getConfigDir() + .resolve("oauth-fabric.db") + .normalize(); + + private static final String databaseUrl = "jdbc:sqlite:" + DATABASE_PATH; + private Connection connection = null; + + private void createDatabase() throws SQLException { + try { + connection = DriverManager.getConnection(databaseUrl); + + Statement statement = connection.createStatement(); + statement.executeUpdate(""" + CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY, + uuid TEXT UNIQUE, + oauth_id TEXT UNIQUE, + session_token TEXT UNIQUE + ) + """); + } catch (SQLException e) { + LOGGER.error("oauth-fabric: Unable to create SQLite database"); + } finally { + try { + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + LOGGER.error("oauth-fabric: Could not close SQLite connection"); + } + } + } + + public void addUser(String uuid, String oauthId, String sessionToken) { + try { + createDatabase(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + try { + connection = DriverManager.getConnection(databaseUrl); + + PreparedStatement statement = connection.prepareStatement(""" + INSERT INTO users ( + uuid, + oauth_id, + session_token + ) VALUES ( + ?, + ?, + ? + ) + """); + + statement.setString(1, uuid); + statement.setString(2, oauthId); + statement.setString(3, sessionToken); + statement.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public void updateUser(String uuid, String oauthId, String sessionToken) { + try { + createDatabase(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + try { + connection = DriverManager.getConnection(databaseUrl); + + PreparedStatement statement = connection.prepareStatement("UPDATE users SET oauth_id = ?, session_token = ? WHERE uuid = ?"); + statement.setString(1, oauthId); + statement.setString(2, sessionToken); + statement.setString(3, uuid); + statement.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public void removeUser(String uuid) { + try { + createDatabase(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + try { + connection = DriverManager.getConnection(databaseUrl); + + PreparedStatement statement = connection.prepareStatement("DELETE FROM users WHERE uuid = ?"); + statement.setString(1, uuid); + statement.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public boolean isRegisteredUser(String uuid) { + try { + createDatabase(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + try { + connection = DriverManager.getConnection(databaseUrl); + + PreparedStatement statement = connection.prepareStatement("SELECT * FROM users WHERE uuid = ?"); + statement.setString(1, uuid); + ResultSet resultSet = statement.executeQuery(); + + int results = 0; + while (resultSet.next()) { + results++; + } + + if (results > 0) { + return true; + } + + return false; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public String getOauthId(String uuid) { + try { + createDatabase(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + try { + connection = DriverManager.getConnection(databaseUrl); + + PreparedStatement statement = connection.prepareStatement("SELECT * FROM users WHERE uuid = ?"); + statement.setString(1, uuid); + ResultSet resultSet = statement.executeQuery(); + + String userOauthId = ""; + + while (resultSet.next()) { + userOauthId = resultSet.getString("oauth_id"); + } + + return userOauthId; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public String getSessionToken(String uuid) { + try { + createDatabase(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + try { + connection = DriverManager.getConnection(databaseUrl); + + PreparedStatement statement = connection.prepareStatement("SELECT * FROM users WHERE uuid = ?"); + statement.setString(1, uuid); + ResultSet resultSet = statement.executeQuery(); + + String userSessionToken = ""; + + while (resultSet.next()) { + userSessionToken = resultSet.getString("session_token"); + } + + return userSessionToken; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +}