Yggdrasil/modules/certificatesManager.js
azures04 1fe46a03fd Add skin upload and texture management endpoints
Introduces endpoints and logic for uploading Minecraft skins via file or URL, storing textures, and managing player skins. Adds new repository and service methods for texture registration and retrieval, updates authorization handling, and uses process.cwd() for data paths. Also includes static serving of textures and rate limiting for skin uploads.
2025-12-28 07:49:31 +01:00

64 lines
2.0 KiB
JavaScript

const fs = require("node:fs")
const path = require("node:path")
const crypto = require("node:crypto")
const keysRoot = path.join(process.cwd(), "data", "keys")
const keysList = ["authenticationKeys", "profilePropertyKeys", "playerCertificateKeys"]
function generateKeysPair() {
try {
const { privateKey, publicKey } = crypto.generateKeyPairSync("rsa", {
modulusLength: 2048,
publicKeyEncoding: {
type: "spki",
format: "pem"
},
privateKeyEncoding: {
type: "pkcs8",
format: "pem"
}
})
return { privateKey, publicKey }
} catch (error) {
console.error("Erreur lors de la génération des clés :", error)
return { publicKey: null, privateKey: null }
}
}
function setupKeys() {
if (!fs.existsSync(keysRoot)) {
fs.mkdirSync(keysRoot, { recursive: true })
}
for (const key of keysList) {
if (!fs.existsSync(path.join(keysRoot, `${key}-public.pem`)) || !fs.existsSync(path.join(keysRoot, `${key}-private.pem`))) {
const { publicKey, privateKey } = generateKeysPair()
fs.writeFileSync(path.join(keysRoot, `${key}-public.pem`), publicKey)
fs.writeFileSync(path.join(keysRoot, `${key}-private.pem`), privateKey)
}
}
}
function getKeys() {
const keys = {}
for (const key of keysList) {
keys[key] = {}
keys[key].public = fs.readFileSync(path.join(keysRoot, `${key}-public.pem`)).toString("utf8")
keys[key].private = fs.readFileSync(path.join(keysRoot, `${key}-private.pem`)).toString("utf8")
}
return keys
}
function extractKeyFromPem(pemKey) {
const keyRegex = /-----BEGIN (?:PUBLIC|PRIVATE) KEY-----\s*([\s\S]+?)\s*-----END (?:PUBLIC|PRIVATE) KEY-----/
const match = pemKey.match(keyRegex)
if (match && match[1]) {
const rawKey = match[1].replace(/\s/g, "")
return rawKey
}
return null
}
module.exports = {
extractKeyFromPem,
setupKeys,
getKeys,
}