Add Minecraft services API routes and user service
Introduces new routes under /minecraftservices and /mojangapi for profile, skin, cape, blocklist, privileges, and certificate management. Adds a comprehensive userService module to handle user-related operations, and extends userRepository with methods for username changes, skin/cape management, blocking, and profile lookups. Refactors username availability logic into authService, updates error handling, and improves logger and utility functions. Also updates route handlers to use consistent return statements and enhances route registration logging.
This commit is contained in:
+57
-4
@@ -9,13 +9,15 @@ const { DefaultError } = require("../errors/errors")
|
||||
|
||||
const keys = certsManager.getKeys()
|
||||
const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/
|
||||
const usernameRegex = /^[a-zA-Z0-9_]{3,16}$/
|
||||
|
||||
async function registerUser({ username, password, email, registrationCountry, preferredLanguage, clientIp }) {
|
||||
try {
|
||||
await authRepository.getUser(username)
|
||||
const availability = await checkUsernameAvailability(username)
|
||||
if (availability.status === "DUPLICATE") {
|
||||
throw new DefaultError(409, "Username taken.", "ForbiddenOperationException")
|
||||
} catch (error) {
|
||||
if (error.code !== 404) throw error
|
||||
}
|
||||
if (availability.status === "NOT_ALLOWED") {
|
||||
throw new DefaultError(400, availability.message || "Username invalid.", "InvalidUsernameException")
|
||||
}
|
||||
|
||||
if (email) {
|
||||
@@ -225,6 +227,56 @@ async function verifyAccessToken({ accessToken }) {
|
||||
}
|
||||
}
|
||||
|
||||
async function checkUsernameAvailability(username) {
|
||||
if (!usernameRegex.test(username)) {
|
||||
return {
|
||||
code: 200,
|
||||
status: "NOT_ALLOWED",
|
||||
message: "Invalid characters or length."
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await authRepository.getUser(username)
|
||||
return {
|
||||
code: 200,
|
||||
status: "DUPLICATE",
|
||||
message: "Username is already in use."
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.code !== 404) throw error
|
||||
}
|
||||
|
||||
const blocklist = await authRepository.getUsernamesRules()
|
||||
const normalizedUsername = username.toLowerCase()
|
||||
|
||||
for (const entry of blocklist) {
|
||||
if (entry.type === "literal") {
|
||||
if (normalizedUsername === entry.value) {
|
||||
return {
|
||||
code: 200,
|
||||
status: "NOT_ALLOWED",
|
||||
message: "Username is reserved."
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (entry.type === "regex") {
|
||||
if (entry.pattern.test(username)) {
|
||||
return {
|
||||
code: 200,
|
||||
status: "NOT_ALLOWED",
|
||||
message: "Username contains forbidden words."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
status: "AVAILABLE"
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
signout,
|
||||
validate,
|
||||
@@ -233,4 +285,5 @@ module.exports = {
|
||||
authenticate,
|
||||
refreshToken,
|
||||
verifyAccessToken,
|
||||
checkUsernameAvailability
|
||||
}
|
||||
Reference in New Issue
Block a user