const { resolve } = require("path"); const { __glob } = require("../modules/global-variables"); const { LogType } = require('./sub-log'); const fs = require("fs") const alog = new LogType("Authentification") var users = new Map() var sessions = new Array() updateUsers() module.exports.checkUser = function (token) { if(users.has(token)) { return true } else { return false } } module.exports.getSession = function (session) { if(sessions.includes(session)) { return true } else { return false } } module.exports.getDiscordUser = function (code, session) { return new Promise((resolve, reject) => { alog.log("Récupération de l'autorisation de récupération des informations de l'utilisateur associé à la session : " + session + " [ETAPE 2]") var link = "https://subsonics.raphix.fr" if(process.env.DEV == "true") { link = "http://localhost:4000" } const params = new URLSearchParams(); params.append('client_id', "1094727789682380922"); params.append('client_secret', "uwtyPOPKCgw6ciBs20qiJ7LJrW9Ziclo"); params.append('grant_type', 'authorization_code'); params.append('code', code); params.append('redirect_uri', link + "/internal/redirect"); params.append('scope', 'identify guilds'); fetch('https://discord.com/api/oauth2/token', { method: "POST", headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body : params }).then(resp1 => resp1.json()).then(authorizationKey => { alog.log("Récupération des informations de l'utilisateur associé à l'autorisation : '" + authorizationKey.access_token + "' et associé à la session : " + session + " [ETAPE 3]") fetch('https://discord.com/api/users/@me/guilds/137291455336022018/member', { headers: { authorization: `${authorizationKey.token_type} ${authorizationKey.access_token}`, }, }).then(resp2 => resp2.json()).then(userInfo => { var user = {} if(typeof userInfo.user.global_name == "undefined") { reject("MIGRATE_ACCOUNT_ONLY") } user.auth = authorizationKey Object.assign(user, userInfo) resolve(user) }).catch(error => reject(error)) }).catch(error => reject(error)) }) } module.exports.saveSession = function (session) { sessions.push(session) alog.log("Nouvelle session enregistré : " + session) } module.exports.removeSession = function (session) { const index = sessions.indexOf(session) sessions.splice(index, 1) alog.log("Supression de la session : " + session) } module.exports.getUser = function (token) { return users.get(token) } module.exports.addUser = function (user) { if(!fs.existsSync(__glob.USERS)){ fs.writeFileSync(__glob.USERS, '[]') } const userDB = JSON.parse(fs.readFileSync(__glob.USERS)) userDB.push(user) fs.writeFileSync(__glob.USERS, JSON.stringify(userDB, null, 2)) updateUsers() alog.log("Ajout de " + user.user.username + " en tant qu'utilisateur avec le token : " + user.token) } module.exports.removeUser = function (token) { if(!fs.existsSync(__glob.USERS)){ fs.writeFileSync(__glob.USERS, '[]') } const userDB = JSON.parse(fs.readFileSync(__glob.USERS)) var selectedUser = null for (const user of userDB) { if(user.token == token) { selectedUser = user } } const index = userDB.indexOf(selectedUser) alog.log("Supression de " + selectedUser.user.username + " en tant qu'utilisateur avec le token : " + selectedUser.token) userDB.splice(index, 1) fs.writeFileSync(__glob.USERS, JSON.stringify(userDB, null, 2)) updateUsers() } function updateUsers() { if(!fs.existsSync(__glob.USERS)){ fs.writeFileSync(__glob.USERS, '[]') } const userDB = JSON.parse(fs.readFileSync(__glob.USERS)) for (const user of userDB) { users.set(user.token, user) } alog.log("Actualisation de " + userDB.length + " utilisateurs depuis : " + __glob.USERS) }