chore: unify checkstyle, fix violations, cleanup bin/
This commit is contained in:
parent
9c5e5bccae
commit
da9271b283
17 changed files with 363 additions and 67 deletions
|
|
@ -40,7 +40,9 @@ public final class YamlConfig {
|
|||
@SuppressWarnings("unchecked")
|
||||
public <T> T get(final String key, final T defaultValue) {
|
||||
Object val = data.get(key);
|
||||
if (val == null) return defaultValue;
|
||||
if (val == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return (T) val;
|
||||
} catch (ClassCastException e) {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,11 @@ import dev.loki.loAuth.common.util.CodeGenerator;
|
|||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import java.sql.*;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
|
@ -159,7 +163,9 @@ public final class DatabaseManager implements AutoCloseable {
|
|||
throw new SQLException("INSERT returned no rows");
|
||||
} else {
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) return mapUser(rs);
|
||||
if (rs.next()) {
|
||||
return mapUser(rs);
|
||||
}
|
||||
throw new SQLException("INSERT returned no rows");
|
||||
}
|
||||
}
|
||||
|
|
@ -240,7 +246,9 @@ public final class DatabaseManager implements AutoCloseable {
|
|||
}
|
||||
|
||||
public Optional<String> consumeVerificationCode(final String code) {
|
||||
if (sqlite) return consumeDeleteSelect("pending_verifications", "code", code);
|
||||
if (sqlite) {
|
||||
return consumeDeleteSelect("pending_verifications", "code", code);
|
||||
}
|
||||
try (Connection conn = ds.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement("""
|
||||
DELETE FROM pending_verifications
|
||||
|
|
@ -309,7 +317,9 @@ public final class DatabaseManager implements AutoCloseable {
|
|||
}
|
||||
|
||||
public Optional<String> consumeLoginToken(final String token) {
|
||||
if (sqlite) return consumeDeleteSelect("pending_logins", "token", token);
|
||||
if (sqlite) {
|
||||
return consumeDeleteSelect("pending_logins", "token", token);
|
||||
}
|
||||
try (Connection conn = ds.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement("""
|
||||
DELETE FROM pending_logins
|
||||
|
|
@ -327,7 +337,9 @@ public final class DatabaseManager implements AutoCloseable {
|
|||
}
|
||||
|
||||
public Optional<String> consumeLoginTokenByMessageId(final String messageId) {
|
||||
if (sqlite) return consumeDeleteSelect("pending_logins", "message_id", messageId);
|
||||
if (sqlite) {
|
||||
return consumeDeleteSelect("pending_logins", "message_id", messageId);
|
||||
}
|
||||
try (Connection conn = ds.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement("""
|
||||
DELETE FROM pending_logins
|
||||
|
|
@ -378,7 +390,9 @@ public final class DatabaseManager implements AutoCloseable {
|
|||
"SELECT minecraft_nick FROM pending_logins WHERE expires_at <= ?")) {
|
||||
ps.setLong(1, now);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) nicks.add(rs.getString("minecraft_nick"));
|
||||
while (rs.next()) {
|
||||
nicks.add(rs.getString("minecraft_nick"));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!nicks.isEmpty()) {
|
||||
|
|
@ -399,7 +413,9 @@ public final class DatabaseManager implements AutoCloseable {
|
|||
"DELETE FROM pending_logins WHERE expires_at <= ? RETURNING minecraft_nick")) {
|
||||
ps.setLong(1, now);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) nicks.add(rs.getString("minecraft_nick"));
|
||||
while (rs.next()) {
|
||||
nicks.add(rs.getString("minecraft_nick"));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DatabaseException("Failed to pop expired logins", e);
|
||||
|
|
@ -456,23 +472,27 @@ public final class DatabaseManager implements AutoCloseable {
|
|||
}
|
||||
|
||||
private static void validateNick(final String nick) {
|
||||
if (nick == null || !nick.matches("^[a-zA-Z0-9_]{3,16}$"))
|
||||
if (nick == null || !nick.matches("^[a-zA-Z0-9_]{3,16}$")) {
|
||||
throw new IllegalArgumentException("Invalid Minecraft nick: " + nick);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateDiscordId(final String id) {
|
||||
if (id == null || !id.matches("^[0-9]{17,20}$"))
|
||||
if (id == null || !id.matches("^[0-9]{17,20}$")) {
|
||||
throw new IllegalArgumentException("Invalid Discord ID: " + id);
|
||||
}
|
||||
}
|
||||
|
||||
private static RuntimeException translateSqlException(final SQLException e) {
|
||||
String state = e.getSQLState();
|
||||
if ("23505".equals(state) || "23000".equals(state)) {
|
||||
String msg = e.getMessage();
|
||||
if (msg != null && (msg.contains("minecraft_nick") || msg.contains("UNIQUE constraint failed: users.minecraft_nick")))
|
||||
if (msg != null && (msg.contains("minecraft_nick") || msg.contains("UNIQUE constraint failed: users.minecraft_nick"))) {
|
||||
return new DuplicateUserException("Minecraft nick already registered");
|
||||
if (msg != null && (msg.contains("discord_id") || msg.contains("UNIQUE constraint failed: users.discord_id")))
|
||||
}
|
||||
if (msg != null && (msg.contains("discord_id") || msg.contains("UNIQUE constraint failed: users.discord_id"))) {
|
||||
return new DuplicateUserException("Discord ID already registered");
|
||||
}
|
||||
return new DuplicateUserException("Duplicate entry");
|
||||
}
|
||||
return new DatabaseException("Database error", e);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package dev.loki.loAuth.common.exception;
|
||||
|
||||
public final class DatabaseException extends RuntimeException {
|
||||
public DatabaseException(String msg, Throwable cause) { super(msg, cause); }
|
||||
public DatabaseException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
package dev.loki.loAuth.common.exception;
|
||||
|
||||
public final class DuplicateUserException extends RuntimeException {
|
||||
public DuplicateUserException(String msg) { super(msg); }
|
||||
public DuplicateUserException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
|
|
@ -10,12 +10,14 @@ public final class CodeGenerator {
|
|||
private CodeGenerator() {}
|
||||
|
||||
public static String generate(final int length) {
|
||||
if (length < 4 || length > 16)
|
||||
if (length < 4 || length > 16) {
|
||||
throw new IllegalArgumentException("Code length must be between 4 and 16");
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder(length);
|
||||
for (int i = 0; i < length; i++)
|
||||
for (int i = 0; i < length; i++) {
|
||||
sb.append(ALPHABET.charAt(RNG.nextInt(ALPHABET.length())));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue