Add base project files including environment example, license, README, .gitignore, error classes, ESLint config, database modules, texture assets, repositories, routes, schemas, services, and server entry point. This establishes the foundational structure for a Yggdrasil-compatible REST API with modular error handling, database setup, and route organization.
64 lines
2.0 KiB
JavaScript
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,
|
|
} |