Creates all (?) endpoints
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
package ch.gtache.elderscrollslegends.service.account;
|
||||
|
||||
import ch.gtache.elderscrollslegends.service.SteamService;
|
||||
import ch.gtache.elderscrollslegends.service.Token;
|
||||
import ch.gtache.elderscrollslegends.service.account.auth.AuthData;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
@ApplicationScoped
|
||||
public class AccountService {
|
||||
private static final Logger logger = Logger.getLogger(AccountService.class);
|
||||
|
||||
private final SteamService steamService;
|
||||
private final DataSource dataSource;
|
||||
|
||||
@Inject
|
||||
AccountService(final SteamService steamService, final DataSource dataSource) {
|
||||
this.steamService = requireNonNull(steamService);
|
||||
this.dataSource = requireNonNull(dataSource);
|
||||
}
|
||||
|
||||
|
||||
public AuthData authenticate(final String steamID) {
|
||||
if (!exists(steamID)) {
|
||||
final var longId = Long.parseLong(steamID);
|
||||
final var name = steamService.getName(steamID);
|
||||
try (final var connection = dataSource.getConnection();
|
||||
final var statement = connection.prepareStatement("INSERT INTO player (steam_id, name) VALUES (?, ?)")) {
|
||||
statement.setLong(1, longId);
|
||||
statement.setString(2, name);
|
||||
statement.executeUpdate();
|
||||
} catch (final SQLException e) {
|
||||
logger.error("Error inserting player " + steamID, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
updateNameIfNeeded(steamID);
|
||||
final var token = getOrCreateToken(steamID);
|
||||
return getAuthData(steamID, token);
|
||||
}
|
||||
|
||||
private void updateNameIfNeeded(final String steamID) {
|
||||
final var longId = Long.parseLong(steamID);
|
||||
final var name = steamService.getName(steamID);
|
||||
try (final var connection = dataSource.getConnection();
|
||||
final var statement = connection.prepareStatement("UPDATE player SET name=? WHERE steam_id=?")) {
|
||||
statement.setString(1, name);
|
||||
statement.setLong(2, longId);
|
||||
statement.executeUpdate();
|
||||
} catch (final SQLException e) {
|
||||
logger.error("Error updating player " + steamID, e);
|
||||
}
|
||||
}
|
||||
|
||||
private Token getOrCreateToken(final String steamID) {
|
||||
final var longId = Long.parseLong(steamID);
|
||||
try (final var connection = dataSource.getConnection();
|
||||
final var statement = connection.prepareStatement("SELECT token, expiration FROM token WHERE player_id=(SELECT id FROM player WHERE steam_id=?)")) {
|
||||
statement.setLong(1, longId);
|
||||
try (final var rs = statement.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
final var token = rs.getString(1);
|
||||
final var expiration = rs.getTimestamp(2).toInstant();
|
||||
if (expiration.isAfter(java.time.Instant.now())) {
|
||||
return new Token(token, expiration);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (final SQLException e) {
|
||||
logger.error("Error checking for player " + steamID, e);
|
||||
}
|
||||
try (final var connection = dataSource.getConnection();
|
||||
final var statement = connection.prepareStatement("INSERT INTO token (player_id) VALUES ((SELECT id FROM player WHERE steam_id=?)) RETURNING token, expiration")) {
|
||||
statement.setLong(1, longId);
|
||||
try (final var rs = statement.executeQuery()) {
|
||||
return rs.next() ? new Token(rs.getString(1), rs.getTimestamp(2).toInstant()) : null;
|
||||
}
|
||||
} catch (final SQLException e) {
|
||||
logger.error("Error inserting player " + steamID, e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private AuthData getAuthData(final String steamID, final Token token) {
|
||||
if (token != null) {
|
||||
final var longId = Long.parseLong(steamID);
|
||||
try (final var connection = dataSource.getConnection();
|
||||
final var statement = connection.prepareStatement("SELECT 1 FROM player WHERE steam_id=?")) {
|
||||
statement.setLong(1, longId);
|
||||
try (final var rs = statement.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
final var uuid = token.token();
|
||||
final var timeToRefresh = Duration.between(java.time.Instant.now(), token.expirationTimestamp()).getSeconds();
|
||||
return new AuthData(uuid, "", "", "", "", steamID, steamID, steamID,
|
||||
"", steamID, "CH", false, false, (int) timeToRefresh,
|
||||
5, 5, false, "", "",
|
||||
0, List.of(), false);
|
||||
}
|
||||
}
|
||||
} catch (final SQLException e) {
|
||||
logger.error("Error checking for player " + steamID, e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean exists(final String steamID) {
|
||||
final var longId = Long.parseLong(steamID);
|
||||
try (final var connection = dataSource.getConnection();
|
||||
final var statement = connection.prepareStatement("SELECT 1 FROM player WHERE steam_id=?")) {
|
||||
statement.setLong(1, longId);
|
||||
try (final var rs = statement.executeQuery()) {
|
||||
return rs.next();
|
||||
}
|
||||
} catch (final SQLException e) {
|
||||
logger.error("Error checking for player " + steamID, e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Token refresh(final String steamID) {
|
||||
if (exists(steamID)) {
|
||||
final var longId = Long.parseLong(steamID);
|
||||
try (final var connection = dataSource.getConnection();
|
||||
final var statement = connection.prepareStatement("DELETE FROM token WHERE player_id=(SELECT id FROM player WHERE steam_id=?)")) {
|
||||
statement.setLong(1, longId);
|
||||
statement.executeUpdate();
|
||||
} catch (final SQLException e) {
|
||||
logger.error("Error checking for player " + steamID, e);
|
||||
}
|
||||
return getOrCreateToken(steamID);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user