diff --git a/LaunchServer/source/auth/provider/AuthProvider.java b/LaunchServer/source/auth/provider/AuthProvider.java index d0bdbcd..2dd9d0a 100644 --- a/LaunchServer/source/auth/provider/AuthProvider.java +++ b/LaunchServer/source/auth/provider/AuthProvider.java @@ -34,6 +34,7 @@ registerProvider("mariadb-bcrypt", MariaDBBcryptAuthProvider::new); registerProvider("request", RequestAuthProvider::new); registerProvider("postgresql", PostgreSQLAuthProvider::new); + registerProvider("postgresql-bcrypt", PostgreSQLBcryptAuthProvider::new); registerProvider("json", JsonAuthProvider::new); registerProvider("sqlite", SQLiteAuthProvider::new); } diff --git a/LaunchServer/source/auth/provider/MySQLAuthProvider.java b/LaunchServer/source/auth/provider/MySQLAuthProvider.java index d708729..b1ffc0d 100644 --- a/LaunchServer/source/auth/provider/MySQLAuthProvider.java +++ b/LaunchServer/source/auth/provider/MySQLAuthProvider.java @@ -25,7 +25,6 @@ super(block); mySQLHolder = new MySQLSourceConfig("authProviderPool", block); - // Read query query = VerifyHelper.verify(block.getEntryValue("query", StringConfigEntry.class), VerifyHelper.NOT_EMPTY, "MySQL query can't be empty"); queryParams = block.getEntry("queryParams", ListConfigEntry.class). diff --git a/LaunchServer/source/auth/provider/PostgreSQLBcryptAuthProvider.java b/LaunchServer/source/auth/provider/PostgreSQLBcryptAuthProvider.java new file mode 100644 index 0000000..483b418 --- /dev/null +++ b/LaunchServer/source/auth/provider/PostgreSQLBcryptAuthProvider.java @@ -0,0 +1,60 @@ +package launchserver.auth.provider; + +import launcher.helper.CommonHelper; +import launcher.helper.SecurityHelper; +import launcher.helper.VerifyHelper; +import launcher.serialize.config.entry.BlockConfigEntry; +import launcher.serialize.config.entry.ListConfigEntry; +import launcher.serialize.config.entry.StringConfigEntry; +import launchserver.auth.AuthException; +import launchserver.auth.PostgreSQLSourceConfig; +import org.mindrot.jbcrypt.BCrypt; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class PostgreSQLBcryptAuthProvider extends AuthProvider +{ + private final PostgreSQLSourceConfig postgreSQLHolder; + private final String query; + private final String[] queryParams; + + PostgreSQLBcryptAuthProvider(BlockConfigEntry block) { + super(block); + postgreSQLHolder = new PostgreSQLSourceConfig("authProviderPool", block); + + // Read query + query = VerifyHelper.verify(block.getEntryValue("query", StringConfigEntry.class), + VerifyHelper.NOT_EMPTY, "PostgreSQL query can't be empty"); + queryParams = block.getEntry("queryParams", ListConfigEntry.class). + stream(StringConfigEntry.class).toArray(String[]::new); + } + + @Override + public AuthProviderResult auth(String login, String password, String ip) throws SQLException, AuthException + { + try (Connection c = postgreSQLHolder.getConnection(); PreparedStatement s = c.prepareStatement(query)) + { + String[] replaceParams = {"login", login, "password", password, "ip", ip}; + for (int i = 0; i < queryParams.length; i++) + { + s.setString(i + 1, CommonHelper.replace(queryParams[i], replaceParams)); + } + + // Execute SQL query + s.setQueryTimeout(PostgreSQLSourceConfig.TIMEOUT); + try (ResultSet set = s.executeQuery()) + { + return set.next() ? BCrypt.checkpw(password, "$2a" + set.getString(1).substring(3)) ? new AuthProviderResult(set.getString(2), SecurityHelper.randomStringToken()) : authError("Incorrect username or password") : authError("Incorrect username or password"); + } + } + } + + @Override + public void close() + { + // Do nothing + } +}