Yggdrasil/repositories/sessionsRepository.js
azures04 2519d8078a Refactor logger usage and add userRepository module
Replaces custom logger instantiation with a shared logger import across modules and routes. Moves player property and privilege management from authRepository to a new userRepository, expanding userRepository with additional user management functions (ban, unban, preferences, privileges, bans). Updates service and route files to use userRepository where appropriate. Adds new session join route and schema, and utility for UUID formatting.
2025-12-24 04:22:43 +01:00

163 lines
5.7 KiB
JavaScript

const logger = require("../modules/logger")
const database = require("../modules/database")
const { DefaultError } = require("../errors/errors")
async function insertLegacyClientSessions(sessionId, uuid) {
try {
await database.query(`DELETE FROM legacyClientSessions WHERE uuid = ?`, [uuid])
const sql = `INSERT INTO legacyClientSessions (sessionId, uuid) VALUES (?, ?)`
const result = await database.query(sql, [sessionId, uuid])
if (result.affectedRows > 0) {
return { code: 200, sessionId, uuid }
} else {
throw new DefaultError(500, "Internal Server Error", "Unknown DB Error")
}
} catch (error) {
if (error instanceof DefaultError) throw error
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Internal Server Error", "Please contact an administrator.")
}
}
async function validateLegacyClientSession(sessionId, uuid) {
try {
const sql = `SELECT * FROM legacyClientSessions WHERE sessionId = ? AND uuid = ?`
const rows = await database.query(sql, [sessionId, uuid])
const session = rows[0]
if (session) {
return {
code: 200,
message: "Client session valid."
}
} else {
return {
code: 404,
message: "Client session not found for this accessToken/clientToken combination"
}
}
} catch (error) {
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Internal Server Error", "Please contact an administrator.")
}
}
async function getBlockedServers() {
try {
const sql = `SELECT * FROM blockedServers`
const blockedServers = await database.query(sql)
return {
code: 200,
blockedServers: blockedServers.map(bannedServer => ({ sha1: bannedServer.hashedIp }))
}
} catch (error) {
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Internal Server Error", "Please contact an administrator.")
}
}
async function getActiveSkin(uuid) {
try {
const sql = `
SELECT t.url, ps.variant
FROM playersSkins ps
JOIN textures t ON ps.assetHash = t.hash
WHERE ps.playerUuid = ? AND ps.isSelected = 1
`
const rows = await database.query(sql, [uuid])
const skin = rows[0]
return { code: 200, data: skin || null }
} catch (error) {
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Internal Server Error", "Please contact an administrator.")
}
}
async function getActiveCape(uuid) {
try {
const sql = `
SELECT t.url
FROM playersCapes pc
JOIN textures t ON pc.assetHash = t.hash
WHERE pc.playerUuid = ? AND pc.isSelected = 1
`
const rows = await database.query(sql, [uuid])
const cape = rows[0]
return { code: 200, data: cape || null }
} catch (error) {
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Internal Server Error", "Please contact an administrator.")
}
}
async function getProfileActionsList(uuid) {
try {
const cleanUuid = uuid.replace(/-/g, "")
const sql = "SELECT action FROM playerProfileActions WHERE uuid = ?"
const rows = await database.query(sql, [cleanUuid])
const actions = rows.map(row => row.action)
return { code: 200, data: actions }
} catch (error) {
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Internal Server Error", "Please contact an administrator.")
}
}
async function saveServerSession(uuid, accessToken, serverId, ip) {
try {
const sql = `
INSERT INTO serverSessions (uuid, accessToken, serverId, ip, createdAt)
VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)
ON DUPLICATE KEY UPDATE
accessToken = VALUES(accessToken),
serverId = VALUES(serverId),
ip = VALUES(ip),
createdAt = CURRENT_TIMESTAMP
`
const result = await database.query(sql, [uuid, accessToken, serverId, ip])
return { code: 200, success: result.affectedRows > 0 }
} catch (error) {
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Internal Server Error", "Please contact an administrator.")
}
}
async function getServerSession(uuid, serverId) {
try {
const sql = `
SELECT ip
FROM serverSessions
WHERE uuid = ? AND serverId = ?
`
const rows = await database.query(sql, [uuid, serverId])
const session = rows[0]
if (!session) {
return { code: 404, valid: false }
}
return { code: 200, valid: true, ip: session.ip }
} catch (error) {
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Internal Server Error", "Please contact an administrator.")
}
}
module.exports = {
insertLegacyClientSessions,
validateLegacyClientSession,
getBlockedServers,
getActiveSkin,
getActiveCape,
getProfileActionsList,
saveServerSession,
getServerSession
}